monittr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -17,16 +17,16 @@ You can try the interface in a IRB console:
17
17
 
18
18
  $ irb -Ilib -rubygems -rmonittr
19
19
 
20
- You have to pass one or more full URLs to a running Monit web server XML output, eg. `http://admin:monit@example.com:2812/_status?format=xml`.
20
+ You have to pass one or more full URLs to a running Monit web server XML output, eg. `http://admin:monit@example.com:2812/`.
21
21
 
22
22
  In case you don't have a running Monit handy, fake its output:
23
23
 
24
24
  require 'fakeweb'
25
- FakeWeb.register_uri(:get, 'http://localhost:2812/_status?format=xml', :body => File.read('test/fixtures/status.xml') ); nil
25
+ FakeWeb.register_uri(:get, 'http://localhost:2812/', :body => File.read('test/fixtures/status.xml') ); nil
26
26
 
27
27
  Now, retrieve information from the cluster:
28
28
 
29
- cluster = Monittr::Cluster.new ['http://localhost:2812/_status?format=xml']
29
+ cluster = Monittr::Cluster.new ['http://localhost:2812/']
30
30
  cluster.servers.size
31
31
 
32
32
  server = cluster.servers.first
@@ -51,7 +51,7 @@ You should see the information about two faked Monit instances in your browser.
51
51
 
52
52
  Provide the URLs to _Monit_ instances by setting the appropriate option:
53
53
 
54
- set :monit_urls, ['http://production.example.com:2812/_status?format=xml', 'http://staging.example.com:2812/_status?format=xml']
54
+ set :monit_urls, %w[ http://production.example.com:2812 http://staging.example.com:2812 ]
55
55
 
56
56
 
57
57
  ## Customization ##
@@ -9,7 +9,7 @@ require 'monittr/sinatra/monittr'
9
9
 
10
10
  FakeWeb.register_uri(:get, 'http://localhost:2812/_status?format=xml', :body => File.read( File.expand_path('../../test/fixtures/status.xml', __FILE__)))
11
11
 
12
- set :monit_urls, ['http://localhost:2812/_status?format=xml', 'http://localhost:2812/_status?format=xml']
12
+ set :monit_urls, %w[ http://localhost:2812 http://localhost:2812 ]
13
13
 
14
14
  # set :template, Proc.new { File.join(root, 'template.erb') }
15
15
  # set :stylesheet, '/path/to/my/stylesheet'
data/lib/monittr.rb CHANGED
@@ -22,10 +22,11 @@ module Monittr
22
22
  #
23
23
  class Server
24
24
 
25
- attr_reader :xml, :system, :filesystems, :processes
25
+ attr_reader :url, :xml, :system, :filesystems, :processes
26
26
 
27
- def initialize(xml)
28
- @xml = Nokogiri::XML(xml)
27
+ def initialize(url, xml)
28
+ @url = url
29
+ @xml = Nokogiri::XML(xml)
29
30
  if error = @xml.xpath('error').first
30
31
  @system = Services::Base.new :name => error.attributes['name'].content,
31
32
  :message => error.attributes['message'].content,
@@ -39,11 +40,13 @@ module Monittr
39
40
 
40
41
  # Retrieve Monit status XML from the URL
41
42
  #
42
- def self.fetch(url=nil)
43
- url = url || ENV['MONIT_URL'] || 'http://admin:monit@localhost:2812/_status?format=xml'
44
- self.new( RestClient.get(url) )
43
+ def self.fetch(url='http://admin:monit@localhost:2812')
44
+ monit_url = url
45
+ monit_url += '/' unless url =~ /\/$/
46
+ monit_url += '_status?format=xml' unless url =~ /_status\?format=xml$/
47
+ self.new url, RestClient.get(monit_url)
45
48
  rescue Exception => e
46
- self.new(%Q|<error status="3" name="#{e.class}" message="#{e.message}" />|)
49
+ self.new url, %Q|<error status="3" name="#{e.class}" message="#{e.message}" />|
47
50
  end
48
51
 
49
52
  def inspect
@@ -59,6 +62,11 @@ module Monittr
59
62
  class Base < OpenStruct
60
63
  TYPES = { 0 => "Filesystem", 1 => "Directory", 2 => "File", 3 => "Daemon", 4 => "Connection", 5 => "System" }
61
64
 
65
+ def load
66
+ # Note: the `load` gives some headaches, let's be explicit
67
+ @table[:load]
68
+ end
69
+
62
70
  def value(matcher, converter=:to_s)
63
71
  @xml.xpath(matcher).first.content.send(converter) rescue nil
64
72
  end
@@ -81,7 +89,7 @@ module Monittr
81
89
  :load => value('system/load/avg01', :to_f),
82
90
  :cpu => value('system/cpu/user', :to_f),
83
91
  :memory => value('system/memory/percent', :to_f),
84
- :uptime => value('//server/uptime', :to_f)
92
+ :uptime => value('//server/uptime', :to_i)
85
93
  } )
86
94
  end
87
95
  end
@@ -32,12 +32,33 @@ module Sinatra
32
32
  app.settings.stylesheet ? File.read( app.settings.stylesheet ) : ''
33
33
  end
34
34
 
35
+ def time_in_words(seconds)
36
+ case seconds
37
+ when 0..60
38
+ "#{seconds } seconds"
39
+ when 60..3600
40
+ value = seconds/60
41
+ "#{value} minute#{value > 1 ? 's' : ''}"
42
+ when 3600..86400
43
+ value = seconds/3600
44
+ "#{value} hour#{ value > 1 ? 's' : ''}"
45
+ when 86400..604800
46
+ value = seconds/86400
47
+ "#{value} day#{ value > 1 ? 's' : ''}"
48
+ when 604800..2419200
49
+ value = seconds/604800
50
+ "#{value} week#{ value > 1 ? 's' : ''}"
51
+ else
52
+ nil
53
+ end
54
+ end
55
+
35
56
  end
36
57
 
37
58
  def self.registered(app)
38
59
  app.helpers MonittrHTML::Helpers
39
60
 
40
- app.set :monit_urls, ['http://admin:monit@localhost:2812/_status?format=xml']
61
+ app.set :monit_urls, ['http://admin:monit@localhost:2812']
41
62
  app.set :template, File.expand_path('../template.erb', __FILE__)
42
63
  app.set :stylesheet, File.expand_path('../style.css', __FILE__)
43
64
  end
@@ -1,5 +1,5 @@
1
1
  #monittr
2
- { font-family: sans-serif; }
2
+ { color: #222; font-family: sans-serif; }
3
3
 
4
4
  #monittr .clear
5
5
  { clear:both; display:block; overflow:hidden; visibility:hidden; width:0; height:0 }
@@ -9,6 +9,13 @@
9
9
  -webkit-border-radius: 0.5em;
10
10
  border-radius: 0.5em }
11
11
 
12
+ #monittr a
13
+ { color: #222;
14
+ text-decoration: none; }
15
+
16
+ #monittr a:hover
17
+ { text-decoration: underline; }
18
+
12
19
  #monittr #toggle
