From ca9be00dc45d0a71210ad16423d6d319c87b6727 Mon Sep 17 00:00:00 2001
From: Steve Kemp <steve@steve.org.uk>
Date: Tue, 13 Jan 2015 16:14:56 +0000
Subject: Updated to actually test the backend MX servers.

We now connec to the MX-servers via XX:25 and alert if that
fails.:wq
---
 lib/custodian/protocoltest/mx.rb | 53 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)

(limited to 'lib')

diff --git a/lib/custodian/protocoltest/mx.rb b/lib/custodian/protocoltest/mx.rb
index ed46c4c..8b415ec 100644
--- a/lib/custodian/protocoltest/mx.rb
+++ b/lib/custodian/protocoltest/mx.rb
@@ -88,10 +88,57 @@ module Custodian
           return false
         end
 
-        mx.each do |bob|
-          puts "XXXX #{bob}"
+
+        #
+        #  For each host we must make a connection.
+        #
+        #  We'll keep count of failures.
+        #
+        failed = 0
+        passed = 0
+        error  = ""
+
+        mx.each do |backend|
+
+          begin
+            timeout(period) do
+              begin
+                socket = TCPSocket.new( backend, 25 )
+                read = socket.sysread(1024)
+
+                # trim to a sane length & strip newlines.
+                if ( ! read.nil? )
+                  read = read[0,255]
+                  read.gsub!(/[\n\r]/, "")
+                end
+
+                if ( read =~ /^220/ )
+                  passed += 1
+                else
+                  failed += 1
+                end
+              rescue
+                # Failure to connect.
+                failed +=1
+                error += "Error connecting to #{backend}:25. "
+              end
+            end
+          rescue Timeout::Error => ex
+            # Timeout
+            failed +=1
+            error += "Timeout connecting to #{backend}:25. "
+          end
+        end
+
+        #
+        #  At this point we should have tested the things
+        #
+        if ( failed > 0 )
+          @error = "There are #{mx.size} hosts running as MX-servers for domain #{@host} - #{passed}:OK #{failed}:FAILED - #{error}"
+          return false
+        else
+          return true;
         end
-        return true;
       end
 
 
-- 
cgit v1.2.3