📄 nsclientmanager.java
字号:
/** * Constructor. The port number defaults to <code>DEFAULT_PORT</code>. * * @param host * sets the host name to connect to. * @param pass * sets the password to use when performing checks. */ public NsclientManager(String host, String pass) { m_HostName = host; m_Password = pass; } /** * Constructor. Made private to prevent construction without parameters. */ private NsclientManager() { // nothing to do, don't allow it. } /** * This method creates a new socket and attempts to connect to the remote * service. The input and output streams are created after the socket is * connected. * * @throws NsclientException * if the hostname is unknown if the connection is refused if * there is no route to the host if the host did not respond * if there was an unexpected IO error. The thrown exception * contains the causing exception. */ public void init() throws NsclientException { try { // set up socket m_Socket = new Socket(); m_Socket.connect(new InetSocketAddress(m_HostName, m_PortNumber), m_Timeout); m_Socket.setSoTimeout(m_Timeout); // get buffer streams for read/write. m_BufInStream = new BufferedInputStream(m_Socket.getInputStream()); m_ByteArrayOutStream = new ByteArrayOutputStream(); // handle exceptions. } catch (UnknownHostException e) { throw new NsclientException("Unknown host: " + m_HostName, e); } catch (ConnectException e) { throw new NsclientException("Connection refused to " + m_HostName + ":" + m_PortNumber, e); } catch (NoRouteToHostException e) { throw new NsclientException("Unable to connect to host: " + m_HostName + ", no route to host.", e); // there was something here about UndeclaredThrowableException(e) } catch (InterruptedIOException e) { throw new NsclientException("Unable to connect to host: " + m_HostName + ", exceeded timeout of " + m_Timeout); } catch (IOException e) { throw new NsclientException( "An unexpected I/O exception occured connecting to host: " + m_HostName + ":" + m_PortNumber, e); } } /** * Closes the socket. */ public void close() { try { m_Socket.close(); } catch (Exception e) { e.printStackTrace(); } } /** * This method sends the request string to the remote service. * * @param request * the request string to be sent to the remote service * @return a <code>NsclientPacket</code> containing the response from * the remote service. * @throws NsclientException * is thrown if there is an IO error with send/receiving * to/from the socket. */ private NsclientPacket sendCheckRequest(String request) throws NsclientException { byte[] buffer = new byte[1024]; m_ByteArrayOutStream.reset(); try { m_Socket.getOutputStream().write(request.getBytes()); m_Socket.getOutputStream().flush(); int read = m_BufInStream.read(buffer); if (read > 0) m_ByteArrayOutStream.write(buffer, 0, read); return new NsclientPacket(m_ByteArrayOutStream.toString()); } catch (Exception e) { throw new NsclientException("Unknown exception: " + e.getMessage(), e); } } /** * This method determines which check method to call to create a request, * send to the server and process the results. It merely determines the * method to be called based on the type param. * * @param type * the short ID of the type of check to be processed. * @param param * the object containing the parameters for performing checks * on the respones from the remote service. * @return the NsclientPacket as processed by the check command method * that is called. * @throws NsclientException * this method rethrows NsclientExceptions caused by the check * commands. */ public NsclientPacket processCheckCommand(short type, NsclientCheckParams param) throws NsclientException { try { switch (type) { case CHECK_CLIENTVERSION: return checkClientVersion(param); case CHECK_CPULOAD: return checkCpuLoad(param); case CHECK_UPTIME: return checkUptime(param); case CHECK_SERVICESTATE: return checkServiceState(param); case CHECK_USEDDISKSPACE: return checkUsedDiskSpace(param); case CHECK_PROCSTATE: return checkProcState(param); case CHECK_MEMUSE: return checkMemoryUsage(param); case CHECK_COUNTER: return checkPerfCounter(param); case CHECK_FILEAGE: return checkFileAge(param); } return null; } catch (NsclientException e) { throw e; } } /** * This method performs a check of the client version on the remote * service. From the <code>NsclientCheckParams</code> object passed to * this method only the 'parameter' string is used, this contains the four * digit version number which should be formatted like: 2.0.1.0 If the * parameter does not contain for period delimited digits, the check will * return the packet with with * <code>NsclientPacket.RES_STATE_UNKNOWN</code> for a result code. * * @param param * The param string member of this value contains the minimum * client version. * @return the processed <code>NsclientPacket</code>. * @throws NsclientException * this method rethrows the exception thrown by * <code>sendCheckRequest</code> */ private NsclientPacket checkClientVersion(NsclientCheckParams param) throws NsclientException { NsclientPacket pack = null; // get the client version response. try { pack = sendCheckRequest(m_Password + "&" + CHECK_CLIENTVERSION); } catch (NsclientException e) { throw e; } // Check for "ERROR" string. if (pack.getResponse().matches(".*ERROR.*")) { pack.setResultCode(NsclientPacket.RES_STATE_UNKNOWN); return pack; } // if we're not checking the clientversion, just return OK. if (param.getParamString() == null || param.getParamString().equals("")) { pack.setResultCode(NsclientPacket.RES_STATE_OK); return pack; } else { // otherwise, if we are checking, split it up into four octets and // compare. pack.setResultCode(NsclientPacket.RES_STATE_CRIT); String[] minimum = param.getParamString().split("\\."); String[] remote = pack.getResponse().split("\\."); // make sure they both contain the same number of version digits. if (remote.length != 4 || minimum.length != 4) { pack.setResultCode(NsclientPacket.RES_STATE_UNKNOWN); return pack; } // then convert them to arrays. Integer[] remVer = { new Integer(Integer.parseInt(remote[0])), new Integer(Integer.parseInt(remote[1])), new Integer(Integer.parseInt(remote[2])), new Integer(Integer.parseInt(remote[3])) }; Integer[] minVer = { new Integer(Integer.parseInt(minimum[0])), new Integer(Integer.parseInt(minimum[1])), new Integer(Integer.parseInt(minimum[2])), new Integer(Integer.parseInt(minimum[3])) }; if (remVer[0].compareTo(minVer[0]) > 0) { pack.setResultCode(NsclientPacket.RES_STATE_OK); } else if (remVer[0].compareTo(minVer[0]) == 0) { if (remVer[1].compareTo(minVer[1]) > 0) { pack.setResultCode(NsclientPacket.RES_STATE_OK); } else if (remVer[1].compareTo(minVer[1]) == 0) { if (remVer[2].compareTo(minVer[2]) > 0) { pack.setResultCode(NsclientPacket.RES_STATE_OK); } else if (remVer[2].compareTo(minVer[2]) == 0) { if (remVer[3].compareTo(minVer[3]) > 0) { pack.setResultCode(NsclientPacket.RES_STATE_OK); } else if (remVer[3].compareTo(minVer[3]) == 0) { pack.setResultCode(NsclientPacket.RES_STATE_OK); } } } } return pack; } } /** * This method is used to perform a check of the CPU percent in use it * higher than the warning and critical percent thresholds. * * @param param * The param warning and critical percent members are used for * validating CPU load results. * @return the processed <code>NsclientPacket</code>. * @throws NsclientException * this method rethrows the exception thrown by * <code>sendCheckRequest</code> */ private NsclientPacket checkCpuLoad(NsclientCheckParams param) throws NsclientException { NsclientPacket pack = null; try { // get the packet from the server and assume it is okay. We'll // rule it out as we go. pack = sendCheckRequest(m_Password + "&" + CHECK_CPULOAD + "&1&1&1"); pack.setResultCode(NsclientPacket.RES_STATE_OK); // Check for "ERROR" string. if (pack.getResponse().matches(".*ERROR.*")) { pack.setResultCode(NsclientPacket.RES_STATE_UNKNOWN); return pack; } // if a warning percent was configured, check it. if (param.getWarningPercent() != 0) { if (Integer.parseInt(pack.getResponse()) > param.getWarningPercent()) { pack.setResultCode(NsclientPacket.RES_STATE_WARNING); } } // if a crtical percent was configured, check it, overriding // warning percent. if (param.getCriticalPercent() != 0) { if (Integer.parseInt(pack.getResponse()) > param.getCriticalPercent()) { pack.setResultCode(NsclientPacket.RES_STATE_CRIT); } } return pack; } catch (NsclientException e) { throw e; } } /** * This method simply performs a check of the uptime from the remove * service and returns the results. No response validation is performed. * * @param param * The param member is not currently in use. * @return the processed <code>NsclientPacket</code>. * @throws NsclientException * this method rethrows the exception thrown by * <code>sendCheckRequest</code> */ private NsclientPacket checkUptime(NsclientCheckParams param) throws NsclientException { NsclientPacket pack = null; try { pack = sendCheckRequest(m_Password + "&" + CHECK_UPTIME); pack.setResultCode(NsclientPacket.RES_STATE_OK); // Check for "ERROR" string. if (pack.getResponse().matches(".*ERROR.*")) { pack.setResultCode(NsclientPacket.RES_STATE_UNKNOWN); return pack; } return pack; } catch (NsclientException e) { throw e; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -