From 8cd8125b071f4a0be8ec05d40471da6821067b07 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Thu, 22 Nov 2012 15:38:07 +0000 Subject: Parse the configuration file into arrays of jobs, via our test-factory --- lib/custodian/parser.rb | 258 +++++++-------------------------- lib/custodian/protocol-tests/dns.rb | 211 --------------------------- lib/custodian/protocol-tests/ftp.rb | 149 ------------------- lib/custodian/protocol-tests/http.rb | 144 ------------------ lib/custodian/protocol-tests/jabber.rb | 151 ------------------- lib/custodian/protocol-tests/ldap.rb | 141 ------------------ lib/custodian/protocol-tests/ping.rb | 134 ----------------- lib/custodian/protocol-tests/rsync.rb | 151 ------------------- lib/custodian/protocol-tests/smtp.rb | 150 ------------------- lib/custodian/protocol-tests/ssh.rb | 149 ------------------- lib/custodian/protocol-tests/tcp.rb | 168 --------------------- lib/custodian/protocoltest/dns.rb | 14 +- lib/custodian/protocoltest/ftp.rb | 16 +- lib/custodian/protocoltest/http.rb | 15 +- lib/custodian/protocoltest/jabber.rb | 17 +-- lib/custodian/protocoltest/ldap.rb | 17 +-- lib/custodian/protocoltest/ping.rb | 15 +- lib/custodian/protocoltest/rsync.rb | 17 +-- lib/custodian/protocoltest/smtp.rb | 17 +-- lib/custodian/protocoltest/ssh.rb | 18 +-- lib/custodian/protocoltest/tcp.rb | 18 +-- 21 files changed, 77 insertions(+), 1893 deletions(-) delete mode 100755 lib/custodian/protocol-tests/dns.rb delete mode 100755 lib/custodian/protocol-tests/ftp.rb delete mode 100755 lib/custodian/protocol-tests/http.rb delete mode 100755 lib/custodian/protocol-tests/jabber.rb delete mode 100755 lib/custodian/protocol-tests/ldap.rb delete mode 100755 lib/custodian/protocol-tests/ping.rb delete mode 100755 lib/custodian/protocol-tests/rsync.rb delete mode 100755 lib/custodian/protocol-tests/smtp.rb delete mode 100755 lib/custodian/protocol-tests/ssh.rb delete mode 100755 lib/custodian/protocol-tests/tcp.rb (limited to 'lib/custodian') diff --git a/lib/custodian/parser.rb b/lib/custodian/parser.rb index 4e6bfef..7f5838e 100644 --- a/lib/custodian/parser.rb +++ b/lib/custodian/parser.rb @@ -1,10 +1,20 @@ -require 'json' require 'net/http' require 'net/https' require 'uri' +require 'custodian/protocoltest/tcp.rb' +require 'custodian/protocoltest/dns.rb' +require 'custodian/protocoltest/ftp.rb' +require 'custodian/protocoltest/http.rb' +require 'custodian/protocoltest/jabber.rb' +require 'custodian/protocoltest/ldap.rb' +require 'custodian/protocoltest/ping.rb' +require 'custodian/protocoltest/rsync.rb' +require 'custodian/protocoltest/ssh.rb' +require 'custodian/protocoltest/smtp.rb' +require 'custodian/testfactory' @@ -42,9 +52,7 @@ class MonitorConfig attr_reader :timeout # - # An array of job-objects. - # - # TODO: This is just an array of hashes at the moment. + # An array of test-objects, which are subclasses of our test-factory. # attr_reader :jobs @@ -66,20 +74,6 @@ class MonitorConfig end - # - # Get the current value of the timeout figure. - # - def get_timeout() - @timeout - end - - - # - # Set the timeout value. - # - def set_timeout( new_val ) - @timeout = new_val - end # @@ -185,8 +179,6 @@ class MonitorConfig # val.push( hosts ) end - - end if ( is_macro?( name ) ) @@ -194,6 +186,8 @@ class MonitorConfig end @MACROS[name] = val + + true end @@ -229,6 +223,30 @@ class MonitorConfig + # + # Return multiple copies of a line for each macro-target + # + def expand_macro( input ) + + r = Array.new() + + if ( input =~ /^(\S+)\s+(.*)$/ ) + macro=$1.dup + rest=$2.dup + end + + + if ( is_macro?( macro ) ) + get_macro_targets(macro).each do |host| + r.push( "#{host} #{rest}" ) + end + else + r.push( input ) + end + + r + end + # # Parse a single line from the configuration file. @@ -258,7 +276,6 @@ class MonitorConfig define_macro( line ) elsif ( line =~ /^(\S+)\s+must\s+ping(.*)/ ) - # # Ping is a special case because the configuration file entry # would read: @@ -283,193 +300,29 @@ class MonitorConfig elsif ( line =~ /^\S+\s+must\s+run\s+([^\s]+)(\s+|\.|$)/i ) # - # Get the service we're testing, and remove any trailing "." - # - # This handles the case of: - # - # LINN_HOSTS must run ssh. - # - service = $1.dup - service.chomp!(".") - - # - # Target of the service-test. - # - targets = Array.new - target = line.split( /\s+/)[0] - - # - # If the target is a macro then get the list of hosts to - # which the test will apply. - # - if ( is_macro?( target ) ) - targets = get_macro_targets( target ) - else - - # - # Otherwise a list of one, literal, entry. - # - targets.push( target ) - end - - # - # All our service tests, except ping, require a port - we setup the defaults here, - # but the configuration file will allow users to specify an alternative - # via " on XXX ". - # - case service - when /ssh/ then - port=22 - when /jabber/ then - port=5222 - when /ldap/ then - port=389 - when /^https$/ then - port=443 - when /^http$/ then - port=80 - when /rsync/i then - port=873 - when /ftp/i then - port=21 - when /telnet/i then - port=23 - when /smtp/i then - port=25 - when /dns/i then - port=53 - end - - # - # Allow the port to be changed, for example: + # Expand the macro if we should # - # must run ssh on 33 otherwise .. - # must run ftp on 44 otherwise .. - # must run http on 8000 otherwise .. - # - if ( line =~ /\s+on\s+([0-9]+)/ ) - port = $1.dup - end - + tests = expand_macro( line ) # - # The array of JSON objects we will return to the caller. + # The array of objects we will return to the caller. # ret = Array.new() # # For each host in our possibly-macro-expanded list: # - targets.each do |host| + tests.each do |macro_expanded| - # - # The test we'll apply. - # - test = { - :target_host => host, - :test_type => service, - :test_port => port, - :verbose => true, - :timeout => @timeout - } - - # - # Sanity check the hostname for ping-tests, to - # avoid this security hole: - # - # $(/tmp/exploit.sh) must run ping .. - # - if ( service == "ping" ) - raise ArgumentError, "Invalid hostname for ping-test: #{host}" unless( host =~ /^([a-zA-Z0-9:\-\.]+)$/ ) - end - - - # - # Alert text will have a default, which may be overridden. - # - alert = "#{service} failed on #{host}" - if ( line =~ /otherwise '([^']+)'/ ) - alert=$1.dup - end - - # - # Store the alert - # - # Note: We do this in the loop so that we can have "on $host" with - # the per-test hostname inserted. - # - test[:test_alert] = alert - - # - # TCP-tests will include a banner, optionally - # - if ( test[:test_type] =~ /tcp/ ) - if ( line =~ /\s+with\s+banner\s+'([^']+)'/ ) - test[:banner]=$1.dup - else - puts "You did not specify a banner to match against in line: #{line}" - end - end - - - # - # HTTP-tests will include the expected result in one of two forms: - # - # must run http with status 200 - # - # must run http with content 'text' - # - # If those are sepcified then include them here. - # - # Note we're deliberately fast and loose here - which allows both to - # be specified: - # - # http://example.vm/ must run http with status 200 and content 'OK'. - # - # - if ( test[:test_type] =~ /^https?/ ) - - # - # If a status code is specified use it; otherwise default - # to 200. - if ( line =~ /\s+with\s+status\s+([0-9]+)\s+/ ) - test[:http_status] = $1.dup - else - test[:http_status] = 200 - end - - # - # If a content-check is in place then use it. - # - if ( line =~ /\s+with\s+content\s+'([^']+)'/ ) - test[:http_text]=$1.dup - end - - end + job = nil - - # - # These are special cased for the DNS types - # - if ( test[:test_type] =~ /dns/ ) - - # - # Sample line: - # - # DNSHOSTS must run dns for www.bytemark.co.uk resolving A as '212.110.161.144'. - # - # - if ( line =~ /for\s+([^\s]+)\sresolving\s([A-Z]+)\s+as\s'([^']+)'/ ) - test[:resolve_name] = $1.dup - test[:resolve_type] = $2.dup - test[:resolve_expected] = $3.dup - end + begin + job = Custodian::TestFactory.create( macro_expanded ) + ret.push( job ) + rescue => ex + puts "ERROR: #{ex}" + return nil end - - # - # Add the job to the results. - # - ret.push( test ) end ret @@ -520,7 +373,7 @@ class MonitorConfig if ( block_given? ) if ( ret.kind_of?( Array ) ) ret.each do |probe| - yield probe + yield probe.to_s end end end @@ -531,14 +384,3 @@ class MonitorConfig end - -if __FILE__ == $0 then - p = MonitorConfig.new( "/home/steve/hg/custodian/cfg/steve.cfg" ); - p.parse_file do |test| - puts "Test: #{test} received" - end - - p.jobs.each do |job| - puts "Job: #{job}" - end -end diff --git a/lib/custodian/protocol-tests/dns.rb b/lib/custodian/protocol-tests/dns.rb deleted file mode 100755 index 3d9f9fd..0000000 --- a/lib/custodian/protocol-tests/dns.rb +++ /dev/null @@ -1,211 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'resolv-replace' - - -# -# This is a protocol tester for DNS. -# -class DNSTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - # - # Ensure we have a host to monitor - # - if ( @test_data["target_host"].nil? ) - @error = "Missing host to test against." - raise ArgumentError, @error - end - - # - # Ensure we have test-specific keys. - # - if ( @test_data["resolve_name"].nil? ) - @error = "Missing host to resolve." - raise ArgumentError, @error - end - if ( @test_data["resolve_type"].nil? ) - @error = "Missing type to resolve, for example A/MX/NS." - raise ArgumentError, @error - end - if ( @test_data["resolve_expected"].nil? ) - @error = "Missing expected results to compare against.." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the nameserver to resolve with - # - nameserver = @test_data["target_host"] - - # - # Get the record type to lookup - # - record = @test_data["resolve_type"] - - # - # Get the hostname to lookup - # - lookup = @test_data["resolve_name"] - - # - # Get the expected results - # - expected = @test_data["resolve_expected"] - - # - # Do the lookup abort if that fails. - # - timeout = @test_data["timeout"].to_i - - results = resolve_via( nameserver, record, lookup, timeout ) - return false if ( results.nil? ) - - - # - # Show the results if we found something. - # - if ( @test_data['verbose'] ) - results.each do |res| - puts "Result: #{res}" - end - end - - # - # OK we have an array of results. If every one of the expected - # results is contained in those results returned then we're good. - # - expected.split( /;/ ).each do |required| - if ( ! results.include?( required ) ) - @error = "The expected result #{required} was not found in the results: #{results.join(",")}" - return false - end - end - - return true - end - - - # - # Resolve an IP - # - def resolve_via( server, ltype, name, period ) - puts "Resolving #{name} [#{ltype}] via server #{server}" - - results = Array.new() - - begin - timeout( period ) do - - begin - Resolv::DNS.open(:nameserver=>[server]) do |dns| - case ltype - when /^A$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push r.address.to_s() } - when /^AAAA$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push r.address.to_s() } - - when /^NS$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::NS).map{ |r| results.push Resolv.getaddresses(r.name.to_s()) } - - when /^MX$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push Resolv.getaddresses(r.exchange.to_s()) } - end - end - rescue Exception => x - @error = "Exception was received when resolving : #{x}" - return nil - end - end - rescue Timeout::Error => e - @error = "Timed-out connecting #{e}" - return nil - end - results.flatten! - return results - end - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - - - - -end - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "a.ns.bytemark.co.uk", - "test_type" => "dns", - "verbose" => 1, - "timeout" => "4", - "test_alert" => "DNS failure", - "resolve_name" => "support.bytemark.co.uk", - "resolve_type" => "MX", - "resolve_expected" => "89.16.184.148;89.16.184.149;89.16.184.150" - } - - - # - # Run the test. - # - obj = DNSTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/ftp.rb b/lib/custodian/protocol-tests/ftp.rb deleted file mode 100755 index 1ecd68a..0000000 --- a/lib/custodian/protocol-tests/ftp.rb +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'timeout' -require 'socket' - -# -# This class is responsible for performing tests against a remote FTP server -# -# -class FTPTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data["target_host"] - port = @test_data["test_port"] - - puts "FTP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout( @test_data["timeout"].to_i ) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - banner = socket.gets(nil) - banner = banner[0,20] unless( banner.nil? ) - - socket.close() - - if ( ( !banner.nil? ) && ( banner =~ /^220/ ) ) - return true - else - @error = "Banner didn't report OK: #{banner}" - end - rescue - @error = "FTP exception on host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "Timed-out connecting #{e}" - return false - end - @error = "Misc. failure." - return false - end - - - # - # Return the error. - # - def error - return @error - end - -end - - - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "mirror.bytemark.co.uk", - "test_type" => "ftp", - "test_port" => 21, - "verbose" => 1, - "timeout" => 4, - "test_alert" => "The FTP server no worky", - } - - - # - # Run the test. - # - tst = FTPTest.new( test ) - if ( tst.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts tst.error() - end - -end diff --git a/lib/custodian/protocol-tests/http.rb b/lib/custodian/protocol-tests/http.rb deleted file mode 100755 index 102ffea..0000000 --- a/lib/custodian/protocol-tests/http.rb +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'custodian/webfetch' - - -class HTTPTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - # - # Ensure we have an URL - # - if ( @test_data["target_host"].nil? ) - @error = "Missing URL for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - - # - # Run the fetch. - # - obj = WebFetch.new( @test_data["target_host"], @test_data["timeout"].to_i ) - - # - # If we succeeded in the fetch - # - if ( obj.fetch() ) - - # - # Do we need to test for a HTTP status code? - # - if ( @test_data["http_status"] ) - - puts "Testing for HTTP status code: #{@test_data['http_status']}" if ( @test_data['verbose'] ) - - if ( obj.status().to_i != @test_data['http_status'].to_i) - @error = "#{@error}

The HTTP status-code was '#{obj.status}' not '#{@test_data['http_status']}'.

" - end - end - - # - # Do we need to search for text in the body of the reply? - # - if ( @test_data['http_text'] ) - puts "Testing for text in the response: '#{@test_data['http_text']}'" if ( @test_data['verbose'] ) - - if (! obj.content.match(/#{@test_data['http_text']}/i) ) - @error = "#{@error}

The response did not contain our expected text '#{test_data['http_text']}'

" - end - end - - return true if ( @error.nil? ) - - return false - end - - @error = obj.error() - return false - end - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - - -end - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "http://itsdanbull.co.uk/", - "test_type" => "http", - "verbose" => 1, - "timeout" => 3, - "test_port" => 80, - "test_alert" => "Collector is unavailable", - "http_status" => "200", - "http_text" => "Dan Bull" - } - - - # - # Run the test. - # - http = HTTPTest.new( test ) - if ( http.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts http.error() - end - -end diff --git a/lib/custodian/protocol-tests/jabber.rb b/lib/custodian/protocol-tests/jabber.rb deleted file mode 100755 index a35ef28..0000000 --- a/lib/custodian/protocol-tests/jabber.rb +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from a Jabber server that looks -# reasonable. -# -class JABBERTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - puts "Jabber testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - banner = socket.gets(nil) - banner = banner[0,20] unless( banner.nil? ) - - socket.close() - - if ( ( !banner.nil? ) && ( banner =~ /xml version/i ) ) - puts "Jabber alive: #{banner}" if ( @test_data['verbose'] ) - return true - else - @error = "Banner didn't seem reasonable: #{banner}" - return false; - end - rescue - @error = "Jabber exception on host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "chat.bytemark.co.uk", - "test_type" => "jabber", - "test_port" => "5222", - "timeout" => 4, - "verbose" => 1, - "test_alert" => "Chat is down?", - } - - - # - # Run the test. - # - obj = JABBERTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/ldap.rb b/lib/custodian/protocol-tests/ldap.rb deleted file mode 100755 index af0addc..0000000 --- a/lib/custodian/protocol-tests/ldap.rb +++ /dev/null @@ -1,141 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an LDAP server. -# -class LDAPTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - puts "LDAP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - socket.close() - - puts "LDAP alive" if ( @test_data['verbose'] ) - return true - rescue - @error = "Exception connecting to host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "auth.bytemark.co.uk", - "test_type" => "ldap", - "test_port" => 389, - "verbose" => 1, - "timeout" => 5, - "test_alert" => "LDAP is down?", - } - - - # - # Run the test. - # - obj = LDAPTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/ping.rb b/lib/custodian/protocol-tests/ping.rb deleted file mode 100755 index 309c77b..0000000 --- a/lib/custodian/protocol-tests/ping.rb +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a ping response from the remote host. -# -class PINGTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - - # - # Find the binary we're going to invoke. - # - binary = nil - binary = "/usr/bin/multi-ping" if ( File.exists?( "/usr/bin/multi-ping" ) ) - - if ( binary.nil? ) - @error = "Failed to find 'multi-ping'" - return false - end - - - # - # Get the hostname to test against. - # - host = @test_data['target_host'] - - - # - # Sanity check the hostname for ping-tests, to - # avoid this security hole: - # - # $(/tmp/exploit.sh) must run ping .. - # - raise ArgumentError, "Invalid hostname for ping-test: #{host}" unless( host =~ /^([a-zA-Z0-9:\-\.]+)$/ ) - - - - # - # Show the hostname. - # - puts "ping testing host #{host}" if ( @test_data['verbose'] ) - - - if ( system( "#{binary} #{host}" ) == true ) - puts "PING OK" if ( @test_data['verbose'] ) - return true - else - @error = "Ping failed. TODO: Mtr" - return false - end - - end - - - # - # Return the error text for why this test failed. - # - def error() - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "upload.ns.bytemark.co.uk", - "test_type" => "ping", - "verbose" => 1, - "timeout" => 5, - "test_alert" => "Pingly faily", - } - - - # - # Run the test. - # - obj = PINGTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/rsync.rb b/lib/custodian/protocol-tests/rsync.rb deleted file mode 100755 index 4493d0f..0000000 --- a/lib/custodian/protocol-tests/rsync.rb +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an rsync server that looks -# reasonable. -# -class RSYNCTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error()". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - puts "rsync testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - banner = socket.gets(nil) - banner = banner[0,20] unless( banner.nil? ) - - socket.close() - - if ( ( !banner.nil? ) && ( banner =~ /rsyncd/i ) ) - puts "rsync alive: #{banner}" if ( @test_data['verbose'] ) - return true - else - @error = "Banner didn't seem reasonable: #{banner}" - return false; - end - rescue - @error = "rsync exception on host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "upload.ns.bytemark.co.uk", - "test_type" => "rsync", - "test_port" => "873", - "verbose" => 1, - "timeout" => 5, - "test_alert" => "DNS upload service failure", - } - - - # - # Run the test. - # - obj = RSYNCTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end - diff --git a/lib/custodian/protocol-tests/smtp.rb b/lib/custodian/protocol-tests/smtp.rb deleted file mode 100755 index 5da672f..0000000 --- a/lib/custodian/protocol-tests/smtp.rb +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an SMTP server that looks -# reasonable. -# -class SMTPTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error". - # - def run_test - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - puts "SMTP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - banner = socket.gets(nil) - banner = banner[0,40] unless( banner.nil? ) - - socket.close() - - if ( ( !banner.nil? ) && ( banner =~ /SMTP/i ) ) - puts "SMTP alive: #{banner}" if ( @test_data['verbose'] ) - return true - else - @error = "Banner didn't seem reasonable: #{banner}" - return false; - end - rescue - @error = "SMTP exception on host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "mail.steve.org.uk", - "test_type" => "smtp", - "test_port" => "25", - "verbose" => 1, - "timeout" => 5, - "test_alert" => "SMTP failure", - } - - - # - # Run the test. - # - obj = SMTPTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/ssh.rb b/lib/custodian/protocol-tests/ssh.rb deleted file mode 100755 index 4ee9974..0000000 --- a/lib/custodian/protocol-tests/ssh.rb +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/ruby1.8 - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an SSH server that looks -# reasonable. -# -class SSHTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error". - # - def run_test - - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - puts "ssh testing host #{host}:#{port}" if ( @test_data['verbose'] ) - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - banner = socket.gets(nil) - banner = banner[0,20] unless( banner.nil? ) - - socket.close() - - if ( !banner.nil? && ( banner =~ /ssh/i ) ) - puts "ssh alive: #{banner}" if ( @test_data['verbose'] ) - return true - else - @error = "Banner didn't seem reasonable: #{banner}" - return false; - end - rescue - @error = "ssh exception on host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "ssh.steve.org.uk", - "test_type" => "ssh", - "test_port" => 2222, - "verbose" => 1, - "timeout" => 5, - "test_alert" => "Steve's host isn't running SSH?", - } - - - # - # Run the test. - # - obj = SSHTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocol-tests/tcp.rb b/lib/custodian/protocol-tests/tcp.rb deleted file mode 100755 index a6770e4..0000000 --- a/lib/custodian/protocol-tests/tcp.rb +++ /dev/null @@ -1,168 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from a TCP server that matches -# a given banner. -# -class TCPTest - - # - # Data passed from the JSON hash. - # - attr_reader :test_data - - # - # The error text we return on failure. - # - attr_reader :error - - - - # - # Save the data away. - # - def initialize( data ) - @test_data = data - @error = nil - - - # - # Ensure we have a host to probe - # - if ( @test_data["target_host"].nil? ) - @error = "Missing target for the test." - raise ArgumentError, @error - end - - # - # Ensure we have a port to test. - # - if ( @test_data["test_port"].nil? ) - @error = "Missing port for the test." - raise ArgumentError, @error - end - end - - - # - # Run the test. - # - # Return "true" on success - # - # Return "false" on failure. - # - # If the test fails the details should be retrieved from "error". - # - def run_test - # - # Reset state from previous test. - # - @error = nil - - # - # Get the hostname & port to test against. - # - host = @test_data['target_host'] - port = @test_data['test_port'] - - # - # Get the banner we expect - # - banner = @test_data['banner'] - - puts "TCP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - if ( @test_data['verbose'] && ( !banner.nil? ) ) - puts "Looking for banner '#{banner}'" - end - - begin - timeout(@test_data["timeout"].to_i) do - - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - # read a banner from the remote server - read = socket.gets(nil) - - # trim to a sane length & strip newlines. - read = read[0,255] unless ( read.nil? ) - read.gsub!(/[\n\r]/, "") unless ( read.nil? ) - - socket.close() - - - if ( banner.nil? ) - return true - else - # test for banner - if ( ( !read.nil? ) && ( read =~ /#{banner}/i ) ) - puts "We connected and matched the banner against '#{read}'" if ( @test_data['verbose'] ) - return true - end - - @error = "We expected a banner matching '#{banner}' but we got '#{read}'" - return false - end - rescue - @error = "Exception connecting to host #{host}:#{port} - #{$!}" - return false - end - end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - @error = "Misc failure" - return false - end - - - - # - # Return the error text for why this test failed. - # - def error - return @error - end - -end - - -# -# Sample test, for basic testing. -# -if __FILE__ == $0 then - - # - # Sample data. - # - test = { - "target_host" => "mail.steve.org.uk", - "test_type" => "tcp", - "test_port" => "25", - "banner" => "SMTP", - "verbose" => 1, - "timeout" => 5, - "test_alert" => "TCP-port failure", - } - - - # - # Run the test. - # - obj = TCPTest.new( test ) - if ( obj.run_test ) - puts "TEST OK" - else - puts "TEST FAILED" - puts obj.error() - end - -end diff --git a/lib/custodian/protocoltest/dns.rb b/lib/custodian/protocoltest/dns.rb index cf37abf..6c5e8db 100644 --- a/lib/custodian/protocoltest/dns.rb +++ b/lib/custodian/protocoltest/dns.rb @@ -39,25 +39,15 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "dns-test - TODO." + @line end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - # # Run the test. # diff --git a/lib/custodian/protocoltest/ftp.rb b/lib/custodian/protocoltest/ftp.rb index db05ac4..a816bfd 100644 --- a/lib/custodian/protocoltest/ftp.rb +++ b/lib/custodian/protocoltest/ftp.rb @@ -71,27 +71,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "ftp-test of #{@host}:#{@port}." + @line end # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # Run the TCP-protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index b3f446e..1b7f442 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -53,21 +53,10 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "http-test of #{@url}." - end - - - - - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json + @line end diff --git a/lib/custodian/protocoltest/jabber.rb b/lib/custodian/protocoltest/jabber.rb index e043481..9c0eb6d 100644 --- a/lib/custodian/protocoltest/jabber.rb +++ b/lib/custodian/protocoltest/jabber.rb @@ -66,28 +66,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "jabber-test of #{@host}:#{@port}." + @line end # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # - # Run the TCP-protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/ldap.rb b/lib/custodian/protocoltest/ldap.rb index bfda9f8..b27e5d6 100644 --- a/lib/custodian/protocoltest/ldap.rb +++ b/lib/custodian/protocoltest/ldap.rb @@ -68,28 +68,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "ldap-test of #{@host}:#{@port}." + @line end # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # - # Run the TCP-protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/ping.rb b/lib/custodian/protocoltest/ping.rb index 173f4f4..c13c17c 100644 --- a/lib/custodian/protocoltest/ping.rb +++ b/lib/custodian/protocoltest/ping.rb @@ -52,21 +52,10 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "ping-test - #{@host}." - end - - - - - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json + @line end diff --git a/lib/custodian/protocoltest/rsync.rb b/lib/custodian/protocoltest/rsync.rb index 34745c7..8b4e84c 100644 --- a/lib/custodian/protocoltest/rsync.rb +++ b/lib/custodian/protocoltest/rsync.rb @@ -67,28 +67,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "rsync-test of #{@host}:#{@port}." + @line end # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # - # Run the protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/smtp.rb b/lib/custodian/protocoltest/smtp.rb index ee52131..0d22c95 100644 --- a/lib/custodian/protocoltest/smtp.rb +++ b/lib/custodian/protocoltest/smtp.rb @@ -68,28 +68,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "smtp-test of #{@host}:#{@port}." + @line end # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # - # Run the TCP-protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/ssh.rb b/lib/custodian/protocoltest/ssh.rb index b8a6354..205099e 100644 --- a/lib/custodian/protocoltest/ssh.rb +++ b/lib/custodian/protocoltest/ssh.rb @@ -68,29 +68,17 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "ssh-test of #{@host}:#{@port}." + return( @line ) end # - # Convert this class to JSON such that it may be - # serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - - # - # Run the TCP-protocol test. + # Run the test. # def run_test diff --git a/lib/custodian/protocoltest/tcp.rb b/lib/custodian/protocoltest/tcp.rb index 21d2ecd..c14f686 100644 --- a/lib/custodian/protocoltest/tcp.rb +++ b/lib/custodian/protocoltest/tcp.rb @@ -94,28 +94,16 @@ module Custodian # - # Helper for development. + # Allow this test to be serialized. # def to_s - "tcp-test of #{@host}:#{@port} looking for banner '#{@banner}'." + return( @line ) end - - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end - - - - # - # Run the TCP-protocol test. + # Run the test. # def run_test -- cgit v1.2.1