Class Symbiosis::Monitor::Check
In: monit/lib/symbiosis/monitor/check.rb
Parent: Object
Array Utmp EventMachine::Connection ApacheLogger StandardError CrontabFormatError Enumerable IPAddr ::IPAddr Host Range Domain ConfigFile Prosody Tinydns Apache Webalizer Directory IPListDirectory TemplateDirectory Domains Alert Crontab CrontabRecord StateDB Check Runner TCPConnection Test Process BlacklistDB Template Logtail Pattern Ports Blacklist Monitor ConfigFiles Utils Firewall Symbiosis dot/f_0.png

This class is the parent class that can be used by individual service tests run by symbiosis-monit.

This class should be inherited by tests to do checks. Currently this class can check processes, TCP banners/responses, and can restart a process if needed.

An example test:

 #
 # Inherit the Check class.
 #
 class SshdCheck < Symbiosis::Monitor::Check

  #
  # Return the port as needed
  #
  attr_reader :port

  #
  # Set up some defailt locations
  # -- the pidfile, initscript, process name and TCP port.
  #
  def initialize
    super
    @process.pidfile = "/var/run/sshd.pid"
    @process.initscript = "/etc/init.d/ssh"
    @name = "sshd"
    @port = 22

    #
    # See if the port can be gleaned from the SSH config.
    #
    if ( File.exists?( "/etc/sshd/sshd_config" ) )

      File("file").readlines.each |l|
        @port = $1.to_i if l =~ /^Port\s+(\d+)/
    end
  end

  #
  # Run the check -- this overrides the default do_check class in
  # Symbiosis::Monitor::Check
  #
  def do_check
    #
    # Check our initscript, and return a config error if it is wrong.
    #
    return SystemExit::EX_CONFIG unless initscript_ok?

    #
    # Do the process check
    #
    r = do_process_check

    #
    # Restart if the process check returns a temporary error.
    #
    self.restart if SystemExit::EX_TEMPFAIL == r

    #
    # Return if the process check wasn't successful.
    #
    return r unless r.successful?

    #
    # Set up a TCP connection test.
    #
    tcpconnection = Symbiosis::Monitor::TCPConnection.new(
      "localhost", @port, [nil,"SSH-2.0-OpenSSH-4.3p2\n"]
    )

    #
    # Run the TCP connection check,
    #
    r = do_tcpconnection_check(tcpconnection)

    #
    # Try to restart if the check returned a temporary failure.
    #
    self.restart if SystemExit::EX_TEMPFAIL == r

    #
    # Finally return the result from the TCP check.
    #
    return r
  end

  #
  # This method is used in the TCP connection test to check the TCP responses.
  #
  def do_response_check(responses)
    raise "Unexpected response '#{responses.first}'" unless responses.first =~ /^SSH/
  end

 end

 #
 # If this file is called as a script, run the check.
 #
 exit SshdCheck.new.do_check if $0 == __FILE__

Methods

Attributes

name  [R]  The name of the process to check.

Public Class methods

Checks if dpkg/apt/aptitude is running.

Public Instance methods

This tests a TCP connection and the responses it receives. It takes a single argument of a Symbiosis::Monitor::TCPConnection object

Should we run the test?

[Validate]