snmptraphelper.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,139 行 · 第 1/3 页

JAVA
1,139
字号
         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) {                try {                    return new SnmpVarBind(name, new SnmpIPAddress(value));                }                catch (SnmpBadConversionException e) {                    throw new SnmpTrapHelperException("Value " + value + "is invalid, or host unknown for SnmpIPAddress");                }            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpIPAddress");            }        }    }    /**     * Variable binding factory for SnmpTimeTicks     */    private class SnmpTimeTicksFactory implements VarBindFactory {        /**         * Constructs a new SnmpVarBind with the specified name and value. The         * value will be encoded as an SnmpTimeTicks. The value is assumed to         * have been encoded with the specified encoding (only XML_ENCODING_TEXT         * is supported).         *          * @param name         *            The name (a.k.a. "id") of the variable binding to be         *            created         * @param encoding         *            Describes the way in which the value content has been         *            encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)         * @param value         *            The variable binding value         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) {                try {                    return new SnmpVarBind(name, new SnmpTimeTicks(value));                }                catch (IllegalArgumentException e) {                    throw new SnmpTrapHelperException("Value " + value + "is invalid for SnmpTimeTicks");                }                catch (NullPointerException e) {                    throw new SnmpTrapHelperException("Value is null for SnmpTimeTicks");                }            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpTimeTicks");            }        }    }    /**     * Variable binding factory for SnmpCounter32     */    private class SnmpCounter32Factory implements VarBindFactory {        /**         * Constructs a new SnmpVarBind with the specified name and value. The         * value will be encoded as an SnmpCounter32. The value is assumed to         * have been encoded with the specified encoding (only XML_ENCODING_TEXT         * is supported).         *          * @param name         *            The name (a.k.a. "id") of the variable binding to be         *            created         * @param encoding         *            Describes the way in which the value content has been         *            encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)         * @param value         *            The variable binding value         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) {                try {                    return new SnmpVarBind(name, new SnmpCounter32(value));                }                catch (IllegalArgumentException e) {                    throw new SnmpTrapHelperException("Value " + value + "is invalid for SnmpCounter32");                }                catch (NullPointerException e) {                    throw new SnmpTrapHelperException("Value is null for SnmpCounter32");                }            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpCounter32");            }        }    }    /**     * Variable binding factory for SnmpGuage32     */    private class SnmpGauge32Factory implements VarBindFactory {        /**         * Constructs a new SnmpVarBind with the specified name and value. The         * value will be encoded as an SnmpGuage32. The value is assumed to have         * been encoded with the specified encoding (only XML_ENCODING_TEXT is         * supported).         *          * @param name         *            The name (a.k.a. "id") of the variable binding to be         *            created         * @param encoding         *            Describes the way in which the value content has been         *            encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)         * @param value         *            The variable binding value         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) {                try {                    return new SnmpVarBind(name, new SnmpGauge32(value));                }                catch (IllegalArgumentException e) {                    throw new SnmpTrapHelperException("Value " + value + "is invalid for SnmpGauge32");                }                catch (NullPointerException e) {                    throw new SnmpTrapHelperException("Value is null for SnmpGauge32");                }            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpGauge32");            }        }    }    /**     * Variable binding factory for SnmpOpaque     */    private class SnmpOpaqueFactory implements VarBindFactory {        /**         * Constructs a new SnmpVarBind with the specified name and value. The         * value will be encoded as an SnmpOpaque. The value is assumed to have         * been encoded with the specified encoding (only XML_ENCODING_BASE64 is         * supported).         *          * @param name         *            The name (a.k.a. "id") of the variable binding to be         *            created         * @param encoding         *            Describes the way in which the value content has been         *            encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)         * @param value         *            The variable binding value         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_BASE64.equals(encoding)) {                return new SnmpVarBind(name, new SnmpOpaque(Base64.decodeBase64(value.toCharArray())));            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpOpaque");            }        }    }    /**     * Variable binding factory for SnmpCounter64     */    private class SnmpCounter64Factory implements VarBindFactory {        /**         * Constructs a new SnmpVarBind with the specified name and value. The         * value will be encoded as an SnmpCounter64. The value is assumed to         * have been encoded with the specified encoding (only XML_ENCODING_TEXT         * is supported).         *          * @param name         *            The name (a.k.a. "id") of the variable binding to be         *            created         * @param encoding         *            Describes the way in which the value content has been         *            encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)         * @param value         *            The variable binding value         *          * @return The newly-created variable binding         * @exception Throws         *                SnmpTrapHelperException if the variable binding cannot         *                be created for any reason (e.g. encoding not         *                supported, invalid value, etc.).         */        public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException {            if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) {                try {                    return new SnmpVarBind(name, new SnmpCounter64(value));                }                catch (IllegalArgumentException e) {                    throw new SnmpTrapHelperException("Value " + value + "is invalid for SnmpCounter64");                }                catch (NullPointerException e) {                    throw new SnmpTrapHelperException("Value is null for SnmpCounter64");                }            } else {                throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpCounter64");            }        }    }    /**     * Send the specified SNMP V1 trap to the specified address and port, with     * the specified community string.     *      * @param community     *            The community string to be used.     * @param trap     *            The trap to be sent.     * @param destAddr     *            The IP address to which the trap should be sent.     * @param destPort     *            The port to which the trap should be sent.     *      * @exception Throws     *                SnmpTrapHelperException if the trap cannot be sent for any     *                reason.     */    public void sendTrap(String community, SnmpPduTrap trap, String destAddr, int destPort) throws SnmpTrapHelperException {        try {            if (m_trapSession != null) {                SnmpPeer peer = new SnmpPeer(InetAddress.getByName(destAddr), destPort);                peer.setParameters(new SnmpParameters(community));                m_trapSession.send(peer, trap);            }        }        catch (SnmpPduEncodingException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }        catch (AsnEncodingException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }        catch (IOException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }    }    /**     * Send the specified SNMP V2 trap to the specified address and port, with     * the specified community string.     *      * @param community     *            The community string to be used.     * @param packet     *            The trap to be sent.     * @param destAddr     *            The IP address to which the trap should be sent.     * @param destPort     *            The port to which the trap should be sent.     *      * @exception Throws     *                SnmpTrapHelperException if the trap cannot be sent for any     *                reason.     */    public void sendTrap(String community, SnmpPduPacket packet, String destAddr, int destPort) throws SnmpTrapHelperException {        try {            if (m_trapSession != null) {                SnmpPeer peer = new SnmpPeer(InetAddress.getByName(destAddr), destPort);                peer.setParameters(new SnmpParameters(community));                m_trapSession.send(peer, packet);            }        }        catch (SnmpPduEncodingException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }        catch (AsnEncodingException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }        catch (IOException e) {            throw new SnmpTrapHelperException("Failed to send trap", e);        }    }    /**     * Create an SNMP V1 trap with the specified enterprise IS, agent address,     * generic ID, specific ID, and time stamp.     *      * @param entId     *            The enterprise ID for the trap.     * @param agentAddr     *            The agent address for the trap.     * @param generic     *            The generic ID for the trap.     * @param specific     *            The specific ID for the trap.     * @param timeStamp     *            The time stamp for the trap.     *      * @return The newly-created trap.     */    public SnmpPduTrap createV1Trap(String entId, String agentAddr, int generic, int specific, long timeStamp) {        SnmpPduTrap trap = new SnmpPduTrap();        trap.setEnterprise(entId);        IPv4Address ipv4AgentAddr = new IPv4Address(agentAddr);        trap.setAgentAddress(new SnmpIPAddress(ipv4AgentAddr.getAddressBytes()));        trap.setGeneric(generic);        trap.setSpecific(specific);        trap.setTimeStamp(timeStamp);        return trap;    }    /**     * Create an SNMP V2 trap with the specified trap object ID, and sysUpTime     * value.     *      * @param trapOid     *            The trap object id.     * @param sysUpTime     *            The system up time.     *      * @return The newly-created trap.     * @exception Throws     *                SnmpTrapHelperException if the trap cannot be created for     *                any reason.     */    public SnmpPduPacket createV2Trap(String trapOid, String sysUpTime) throws SnmpTrapHelperException {        SnmpPduRequest packet = new SnmpPduRequest();        addVarBinding(packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, sysUpTime);        addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, trapOid);        return packet;    }    /**     * Crate a new variable binding and add it to the specified SNMP V1 trap.     * The value encoding is assumed to be XML_ENCODING_TEXT.     *      * @param trap     *            The trap to which the variable binding should be added.     * @param name     *            The name (a.k.a. "id") of the variable binding to be created

⌨️ 快捷键说明

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