Class NsclientManager


  • public class NsclientManager
    extends Object

    This class is designed to be used by plugins, services and programs to perform checks against an NSClient service.

    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: NsclientCheckParams params = new NsclientCheckParams(critPerc, warnPerc, parameter); NsclientManager client = new NsclientManager(host.getHostAddress(), port); client.init(); NsclientPacket pack=client.processCheckCommand(NsclientManager.convertStringToType(command), params);

    Author:
    Matt Raykowski, Benjamin Reed, Jeff Gehlbach
    • Field Detail

      • DEFAULT_SOCKET_TIMEOUT

        public static final int DEFAULT_SOCKET_TIMEOUT
        The default socket timeout.
        See Also:
        Constant Field Values
      • DEFAULT_PORT

        public static final int DEFAULT_PORT
        The default NSClient TCP port.
        See Also:
        Constant Field Values
      • CHECK_CLIENTVERSION

        public static final String CHECK_CLIENTVERSION
        The ID for checking the remote client version.
        See Also:
        Constant Field Values
      • CHECK_USEDDISKSPACE

        public static final String CHECK_USEDDISKSPACE
        The ID for checking the remote used disk space.
        See Also:
        Constant Field Values
      • CHECK_SERVICESTATE

        public static final String CHECK_SERVICESTATE
        The ID for checking the state of a remote service.
        See Also:
        Constant Field Values
      • CHECK_PROCSTATE

        public static final String CHECK_PROCSTATE
        The ID for checking the state of a remote process.
        See Also:
        Constant Field Values
      • CHECK_MEMUSE

        public static final String CHECK_MEMUSE
        The ID for checking the state of the remote memory usage.
        See Also:
        Constant Field Values
      • CHECK_COUNTER

        public static final String CHECK_COUNTER
        The ID for checking the value of a remote Perfmon counter.
        See Also:
        Constant Field Values
      • CHECK_FILEAGE

        public static final String CHECK_FILEAGE
        The ID for checking the age of a remote file.
        See Also:
        Constant Field Values
      • CHECK_INSTANCES

        public static final String CHECK_INSTANCES
        This check type is used by the NSClient developers as a utility for an easy remote method of looking up potential COUNTER instances.
        See Also:
        Constant Field Values
      • CHECK_FILESIZE

        public static final String CHECK_FILESIZE
        The ID for checking the size of a directory. NSClient++ only.
        See Also:
        Constant Field Values
      • CheckStrings

        public static final Map<String,​String> CheckStrings
        Stores the String -> CHECK_ id mappings for lookups.
    • Constructor Detail

      • NsclientManager

        public NsclientManager​(String host)
        Constructor.
        Parameters:
        host - sets the host name to connect to.
      • NsclientManager

        public NsclientManager​(String host,
                               int port)
        Constructor. The password defaults to "None"
        Parameters:
        host - sets the host name to connect to.
        port - sets the port number to connect using.
      • NsclientManager

        public NsclientManager​(String host,
                               int port,
                               String pass)
        Constructor.
        Parameters:
        host - sets the host name to connect to.
        port - sets the port number to connect using.
        pass - sets the password to use when performing checks.
      • NsclientManager

        public NsclientManager​(String host,
                               String pass)
        Constructor. The port number defaults to DEFAULT_PORT.
        Parameters:
        host - sets the host name to connect to.
        pass - sets the password to use when performing checks.
    • Method Detail

      • convertTypeToString

        public static String convertTypeToString​(String type)
        This method uses CheckStrings to convert from a short value such as CHECK_CLIENTVERSION to the a string, for example "CLIENTVERSION"
        Parameters:
        type - the CHECK_ type to look up in the CheckStrings map.
        Returns:
        a string containing "NONE" if the short is not found in the map, or the string in the map that corresponds to type.
        See Also:
        CheckStrings, convertStringToType(java.lang.String)
      • convertStringToType

        public static String convertStringToType​(String type)
        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.
        Parameters:
        type - A string to look up a CHECK_ value from the CheckStrings HashMap.
        Returns:
        a short id corresponding to the CHECK_ value that matches the string.
        See Also:
        CheckStrings, convertTypeToString(java.lang.String)
      • setPassword

        public void setPassword​(String pass)
        This method is used for setting the password used to perform service checks.
        Parameters:
        pass - the password to use when performing service checks.
      • setPortNumber

        public void setPortNumber​(int port)
        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.
        Parameters:
        port - the remote service port.
      • getPortNumber

        public int getPortNumber()
        Returns the port being used to connect to the remote service.
        Returns:
        the port being used to connect to the remote service.
        See Also:
        init()
      • setHostName

        public void setHostName​(String host)
        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.
        Parameters:
        host - the host name to connect to.
        See Also:
        init()
      • getHostName

        public String getHostName()
        Returns the host name being used to connect to the remote service.
        Returns:
        the host name being used to connect to the remote service.
      • setTimeout

        public void setTimeout​(int timeout)
        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 init or it will have no effect.
        Parameters:
        timeout - the TCP socket timeout.
      • getTimeout

        public int getTimeout()
        Returns the TCP socket timeout used when connecting to the remote service.
        Returns:
        the tcp socket timeout.
      • init

        public void init()
                  throws NsclientException
        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.
      • close

        public void close()
        Closes the socket.
      • processCheckCommand

        public NsclientPacket processCheckCommand​(String type,
                                                  NsclientCheckParams param)
                                           throws NsclientException
        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.
        Parameters:
        type - the short ID of the type of check to be processed.
        param - the object containing the parameters for performing checks on the respones from the remote service.
        Returns:
        the NsclientPacket as processed by the check command method that is called.
        Throws:
        NsclientException - this method rethrows org.opennms.netmgt.poller.nsclient.NsclientExceptions caused by the check commands.