001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - SNMPv2MIB.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent.mo.snmp;
022:
023: import org.snmp4j.agent.*;
024: import org.snmp4j.agent.mo.*;
025: import org.snmp4j.mp.*;
026: import org.snmp4j.smi.*;
027: import org.snmp4j.agent.mo.snmp.DisplayString;
028: import org.snmp4j.event.CounterListener;
029: import org.snmp4j.event.CounterEvent;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Collections;
033:
034: /**
035: * The <code>SystemGroup</code> implements the objects
036: * @author Frank Fock
037: * @version 1.0
038: */
039: public class SNMPv2MIB implements MOGroup, CounterListener,
040: AgentCapabilityList {
041:
042: // Constants
043: private static final OID snmpStatsPrefix = new OID(new int[] { 1,
044: 3, 6, 1, 2, 1, 11 });
045: private static final OID usmStatsPrefix = new OID(new int[] { 1, 3,
046: 6, 1, 6, 3, 15, 1, 1 });
047:
048: private static final OID oidSnmpInPkts = SnmpConstants.snmpInPkts;
049: private static final OID oidSnmpInBadVersions = SnmpConstants.snmpInBadVersions;
050: private static final OID oidSnmpInBadCommunityNames = SnmpConstants.snmpInBadCommunityNames;
051: private static final OID oidSnmpInBadCommunityUses = SnmpConstants.snmpInBadCommunityUses;
052: private static final OID oidSnmpInASNParseErrs = SnmpConstants.snmpInASNParseErrs;
053: private static final OID oidSnmpSilentDrops = SnmpConstants.snmpSilentDrops;
054: private static final OID oidSnmpProxyDrops = SnmpConstants.snmpProxyDrops;
055:
056: private static final OID oidSysORLastChange = new OID(new int[] {
057: 1, 3, 6, 1, 2, 1, 1, 8, 0 });
058:
059: private static final OID oidSnmpEnableAuthenTraps = new OID(
060: new int[] { 1, 3, 6, 1, 2, 1, 11, 30, 0 });
061:
062: private static final OID oidSnmpSetSerialNo = new OID(new int[] {
063: 1, 3, 6, 1, 6, 3, 1, 1, 6, 1, 0 });
064:
065: private MOScalar descr;
066: private SysUpTimeImpl upTime;
067: private MOScalar objectID;
068: private MOScalar contact;
069: private MOScalar name;
070: private MOScalar location;
071: private MOScalar services;
072: private MOScalar snmpEnableAuthenTraps;
073: private MOScalar snmpSetSerialNo;
074: private MOScalar sysORLastChange;
075:
076: private static final OID[] snmpStatsOIDs = { oidSnmpInPkts,
077: oidSnmpInBadVersions, oidSnmpInBadCommunityNames,
078: oidSnmpInBadCommunityUses, oidSnmpInASNParseErrs,
079: oidSnmpSilentDrops, oidSnmpProxyDrops };
080:
081: private MOScalar[] snmpStats;
082:
083: private static final OID oidSysOREntry = new OID(new int[] { 1, 3,
084: 6, 1, 2, 1, 1, 9, 1 });
085:
086: // Column sub-identifer defintions for sysOREntry:
087: private static final int colSysORID = 2;
088: private static final int colSysORDescr = 3;
089: private static final int colSysORUpTime = 4;
090:
091: // Column index defintions for sysOREntry:
092: private static final int idxSysORID = 0;
093: private static final int idxSysORDescr = 1;
094: private static final int idxSysORUpTime = 2;
095: private static MOTableSubIndex[] sysOREntryIndexes = new MOTableSubIndex[] { new MOTableSubIndex(
096: SMIConstants.SYNTAX_INTEGER, 1, 1) };
097:
098: private static MOTableIndex sysOREntryIndex = new MOTableIndex(
099: sysOREntryIndexes, false);
100:
101: private DefaultMOTable sysOREntry;
102: private DefaultMOMutableTableModel sysOREntryModel;
103:
104: // Context to sysUpTime Map
105: private static Map sysUpTimes = Collections
106: .synchronizedMap(new HashMap());
107:
108: public SNMPv2MIB(OctetString sysDescr, OID sysOID,
109: Integer32 sysServices) {
110: this .descr = new MOScalar(new OID(SnmpConstants.sysDescr),
111: MOAccessImpl.ACCESS_READ_ONLY, sysDescr);
112: this .upTime = new SysUpTimeImpl();
113: this .objectID = new MOScalar(
114: new OID(SnmpConstants.sysObjectID),
115: MOAccessImpl.ACCESS_READ_ONLY, sysOID);
116: this .contact = new DisplayStringScalar(new OID(
117: SnmpConstants.sysContact),
118: MOAccessImpl.ACCESS_READ_WRITE, new OctetString());
119: this .name = new DisplayStringScalar(new OID(
120: SnmpConstants.sysName), MOAccessImpl.ACCESS_READ_WRITE,
121: new OctetString());
122: this .location = new DisplayStringScalar(new OID(
123: SnmpConstants.sysLocation),
124: MOAccessImpl.ACCESS_READ_WRITE, new OctetString());
125: this .services = new MOScalar(
126: new OID(SnmpConstants.sysServices),
127: MOAccessImpl.ACCESS_READ_ONLY, sysServices);
128: snmpEnableAuthenTraps = new EnumeratedScalar(
129: oidSnmpEnableAuthenTraps,
130: MOAccessImpl.ACCESS_READ_WRITE, new Integer32(
131: SnmpEnableAuthenTrapsEnum.enabled), new int[] {
132: SnmpEnableAuthenTrapsEnum.enabled,
133: SnmpEnableAuthenTrapsEnum.disabled });
134: snmpSetSerialNo = new TestAndIncr(oidSnmpSetSerialNo);
135: sysORLastChange = new TimeStampScalar(oidSysORLastChange,
136: MOAccessImpl.ACCESS_READ_ONLY, upTime);
137: createSysOREntry();
138: createSnmpStats();
139: }
140:
141: private void createSnmpStats() {
142: snmpStats = new MOScalar[snmpStatsOIDs.length];
143: for (int i = 0; i < snmpStatsOIDs.length; i++) {
144: snmpStats[i] = new MOScalar(snmpStatsOIDs[i],
145: MOAccessImpl.ACCESS_READ_ONLY, new Counter32(0));
146: }
147: }
148:
149: private void createSysOREntry() {
150: MOColumn[] sysOREntryColumns = new MOColumn[3];
151: sysOREntryColumns[idxSysORID] = new MOColumn(colSysORID,
152: SMIConstants.SYNTAX_OBJECT_IDENTIFIER,
153: MOAccessImpl.ACCESS_READ_ONLY);
154: sysOREntryColumns[idxSysORDescr] = new DisplayString(
155: colSysORDescr, MOAccessImpl.ACCESS_READ_ONLY, null,
156: true);
157: sysOREntryColumns[idxSysORUpTime] = new MOColumn(
158: colSysORUpTime, SMIConstants.SYNTAX_TIMETICKS,
159: MOAccessImpl.ACCESS_READ_ONLY);
160:
161: sysOREntry = new SysOREntry(oidSysOREntry, sysOREntryIndex,
162: sysOREntryColumns);
163: sysOREntryModel = new DefaultMOMutableTableModel();
164: sysOREntry.setModel(sysOREntryModel);
165: }
166:
167: protected void updateSysORLastChange() {
168: sysORLastChange.setValue(getUpTime());
169: }
170:
171: public OID addSysOREntry(OID sysORID, OctetString sysORDescr) {
172: OID index = new OID(
173: new int[] { sysOREntryModel.getRowCount() + 1 });
174: Variable[] values = new Variable[sysOREntry.getColumnCount()];
175: int n = 0;
176: values[n++] = sysORID;
177: values[n++] = sysORDescr;
178: values[n++] = upTime.get();
179: DefaultMOTableRow row = new DefaultMOTableRow(index, values);
180: sysOREntryModel.addRow(row);
181: updateSysORLastChange();
182: return index;
183: }
184:
185: public MOTableRow removeSysOREntry(OID index) {
186: updateSysORLastChange();
187: return sysOREntryModel.removeRow(index);
188: }
189:
190: public void registerMOs(MOServer server, OctetString context)
191: throws DuplicateRegistrationException {
192: server.register(descr, context);
193: server.register(upTime, context);
194: sysUpTimes.put(context, upTime);
195: server.register(objectID, context);
196: server.register(contact, context);
197: server.register(name, context);
198: server.register(location, context);
199: server.register(services, context);
200: server.register(snmpEnableAuthenTraps, context);
201: server.register(snmpSetSerialNo, context);
202: server.register(sysORLastChange, context);
203: for (int i = 0; i < snmpStats.length; i++) {
204: server.register(snmpStats[i], context);
205: }
206: server.register(sysOREntry, context);
207: }
208:
209: public void unregisterMOs(MOServer server, OctetString context) {
210: server.unregister(descr, context);
211: server.unregister(upTime, context);
212: sysUpTimes.remove(context);
213: server.unregister(objectID, context);
214: server.unregister(contact, context);
215: server.unregister(name, context);
216: server.unregister(location, context);
217: server.unregister(services, context);
218: server.unregister(snmpEnableAuthenTraps, context);
219: server.unregister(snmpSetSerialNo, context);
220: for (int i = 0; i < snmpStats.length; i++) {
221: server.unregister(snmpStats[i], context);
222: }
223: server.unregister(sysOREntry, context);
224: }
225:
226: public static final class SnmpEnableAuthenTrapsEnum {
227: public static final int enabled = 1;
228: public static final int disabled = 2;
229: }
230:
231: public static class SysUpTimeImpl extends MOScalar implements
232: SysUpTime {
233:
234: private long startTime;
235:
236: public SysUpTimeImpl() {
237: super (new OID(SnmpConstants.sysUpTime),
238: MOAccessImpl.ACCESS_READ_ONLY, new TimeTicks(0));
239: startTime = System.currentTimeMillis();
240: }
241:
242: public Variable getValue() {
243: long ticks = (System.currentTimeMillis() - startTime) / 10;
244: TimeTicks v = new TimeTicks(ticks & 0xFFFFFFFFL);
245: return v;
246: }
247:
248: public TimeTicks get() {
249: return (TimeTicks) getValue();
250: }
251:
252: }
253:
254: public OctetString getContact() {
255: return (OctetString) contact.getValue();
256: }
257:
258: public void setContact(OctetString contact) {
259: this .contact.setValue(contact);
260: }
261:
262: public OctetString getDescr() {
263: return (OctetString) descr.getValue();
264: }
265:
266: public OctetString getLocation() {
267: return (OctetString) location.getValue();
268: }
269:
270: public void setLocation(OctetString sysLocation) {
271: this .location.setValue(sysLocation);
272: }
273:
274: public OctetString getName() {
275: return (OctetString) name.getValue();
276: }
277:
278: public void setName(OctetString sysName) {
279: this .name.setValue(sysName);
280: }
281:
282: public OID getObjectID() {
283: return (OID) objectID.getValue();
284: }
285:
286: public Integer32 getServices() {
287: return (Integer32) services.getValue();
288: }
289:
290: public TimeTicks getUpTime() {
291: return (TimeTicks) upTime.getValue();
292: }
293:
294: public SysUpTime getSysUpTime() {
295: return upTime;
296: }
297:
298: public void incrementCounter(CounterEvent event) {
299: if ((event.getOid().startsWith(snmpStatsPrefix))
300: && (event.getOid().size() > snmpStatsPrefix.size())) {
301: int suffix = event.getOid().get(snmpStatsPrefix.size());
302: for (int i = 0; i < snmpStatsOIDs.length; i++) {
303: if (suffix == snmpStatsOIDs[i].get(snmpStatsPrefix
304: .size())) {
305: Counter32 current = (Counter32) snmpStats[i]
306: .getValue();
307: current.increment();
308: event.setCurrentValue((Counter32) current.clone());
309: }
310: }
311: }
312: if ((event.getOid()
313: .equals(SnmpConstants.snmpInBadCommunityNames))
314: || (event.getOid().startsWith(usmStatsPrefix))) {
315: if (((Integer32) snmpEnableAuthenTraps.getValue())
316: .getValue() == SnmpEnableAuthenTrapsEnum.enabled) {
317: if (event.getSource() instanceof NotificationOriginator) {
318: ((NotificationOriginator) event.getSource())
319: .notify(
320: new OctetString(),
321: SnmpConstants.authenticationFailure,
322: new VariableBinding[0]);
323: }
324: }
325: }
326: }
327:
328: /**
329: * Returns the sysUpTime for the supplied context.
330: * @param context
331: * a context or <code>null</code> for the default context.
332: * @return
333: * the SysUpTime instance associated with the given context or
334: * <code>null</code> if such a sysUpTime instance has not been
335: * registered yet.
336: */
337: public static SysUpTime getSysUpTime(OctetString context) {
338: return (SysUpTime) sysUpTimes.get(context);
339: }
340:
341: public class SysOREntry extends DefaultMOTable implements
342: AgentCapabilityList {
343:
344: public SysOREntry(OID oid, MOTableIndex index,
345: MOColumn[] columns) {
346: super (oid, index, columns);
347: }
348:
349: public OID addSysOREntry(OID sysORID, OctetString sysORDescr) {
350: return SNMPv2MIB.this .addSysOREntry(sysORID, sysORDescr);
351: }
352:
353: public MOTableRow removeSysOREntry(OID index) {
354: return SNMPv2MIB.this.removeSysOREntry(index);
355: }
356:
357: }
358:
359: }
|