📄 mibiisw.java
字号:
package no.ntnu.nav.getDeviceData.deviceplugins.MibIISw;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;import no.ntnu.nav.ConfigParser.ConfigParser;import no.ntnu.nav.SimpleSnmp.SimpleSnmp;import no.ntnu.nav.SimpleSnmp.TimeoutException;import no.ntnu.nav.event.Event;import no.ntnu.nav.event.EventQ;import no.ntnu.nav.getDeviceData.Netbox;import no.ntnu.nav.getDeviceData.dataplugins.DataContainer;import no.ntnu.nav.getDeviceData.dataplugins.DataContainers;import no.ntnu.nav.getDeviceData.dataplugins.Module.ModuleContainer;import no.ntnu.nav.getDeviceData.dataplugins.ModuleMon.ModuleMonContainer;import no.ntnu.nav.getDeviceData.dataplugins.Netbox.NetboxContainer;import no.ntnu.nav.getDeviceData.dataplugins.Netbox.NetboxData;import no.ntnu.nav.getDeviceData.dataplugins.Swport.Swport;import no.ntnu.nav.getDeviceData.dataplugins.Swport.SwportContainer;import no.ntnu.nav.getDeviceData.deviceplugins.DeviceHandler;import no.ntnu.nav.logger.Log;import no.ntnu.nav.netboxinfo.NetboxInfo;/** * <p> * DeviceHandler for collecting the standard MIB-II switch port OIDs. * </p> * * <p> * This plugin handles the following OID keys: * </p> * * <ul> * <li>From MIB-II</li> * <ul> * <li>sysname</li> * <li>sysUpTime</li> * <li>ifIndex (not used)</li> * <li>ifSpeed</li> * <li>ifHighSpeed</li> * <li>ifAdminStatus</li> * <li>ifOperStatus</li> * <li>ifDescr</li> * <li>moduleMon</li> * </ul> * </ul> * </p> * */public class MibIISw implements DeviceHandler{ public static final int HANDLE_PRI_MIB_II_SW = -30; private static String[] canHandleOids = { "sysname", "sysUpTime", "ifSpeed", "ifHighSpeed", "ifAdminStatus", "ifOperStatus", "ifDescr", "moduleMon", "vtpVlanState", }; private SimpleSnmp sSnmp; public int canHandleDevice(Netbox nb) { int v = nb.isSupportedOids(canHandleOids) ? HANDLE_PRI_MIB_II_SW : NEVER_HANDLE; Log.d("MIB_II_SW_CANHANDLE", "CHECK_CAN_HANDLE", "Can handle device: " + v); return v; } public void handleDevice(Netbox nb, SimpleSnmp sSnmp, ConfigParser cp, DataContainers containers) throws TimeoutException { Log.setDefaultSubsystem("MIB_II_SW_DEVHANDLER"); NetboxContainer nc; ModuleContainer mc; SwportContainer sc; ModuleMonContainer mmc; { DataContainer dc = containers.getContainer("NetboxContainer"); if (dc == null) { Log.w("NO_CONTAINER", "No NetboxContainer found, plugin may not be loaded"); return; } if (!(dc instanceof NetboxContainer)) { Log.w("NO_CONTAINER", "Container is not a NetboxContainer! " + dc); return; } nc = (NetboxContainer)dc; dc = containers.getContainer("ModuleContainer"); if (dc == null) { Log.w("NO_CONTAINER", "No ModuleContainer found, plugin may not be loaded"); return; } if (!(dc instanceof ModuleContainer)) { Log.w("NO_CONTAINER", "Container is not a ModuleContainer! " + dc); return; } mc = (ModuleContainer)dc; dc = containers.getContainer("SwportContainer"); if (dc == null) { Log.w("NO_CONTAINER", "No SwportContainer found, plugin may not be loaded"); return; } if (!(dc instanceof SwportContainer)) { Log.w("NO_CONTAINER", "Container is not an SwportContainer! " + dc); return; } sc = (SwportContainer)dc; dc = containers.getContainer("ModuleMonContainer"); if (dc == null) { Log.w("NO_CONTAINER", "No ModuleMonContainer found, plugin may not be loaded"); return; } if (!(dc instanceof ModuleMonContainer)) { Log.w("NO_CONTAINER", "Container is not a ModuleMonContainer! " + dc); return; } mmc = (ModuleMonContainer)dc; } String netboxid = nb.getNetboxidS(); String ip = nb.getIp(); String cs_ro = nb.getCommunityRo(); String type = nb.getType(); String sysName = nb.getSysname(); String cat = nb.getCat(); this.sSnmp = sSnmp; processMibII(nb, netboxid, ip, cs_ro, type, nc, mc, sc, mmc); // Commit data sc.commit(); } private void processMibII(Netbox nb, String netboxid, String ip, String cs_ro, String typeid, NetboxContainer nc, ModuleContainer mc, SwportContainer sc, ModuleMonContainer mmc) throws TimeoutException { if (nb.getNumInStack() > 1) { // Do moduleMon // PS40 must use a special OID Map ifindexMap = null; String baseOidAlt = null; if (nb.getOid("3cPS40PortState") != null) { baseOidAlt = nb.getOid("3cPS40PortState"); ifindexMap = new HashMap(); for (Iterator it = mmc.getQueryIfindices(netboxid); it.hasNext();) { Map.Entry me = (Map.Entry)it.next(); String module = (String)me.getKey(); List ifindexList = (List)me.getValue(); for (Iterator ifIt = ifindexList.iterator(); ifIt.hasNext();) { String ifindex = (String)ifIt.next(); if (ifindex.length() < 3) continue; String m = ifindex.substring(0, ifindex.length()-2); String p = ifindex.substring(ifindex.length()-2, ifindex.length()); ifindexMap.put(ifindex, Integer.parseInt(m)+"."+Integer.parseInt(p)); } } } String baseOid = nb.getOid("moduleMon"); String hpMemberStatusOid = nb.getOid("hpStackStatsMemberOperStatus"); if (hpMemberStatusOid != null) { Set modulesUp = new HashSet(); Map moduleStatus = new HashMap(); sSnmp.onlyAskModule("0"); List statusList = sSnmp.getAll(hpMemberStatusOid); sSnmp.onlyAskModule(null); for (Iterator it = statusList.iterator(); it.hasNext();) { String[] s = (String[])it.next(); String module; if (s.length < 4) { System.err.println("Error in ModuleMon, module not found!"); module = s[0]; } else { module = s[3]; } int status = Integer.parseInt(s[1]); moduleStatus.put(module, ""+status); if (status == 10 || status == 12) { // Up modulesUp.add(module); } } for (Iterator it = mmc.getModuleSet(netboxid).iterator(); it.hasNext();) { String module = (String)it.next(); if (modulesUp.contains(module)) { mmc.moduleUp(nb, module); } else { mmc.moduleDown(nb, module); Log.d("MODULE_MON", "HP Module " + module + " on " + nb.getSysname() + " is down ("+moduleStatus.get(module)+")"); sSnmp.ignoreModule(module); mmc.rescheduleNetbox(nb, module, "hpStackStatsMemberOperStatus"); } } mmc.commit(); } else if (baseOid != null) { for (Iterator it = mmc.getQueryIfindices(netboxid); it.hasNext();) { Map.Entry me = (Map.Entry)it.next(); String module = (String)me.getKey(); List ifindexList = (List)me.getValue(); sSnmp.onlyAskModule(module); boolean down = true; int cnt = 0; for (Iterator ifIt = ifindexList.iterator(); ifIt.hasNext() && down && cnt++ < 5;) { String ifindex = (String)ifIt.next(); String ifindexOid = sSnmp.extractIfIndexOID(ifindex); try { String askOid = (ifindexMap != null && ifindexMap.containsKey(ifindexOid) ? baseOidAlt + "." + ifindexMap.get(ifindexOid) + ".1" : baseOid + "." + ifindexOid); List l = sSnmp.getNext(askOid, 1, false, false); if (l != null && !l.isEmpty()) { // We got a response mmc.moduleUp(nb, module); down = false; } else { Log.d("MODULE_MON", "No response on askOid " + askOid + ", trying next (cnt: " + cnt + ")"); } } catch (TimeoutException te) { // Assume the module is down mmc.moduleDown(nb, module); Log.i("MODULE_MON", "Module " + module + ", ifindex " + ifindex + " on " + nb.getSysname() + " is not responding"); sSnmp.ignoreModule(module); break; } } if (down) { // Assume down mmc.moduleDown(nb, module); Log.d("MODULE_MON", "Module " + module + " on " + nb.getSysname() + " returned no values"); sSnmp.ignoreModule(module); List oidL = new ArrayList(); oidL.add("moduleMon"); if (baseOidAlt != null) oidL.add("3cPS40PortState"); if (hpMemberStatusOid != null) oidL.add("hpStackStatsMemberOperStatus"); mmc.rescheduleNetbox(nb, module, oidL); } } sSnmp.onlyAskModule(null); mmc.commit(); } else { Log.w("MODULE_MON", "Netbox " + nb.getSysname() + ", type " + nb.getType() + " does not support the moduleMon OID, skipping"); } } List l; // If vtpVlanState is supported, collect VLANs if (nb.getOid("vtpVlanState") != null) { l = sSnmp.getAll(nb.getOid("vtpVlanState")+".1"); if (l != null) { NetboxData nd = nc.netboxDataFactory(nb); for (Iterator it = l.iterator(); it.hasNext();) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -