📄 nsclientmanager.java
字号:
package org.opennms.netmgt.poller.nsclient;import java.util.HashMap;import java.util.Map;import java.util.Iterator;import java.io.IOException;import java.io.InterruptedIOException;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.net.ConnectException;import java.net.InetSocketAddress;import java.net.NoRouteToHostException;import java.net.Socket;import java.net.UnknownHostException;/** * <P> * This class is designed to be used by plugins, services and programs to * perform checks against an NSClient service. * <P> * To use it you must first create an instance of the manager with the host, * port and/or password. Then you can set the timeout for the socket, if you * want to override DEFAULT_SOCKET_TIMEOUT. Once you have set up the manager, * you call the init() method to connect to the service. Once connected you * use the processCheckCommand() method to receive a NsclientPacket object * containing the response and the result code. Here's an example of using * this manager: <CODE> NsclientCheckParams params = new * NsclientCheckParams(critPerc, warnPerc, parameter); NsclientManager client = * new NsclientManager(host.getHostAddress(), port); client.init(); * NsclientPacket * pack=client.processCheckCommand(NsclientManager.convertStringToType(command), * params); </CODE> * <P> * * @author <A HREF="mailto:matt.raykowski@gmail.com">Matt Raykowski </A> * @author <A HREF="http://www.opennsm.org">OpenNMS </A> */public class NsclientManager { /** * The default socket timeout. */ public static int DEFAULT_SOCKET_TIMEOUT = 5000; /** * The default NSClient TCP port. */ public static int DEFAULT_PORT = 1248; /** * Stores the host name the manager is connect(ing/ed) to. */ private String m_HostName = null; /** * Stores the port the manager is connect(ing/ed) to. Set to DEFAULT_PORT. */ private int m_PortNumber = DEFAULT_PORT; /** * The password to use when requesting a check. Default is "None" */ private String m_Password = "None"; /** * Stores the socket used to connect to the service. */ private Socket m_Socket = null; /** * This is used for receiving input from the server. */ private BufferedInputStream m_BufInStream = null; private ByteArrayOutputStream m_ByteArrayOutStream = null; private int m_Timeout = DEFAULT_SOCKET_TIMEOUT; /** * Default check type. Not supported. */ public static final short CHECK_NONE = 0; /** * The ID for checking the remote client version. */ public static final short CHECK_CLIENTVERSION = 1; /** * The ID for checking the remote CPU usage. */ public static final short CHECK_CPULOAD = 2; /** * The ID for checking the remote uptime. */ public static final short CHECK_UPTIME = 3; /** * The ID for checking the remote used disk space. */ public static final short CHECK_USEDDISKSPACE = 4; /** * The ID for checking the state of a remote service. */ public static final short CHECK_SERVICESTATE = 5; /** * The ID for checking the state of a remote process. */ public static final short CHECK_PROCSTATE = 6; /** * The ID for checking the state of the remote memory usage. */ public static final short CHECK_MEMUSE = 7; /** * The ID for checking the value of a remote Perfmon counter. */ public static final short CHECK_COUNTER = 8; /** * The ID for checking the age of a remote file. */ public static final short CHECK_FILEAGE = 9; /** * This check type is used by the NSClient developers as a utility for an * easy remote method of looking up potential COUNTER instances. This * check type is not currently supported by this manager. */ public static final short CHECK_INSTANCES = 10; /** * Stores the String -> CHECK_ id mappings for lookups. */ public static HashMap CheckStrings = new HashMap(); /** * This static block initialzies the global check strings map with the * default values used for performing string->type->string conversions. */ static { CheckStrings.put("NONE", new Short(CHECK_NONE)); CheckStrings.put("CLIENTVERSION", new Short(CHECK_CLIENTVERSION)); CheckStrings.put("CPULOAD", new Short(CHECK_CPULOAD)); CheckStrings.put("UPTIME", new Short(CHECK_UPTIME)); CheckStrings.put("USEDDISKSPACE", new Short(CHECK_USEDDISKSPACE)); CheckStrings.put("SERVICESTATE", new Short(CHECK_SERVICESTATE)); CheckStrings.put("PROCSTATE", new Short(CHECK_PROCSTATE)); CheckStrings.put("MEMUSE", new Short(CHECK_MEMUSE)); CheckStrings.put("COUNTER", new Short(CHECK_COUNTER)); CheckStrings.put("FILEAGE", new Short(CHECK_FILEAGE)); CheckStrings.put("INSTANCES", new Short(CHECK_INSTANCES)); } /** * This method uses CheckStrings to convert from a short value such as * CHECK_CLIENTVERSION to the a string, for example "CLIENTVERSION" * * @param type * the CHECK_ type to look up in the CheckStrings map. * @return a string containing "NONE" if the short is not found in the * map, or the string in the map that corresponds to type. * @see CheckStrings * @see convertStringToType */ public static String convertTypeToString(short type) { Iterator iter = CheckStrings.entrySet().iterator(); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); short val = ((Short) e.getValue()).shortValue(); if (val == type) return (String) e.getKey(); } return "NONE"; } /** * This method uses the CheckStrings HashMap to convert from a string to a * short value. For example if you passed "CLIENTVERSION" you would * receive the value of CHECK_CLIENTVERSION in return. * * @param type * A string to look up a CHECK_ value from the CheckStrings * HashMap. * @return a short id corresponding to the CHECK_ value that matches the * string. * @see CheckStrings * @see convertTypeToString */ public static short convertStringToType(String type) { return ((Short) CheckStrings.get(type)).shortValue(); } /** * This method is used for setting the password used to perform service * checks. * * @param pass * the password to use when performing service checks. */ public void setPassword(String pass) { m_Password = pass; } /** * This method is used for overriding the port that is used to connect to * the remote service. This method must be called before calling the * init() method or it will have no effect. * * @param port * the remote service port. */ public void setPortNumber(int port) { m_PortNumber = port; } /** * Returns the port being used to connect to the remote service. * * @return the port being used to connect to the remote service. * @see init */ public int getPortNumber() { return m_PortNumber; } /** * This method is used to set the host name to connect to for performing * remote service checks. This method must be called before calling the * init() method or it will have no effect. * * @param host * the host name to connect to. * @see init */ public void setHostName(String host) { m_HostName = host; } /** * Returns the host name being used to connect to the remote service. * * @return the host name being used to connect to the remote service. */ public String getHostName() { return m_HostName; } /** * This method is used to set the TCP socket timeout to be used when * connecting to the remote service. This must be called before calling * <code>init</code> or it will have no effect. * * @param timeout * the TCP socket timeout. */ public void setTimeout(int timeout) { m_Timeout = timeout; } /** * Returns the TCP socket timeout used when connecting to the remote * service. * * @return the tcp socket timeout. */ public int getTimeout() { return m_Timeout; } /** * Constructor. * * @param host * sets the host name to connect to. */ public NsclientManager(String host) { m_HostName = host; } /** * Constructor. The password defaults to "None" * * @param host * sets the host name to connect to. * @param port * sets the port number to connect using. */ public NsclientManager(String host, int port) { m_HostName = host; m_PortNumber = port; } /** * Constructor. * * @param host * sets the host name to connect to. * @param port * sets the port number to connect using. * @param pass * sets the password to use when performing checks. */ public NsclientManager(String host, int port, String pass) { m_HostName = host; m_PortNumber = port; m_Password = pass; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -