RSS
 

Adding Email Alert to Ragios (St. Ruby)

19 Sep

In the last blog post, I started working on St. Ruby, an Object Oriented Ruby Based Network and System Monitoring System. Its just for fun and mostly an excuse to play with ruby and Linux servers.

I have added Email alert to Ragios. Anytime a test fails Ragios fires up an email to the system admin.

We are using the Pony ruby gem to send the email alerts.

To install the Pony gem;

sudo gem install pony

We are using sendmail for mail transport; Installing sendmail on Ubuntu;

sudo apt-get install sendmail

Testing sendmail to verify it works

echo "Subject: test" | /usr/lib/sendmail -v me@domain.com

(courtesy of cool commands)

Sample code to send email with Pony via sendmail;

#!/usr/bin/ruby
require 'rubygems'
require 'pony'
puts 'sending mail...'
Pony.mail :to => 'test@example.com',
          :from => 'obi',
          :subject => 'here we get near yeah yeah!',
          :body => 'wsup!'

Ragios Email Alert
To add the email alert to Ragios we just needed to add a new method called email_alert to the System Monitor Class. This method is invoked by the failed method. Ragios runs the failed method anytime a test fails in the system.

The email_alert method

 def email_alert
       puts 'sending mail alert...'
       Pony.mail  :to => @contact,
                  :from => 'Ragios',
                  :subject => @test_description + " FAILED",
              :body => @test_description + " FAILED \n\n" + @describe_test_result + " = " + @test_result
   end

We also added a new attribute @decribe_test_result, this describes the meaning of @test_result, this is very useful in reporting to the system admin what exactly what went wrong. More on this later.

The new System monitor class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#base class that defines the behavior of all system monitors
class System_monitor
 
    # a short description of the test performed by this system monitor
   attr_reader :test_description
 
   #hostname of the system being monitored
   attr_reader :hostname
 
   #ip address of the system being monitored
   attr_reader :address
 
   #email address of admin to contact when a test fails
   attr_reader :contact
 
   #results of a test
   attr_reader :test_result
 
    #describes the test result, this gives technical details on the test_result
    #This gives the sysadmin info on the technical details of the test results
    attr_reader :describe_test_result
 
   def initialize
 
   end
 
   #defines the tests to run on a system
   def test_command
   end
 
   #defines the action to take when a test fails
   #- May send email or SMS to the admin under contact
   #- May also take action to fix the issue like restart a process/service
   def failed
       email_alert
   end
 
   def email_alert
       puts 'sending mail alert...'
       Pony.mail  :to => @contact,
                  :from => 'Ragios',
                  :subject => @test_description + " FAILED",
              :body => @test_description + " FAILED \n\n" + @describe_test_result + " = " + @test_result
   end
end

On the last post we had a sample test class Test_webpage_for_http_200 , that tests a website to see if the home page is loading, it returns PASSED when the home page loads and FAILED when the site is down. Now that we have an email alert, we just need to add a contact email address to be alerted when the test fails.

New Test_webpage_for_http_200 class

#monitors a webpage to check if the site is loading
#PASSED if it gets a HTTP 200 Response status code from the website
class Test_webpage_for_http_200 < Service
 
   attr_reader :test_url
 
   def initialize
        @contact = "obi@mail.com"
        @describe_test_result = "HTTP Request to " + @test_url
        super
   end
 
   #returns true when http request to test_url returns a 200 OK Response
   def test_command
       response = Net::HTTP.get_response(URI.parse(test_url))
        @test_result = response.code
       if response.code != "200"
            return FALSE
       else
	    return TRUE
       end
   end
 
end

If we wanted to use the same contact email for all service tests, we simply assign the email address to @contact on the Service class. If we wanted a system wide contact email we just initialize @contact on the System_monitor class which is the base class of the system.

A specific test on my website looks like this;

class Test_my_site < Test_webpage_for_http_200
   def initialize
      @test_description  = "My Website Test"
      @test_url = "http://www.whisperservers.com/blog/"
      super
   end
end

An Email Alert from Ragios when this test fails will looks like this;

    From: Ragios@domain.com
        To: obi@mail.com
 
My Website Test FAILED
 
HTTP Request to http://www.whisperservers.com/blog/ = 500

This tells the site admin exactly what is going on.

Edit: View the most recent source code here: http://github.com/obi-a/Ragios

New Ragios complete Source code with Email Alert;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/ruby
#:Title   :Ragios (aka St. Ruby)
#:Date    :9/16/2010
#:Author  :Obi Akubue
#:Version :0.2
#description: This is the framework for a Ruby Based System and Network Monitoring Tool
 
 
require 'net/http'
require 'rubygems'
require 'pony'
 
#base class that defines the behavior of all system monitors
class System_monitor
 
    # a short description of the test performed by this system monitor
   attr_reader :test_description
 
   #hostname of the system being monitored
   attr_reader :hostname
 
   #ip address of the system being monitored
   attr_reader :address
 
   #email address of admin to contact when a test fails
   attr_reader :contact
 
   #results of a test
   attr_reader :test_result
 
    #describes the test result, this gives technical details on the test_result
    #This gives the sysadmin info on the technical details of the test results
    attr_reader :describe_test_result
 
   def initialize
 
   end
 
   #defines the tests to run on a system
   def test_command
   end
 
   #defines the action to take when a test fails
   #- May send email or SMS to the admin under contact
   #- May also take action to fix the issue like restart a process/service
   def failed
       email_alert
   end
 
   def email_alert
       puts 'sending mail alert...'
       Pony.mail  :to => @contact,
                  :from => 'Ragios',
                  :subject => @test_description + " FAILED",
              :body => @test_description + " FAILED \n\n" + @describe_test_result + " = " + @test_result
   end
end
 
#defines how computers,servers,network devices will be monitored
class Host < System_monitor
    def initialize
     super
    end
 
end
 
#defines how services will be monitored
class Service < System_monitor
    def initialize
     super
    end
end
 
 
#monitors a webpage to check if the site is loading
#PASSED if it gets a HTTP 200 Response status code from the website
class Test_webpage_for_http_200 < Service
 
   attr_reader :test_url
 
   def initialize
        @contact = "obi@mail.com"
        @describe_test_result = "HTTP Request to " + @test_url
        super
   end
 
   #returns true when http request to test_url returns a 200 OK Response
   def test_command
       response = Net::HTTP.get_response(URI.parse(test_url))
        @test_result = response.code
       if response.code != "200"
            return FALSE
       else
	    return TRUE
       end
   end
 
end
 
class Test_my_site < Test_webpage_for_http_200
   def initialize
      @test_description  = "My Website Test"
      @test_url = "http://www.whisperservers.com/blog/"
      super
   end
end
 
class Test_my_blog < Test_webpage_for_http_200
#tests my blog, to check if the blog is loading
 
   def initialize
      @test_description  = "My Blog Test"
      @test_url = "http://obi-akubue.homeunix.org/"
      super
   end
end
 
 
jobs = [ Test_my_site.new, Test_my_blog.new]
 
jobs.each do |job|
 puts job.test_description
 if job.test_command
        puts "   [PASSED]"
     else
        puts "   [FAILED]"
        job.failed
     end
end

[UPDATE 10-1-2010] View the complete Ragios source with latest revisions
http://github.com/obi-a/Ragios.

 
 

Tags: , , , , ,

 
Premium Wordpress Plugin