KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > trapd > TrapReceiver


1 /*
2  * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This package and its source code is available at www.jboss.org
19 **/

20 package org.jboss.jmx.adaptor.snmp.trapd;
21
22 import java.net.InetAddress JavaDoc;
23
24 import org.jboss.logging.Logger;
25 import org.opennms.protocols.snmp.SnmpOctetString;
26 import org.opennms.protocols.snmp.SnmpPduPacket;
27 import org.opennms.protocols.snmp.SnmpPduRequest;
28 import org.opennms.protocols.snmp.SnmpPduTrap;
29 import org.opennms.protocols.snmp.SnmpTrapHandler;
30 import org.opennms.protocols.snmp.SnmpTrapSession;
31 import org.opennms.protocols.snmp.SnmpVarBind;
32
33 /**
34  * Implements an SNMP trap reception engine
35  *
36  * @version $Revision: 30193 $
37  *
38  * @author <a HREF="mailto:spol@intracom.gr">Spyros Pollatos</a>
39  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
40 **/

41 public class TrapReceiver
42     implements SnmpTrapHandler
43 {
44     /** target logger */
45     protected final Logger log;
46     
47    /**
48     * Public CTOR
49     *
50     * @param log the logger used to output info messages
51    **/

52    public TrapReceiver(Logger log)
53    {
54       this.log = log;
55    }
56     
57    /**
58     * Receives and logs information about SNMPv2 traps.
59     *
60     * @param session the trap session that received the PDU
61     * @param agent the address of the remote sender
62     * @param port the remote port where the pdu was transmitted from
63     * @param community the decoded community string
64     * @param pdu the decoded v2 trap pdu
65    **/

66    public void snmpReceivedTrap(SnmpTrapSession session, InetAddress JavaDoc agent,
67                                 int port, SnmpOctetString community,
68                                 SnmpPduPacket pdu)
69    {
70       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
71         
72       sbuf.append("V2 Trap from ").append(agent.toString());
73       sbuf.append(" on port ").append(port);
74       sbuf.append("\nPDU command......... ").append(pdu.getCommand());
75       sbuf.append("\nPDU Length.......... ").append(pdu.getLength());
76       sbuf.append("\nCommunity string.... ").append(community.toString());
77         
78       if(pdu instanceof SnmpPduRequest)
79       {
80          SnmpPduRequest spdu = (SnmpPduRequest) pdu;
81          
82          sbuf.append("\nPDU Error Status.... ").append(spdu.getErrorStatus());
83          sbuf.append("\nPDU Error Index..... ").append(spdu.getErrorIndex());
84          sbuf.append("\n");
85       }
86     
87       for (int i = 0; i < pdu.getLength(); i++ )
88       {
89          SnmpVarBind vb = pdu.getVarBindAt(i);
90          
91          sbuf.append("Varbind[").append(i).append("] := ");
92          sbuf.append(vb.getName().toString()).append(" --> ");
93          sbuf.append(vb.getValue().toString()).append("\n");
94       }
95       log.debug(sbuf.toString());
96    } // snmpReceivedTrap
97

98    /**
99     * Receives and logs information about SNMPv1 traps.
100     *
101      * @param session the trap session that received the PDU
102     * @param agent the address of the remote sender
103     * @param port the remote port where the pdu was transmitted from
104     * @param community the decoded community string
105     * @param pdu the decoded v1 trap pdu
106    **/

107    public void snmpReceivedTrap(SnmpTrapSession session, InetAddress JavaDoc agent,
108                                 int port, SnmpOctetString community,
109                                 SnmpPduTrap pdu)
110    {
111       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
112         
113       sbuf.append("V1 Trap from agent ").append(agent.toString());
114       sbuf.append(" on port ").append(port);
115       sbuf.append("\nIP Address......... ").append(pdu.getAgentAddress());
116       sbuf.append("\nEnterprise Id...... ").append(pdu.getEnterprise());
117       sbuf.append("\nGeneric ........... ").append(pdu.getGeneric());
118       sbuf.append("\nSpecific .......... ").append(pdu.getSpecific());
119       sbuf.append("\nTimeStamp ......... ").append(pdu.getTimeStamp());
120       sbuf.append("\nLength............. ").append(pdu.getLength());
121       sbuf.append("\nCommunity string... ").append(community.toString());
122       sbuf.append("\n");
123     
124       for (int i = 0; i < pdu.getLength(); i++ )
125       {
126          SnmpVarBind vb = pdu.getVarBindAt(i);
127          
128          sbuf.append("Varbind[").append(i).append("] := ");
129          sbuf.append(vb.getName().toString()).append(" --> ");
130          sbuf.append(vb.getValue().toString()).append("\n");
131       }
132       log.debug(sbuf.toString());
133    } // snmpReceivedTrap
134

135    /**
136     * Processes session errors.
137     *
138     * @param session the trap session in error
139     * @param error the error condition
140     * @param ref the reference object, if any
141    **/

142    public void snmpTrapSessionError(SnmpTrapSession session,
143                                     int error, Object JavaDoc ref)
144    {
145       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
146            
147       if(ref != null) {
148           sbuf.append("Session error (").append(error).append(") reference: ");
149           sbuf.append(ref.toString());
150       }
151       else {
152            sbuf.append("Session error (").append(error).append(")");
153       }
154     
155       try {
156          if(error == SnmpTrapSession.ERROR_EXCEPTION)
157             session.raise();
158       }
159       catch (Throwable JavaDoc e) {
160           sbuf.append(" <").append(e).append(">");
161       }
162        
163       log.error(sbuf.toString());
164        
165    } // snmpTrapSessionError
166

167 } // class TrapReceiver
168
Popular Tags