⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpmonitor.java

📁 opennms得相关源码 请大家看看
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // Instantiate new SnmpPeer object for this interface        //        SnmpPeer peer = SnmpPeerFactory.getInstance().getPeer(ipAddr, SnmpSMI.SNMPV1);        if (log.isDebugEnabled()) {            String nl = System.getProperty("line.separator");            log.debug("initialize: SnmpPeer configuration: address: " + peer.getPeer() + nl + "      version: " + ((peer.getParameters().getVersion() == SnmpSMI.SNMPV1) ? "SNMPv1" : "SNMPv2") + nl + "      timeout: " + peer.getTimeout() + nl + "      retries: " + peer.getRetries() + nl + "      read commString: " + peer.getParameters().getReadCommunity() + nl + "      write commString: " + peer.getParameters().getWriteCommunity());        }        // Add the snmp config object as an attribute of the interface        //        if (log.isDebugEnabled())            log.debug("initialize: setting SNMP peer attribute for interface " + ipAddr.getHostAddress());        iface.setAttribute(SNMP_PEER_KEY, peer);        log.debug("initialize: interface: " + ipAddr.getHostAddress() + " initialized.");        return;    }    /**     * <P>     * The poll() method is responsible for polling the specified address for     * SNMP service availability.     * </P>     *      * @param iface     *            The network interface to test the service on.     * @param parameters     *            The package parameters (timeout, retry, etc...) to be used for     *            this poll.     *      * @return The availability of the interface and if a transition event     *         should be supressed.     *      * @exception RuntimeException     *                Thrown for any uncrecoverable errors.     */    public int poll(NetworkInterface iface, Map parameters, org.opennms.netmgt.config.poller.Package pkg) {        // Log4j category        //        Category log = ThreadCategory.getInstance(getClass());        int status = SERVICE_UNAVAILABLE;        InetAddress ipaddr = (InetAddress) iface.getAddress();        SnmpSession session = null;        // Retrieve this interface's SNMP peer object        //        SnmpPeer peer = (SnmpPeer) iface.getAttribute(SNMP_PEER_KEY);        if (peer == null)            throw new RuntimeException("SnmpPeer object not available for interface " + ipaddr);        // Get configuration parameters        //        int timeout = ParameterMap.getKeyedInteger(parameters, "timeout", peer.getTimeout());        int retries = ParameterMap.getKeyedInteger(parameters, "retries", peer.getRetries());        int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);        String oid = ParameterMap.getKeyedString(parameters, "oid", DEFAULT_OBJECT_IDENTIFIER);        String operator = ParameterMap.getKeyedString(parameters, "operator", null);        String operand = ParameterMap.getKeyedString(parameters, "operand", null);        // set timeout and retries on SNMP peer object        //        peer.setTimeout(timeout);        peer.setRetries(retries);        if (log.isDebugEnabled())            log.debug("poll: service= SNMP address= " + ipaddr.getHostAddress() + " port= " + port + " oid=" + oid + " timeout= " + timeout + " retries= " + retries + " operator = " + operator + " operand = " + operand);        // Establish SNMP session with interface        //        try {            peer.setPort(port);            if (log.isDebugEnabled()) {                String nl = System.getProperty("line.separator");                log.debug("SnmpMonitor.poll: SnmpPeer configuration: address: " + peer.getPeer() + nl + "      version: " + ((peer.getParameters().getVersion() == SnmpSMI.SNMPV1) ? "SNMPv1" : "SNMPv2") + nl + "      timeout: " + peer.getTimeout() + nl + "      retries: " + peer.getRetries() + nl + "      read commString: " + peer.getParameters().getReadCommunity() + nl + "      write commString: " + peer.getParameters().getWriteCommunity());            }            session = new SnmpSession(peer);        } catch (SocketException e) {            if (log.isEnabledFor(Priority.ERROR))                log.error("poll: Error creating the SnmpSession to collect from " + ipaddr.getHostAddress(), e);            if (session != null) {                try {                    session.close();                } catch (Exception ex) {                    if (log.isInfoEnabled())                        log.info("poll: an error occured closing the SNMP session", ex);                }            }            return SERVICE_UNAVAILABLE;        }        // Need to be certain that we close the SNMP session when the data        // retrieval is completed...wrapping in a try/finally block        //        try {            // Create SNMP response handler, send SNMP Get request and            // block waiting for response.            //            SnmpResponseHandler handler = new SnmpResponseHandler();            SnmpPduPacket out = new SnmpPduRequest(SnmpPduPacket.GET, new SnmpVarBind[] { new SnmpVarBind(new SnmpObjectId(oid)) });            synchronized (handler) {                session.send(out, handler);                try {                    // wait for response for no longer than                    // (timeout)*(retries)+1000ms                    handler.wait(timeout * retries + 1000);                } catch (InterruptedException e) {                    Thread.currentThread().interrupt();                }            }            if (handler.getResult() != null) {                log.debug("poll: SNMP poll succeeded, addr=" + ipaddr.getHostAddress() + " oid=" + oid + " value=" + handler.getResult().getValue());                try {                    status = (meetsCriteria(handler.getResult().getValue(), operator, operand) ? SERVICE_AVAILABLE : SERVICE_UNAVAILABLE);                } catch (NumberFormatException e) {                    log.warn("Number operator used on a non-number " + e.getMessage());                    status = SERVICE_AVAILABLE;                } catch (IllegalArgumentException e) {                    log.warn("Invalid Snmp Criteria: " + e.getMessage());                    status = SERVICE_UNAVAILABLE;                }            } else {                log.debug("poll: SNMP poll failed, addr=" + ipaddr.getHostAddress() + " oid=" + oid);                status = SERVICE_UNAVAILABLE;            }        } catch (Throwable t) {            log.warn("poll: Unexpected exception during SNMP poll of interface " + ipaddr.getHostAddress(), t);            status = SERVICE_UNAVAILABLE;        } finally {            // Regardless of what happens with the collection, close the session            // when we're finished collecting data.            //            try {                session.close();            } catch (Exception e) {                if (log.isEnabledFor(Priority.WARN))                    log.warn("collect: An error occured closing the SNMP session for " + ipaddr.getHostAddress(), e);            }        }        return status;    }    /**     * Verifies that the result of the SNMP query meets the criteria specified     * by the operator and the operand from the configuration file.     *      * @param result     * @param operator     * @param operand     * @return     */    public boolean meetsCriteria(SnmpSyntax result, String operator, String operand) {        if (result == null)            return false;        if (operator == null || operand == null)            return true;        String value = result.toString();        if (EQUALS.equals(operator))            return operand.equals(value);        else if (NOT_EQUAL.equals(operator))            return !operand.equals(value);        else if (MATCHES.equals(operator))            return Pattern.compile(operand).matcher(value).find();        BigInteger val = null;        switch (result.typeId()) {        case SnmpSMI.SMI_INTEGER:            val = BigInteger.valueOf(((SnmpInt32) result).getValue());            break;        case SnmpSMI.SMI_COUNTER64:            val = ((SnmpCounter64) result).getValue();            break;        case SnmpSMI.SMI_GAUGE32:        case SnmpSMI.SMI_TIMETICKS:        case SnmpSMI.SMI_COUNTER32:            val = BigInteger.valueOf(((SnmpUInt32) result).getValue());            break;        default:            val = new BigInteger(result.toString());            break;        }        BigInteger intOperand = new BigInteger(operand);        if (LESS_THAN.equals(operator)) {            return val.compareTo(intOperand) < 0;        } else if (LESS_THAN_EQUALS.equals(operator)) {            return val.compareTo(intOperand) <= 0;        } else if (GREATER_THAN.equals(operator)) {            return val.compareTo(intOperand) > 0;        } else if (GREATER_THAN_EQUALS.equals(operator)) {            return val.compareTo(intOperand) >= 0;        } else {            throw new IllegalArgumentException("operator " + operator + " is unknown");        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -