monit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .yardoc
5
+ doc/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in monit.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ monit (0.1.0)
5
+ crack (~> 0.1.8)
6
+ curb (~> 0.7.8)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ awesome_print (0.2.1)
12
+ crack (0.1.8)
13
+ curb (0.7.8)
14
+ diff-lcs (1.1.2)
15
+ rake (0.8.7)
16
+ rspec (2.0.0.beta.20)
17
+ rspec-core (= 2.0.0.beta.20)
18
+ rspec-expectations (= 2.0.0.beta.20)
19
+ rspec-mocks (= 2.0.0.beta.20)
20
+ rspec-core (2.0.0.beta.20)
21
+ rspec-expectations (2.0.0.beta.20)
22
+ diff-lcs (>= 1.1.2)
23
+ rspec-mocks (2.0.0.beta.20)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ awesome_print
30
+ bundler (~> 1.0.0)
31
+ crack (~> 0.1.8)
32
+ curb (~> 0.7.8)
33
+ monit!
34
+ rake
35
+ rspec (~> 2.0.0.beta.20)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Matias Korhonen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ Monit Rubygem
2
+ =============
3
+
4
+ A Ruby interface for Monit.
5
+
6
+ ## Installation
7
+
8
+ Just like any other gem:
9
+
10
+ gem install monit
11
+
12
+ ## Usage
13
+
14
+ status = Monit::Status.new({ :host => "monit.myserver.com",
15
+ :auth => true,
16
+ :username => "foo",
17
+ :password => "bar" })
18
+ status.get # => true
19
+ status.platform # => <Monit::Platform:0x00000003673dd0 ... >
20
+ status.platform.cpu # => 2
21
+ status.platform.memory # => 4057712
22
+
23
+ For more options see the API documentation
24
+
25
+ ## Compatibility
26
+
27
+ At the moment the gem is only compatible with Ruby 1.9.2.
28
+
29
+ ## License and copyright
30
+
31
+ Copyright (c) 2010 Matias Korhonen
32
+
33
+ Licensed under the MIT license, see the LICENSE file for details.
@@ -0,0 +1,14 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ gemspec = eval(File.read("monit.gemspec"))
8
+
9
+ task :build => "#{gemspec.full_name}.gem"
10
+
11
+ file "#{gemspec.full_name}.gem" => gemspec.files + ["monit.gemspec"] do
12
+ system "gem build monit.gemspec"
13
+ system "gem install monit-#{Monit::VERSION}.gem"
14
+ end
@@ -0,0 +1,5 @@
1
+ require "monit/httpd"
2
+ require "monit/platform"
3
+ require "monit/server"
4
+ require "monit/service"
5
+ require "monit/status"
@@ -0,0 +1,12 @@
1
+ module Monit
2
+ # The HTTPD section from the Monit XML
3
+ class HTTPD
4
+ attr_reader :address, :port, :ssl
5
+
6
+ def initialize(options = {})
7
+ @address = options[address]
8
+ @port = options["port"].to_i
9
+ @ssl = options["ssl"] == "1" ? true : false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Monit
2
+ # The platform section from the Monit XML
3
+ class Platform
4
+ attr_reader :name, :release, :version, :machine, :cpu, :memory
5
+
6
+ def initialize(options = {})
7
+ @name = options["name"]
8
+ @release = options["release"]
9
+ @version = options["version"]
10
+ @machine = options["machine"]
11
+ @cpu = options["cpu"].to_i
12
+ @memory = options["memory"].to_i
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Monit
2
+ # The server section from the Monit XML
3
+ class Server
4
+ attr_reader :id, :incarnation, :version, :uptime, :poll, :startdelay, :localhostname, :controlfile, :httpd
5
+
6
+ def initialize(options = {})
7
+ @id = options["id"]
8
+ @incarnation = options["incarnation"]
9
+ @version = options["version"]
10
+ @uptime = options["uptime"].to_i
11
+ @poll = options["poll"].to_i
12
+ @startdelay = options["startdelay"].to_i
13
+ @localhostname = options["localhostname"]
14
+ @controlfile = options["controlfile"]
15
+ @httpd = options["httpd"].is_a?(HTTPD) ? options["httpd"] : HTTPD.new(options["httpd"])
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require "ostruct"
2
+ module Monit
3
+ # The service section from the Monit XML. Inherits from OpenStruct.
4
+ class Service < OpenStruct
5
+ TYPES = { 0 => "Filesystem", 1 => "Directory", 2 => "File", 3 => "Daemon", 4 => "Connection", 5 => "System" }
6
+ end
7
+ end
@@ -0,0 +1,75 @@
1
+ require "curb"
2
+ require "uri"
3
+ require "crack/xml"
4
+
5
+ # A Ruby interface for Monit
6
+ module Monit
7
+ # The +Status+ class is used to get data from the Monit instance. You should not
8
+ # need to manually instantiate any of the other classes.
9
+ class Status
10
+ attr_reader :url, :hash, :xml, :server, :platform, :services
11
+ attr_accessor :username, :auth, :host, :port, :ssl, :auth, :username
12
+ attr_writer :password
13
+
14
+ # Create a new instance of the status class with the given options
15
+ #
16
+ # <b>Options:</b>
17
+ # * +host+ - the host for monit, defaults to +localhost+
18
+ # * +port+ - the Monit port, defaults to +2812+
19
+ # * +ssl+ - should we use SSL for the connection to Monit (default: false)
20
+ # * +auth+ - should authentication be used, defaults to false
21
+ # * +username+ - username for authentication
22
+ # * +password+ - password for authentication
23
+ def initialize(options = {})
24
+ @host ||= options[:host] ||= "localhost"
25
+ @port ||= options[:port] ||= 2812
26
+ @ssl ||= options[:ssl] ||= false
27
+ @auth ||= options[:auth] ||= false
28
+ @username = options[:username]
29
+ @password = options[:password]
30
+ @services = []
31
+ end
32
+
33
+ # Construct the URL
34
+ def url
35
+ url_params = { :host => @host, :port => @port, :path => "/_status", :query => "format=xml" }
36
+ @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
37
+ end
38
+
39
+ # Get the status from Monit.
40
+ def get
41
+ c = Curl::Easy.new(self.url.to_s) do |curl|
42
+ if @auth
43
+ curl.password = @password
44
+ curl.username = @username
45
+ end
46
+ curl.headers["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
47
+ end
48
+ c.perform
49
+
50
+ if c.response_code == 200
51
+ @xml = c.body_str
52
+ return self.parse(@xml)
53
+ else
54
+ return false
55
+ end
56
+ end
57
+
58
+ # Parse the XML from Monit into a hash and into a Ruby representation.
59
+ def parse(xml)
60
+ @hash = Crack::XML.parse(xml)
61
+ @server = Server.new(@hash["monit"]["server"])
62
+ @platform = Platform.new(@hash["monit"]["platform"])
63
+ if @hash["monit"]["service"].is_a? Array
64
+ @services = @hash["monit"]["service"].map do |service|
65
+ Service.new(service)
66
+ end
67
+ else
68
+ @services = [Service.new(@hash["monit"]["service"])]
69
+ end
70
+ true
71
+ rescue
72
+ false
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module Monit
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/monit/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "monit"
6
+ s.version = Monit::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Matias Korhonen"]
9
+ s.email = ["matias@kiskolabs.com"]
10
+ s.homepage = "http://rubygems.org/gems/monit"
11
+ s.summary = "Connect to Monit"
12
+ s.description = "Retrieve server information from Monit."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "monit"
16
+
17
+ s.add_development_dependency "bundler", "~> 1.0.0"
18
+ s.add_development_dependency "rake"
19
+ s.add_development_dependency "rspec", "~> 2.0.0.beta.20"
20
+ s.add_development_dependency "awesome_print"
21
+
22
+ s.add_dependency "crack", "~> 0.1.8"
23
+ s.add_dependency "curb", "~> 0.7.8"
24
+
25
+ s.files = `git ls-files`.split("\n")
26
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
27
+ s.require_path = 'lib'
28
+ end
@@ -0,0 +1,165 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe Monit do
5
+ context "Status" do
6
+ it "should be possible to instatiate it with no options" do
7
+ @status = Monit::Status.new
8
+ @status.should be_kind_of Monit::Status
9
+ @status.host.should == "localhost"
10
+ @status.port.should == 2812
11
+ @status.ssl.should be_false
12
+ @status.auth.should be_false
13
+ @status.username.should be_nil
14
+ lambda { @status.password }.should raise_error(NoMethodError)
15
+ @status.services.should == []
16
+ end
17
+
18
+ it "should generate the correct URL" do
19
+ @status = Monit::Status.new
20
+ @status.url.should be_kind_of URI
21
+ @status.url.to_s.should == "http://localhost:2812/_status?format=xml"
22
+ @status.ssl = true
23
+ @status.url.to_s.should == "https://localhost:2812/_status?format=xml"
24
+ @status.host = "rails.fi"
25
+ @status.url.to_s.should == "https://rails.fi:2812/_status?format=xml"
26
+ @status.port = 8080
27
+ @status.url.to_s.should == "https://rails.fi:8080/_status?format=xml"
28
+ end
29
+
30
+ it "should parse XML into a Hash" do
31
+ status = Monit::Status.new
32
+ status.stub!(:xml).and_return(File.read("./spec/samples/smallish_status.xml"))
33
+ status.parse(status.xml)
34
+ status.hash.should be_kind_of Hash
35
+ status.hash["monit"].should_not be_nil
36
+ status.hash["monit"]["server"].should_not be_nil
37
+ status.hash["monit"]["platform"].should_not be_nil
38
+ status.hash["monit"]["service"].should_not be_nil
39
+
40
+ status.stub!(:xml).and_return(File.read("./spec/samples/largish_status.xml"))
41
+ status.parse(status.xml)
42
+ status.hash.should be_kind_of Hash
43
+ status.hash["monit"].should_not be_nil
44
+ status.hash["monit"]["server"].should_not be_nil
45
+ status.hash["monit"]["platform"].should_not be_nil
46
+ status.hash["monit"]["service"].should_not be_nil
47
+ end
48
+
49
+ it "should parse XML into a Ruby representation" do
50
+ status = Monit::Status.new
51
+ status.stub!(:xml).and_return(File.read("./spec/samples/smallish_status.xml"))
52
+ status.parse(status.xml)
53
+
54
+ status.server.should be_kind_of Monit::Server
55
+ status.server.httpd.should be_kind_of Monit::HTTPD
56
+ status.platform.should be_kind_of Monit::Platform
57
+ status.services.should be_kind_of Array
58
+ status.services.first.should be_kind_of Monit::Service
59
+ status.services.first.should be_kind_of OpenStruct
60
+
61
+ status.stub!(:xml).and_return(File.read("./spec/samples/largish_status.xml"))
62
+ status.parse(status.xml)
63
+
64
+ status.server.should be_kind_of Monit::Server
65
+ status.server.httpd.should be_kind_of Monit::HTTPD
66
+ status.platform.should be_kind_of Monit::Platform
67
+ status.services.should be_kind_of Array
68
+ status.services.first.should be_kind_of Monit::Service
69
+ status.services.first.should be_kind_of OpenStruct
70
+ end
71
+ end
72
+
73
+ context "Server" do
74
+ it "should create a new instance of Monit::Server from a hash" do
75
+ hash = { "id" => "52255a0b8999c46c98de9697a8daef67",
76
+ "incarnation" => "1283946152",
77
+ "version" => "5.1.1",
78
+ "uptime" => "4",
79
+ "poll" => "30",
80
+ "startdelay" => "0",
81
+ "localhostname" => "Example.local",
82
+ "controlfile" => "/etc/monitrc",
83
+ "httpd" => { "address" => nil,
84
+ "port" => "2812",
85
+ "ssl" => "0" } }
86
+ server = Monit::Server.new(hash)
87
+ server.id.should == "52255a0b8999c46c98de9697a8daef67"
88
+ server.incarnation.should == "1283946152"
89
+ server.version.should == "5.1.1"
90
+ server.uptime.should == 4
91
+ server.poll.should == 30
92
+ server.startdelay.should == 0
93
+ server.localhostname.should == "Example.local"
94
+ server.controlfile.should == "/etc/monitrc"
95
+ server.httpd.should be_kind_of Monit::HTTPD
96
+ end
97
+ end
98
+
99
+ context "HTTPD" do
100
+ it "should create a new instance of Monit::HTTPD from a hash" do
101
+ hash = { "address" => nil,
102
+ "port" => "2812",
103
+ "ssl" => "0" }
104
+ httpd = Monit::HTTPD.new(hash)
105
+ httpd.address.should be_nil
106
+ httpd.port.should == 2812
107
+ httpd.ssl.should be_false
108
+ end
109
+ end
110
+
111
+ context "Platform" do
112
+ it "should create a new instance of Monit::Platform from a hash" do
113
+ hash = { "name" => "Darwin",
114
+ "release" => "10.4.0",
115
+ "version" => "Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386",
116
+ "machine" => "i386",
117
+ "cpu" => "2",
118
+ "memory" => "4194304" }
119
+ platform = Monit::Platform.new(hash)
120
+ platform.name.should == "Darwin"
121
+ platform.release.should == "10.4.0"
122
+ platform.version.should == "Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386"
123
+ platform.machine.should == "i386"
124
+ platform.cpu.should == 2
125
+ platform.memory.should == 4194304
126
+ end
127
+ end
128
+
129
+ context "Service" do
130
+ it "should create a new instance of Monit::Platform from a hash" do
131
+ hash = { "collected_sec" => "1283946152",
132
+ "collected_usec" => "309973",
133
+ "name" => "Example.local",
134
+ "status" => "0",
135
+ "status_hint" => "0",
136
+ "monitor" => "1",
137
+ "monitormode" => "0",
138
+ "pendingaction" => "0",
139
+ "groups" => { "name" => "server" },
140
+ "system" => { "load" => { "avg01" => "0.28",
141
+ "avg05" => "0.43",
142
+ "avg15" => "0.48" },
143
+ "cpu" => { "user" => "10.0",
144
+ "system" => "4.1" },
145
+ "memory" => { "percent" => "44.1",
146
+ "kilobyte" => "1850152" }
147
+ },
148
+ "type" => "5" }
149
+ service = Monit::Service.new(hash)
150
+ service.should be_kind_of OpenStruct
151
+ service.should be_kind_of Monit::Service
152
+ service.collected_sec.should == "1283946152"
153
+ service.collected_usec.should == "309973"
154
+ service.name.should == "Example.local"
155
+ service.status.should == "0"
156
+ service.status_hint.should == "0"
157
+ service.monitor.should == "1"
158
+ service.monitormode.should == "0"
159
+ service.pendingaction.should == "0"
160
+ service.groups.should be_kind_of Hash
161
+ service.system.should be_kind_of Hash
162
+ service.type.should == "5"
163
+ end
164
+ end
165
+ end