KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > SNMPv2MIB


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - SNMPv2MIB.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo.snmp;
23
24 import org.snmp4j.agent.*;
25 import org.snmp4j.agent.mo.*;
26 import org.snmp4j.mp.*;
27 import org.snmp4j.smi.*;
28 import org.snmp4j.agent.mo.snmp.DisplayString;
29 import org.snmp4j.event.CounterListener;
30 import org.snmp4j.event.CounterEvent;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 /**
36  * The <code>SystemGroup</code> implements the objects
37  * @author Frank Fock
38  * @version 1.0
39  */

40 public class SNMPv2MIB implements MOGroup, CounterListener, AgentCapabilityList {
41
42   // Constants
43
private static final OID snmpStatsPrefix =
44       new OID(new int[] { 1,3,6,1,2,1,11 });
45   private static final OID usmStatsPrefix =
46       new OID(new int[] { 1,3,6,1,6,3,15,1,1 });
47
48
49   private static final OID oidSnmpInPkts =
50       SnmpConstants.snmpInPkts;
51   private static final OID oidSnmpInBadVersions =
52       SnmpConstants.snmpInBadVersions;
53   private static final OID oidSnmpInBadCommunityNames =
54       SnmpConstants.snmpInBadCommunityNames;
55   private static final OID oidSnmpInBadCommunityUses =
56       SnmpConstants.snmpInBadCommunityUses;
57   private static final OID oidSnmpInASNParseErrs =
58       SnmpConstants.snmpInASNParseErrs;
59   private static final OID oidSnmpSilentDrops =
60       SnmpConstants.snmpSilentDrops;
61   private static final OID oidSnmpProxyDrops =
62       SnmpConstants.snmpProxyDrops;
63
64   private static final OID oidSysORLastChange =
65     new OID(new int[] { 1,3,6,1,2,1,1,8,0 });
66
67   private static final OID oidSnmpEnableAuthenTraps =
68     new OID(new int[] { 1,3,6,1,2,1,11,30,0 });
69
70   private static final OID oidSnmpSetSerialNo =
71       new OID(new int[] { 1,3,6,1,6,3,1,1,6,1,0 });
72
73
74   private MOScalar descr;
75   private SysUpTimeImpl upTime;
76   private MOScalar objectID;
77   private MOScalar contact;
78   private MOScalar name;
79   private MOScalar location;
80   private MOScalar services;
81   private MOScalar snmpEnableAuthenTraps;
82   private MOScalar snmpSetSerialNo;
83   private MOScalar sysORLastChange;
84
85   private static final OID[] snmpStatsOIDs = {
86       oidSnmpInPkts, oidSnmpInBadVersions, oidSnmpInBadCommunityNames,
87       oidSnmpInBadCommunityUses, oidSnmpInASNParseErrs,
88       oidSnmpSilentDrops, oidSnmpProxyDrops
89   };
90
91   private MOScalar[] snmpStats;
92
93   private static final OID oidSysOREntry =
94       new OID(new int[] { 1,3,6,1,2,1,1,9,1 });
95
96   // Column sub-identifer defintions for sysOREntry:
97
private static final int colSysORID = 2;
98   private static final int colSysORDescr = 3;
99   private static final int colSysORUpTime = 4;
100
101   // Column index defintions for sysOREntry:
102
private static final int idxSysORID = 0;
103   private static final int idxSysORDescr = 1;
104   private static final int idxSysORUpTime = 2;
105   private static MOTableSubIndex[] sysOREntryIndexes =
106       new MOTableSubIndex[] {
107       new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1) };
108
109   private static MOTableIndex sysOREntryIndex =
110       new MOTableIndex(sysOREntryIndexes, false);
111
112   private DefaultMOTable sysOREntry;
113   private DefaultMOMutableTableModel sysOREntryModel;
114
115   // Context to sysUpTime Map
116
private static Map JavaDoc sysUpTimes = Collections.synchronizedMap(new HashMap JavaDoc());
117
118   public SNMPv2MIB(OctetString sysDescr,
119                    OID sysOID,
120                    Integer32 sysServices) {
121     this.descr = new MOScalar(new OID(SnmpConstants.sysDescr),
122                               MOAccessImpl.ACCESS_READ_ONLY,
123                               sysDescr);
124     this.upTime = new SysUpTimeImpl();
125     this.objectID = new MOScalar(new OID(SnmpConstants.sysObjectID),
126                                  MOAccessImpl.ACCESS_READ_ONLY,
127                                  sysOID);
128     this.contact =
129         new DisplayStringScalar(new OID(SnmpConstants.sysContact),
130                                 MOAccessImpl.ACCESS_READ_WRITE,
131                                 new OctetString());
132     this.name =
133         new DisplayStringScalar(new OID(SnmpConstants.sysName),
134                                 MOAccessImpl.ACCESS_READ_WRITE,
135                                 new OctetString());
136     this.location =
137         new DisplayStringScalar(new OID(SnmpConstants.sysLocation),
138                                 MOAccessImpl.ACCESS_READ_WRITE,
139                                 new OctetString());
140     this.services = new MOScalar(new OID(SnmpConstants.sysServices),
141                                  MOAccessImpl.ACCESS_READ_ONLY,
142                                  sysServices);
143     snmpEnableAuthenTraps =
144       new EnumeratedScalar(oidSnmpEnableAuthenTraps,
145                            MOAccessImpl.ACCESS_READ_WRITE,
146                            new Integer32(SnmpEnableAuthenTrapsEnum.enabled),
147                            new int[] { SnmpEnableAuthenTrapsEnum.enabled,
148                                        SnmpEnableAuthenTrapsEnum.disabled });
149     snmpSetSerialNo = new TestAndIncr(oidSnmpSetSerialNo);
150     sysORLastChange =
151         new TimeStampScalar(oidSysORLastChange,
152                             MOAccessImpl.ACCESS_READ_ONLY, upTime);
153     createSysOREntry();
154     createSnmpStats();
155   }
156
157   private void createSnmpStats() {
158     snmpStats = new MOScalar[snmpStatsOIDs.length];
159     for (int i=0; i<snmpStatsOIDs.length; i++) {
160       snmpStats[i] = new MOScalar(snmpStatsOIDs[i],
161                                   MOAccessImpl.ACCESS_READ_ONLY,
162                                   new Counter32(0));
163     }
164   }
165
166   private void createSysOREntry() {
167     MOColumn[] sysOREntryColumns = new MOColumn[3];
168     sysOREntryColumns[idxSysORID] =
169       new MOColumn(colSysORID,
170                    SMIConstants.SYNTAX_OBJECT_IDENTIFIER,
171                    MOAccessImpl.ACCESS_READ_ONLY);
172     sysOREntryColumns[idxSysORDescr] =
173       new DisplayString(colSysORDescr,
174                         MOAccessImpl.ACCESS_READ_ONLY,
175                         null,
176                         true);
177     sysOREntryColumns[idxSysORUpTime] =
178       new MOColumn(colSysORUpTime,
179                    SMIConstants.SYNTAX_TIMETICKS,
180                    MOAccessImpl.ACCESS_READ_ONLY);
181
182     sysOREntry =
183       new SysOREntry(oidSysOREntry,
184                      sysOREntryIndex,
185                      sysOREntryColumns);
186     sysOREntryModel = new DefaultMOMutableTableModel();
187     sysOREntry.setModel(sysOREntryModel);
188   }
189
190   protected void updateSysORLastChange() {
191     sysORLastChange.setValue(getUpTime());
192   }
193
194   public OID addSysOREntry(OID sysORID, OctetString sysORDescr) {
195     OID index = new OID(new int[] { sysOREntryModel.getRowCount()+1 });
196     Variable[] values = new Variable[sysOREntry.getColumnCount()];
197     int n = 0;
198     values[n++] = sysORID;
199     values[n++] = sysORDescr;
200     values[n++] = upTime.get();
201     DefaultMOTableRow row = new DefaultMOTableRow(index, values);
202     sysOREntryModel.addRow(row);
203     updateSysORLastChange();
204     return index;
205   }
206
207   public MOTableRow removeSysOREntry(OID index) {
208     updateSysORLastChange();
209     return sysOREntryModel.removeRow(index);
210   }
211
212   public void registerMOs(MOServer server, OctetString context)
213       throws DuplicateRegistrationException
214   {
215     server.register(descr, context);
216     server.register(upTime, context);
217     sysUpTimes.put(context, upTime);
218     server.register(objectID, context);
219     server.register(contact, context);
220     server.register(name, context);
221     server.register(location, context);
222     server.register(services, context);
223     server.register(snmpEnableAuthenTraps, context);
224     server.register(snmpSetSerialNo, context);
225     server.register(sysORLastChange, context);
226     for (int i=0; i<snmpStats.length; i++) {
227       server.register(snmpStats[i], context);
228     }
229     server.register(sysOREntry, context);
230   }
231
232   public void unregisterMOs(MOServer server, OctetString context) {
233     server.unregister(descr, context);
234     server.unregister(upTime, context);
235     sysUpTimes.remove(context);
236     server.unregister(objectID, context);
237     server.unregister(contact, context);
238     server.unregister(name, context);
239     server.unregister(location, context);
240     server.unregister(services, context);
241     server.unregister(snmpEnableAuthenTraps, context);
242     server.unregister(snmpSetSerialNo, context);
243     for (int i=0; i<snmpStats.length; i++) {
244       server.unregister(snmpStats[i], context);
245     }
246     server.unregister(sysOREntry, context);
247   }
248
249   public static final class SnmpEnableAuthenTrapsEnum {
250     public static final int enabled = 1;
251     public static final int disabled = 2;
252   }
253
254   public static class SysUpTimeImpl extends MOScalar implements SysUpTime {
255
256     private long startTime;
257
258     public SysUpTimeImpl() {
259       super(new OID(SnmpConstants.sysUpTime), MOAccessImpl.ACCESS_READ_ONLY,
260             new TimeTicks(0));
261       startTime = System.currentTimeMillis();
262     }
263
264     public Variable getValue() {
265       long ticks = (System.currentTimeMillis() - startTime) / 10;
266       TimeTicks v = new TimeTicks(ticks & 0xFFFFFFFFL);
267       return v;
268     }
269
270     public TimeTicks get() {
271       return (TimeTicks)getValue();
272     }
273
274   }
275
276   public OctetString getContact() {
277     return (OctetString)contact.getValue();
278   }
279
280   public void setContact(OctetString contact) {
281     this.contact.setValue(contact);
282   }
283
284   public OctetString getDescr() {
285     return (OctetString)descr.getValue();
286   }
287
288   public OctetString getLocation() {
289     return (OctetString)location.getValue();
290   }
291
292   public void setLocation(OctetString sysLocation) {
293     this.location.setValue(sysLocation);
294   }
295
296   public OctetString getName() {
297     return (OctetString)name.getValue();
298   }
299
300   public void setName(OctetString sysName) {
301     this.name.setValue(sysName);
302   }
303
304   public OID getObjectID() {
305     return (OID)objectID.getValue();
306   }
307
308   public Integer32 getServices() {
309     return (Integer32)services.getValue();
310   }
311
312   public TimeTicks getUpTime() {
313     return (TimeTicks)upTime.getValue();
314   }
315
316   public SysUpTime getSysUpTime() {
317     return upTime;
318   }
319
320   public void incrementCounter(CounterEvent event) {
321     if ((event.getOid().startsWith(snmpStatsPrefix)) &&
322         (event.getOid().size() > snmpStatsPrefix.size())) {
323       int suffix = event.getOid().get(snmpStatsPrefix.size());
324       for (int i=0; i<snmpStatsOIDs.length; i++) {
325         if (suffix == snmpStatsOIDs[i].get(snmpStatsPrefix.size())) {
326           Counter32 current = (Counter32) snmpStats[i].getValue();
327           current.increment();
328           event.setCurrentValue((Counter32)current.clone());
329         }
330       }
331     }
332     if ((event.getOid().equals(SnmpConstants.snmpInBadCommunityNames)) ||
333         (event.getOid().startsWith(usmStatsPrefix))) {
334       if (((Integer32)snmpEnableAuthenTraps.getValue()).getValue() ==
335           SnmpEnableAuthenTrapsEnum.enabled) {
336         if (event.getSource() instanceof NotificationOriginator) {
337           ((NotificationOriginator)
338            event.getSource()).notify(new OctetString(),
339                                      SnmpConstants.authenticationFailure,
340                                      new VariableBinding[0]);
341         }
342       }
343     }
344   }
345
346   /**
347    * Returns the sysUpTime for the supplied context.
348    * @param context
349    * a context or <code>null</code> for the default context.
350    * @return
351    * the SysUpTime instance associated with the given context or
352    * <code>null</code> if such a sysUpTime instance has not been
353    * registered yet.
354    */

355   public static SysUpTime getSysUpTime(OctetString context) {
356     return (SysUpTime) sysUpTimes.get(context);
357   }
358
359   public class SysOREntry extends DefaultMOTable implements AgentCapabilityList{
360
361     public SysOREntry(OID oid, MOTableIndex index, MOColumn[] columns) {
362       super(oid, index, columns);
363     }
364
365     public OID addSysOREntry(OID sysORID, OctetString sysORDescr) {
366       return SNMPv2MIB.this.addSysOREntry(sysORID, sysORDescr);
367     }
368
369     public MOTableRow removeSysOREntry(OID index) {
370       return SNMPv2MIB.this.removeSysOREntry(index);
371     }
372
373   }
374
375 }
376
Popular Tags