From 22e1d5a39e0a8a885adc3d88bff91fa58a3eb119 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Tue, 13 Nov 2012 11:51:26 +0000 Subject: Converted to class-based API --- worker/tests/ldap.rb | 130 ++++++++++++++++++++++++++++++++++++++--------- worker/tests/ping.rb | 122 ++++++++++++++++++++++++++++++++++++-------- worker/tests/rsync.rb | 138 +++++++++++++++++++++++++++++++++++++++----------- worker/tests/smtp.rb | 135 +++++++++++++++++++++++++++++++++++++----------- worker/tests/ssh.rb | 134 +++++++++++++++++++++++++++++++++++++----------- 5 files changed, 525 insertions(+), 134 deletions(-) diff --git a/worker/tests/ldap.rb b/worker/tests/ldap.rb index 35151ba..a966b6a 100755 --- a/worker/tests/ldap.rb +++ b/worker/tests/ldap.rb @@ -1,42 +1,122 @@ +#!/usr/bin/ruby + + + +require 'socket' require 'timeout' # -# Run an LDAP test. -# -# -# Return value -# TRUE: The host is up -# -# FALSE: The host is not up +# Test that we can receive a response from an LDAP server. # -def ldap_test ( params ) +class LDAPTest # - # Get the hostname & port to test against. + # Data passed from the JSON hash. # - host = params['target_host'] - port = params['test_port'] + 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 "get_details". + # + def run_test + + # + # Until the test runs we have no error. + # + @error = "" - puts "LDAP testing host #{host}:#{port}" if ( params['verbose'] ) + # + # 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(3) do + begin + timeout(3) do - begin - socket = TCPSocket.new( host, port ) - socket.close() - return true - rescue - puts "LDAP exception on host #{host}:#{port} - #{$!}" if ( params['verbose'] ) - return false + 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 - rescue Timeout::Error => e - puts "TIMEOUT: #{e}" if ( params['verbose'] ) + + @error = "Misc failure" return false end - puts "Misc failure" if ( params['verbose'] ) - return false + + + + # + # Return the error text for why this test failed. + # + def get_details + 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, + "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.get_details() + end + end diff --git a/worker/tests/ping.rb b/worker/tests/ping.rb index 72769a6..83a1c41 100755 --- a/worker/tests/ping.rb +++ b/worker/tests/ping.rb @@ -1,36 +1,116 @@ +#!/usr/bin/ruby + + + +require 'socket' +require 'timeout' + # -# Run a PING test. -# +# Test that we can receive a ping response from the remote host. # -# Return value -# TRUE: The host is up +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 "get_details". + # + def run_test + @error = "" + + + # + # 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" ) ) + binary = "../../util/multi-ping" if ( File.exists?( "../../util/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'] + 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 get_details + return @error + end + +end + + # -# FALSE: The host is not up +# Sample test, for testing. # -def ping_test( params ) +if __FILE__ == $0 then # - # Find the binary + # Sample data. # - binary = nil - binary = "./util/multi-ping" if ( File.exists?( "./util/multi-ping" ) ) - binary = "../util/multi-ping" if ( File.exists?( "../util/multi-ping" ) ) + test = { + "target_host" => "upload.ns.bytemark.co.uk", + "test_type" => "ping", + "verbose" => 1, + "test_alert" => "Pingly faily", + } - if ( binary.nil? ) - puts "Failed to find 'multi-ping'" - exit 1 - end # - # Is it IPv6 or IPv4a + # Run the test. # - host = params['target_host'] - if ( system( "#{binary} #{host}" ) == true ) - puts "PING OK" - return true + obj = PINGTest.new( test ) + if ( obj.run_test ) + puts "TEST OK" else - puts "PING FAILED" - return false + puts "TEST FAILED" + puts obj.get_details() end + end diff --git a/worker/tests/rsync.rb b/worker/tests/rsync.rb index 2c781d8..e410454 100755 --- a/worker/tests/rsync.rb +++ b/worker/tests/rsync.rb @@ -1,48 +1,128 @@ +#!/usr/bin/ruby + + + +require 'socket' require 'timeout' # -# Run an rsync test. -# -# -# Return value -# TRUE: The host is up -# -# FALSE: The host is not up +# Test that we can receive a response from an rsync server that looks +# reasonable. # -def rsync_test ( params ) +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 + end + + + # + # Run the test. + # + # Return "true" on success # - # Get the hostname + # Return "false" on failure. # - host = params['target_host'] - port = 873 + # If the test fails the details should be retrieved from "get_details". + # + def run_test + @error = "" + + # + # Get the hostname & port to test against. + # + host = @test_data['target_host'] + port = 873 + + puts "rsync testing host #{host}:#{port}" if ( @test_data['verbose'] ) - puts "rsync testing host #{host}:#{port}" + begin + timeout(3) do + begin + socket = TCPSocket.new( host, port ) + socket.puts( "QUIT") - begin - timeout(3) do + banner = socket.gets(nil) + banner = banner[0,20] - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - banner = socket.gets(nil) - socket.close() + socket.close() - banner = banner[0,20] - if ( banner =~ /rsyncd/i ) - puts "rsync alive: #{banner}" - return true + if ( 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 - rescue - puts "Exception on host #{host}:#{port} - #{$!}" - return false end + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false end - rescue Timeout::Error => e - puts "TIMEOUT: #{e}" + + @error = "Misc failure" return false end - return false + + + + # + # Return the error text for why this test failed. + # + def get_details + 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", + "verbose" => 1, + "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.get_details() + end + +end + diff --git a/worker/tests/smtp.rb b/worker/tests/smtp.rb index 66da545..113dd5f 100755 --- a/worker/tests/smtp.rb +++ b/worker/tests/smtp.rb @@ -1,52 +1,127 @@ +#!/usr/bin/ruby + + + +require 'socket' require 'timeout' # -# Run an SMTP test. -# -# -# Return value -# TRUE: The host is up -# -# FALSE: The host is not up +# Test that we can receive a response from an SMTP server that looks +# reasonable. # -def smtp_test ( params ) +class SMTPTest + + # + # Data passed from the JSON hash. + # + attr_reader :test_data + + # + # The error text we return on failure. + # + attr_reader :error + + # - # Get the hostname & port to test against. + # 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 "get_details". # - host = params['target_host'] - port = 25 + def run_test + @error = "" - puts "SMTP testing host #{host}:#{port}" + # + # Get the hostname & port to test against. + # + host = @test_data['target_host'] + port = 25 + puts "SMTP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - begin - timeout(3) do + begin + timeout(3) do - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT\n\n") + begin + socket = TCPSocket.new( host, port ) + socket.puts( "QUIT") - banner = socket.gets(nil) - banner = banner[0,20] + banner = socket.gets(nil) + banner = banner[0,40] - socket.close() + socket.close() - if ( banner =~ /SMTP/i ) - puts "SMTP alive: #{banner}" - return true + if ( 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 - rescue - puts "SMTP exception on host #{host}:#{port} - #{$!}" - return false end + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false end - rescue Timeout::Error => e - puts "SMTP TIMEOUT: #{e}" + @error = "Misc failure" return false end - puts "SMTP Misc Failure" - return false + + + + # + # Return the error text for why this test failed. + # + def get_details + return @error + end + +end + + +# +# Sample test, for testing. +# +if __FILE__ == $0 then + + # + # Sample data. + # + test = { + "target_host" => "mail.steve.org.uk", + "test_type" => "smtp", + "verbose" => 1, + "test_alert" => "SMTP failure", + } + + + # + # Run the test. + # + obj = SMTPTest.new( test ) + if ( obj.run_test ) + puts "TEST OK" + else + puts "TEST FAILED" + puts obj.get_details() + end + end diff --git a/worker/tests/ssh.rb b/worker/tests/ssh.rb index 4264c8e..9fd2277 100755 --- a/worker/tests/ssh.rb +++ b/worker/tests/ssh.rb @@ -1,50 +1,126 @@ +#!/usr/bin/ruby + +require 'socket' require 'timeout' # -# Run an SSH test. -# -# -# Return value -# TRUE: The host is up +# Test that we can receive a response from an SSH server that looks +# reasonable. # -# FALSE: The host is not up -# -def ssh_test ( params ) +class SSHTest # - # Get the hostname & port to test against. + # Data passed from the JSON hash. + # + attr_reader :test_data + + # + # The error text we return on failure. + # + attr_reader :error + + + # - host = params['target_host'] - port = params['test_port'] + # 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 "get_details". + # + def run_test + @error = "" + + # + # Get the hostname & port to test against. + # + host = @test_data['target_host'] + port = @test_data['test_port'] - puts "SSH testing host #{host}:#{port}" + puts "ssh testing host #{host}:#{port}" if ( @test_data['verbose'] ) + begin + timeout(3) do - begin - timeout(3) do + begin + socket = TCPSocket.new( host, port ) + socket.puts( "QUIT") - begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") + banner = socket.gets(nil) + banner = banner[0,20] - banner = socket.gets(nil) - banner = banner[0,20] - socket.close() + socket.close() - if ( banner =~ /ssh/i ) - puts "SSH alive: #{banner}" - return true + if ( 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 - rescue - puts "SSH exception on host #{host}:#{port} - #{$!}" - return false end + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false end - rescue Timeout::Error => e - puts "TIMEOUT: #{e}" + + @error = "Misc failure" return false end - return false + + + # + # Return the error text for why this test failed. + # + def get_details + 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, + "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.get_details() + end + end -- cgit v1.2.1