nsclientplugin.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 218 行
JAVA
218 行
package org.opennms.netmgt.capsd;import java.net.InetAddress;import java.util.Map;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.capsd.AbstractPlugin;import org.opennms.netmgt.poller.nsclient.NsclientManager;import org.opennms.netmgt.poller.nsclient.NsclientException;import org.opennms.netmgt.poller.nsclient.NsclientPacket;import org.opennms.netmgt.poller.nsclient.NsclientCheckParams;import org.opennms.netmgt.utils.ParameterMap;/** * <P> * This class is designed to be used by the capabilities daemon to test * whether a NSClient service is running on the remote server and if the given * command can be successfully executed against the service. * </P> * * @author <A HREF="mailto:matt.raykowski@gmail.com">Matt Raykowski </A> * @author <A HREF="http://www.opennsm.org">OpenNMS </A> */public class NsclientPlugin extends AbstractPlugin { /** * The protocol supported by the plugin */ private final static String PROTOCOL_NAME = "NSCLIENT"; /** * Default number of retries for TCP requests. */ private final static int DEFAULT_RETRY = 0; /** * Default timeout (in milliseconds) for TCP requests. */ private final static int DEFAULT_TIMEOUT = 5000; /** * Returns the name of the protocol that this plugin checks on the target * system for support. * * @return The protocol name for this plugin. */ public String getProtocolName() { return PROTOCOL_NAME; } /** * Returns true if the protocol defined by this plugin is supported. If * the protocol is not supported then a false value is returned to the * caller. * <P> * The NsclientPlugin does not support undirected checks, we must have a * map of parameters to determine how to issue a check to the target * server. * * @param address * The address to check for support. * @return True if the protocol is supported by the address. * @throws java.lang.UnsupportedOperationException * This is always thrown by this plugin. */ public boolean isProtocolSupported(InetAddress address) { throw new UnsupportedOperationException( "Undirected TCP checking not " + "supported"); } /** * Returns true if the protocol defined by this plugin is supported. If * the protocol is not supported then a false value is returned to the * caller. The qualifier map passed to the method is used by the plugin to * return additional information by key-name. These key-value pairs can be * added to service events if needed. * <P> * The following parameters are used by this plugin: * <UL> * <LI>command - the command to be executed on this node. * <LI>port - used to override the default NSClient port. * <LI>retry - overrides the number of times to retry connecting to the * service. * <LI>timeout - tcp port timeout. * <LI>parameter - a string used for checking services. see documentation * on specific check types for use. * <LI>criticalPercent - typically a percentage used for testing results, * for example disk space used. * <LI>warningPercent - typically a percentage used for testing results, * for example memory space used. * </UL> * Protocol will return as supported only if the result code is * <code>NsclientPacket.RES_STATE_OK</code> or * <code>NsclientPacket.RES_STATE_WARNING</code>. * * @param address * The address to check for support. * @param qualifiers * The map where qualification are set by the plugin. * @return True if the protocol is supported by the address. */ public boolean isProtocolSupported(InetAddress address, Map qualifiers) { Category log = ThreadCategory.getInstance(getClass()); int retries = DEFAULT_RETRY; int timeout = DEFAULT_TIMEOUT; int port = NsclientManager.DEFAULT_PORT; String parameter = null; String command = null; int critPerc = 0, warnPerc = 0; if (qualifiers != null) { command = ParameterMap.getKeyedString( qualifiers, "command", NsclientManager.convertTypeToString(NsclientManager.CHECK_CLIENTVERSION)); port = ParameterMap.getKeyedInteger(qualifiers, "port", NsclientManager.DEFAULT_PORT); retries = ParameterMap.getKeyedInteger(qualifiers, "retry", DEFAULT_RETRY); timeout = ParameterMap.getKeyedInteger(qualifiers, "timeout", DEFAULT_TIMEOUT); parameter = ParameterMap.getKeyedString(qualifiers, "parameter", null); critPerc = ParameterMap.getKeyedInteger(qualifiers, "criticalPercent", 0); warnPerc = ParameterMap.getKeyedInteger(qualifiers, "warningPercent", 0); } // set up my check params. NsclientCheckParams params = new NsclientCheckParams(critPerc, warnPerc, parameter); // and perform the check, we'll get a packet back containing the check // data. NsclientPacket pack = isServer(address, port, command, retries, timeout, params); if (pack == null) { log.info("Received a null packet response from isServer."); return false; } // only fail on critical and unknown returns . if (pack.getResultCode() != NsclientPacket.RES_STATE_CRIT && pack.getResultCode() != NsclientPacket.RES_STATE_UNKNOWN && pack.getResultCode() != NsclientPacket.RES_STATE_WARNING) { return true; } else { return false; } } /** * <P> * Test to see if the paassed host-port pair is an endpoint for a TCP * server. If there is a TCP server at the destination value then a * connection is made using the params variable data and a check is * requested from the remote NSClient service. * </P> * * @param host * The remote host to connect to. * @param port * The remote port on the host. * @param command * The command to execute on the remote server. * @param retries * The number of retries to attempt when connecting. * @param timeout * The TCP socket timeout to use. * @param params * The NSClient parameters used to validate the response. * @return The NsclientPacket the server sent, updated by NsclientManager * to contain the proper result code based on the params passed. */ private NsclientPacket isServer(InetAddress host, int port, String command, int retries, int timeout, NsclientCheckParams params) { boolean isAServer = false; // get a logger. Category log = ThreadCategory.getInstance(getClass()); for (int attempts = 0; attempts <= retries && !isAServer; attempts++) { try { NsclientManager client = new NsclientManager( host.getHostAddress(), port); NsclientPacket response = null; client.setTimeout(timeout); client.init(); response = client.processCheckCommand( NsclientManager.convertStringToType(command), params); log.debug("NsclientPlugin: " + command + ": " + response.getResponse()); isAServer = true; log.debug("NsclientPlugin: " + command + ": " + response ); return response; } catch (NsclientException e) { log.debug("NsclientPlugin: Check failed: " + e.getMessage()); log.info("NsclientManager returned exception: " + e.getMessage() + " : " + e.getCause().getMessage()); isAServer = false; } } return null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?