diff options
| author | Steve Kemp <steve@steve.org.uk> | 2012-11-12 21:00:16 +0000 | 
|---|---|---|
| committer | Steve Kemp <steve@steve.org.uk> | 2012-11-12 21:00:16 +0000 | 
| commit | 6334b9cdfc47bd85b2ce236572e08406324d25cd (patch) | |
| tree | bd0bd3cc279d8377efde2affc8dc223bfb858ca2 /worker | |
  Initial dump of code.
Diffstat (limited to 'worker')
| -rw-r--r-- | worker/README | 13 | ||||
| -rw-r--r-- | worker/tests/README | 15 | ||||
| -rw-r--r-- | worker/tests/ftp.rb | 51 | ||||
| -rw-r--r-- | worker/tests/http.rb | 20 | ||||
| -rw-r--r-- | worker/tests/https.rb | 20 | ||||
| -rw-r--r-- | worker/tests/jabber.rb | 51 | ||||
| -rw-r--r-- | worker/tests/ldap.rb | 41 | ||||
| -rwxr-xr-x | worker/tests/ping.rb | 36 | ||||
| -rw-r--r-- | worker/tests/rsync.rb | 48 | ||||
| -rw-r--r-- | worker/tests/smtp.rb | 52 | ||||
| -rwxr-xr-x | worker/tests/ssh.rb | 50 | ||||
| -rwxr-xr-x | worker/worker | 147 | 
12 files changed, 544 insertions, 0 deletions
| diff --git a/worker/README b/worker/README new file mode 100644 index 0000000..f87decb --- /dev/null +++ b/worker/README @@ -0,0 +1,13 @@ + +  This is the worker component. + +  It is designed to pull (testing) jobs from the beanstalk queue, executing them serially. + +  The jobs will be serialized JSON objects which have the following keys in all cases: + +    * test_type => ping | ssh | mysql | http | https | ... +    * test_target => example.com | 192.168.0.1 | etc + +  Tests may implement additional keys, they will be used or ignored as appropriate. + +  There is no concept of an execution time, nor is there any threading to worry about. diff --git a/worker/tests/README b/worker/tests/README new file mode 100644 index 0000000..f40031b --- /dev/null +++ b/worker/tests/README @@ -0,0 +1,15 @@ + +  This directory contains the protocol-tests. + +  For the protocol "XXX" we must have: + +        - The file called XXX.rb + +        - The definition of the single method XXX_test + +  The test will make use of "target_host", and "test_port" (other keys may be optionally + used) and will return the single result: + +        false:  The test failed. + +        true:  The test succeeded.
\ No newline at end of file diff --git a/worker/tests/ftp.rb b/worker/tests/ftp.rb new file mode 100644 index 0000000..cd0e754 --- /dev/null +++ b/worker/tests/ftp.rb @@ -0,0 +1,51 @@ +require 'timeout' + + +# +# Run an FTP test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def ftp_test ( params ) + +  # +  #  Get the hostname & port to test against. +  # +  host = params['target_host'] +  port = params['test_port'] + +  puts "FTP testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.puts( "QUIT") + +        banner = socket.gets(nil) +        banner = banner[0,20] + +        socket.close() + +        if ( banner =~ /^220/ ) +          puts "FTP alive: #{banner}" +          return true +        end +      rescue +        puts "FTP exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e +    puts "TIMEOUT: #{e}" +    return false +  end + +  return false +end diff --git a/worker/tests/http.rb b/worker/tests/http.rb new file mode 100644 index 0000000..56163c8 --- /dev/null +++ b/worker/tests/http.rb @@ -0,0 +1,20 @@ + +# +# Run a HTTP test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def http_test( params ) + +  # +  # Get the URL to fetch. +  # +  host = params['target_host'] + +  puts "HTTP FAILED - TODO - IMPLEMENT" +  return false +end diff --git a/worker/tests/https.rb b/worker/tests/https.rb new file mode 100644 index 0000000..b905d79 --- /dev/null +++ b/worker/tests/https.rb @@ -0,0 +1,20 @@ + +# +# Run a HTTPS test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def https_test( params ) + +  # +  # Get the URL to poll. +  # +  host = params['target_host'] + +  puts "HTTPS FAILED - TODO - IMPLEMENT" +  return false +end diff --git a/worker/tests/jabber.rb b/worker/tests/jabber.rb new file mode 100644 index 0000000..5caeb5b --- /dev/null +++ b/worker/tests/jabber.rb @@ -0,0 +1,51 @@ +require 'timeout' + + +# +# Run a Jabber test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def jabber_test ( params ) + +  # +  #  Get the hostname & port to test against. +  # +  host = params['target_host'] +  port = 5222 + +  puts "Jabber testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.puts( "QUIT") + +        banner = socket.gets(nil) +        banner = banner[0,20] + +        socket.close() + +        if ( banner =~ /xml version/i ) +          puts "Jabber alive: #{banner}" +          return true +        end +      rescue +        puts "Jabber exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e +    puts "TIMEOUT: #{e}" +    return false +  end + +  return false +end diff --git a/worker/tests/ldap.rb b/worker/tests/ldap.rb new file mode 100644 index 0000000..4570f66 --- /dev/null +++ b/worker/tests/ldap.rb @@ -0,0 +1,41 @@ +require 'timeout' + + +# +# Run an LDAP test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def ldap_test ( params ) + +  # +  #  Get the hostname & port to test against. +  # +  host = params['target_host'] +  port = params['test_port'] + +  puts "LDAP testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.close() +        return true +      rescue +        puts "LDAP exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e +    puts "TIMEOUT: #{e}" +    return false +  end +  return false +end diff --git a/worker/tests/ping.rb b/worker/tests/ping.rb new file mode 100755 index 0000000..72769a6 --- /dev/null +++ b/worker/tests/ping.rb @@ -0,0 +1,36 @@ + +# +# Run a PING test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def ping_test( params ) + +  # +  # Find the binary +  # +  binary = nil +  binary = "./util/multi-ping"  if ( File.exists?( "./util/multi-ping" ) ) +  binary = "../util/multi-ping" if ( File.exists?( "../util/multi-ping" ) ) + +  if ( binary.nil? ) +    puts "Failed to find 'multi-ping'" +    exit 1 +  end + +  # +  # Is it IPv6 or IPv4a +  # +  host = params['target_host'] +  if ( system( "#{binary} #{host}" ) == true ) +    puts "PING OK" +    return  true +  else +    puts "PING FAILED" +    return false +  end +end diff --git a/worker/tests/rsync.rb b/worker/tests/rsync.rb new file mode 100644 index 0000000..2c781d8 --- /dev/null +++ b/worker/tests/rsync.rb @@ -0,0 +1,48 @@ +require 'timeout' + + +# +# Run an rsync test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def rsync_test ( params ) + +  # +  #  Get the hostname +  # +  host = params['target_host'] +  port = 873 + +  puts "rsync testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.puts( "QUIT") +        banner = socket.gets(nil) +        socket.close() + +        banner = banner[0,20] +        if ( banner =~ /rsyncd/i ) +          puts "rsync alive: #{banner}" +          return true +        end +      rescue +        puts "Exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e +    puts "TIMEOUT: #{e}" +    return false +  end +  return false +end diff --git a/worker/tests/smtp.rb b/worker/tests/smtp.rb new file mode 100644 index 0000000..66da545 --- /dev/null +++ b/worker/tests/smtp.rb @@ -0,0 +1,52 @@ +require 'timeout' + + +# +# Run an SMTP test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def smtp_test ( params ) + +  # +  #  Get the hostname & port to test against. +  # +  host = params['target_host'] +  port = 25 + +  puts "SMTP testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.puts( "QUIT\n\n") + +        banner = socket.gets(nil) +        banner = banner[0,20] + +        socket.close() + +        if ( banner =~ /SMTP/i ) +          puts "SMTP alive: #{banner}" +          return true +        end +      rescue +        puts "SMTP exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e + +    puts "SMTP TIMEOUT: #{e}" +    return false +  end +  puts "SMTP Misc Failure" +  return false +end diff --git a/worker/tests/ssh.rb b/worker/tests/ssh.rb new file mode 100755 index 0000000..4264c8e --- /dev/null +++ b/worker/tests/ssh.rb @@ -0,0 +1,50 @@ +require 'timeout' + + +# +# Run an SSH test. +# +# +# Return value +#   TRUE:  The host is up +# +#  FALSE:  The host is not up +# +def ssh_test ( params ) + +  # +  #  Get the hostname & port to test against. +  # +  host = params['target_host'] +  port = params['test_port'] + +  puts "SSH testing host #{host}:#{port}" + + +  begin +    timeout(3) do + +      begin +        socket = TCPSocket.new( host, port ) +        socket.puts( "QUIT") + +        banner = socket.gets(nil) +        banner = banner[0,20] +        socket.close() + +        if ( banner =~ /ssh/i ) +          puts "SSH alive: #{banner}" +          return true +        end +      rescue +        puts "SSH exception on host #{host}:#{port} - #{$!}" +        return false +      end +    end +  rescue Timeout::Error => e +    puts "TIMEOUT: #{e}" +    return false +  end + +  return false +end diff --git a/worker/worker b/worker/worker new file mode 100755 index 0000000..6380cf8 --- /dev/null +++ b/worker/worker @@ -0,0 +1,147 @@ +#!/usr/bin/ruby +# +#  This script will pull tests to complete from the Beanstalk Queue, +# where they will be found in JSON form, and executes them. +# +# +#  TODO: Command line parsing: +# +#           1.  set failure count 3 in a row, for example. +# +#           2.  enable/disable logging. +# +#           3.  Specify server name/port for the beanstalk queue. +# +# +# Steve +# -- +# + + + +require 'beanstalk-client' +require 'json'; + + + + +# +# Implementations for our protocol tests. +# +require 'tests/ftp' +require 'tests/http' +require 'tests/https' +require 'tests/jabber' +require 'tests/ldap' +require 'tests/ping' +require 'tests/rsync' +require 'tests/smtp' +require 'tests/ssh' + + + + + + +def clear_alert( hash ) +  puts "CLEARING ALERT: #{hash}" +end + + + + +def raise_alert( hash ) +  puts "Raising ALERT: #{hash}" +end + + + + + + +# +# Connect to the queue server +# +beanstalk = Beanstalk::Pool.new(['localhost:11300']) + + + + +# +# Run until we're killed +# +loop do + +  puts "\n\nWaiting for job.." + + +  # +  #  Find the next job. +  # +  job = beanstalk.reserve +  puts "Job acquired: #{Time.new.inspect}" + + +  # +  #  Parse the JSON of the job body. +  # +  json = job.body +  hash = JSON.parse( json ) + +  # +  #  Output the details. +  # +  puts "JOB: #{job.id}" +  puts "Type of test is #{hash['test_type']}" +  hash.keys.each do |key| +    puts "\t#{key} => #{hash[key]}" +  end + + +  # +  #  Switch on type of test. +  # +  test   = hash['test_type'] +  method = "#{test}_test".to_sym + +  # +  # If it succeeds. +  # +  begin + +    success = false +    count   = 0 + +    # +    #  We'll run no more than MAX times. +    # +    #  We stop the execution on a single success. +    # +    while ( ( count < 5 ) && ( success == false ) ) +      if ( send( method, hash ) ) +        clear_alert(hash) +        success= true +      end +      count += 1 +    end + +    # +    #  If we didn't succeed on any of the attempts raise the alert. +    # +    if ( ! success ) +      raise_alert( hash ) +    end + +  rescue => ex +    puts "Exception: #{ex}" +  ensure +    # +    #  Delete the job - either we received an error, in which case +    # we should remove it to avoid picking it up again, or we handled +    # it successfully so it should be removed. +    # +    job.delete +  end +end + + | 
