Class NsclientManager


  • public class NsclientManager
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CHECK_CLIENTVERSION
      The ID for checking the remote client version.
      static java.lang.String CHECK_COUNTER
      The ID for checking the value of a remote Perfmon counter.
      static java.lang.String CHECK_CPULOAD
      The ID for checking the remote CPU usage.
      static java.lang.String CHECK_FILEAGE
      The ID for checking the age of a remote file.
      static java.lang.String CHECK_FILESIZE
      The ID for checking the size of a directory.
      static java.lang.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.
      static java.lang.String CHECK_MEMUSE
      The ID for checking the state of the remote memory usage.
      static java.lang.String CHECK_NONE
      Default check type.
      static java.lang.String CHECK_PROCSTATE
      The ID for checking the state of a remote process.
      static java.lang.String CHECK_SERVICESTATE
      The ID for checking the state of a remote service.
      static java.lang.String CHECK_UPTIME
      The ID for checking the remote uptime.
      static java.lang.String CHECK_USEDDISKSPACE
      The ID for checking the remote used disk space.
      static java.util.Map<java.lang.String,​java.lang.String> CheckStrings
      Stores the String -> CHECK_ id mappings for lookups.
      static int DEFAULT_PORT
      The default NSClient TCP port.
      static int DEFAULT_SOCKET_TIMEOUT
      The default socket timeout.
    • Constructor Summary

      Constructors 
      Constructor Description
      NsclientManager​(java.lang.String host)
      Constructor.
      NsclientManager​(java.lang.String host, int port)
      Constructor.
      NsclientManager​(java.lang.String host, int port, java.lang.String pass)
      Constructor.
      NsclientManager​(java.lang.String host, java.lang.String pass)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the socket.
      static java.lang.String convertStringToType​(java.lang.String type)
      This method uses the CheckStrings HashMap to convert from a string to a short value.
      static java.lang.String convertTypeToString​(java.lang.String type)
      This method uses CheckStrings to convert from a short value such as CHECK_CLIENTVERSION to the a string, for example "CLIENTVERSION"
      java.lang.String getHostName()
      Returns the host name being used to connect to the remote service.
      int getPortNumber()
      Returns the port being used to connect to the remote service.
      int getTimeout()
      Returns the TCP socket timeout used when connecting to the remote service.
      void init()
      This method creates a new socket and attempts to connect to the remote service.
      NsclientPacket processCheckCommand​(java.lang.String type, NsclientCheckParams param)
      This method determines which check method to call to create a request, send to the server and process the results.
      void setHostName​(java.lang.String host)
      This method is used to set the host name to connect to for performing remote service checks.
      void setPassword​(java.lang.String pass)
      This method is used for setting the password used to perform service checks.
      void setPortNumber​(int port)
      This method is used for overriding the port that is used to connect to the remote service.
      void setTimeout​(int timeout)
      This method is used to set the TCP socket timeout to be used when connecting to the remote service.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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_NONE

        public static final java.lang.String CHECK_NONE
        Default check type. Not supported.
        See Also:
        Constant Field Values
      • CHECK_CLIENTVERSION

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

        public static final java.lang.String CHECK_CPULOAD
        The ID for checking the remote CPU usage.
        See Also:
        Constant Field Values
      • CHECK_UPTIME

        public static final java.lang.String CHECK_UPTIME
        The ID for checking the remote uptime.
        See Also:
        Constant Field Values
      • CHECK_USEDDISKSPACE

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

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

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

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

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

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

        public static final java.lang.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 java.lang.String CHECK_FILESIZE
        The ID for checking the size of a directory. NSClient++ only.
        See Also:
        Constant Field Values
      • CheckStrings

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

      • NsclientManager

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

        public NsclientManager​(java.lang.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​(java.lang.String host,
                               int port,
                               java.lang.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​(java.lang.String host,
                               java.lang.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 java.lang.String convertTypeToString​(java.lang.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 java.lang.String convertStringToType​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.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​(java.lang.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.