13
20
  { font-size: 80%;
14
21
  text-decoration: underline;
@@ -11,7 +11,7 @@
11
11
  <strong>
12
12
  <span class="dot status <%= server.system.status == 0 ? 'running' : 'failure' %>">&middot;</span>
13
13
  <span class="dot monitored <%= server.system.monitored == 1 ? 'running' : 'failure' %>">&middot;</span>
14
- <%= server.system.name %>
14
+ <a href="<%= server.url %>"><%= server.system.name %></a>
15
15
  <%= "<em>#{server.system.message}</em>" if server.system.message %>
16
16
  </strong>
17
17
 
@@ -19,7 +19,7 @@
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">uptime: </span><%= server.system.uptime || 'N/A' %></span>
22
+ <span class="info"><span class="label">uptime: </span><%= time_in_words(server.system.uptime) || 'N/A' %></span>
23
23
  </small>
24
24
  </h2>
25
25
 
@@ -56,7 +56,7 @@
56
56
  <small>
57
57
  <span class="info"><span class="label">cpu: </span><%= process.cpu || 'N/A' %></span>
58
58
  <span class="info"><span class="label">memory: </span><%= process.memory || 'N/A' %></span>
59
- <span class="info"><span class="label">uptime: </span><%= process.uptime || 'N/A' %></span>
59
+ <span class="info"><span class="label">uptime: </span><%= time_in_words(process.uptime) || 'N/A' %></span>
60
60
  </small>
61
61
  </li>
62
62
  <% end %>
@@ -1,3 +1,3 @@
1
1
  module Monittr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/test/monittr_test.rb CHANGED
@@ -8,8 +8,8 @@ module Monittr
8
8
 
9
9
  should "be initialized with URLs" do
10
10
  assert_nothing_raised do
11
- cluster = Monittr::Cluster.new %w[ http://admin:monit@localhost:2812/_status?format=xml
12
- http://admin:monit@localhost:2812/_status?format=xml ]
11
+ cluster = Monittr::Cluster.new %w[ http://admin:monit@localhost:2812
12
+ http://admin:monit@localhost:2812 ]
13
13
  assert_not_nil cluster.servers
14
14
  assert_equal 2, cluster.servers.size
15
15
  end
@@ -17,16 +17,18 @@ module Monittr
17
17
 
18
18
  should "not fail on invalid URLS" do
19
19
  assert_nothing_raised do
20
- cluster = Monittr::Cluster.new %w[ NOTVALID
21
- http://admin:monit@localhost:2812/_status?format=xml ]
20
+ cluster = Monittr::Cluster.new %w[ ~INVALID
21
+ http://admin:monit@localhost:2812 ]
22
22
  assert_not_nil cluster.servers
23
23
  assert_equal 2, cluster.servers.size
24
+ assert_equal 3, cluster.servers.first.system.status
25
+ assert cluster.servers.first.system.message =~ /bad hostname/, "Should be bad hostname"
24
26
  end
25
27
  end
26
28
 
27
29
  should "not fail on out-of-order URLs" do
28
- cluster = Monittr::Cluster.new %w[ http://not-working/_status?format=xml
29
- http://admin:monit@localhost:2812/_status?format=xml ]
30
+ cluster = Monittr::Cluster.new %w[ http://not-working
31
+ http://admin:monit@localhost:2812 ]
30
32
  assert_not_nil cluster.servers
31
33
  assert_equal 2, cluster.servers.size
32
34
  assert_equal 3, cluster.servers.first.system.status
@@ -38,19 +40,25 @@ module Monittr
38
40
  context "Server" do
39
41
 
40
42
  setup do
41
- @server = Server.new( fixture_file('status.xml') )
43
+ @server = Server.new( 'http://localhost:2812', fixture_file('status.xml') )
42
44
  end
43
45
 
44
46
  should "parse error XML on initialization" do
45
47
  assert_nothing_raised do
46
- server = Server.new(%Q|<error status="3" name="ERROR" message="MESSAGE" />|)
48
+ server = Server.new 'http://example.com', %Q|<error status="3" name="ERROR" message="MESSAGE" />|
47
49
  assert_equal 'ERROR', server.system.name
48
50
  end
49
51
  end
50
52
 
51
53
  should "fetch info from Monit embedded web server" do
52
54
  assert_nothing_raised { Server.fetch }
53
- assert_nothing_raised { Server.fetch('http://admin:monit@localhost:2812/_status?format=xml') }
55
+ assert_nothing_raised { Server.fetch('http://admin:monit@localhost:2812') }
56
+ assert_nothing_raised { Server.fetch('http://admin:monit@localhost:2812/') }
57
+ end
58
+
59
+ should "return the URL" do
60
+ server = Server.fetch('http://admin:monit@localhost:2812')
61
+ assert_equal 'http://admin:monit@localhost:2812', server.url
54
62
  end
55
63
 
56
64
  should "return system info" do
data/test/test_helper.rb CHANGED
@@ -17,7 +17,7 @@ class Test::Unit::TestCase
17
17
  def setup
18
18
  FakeWeb.register_uri(:get, 'http://admin:monit@localhost:2812/_status?format=xml', :body => fixture_file('status.xml'))
19
19
  FakeWeb.register_uri(:get, 'http://not-working/_status?format=xml', :body => '', :status => ['500'])
20
- # FakeWeb.allow_net_connect = false
20
+ FakeWeb.allow_net_connect = false
21
21
  end
22
22
 
23
23
  def fixtures_path
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Karel Minarik