monittr 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,6 +1,6 @@
1
1
  # Monittr #
2
2
 
3
- Monittr provides a Ruby interface for the [Monit](http://mmonit.com/monit/) systems management system. Its main goal is to display statistics from multiple Monit instances in an attractive web interface.
3
+ Monittr provides a Ruby interface for the [Monit](http://mmonit.com/monit/) systems management system. Its main goal is to aggregate statistics from multiple Monit instances and display them in an attractive web interface.
4
4
 
5
5
  Monittr loads XML from the [web server embedded in Monit](http://mmonit.com/monit/documentation/monit.html#monit_httpd) and makes it accessible as Ruby objects. It also provides helpers for [Sinatra](http://www.sinatrarb.com/) applications, to display the information as HTML. You can insert the statistics into any page, or create a dedicated one. You can use the default template, or create your own. The default template is located in `lib/monittr/sinatra/template.erb` and pictured below.
6
6
 
@@ -15,11 +15,11 @@ the sources from [Github](https://github.com/karmi/monittr/), to get the latest
15
15
  $ git clone http://github.com/karmi/monittr.git
16
16
  $ cd monittr
17
17
 
18
- You can try the _Ruby_ interface in a IRB console:
18
+ You can try the Ruby interface in a IRB console:
19
19
 
20
20
  $ irb -Ilib -rubygems -rmonittr
21
21
 
22
- You have to pass one or more full URLs to a local or remote Monit web server output:
22
+ You have to pass one or more URLs to a local or remote Monit [HTTP server](http://mmonit.com/monit/documentation/monit.html#monit_httpd):
23
23
 
24
24
  cluster = Monittr::Cluster.new ['http://localhost:2812/']
25
25
 
@@ -52,13 +52,23 @@ You can also check out the HTML display by running the example application:
52
52
  $ ruby examples/application.rb
53
53
  $ open http://localhost:4567/
54
54
 
55
- You should see the information about two faked Monit instances in your browser.
55
+ You should see the information about two faked Monit instances in your browser. (You may need to comment out the FakeWeb section, if you're passing `localhost` URLs.)
56
56
 
57
- Provide the URLs to live Monit instances by setting the appropriate option in `application.rb`:
57
+ To use the gem in a Sinatra application, you have to require the gem, the Sinatra helper and provide the URLs to Monit instances:
58
58
 
59
+ require 'monittr'
60
+ require 'monittr/sinatra/monittr'
59
61
  set :monit_urls, %w[ http://production.example.com:2812 http://staging.example.com:2812 ]
60
62
 
61
- You may also need to comment out the FakeWeb section, if you're passing `localhost` URLs.
63
+ In a “modular” Sinatra application, you have to register the module explicitely as well:
64
+
65
+ register Sinatra::MonittrHTML
66
+
67
+ Then, just call the helper in your template:
68
+
69
+ <%= monittr.html %>
70
+
71
+ You may use the example application as the starting point.
62
72
 
63
73
 
64
74
  ## Customization ##
@@ -68,8 +78,6 @@ It's easy to customize the HTML output by setting the appropriate options in you
68
78
  set :template, Proc.new { File.join(root, 'template.erb') }
69
79
  set :stylesheet, '/path/to/my/stylesheet'
70
80
 
71
- Please see the example application for prepared examples.
72
-
73
81
 
74
82
  ## Installation ##
75
83
 
@@ -5,7 +5,9 @@ require 'monittr'
5
5
  require 'sinatra'
6
6
  require 'fakeweb'
7
7
 
8
+ # --- Require the Sinatra extension -------------------------------------------
8
9
  require 'monittr/sinatra/monittr'
10
+ # -----------------------------------------------------------------------------
9
11
 
10
12
  # --- Comment these lines to enable loading data from http://localhost:2812 ---
11
13
  FakeWeb.register_uri(:get, 'http://localhost:2812/_status?format=xml',
@@ -15,6 +15,9 @@
15
15
  #monittr a:hover
16
16
  { text-decoration: underline; }
17
17
 
18
+ #monittr p
19
+ { margin: 0; padding: 0; }
20
+
18
21
  #monittr #toggle
19
22
  { font-size: 80%;
20
23
  text-decoration: underline;
@@ -19,12 +19,13 @@
19
19
  <span class="info"><span class="label">load: </span><%= server.system.load || 'N/A' %></span>
20
20
  <span class="info"><span class="label">cpu: </span><%= server.system.cpu || 'N/A' %></span>
21
21
  <span class="info"><span class="label">memory: </span><%= server.system.memory || 'N/A' %></span>
22
+ <span class="info"><span class="label">swap: </span><%= server.system.swap || 'N/A' %></span>
22
23
  <span class="info"><span class="label">uptime: </span><%= time_in_words(server.system.uptime) || 'N/A' %></span>
23
24
  </small>
24
25
  </h2>
25
26
 
26
27
  <div class="content">
27
- <div class="segment filesystem">
28
+ <div class="segment filesystems">
28
29
  <h3>Filesystem</h3>
29
30
  <ul class="clearfix">
30
31
  <% server.filesystems.each do |fs| %>
@@ -39,7 +40,7 @@
39
40
  <% end %>
40
41
  </ul>
41
42
  <div class="clear"></div>
42
- </div><!-- /filesystem -->
43
+ </div><!-- /filesystems -->
43
44
 
44
45
  <div class="segment processes">
45
46
  <h3>Processes</h3>
@@ -62,7 +63,28 @@
62
63
  <% end %>
63
64
  </ul>
64
65
  <div class="clear"></div>
65
- </div><!-- /filesystem -->
66
+ </div><!-- /processes -->
67
+
68
+ <div class="segment hosts">
69
+ <h3>Hosts</h3>
70
+ <ul>
71
+ <% server.hosts.each do |host| %>
72
+ <li>
73
+ <strong>
74
+ <span class="dot status <%= host.status == 0 ? 'running' : 'failure' %>">&middot;</span>
75
+ <span class="dot monitored <%= host.monitored == 1 ? 'running' : 'failure' %>">&middot;</span>
76
+ <%= host.name %>
77
+ <% unless host.monitored == 1 %><span class="label warning">NOT MONITORED</span><% end %>
78
+ </strong>
79
+
80
+ <small>
81
+ <span class="info"><span class="label">response time: </span><%= host.response_time || 'N/A' %></span>
82
+ </small>
83
+ </li>
84
+ <% end %>
85
+ </ul>
86
+ <div class="clear"></div>
87
+ </div><!-- /hosts -->
66
88
 
67
89
  </div><!-- /content -->
68
90
 
@@ -1,3 +1,3 @@
1
1
  module Monittr
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/monittr.rb CHANGED
@@ -22,7 +22,7 @@ module Monittr
22
22
  #
23
23
  class Server
24
24
 
25
- attr_reader :url, :xml, :system, :filesystems, :processes
25
+ attr_reader :url, :xml, :system, :filesystems, :processes, :hosts
26
26
 
27
27
  def initialize(url, xml)
28
28
  @url = url
@@ -33,10 +33,12 @@ module Monittr
33
33
  :status => 3
34
34
  @filesystems = []
35
35
  @processes = []
36
+ @hosts = []
36
37
  else
37
38
  @system = Services::System.new(@xml.xpath("//service[@type=5]").first)
38
39
  @filesystems = @xml.xpath("//service[@type=0]").map { |xml| Services::Filesystem.new(xml) }
39
40
  @processes = @xml.xpath("//service[@type=3]").map { |xml| Services::Process.new(xml) }
41
+ @hosts = @xml.xpath("//service[@type=4]").map { |xml| Services::Host.new(xml) }
40
42
  end
41
43
  end
42
44
 
@@ -91,6 +93,7 @@ module Monittr
91
93
  :load => value('system/load/avg01', :to_f),
92
94
  :cpu => value('system/cpu/user', :to_f),
93
95
  :memory => value('system/memory/percent', :to_f),
96
+ :swap => value('system/swap/percent', :to_f),
94
97
  :uptime => value('//server/uptime', :to_i)
95
98
  } )
96
99
  end
@@ -112,11 +115,11 @@ module Monittr
112
115
  :usage => value('block/usage' ),
113
116
  :total => value('block/total' )
114
117
  } )
115
- rescue Exception => e
116
- puts "ERROR: #{e.class} -- #{e.message}, In: #{e.backtrace.first}"
117
- super( { :name => 'Error',
118
- :status => 3,
119
- :message => e.message } )
118
+ rescue Exception => e
119
+ puts "ERROR: #{e.class} -- #{e.message}, In: #{e.backtrace.first}"
120
+ super( { :name => 'Error',
121
+ :status => 3,
122
+ :message => e.message } )
120
123
  end
121
124
  end
122
125
 
@@ -136,13 +139,34 @@ module Monittr
136
139
  :uptime => value('uptime', :to_i),
137
140
  :memory => value('memory/percent', :to_f),
138
141
  :cpu => value('cpu/percent', :to_i)
142
+ } )
143
+ rescue Exception => e
144
+ puts "ERROR: #{e.class} -- #{e.message}, In: #{e.backtrace.first}"
145
+ super( { :name => 'Error',
146
+ :status => 3,
147
+ :message => e.message } )
148
+ end
149
+ end
139
150
 
151
+ # A "host" service in Monit
152
+ #
153
+ # http://mmonit.com/monit/documentation/monit.html#connection_testing
154
+ #
155
+ # <service type="4">
156
+ #
157
+ class Host < Base
158
+ def initialize(xml)
159
+ @xml = xml
160
+ super( { :name => value('name' ),
161
+ :status => value('status', :to_i),
162
+ :monitored => value('monitor', :to_i),
163
+ :response_time => value('port/responsetime' )
140
164
  } )
141
- rescue Exception => e
142
- puts "ERROR: #{e.class} -- #{e.message}, In: #{e.backtrace.first}"
143
- super( { :name => 'Error',
144
- :status => 3,
145
- :message => e.message } )
165
+ rescue Exception => e
166
+ puts "ERROR: #{e.class} -- #{e.message}, In: #{e.backtrace.first}"
167
+ super( { :name => 'Error',
168
+ :status => 3,
169
+ :message => e.message } )
146
170
  end
147
171
  end
148
172
 
@@ -496,4 +496,24 @@
496
496
  </memory>
497
497
  </system>
498
498
  </service>
499
+
500
+ <service type="4">
501
+ <name>www.google.com</name>
502
+ <collected_sec>1291193687</collected_sec>
503
+ <collected_usec>388019</collected_usec>
504
+ <status>0</status>
505
+ <status_hint>0</status_hint>
506
+ <monitor>1</monitor>
507
+ <monitormode>0</monitormode>
508
+ <pendingaction>0</pendingaction>
509
+ <port>
510
+ <hostname>www.google.com</hostname>
511
+ <portnumber>80</portnumber>
512
+ <request>/</request>
513
+ <protocol>HTTP</protocol>
514
+ <type>TCP</type>
515
+ <responsetime>0.009</responsetime>
516
+ </port>
517
+ </service>
518
+
499
519
  </monit>
data/test/monittr_test.rb CHANGED
@@ -96,6 +96,16 @@ module Monittr
96
96
  assert_equal 0.0, thin.cpu
97
97
  end
98
98
 
99
+ should "return hosts info" do
100
+ assert_not_nil @server.hosts
101
+ assert_equal 1, @server.hosts.size
102
+
103
+ assert_equal 'www.google.com', @server.hosts[0].name
104
+ assert_equal 0, @server.hosts[0].status
105
+ assert_equal 1, @server.hosts[0].monitored
106
+ assert_equal '0.009', @server.hosts[0].response_time
107
+ end
108
+
99
109
  end
100
110
 
101
111
  [ Services::System, Services::Filesystem, Services::Process ].each do |klass|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Karel Minarik
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-28 00:00:00 +01:00
17
+ date: 2010-12-01 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency