monit-client 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 33aff1038adc96b78cf28aedeed2f7a5eb3be1f592479c37b3563403773bb14a
4
+ data.tar.gz: 320f5e8eb157b3678c70e7e945363738cebe4906ea7fcaed97f4cfa9ebf1c526
5
+ SHA512:
6
+ metadata.gz: 5a649b7bd32fc219abbb05fc463d3bd9307acfe3fbc951f9b7551835dbfe72aa969c5684543881f25096973f018798c2290c69e55316d4577e1d8eaae4f4e8ab
7
+ data.tar.gz: facd6c2e66dfff0bb34f7516e43b273dda7b55f03dff65ceb6a29a62c61ed3bf83c5f4e707dbb5e0a1036377bccd84fb9cc5a6787e979ae095bec8606e090f0f
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .yardoc
5
+ doc/*
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,10 @@
1
+ script: "bundle exec rake spec"
2
+ language: ruby
3
+ sudo: false
4
+ rvm:
5
+ - 2.2
6
+ - 2.3
7
+ - ruby-head
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in monit.gemspec
4
+ gemspec
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,43 @@
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
+ # => start/stop/restart/monitor/unmonitor
24
+ status.services.each do |service|
25
+ service.stop!
26
+ end
27
+
28
+ For more options see the API documentation
29
+
30
+ ## Compatibility and Build Status
31
+
32
+ [![Build Status](https://travis-ci.org/matiaskorhonen/monit.svg?branch=master)](https://travis-ci.org/matiaskorhonen/monit) [![Code Climate](https://codeclimate.com/github/matiaskorhonen/monit/badges/gpa.svg)](https://codeclimate.com/github/matiaskorhonen/monit)
33
+
34
+ The gem is only compatible with Ruby 1.8.7 and above, including JRuby 1.6+.
35
+ For the build status, check [Travis CI][travis].
36
+
37
+ [travis]: http://travis-ci.org/k33l0r/monit
38
+
39
+ ## License and copyright
40
+
41
+ Copyright © 2010 - 2013 Matias Korhonen & contributors
42
+
43
+ Licensed under the MIT license, see the LICENSE file for details.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ require "monit/httpd"
2
+ require "monit/platform"
3
+ require "monit/server"
4
+ require "monit/service"
5
+ require "monit/status"
6
+ require "monit/version"
@@ -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"]
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,61 @@
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
+
7
+ def initialize(hash = nil, options = {})
8
+ @host ||= options[:host] || "localhost"
9
+ @port ||= options[:port] || 2812
10
+ @ssl ||= options[:ssl] || false
11
+ @auth ||= options[:auth] || false
12
+ @username = options[:username]
13
+ @password = options[:password]
14
+
15
+ hash = rename_service_type(hash) if hash
16
+ super(hash)
17
+ end
18
+
19
+ def url(path)
20
+ url_params = { :host => @host, :port => @port, :path => "/#{path}" }
21
+ @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
22
+ end
23
+
24
+ [:start, :stop, :restart, :monitor, :unmonitor].each do |action|
25
+ define_method "#{action}!" do
26
+ self.do action
27
+ end
28
+ end
29
+
30
+ def do(action)
31
+ uri = self.url self.name
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+
34
+ if @ssl
35
+ http.use_ssl = true
36
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
+ end
38
+
39
+ request = Net::HTTP::Post.new(uri.request_uri)
40
+ request.body = "action=#{action}"
41
+
42
+ request.basic_auth(@username, @password) if @auth
43
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
44
+
45
+ begin
46
+ response = http.request(request)
47
+ !!(response.code =~ /\A2\d\d\z/)
48
+ rescue Errno::ECONNREFUSED => e
49
+ false
50
+ end
51
+ end
52
+
53
+ private
54
+ # Renames the Service type from "type" to "service_type" to avoid conflicts
55
+ def rename_service_type(hash)
56
+ hash["service_type"] = hash["type"]
57
+ hash.delete("type")
58
+ hash
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,104 @@
1
+ #require "crack/xml"
2
+ require "active_support/json"
3
+ require "active_support/xml_mini"
4
+ require "active_support/core_ext/hash/conversions"
5
+ require "net/http"
6
+ require "net/https"
7
+ require "uri"
8
+
9
+ # A Ruby interface for Monit
10
+ module Monit
11
+ # The +Status+ class is used to get data from the Monit instance. You should not
12
+ # need to manually instantiate any of the other classes.
13
+ class Status
14
+ ::ActiveSupport::XmlMini.backend = "Nokogiri"
15
+
16
+ attr_reader :url, :hash, :xml, :server, :platform, :services
17
+ attr_accessor :username, :auth, :host, :port, :ssl, :auth, :username
18
+ attr_writer :password
19
+
20
+ # Create a new instance of the status class with the given options
21
+ #
22
+ # <b>Options:</b>
23
+ # * +host+ - the host for monit, defaults to +localhost+
24
+ # * +port+ - the Monit port, defaults to +2812+
25
+ # * +ssl+ - should we use SSL for the connection to Monit (default: false)
26
+ # * +auth+ - should authentication be used, defaults to false
27
+ # * +username+ - username for authentication
28
+ # * +password+ - password for authentication
29
+ def initialize(options = {})
30
+ @host = options[:host] || "localhost"
31
+ @port = (options[:port] || 2812).to_i
32
+ @ssl = options[:ssl] || false
33
+ @auth = options[:auth] || false
34
+ @username = options[:username]
35
+ @password = options[:password]
36
+ @services = []
37
+ end
38
+
39
+ # Construct the URL
40
+ def url
41
+ url_params = { :host => @host, :port => @port, :path => "/_status", :query => "format=xml" }
42
+ @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
43
+ end
44
+
45
+ # Get the status from Monit.
46
+ def get
47
+ uri = self.url
48
+ http = Net::HTTP.new(uri.host, uri.port)
49
+
50
+ if @ssl
51
+ http.use_ssl = true
52
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
53
+ end
54
+
55
+ request = Net::HTTP::Get.new(uri.request_uri)
56
+
57
+ if @auth
58
+ request.basic_auth(@username, @password)
59
+ end
60
+
61
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
62
+
63
+ begin
64
+ response = http.request(request)
65
+ rescue Errno::ECONNREFUSED
66
+ return false
67
+ end
68
+
69
+ if (response.code =~ /\A2\d\d\z/)
70
+ @xml = response.body
71
+ return self.parse(@xml)
72
+ else
73
+ return false
74
+ end
75
+ end
76
+
77
+ # Parse the XML from Monit into a hash and into a Ruby representation.
78
+ def parse(xml)
79
+ @hash = Hash.from_xml(xml)
80
+ @server = Server.new(@hash["monit"]["server"])
81
+ @platform = Platform.new(@hash["monit"]["platform"])
82
+
83
+ options = {
84
+ :host => @host,
85
+ :port => @port,
86
+ :ssl => @ssl,
87
+ :auth => @auth,
88
+ :username => @username,
89
+ :password => @password
90
+ }
91
+
92
+ if @hash["monit"]["service"].is_a? Array
93
+ @services = @hash["monit"]["service"].map do |service|
94
+ Service.new(service, options)
95
+ end
96
+ else
97
+ @services = [Service.new(@hash["monit"]["service"], options)]
98
+ end
99
+ true
100
+ rescue
101
+ false
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module Monit
2
+ VERSION = "0.3.2"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+ require "monit/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Julien Chabanon"]
7
+ gem.email = ["julien@chabanon.me"]
8
+ gem.homepage = "https://github.com/modulis/monit"
9
+ gem.summary = "Connect to Monit"
10
+ gem.description = "Retrieve server information from Monit."
11
+ gem.license = 'MIT'
12
+
13
+ gem.name = "monit-client"
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Monit::VERSION
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "bundler"
22
+ gem.add_development_dependency "rspec", "~> 2.9.0"
23
+ gem.add_development_dependency "webmock", "~> 1.11.0"
24
+
25
+ #gem.add_runtime_dependency "nokogiri", "~> 1.5"
26
+ #gem.add_runtime_dependency "activesupport", ">= 3.2.22.5"
27
+ end
@@ -0,0 +1,224 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ SMALLISH_STATUS_PATH = File.expand_path("../samples/smallish_status.xml", __FILE__)
5
+ LARGISH_STATUS_PATH = File.expand_path("../samples/largish_status.xml", __FILE__)
6
+
7
+ describe Monit do
8
+ describe "Status" do
9
+ it "should be possible to instatiate it with no options" do
10
+ @status = Monit::Status.new
11
+ @status.should be_kind_of Monit::Status
12
+ @status.host.should == "localhost"
13
+ @status.port.should == 2812
14
+ @status.ssl.should be_false
15
+ @status.auth.should be_false
16
+ @status.username.should be_nil
17
+ lambda { @status.password }.should raise_error(NoMethodError)
18
+ @status.services.should == []
19
+ end
20
+
21
+ it "should generate the correct URL" do
22
+ @status = Monit::Status.new
23
+ @status.url.should be_kind_of URI
24
+ @status.url.to_s.should == "http://localhost:2812/_status?format=xml"
25
+ @status.ssl = true
26
+ @status.url.to_s.should == "https://localhost:2812/_status?format=xml"
27
+ @status.host = "rails.fi"
28
+ @status.url.to_s.should == "https://rails.fi:2812/_status?format=xml"
29
+ @status.port = 8080
30
+ @status.url.to_s.should == "https://rails.fi:8080/_status?format=xml"
31
+ end
32
+
33
+ it "should parse XML into a Hash" do
34
+ status = Monit::Status.new
35
+ status.stub!(:xml).and_return(File.read(SMALLISH_STATUS_PATH))
36
+ status.parse(status.xml)
37
+ status.hash.should be_kind_of Hash
38
+ status.hash["monit"].should_not be_nil
39
+ status.hash["monit"]["server"].should_not be_nil
40
+ status.hash["monit"]["platform"].should_not be_nil
41
+ status.hash["monit"]["service"].should_not be_nil
42
+
43
+ status.stub!(:xml).and_return(File.read(LARGISH_STATUS_PATH))
44
+ status.parse(status.xml)
45
+ status.hash.should be_kind_of Hash
46
+ status.hash["monit"].should_not be_nil
47
+ status.hash["monit"]["server"].should_not be_nil
48
+ status.hash["monit"]["platform"].should_not be_nil
49
+ status.hash["monit"]["service"].should_not be_nil
50
+ end
51
+
52
+ it "should parse XML into a Ruby representation" do
53
+ status = Monit::Status.new
54
+ status.stub!(:xml).and_return(File.read(SMALLISH_STATUS_PATH))
55
+ status.parse(status.xml)
56
+
57
+ status.server.should be_kind_of Monit::Server
58
+ status.server.httpd.should be_kind_of Monit::HTTPD
59
+ status.platform.should be_kind_of Monit::Platform
60
+ status.services.should be_kind_of Array
61
+ status.services.first.should be_kind_of Monit::Service
62
+ status.services.first.should be_kind_of OpenStruct
63
+
64
+ status.stub!(:xml).and_return(File.read(LARGISH_STATUS_PATH))
65
+ status.parse(status.xml)
66
+
67
+ status.server.should be_kind_of Monit::Server
68
+ status.server.httpd.should be_kind_of Monit::HTTPD
69
+ status.platform.should be_kind_of Monit::Platform
70
+ status.services.should be_kind_of Array
71
+ status.services.first.should be_kind_of Monit::Service
72
+ status.services.first.should be_kind_of OpenStruct
73
+ end
74
+ end
75
+
76
+ describe "Server" do
77
+ it "should create a new instance of Monit::Server from a hash" do
78
+ hash = { "id" => "52255a0b8999c46c98de9697a8daef67",
79
+ "incarnation" => "1283946152",
80
+ "version" => "5.1.1",
81
+ "uptime" => "4",
82
+ "poll" => "30",
83
+ "startdelay" => "0",
84
+ "localhostname" => "Example.local",
85
+ "controlfile" => "/etc/monitrc",
86
+ "httpd" => { "address" => nil,
87
+ "port" => "2812",
88
+ "ssl" => "0" } }
89
+ server = Monit::Server.new(hash)
90
+ server.id.should == "52255a0b8999c46c98de9697a8daef67"
91
+ server.incarnation.should == "1283946152"
92
+ server.version.should == "5.1.1"
93
+ server.uptime.should == 4
94
+ server.poll.should == 30
95
+ server.startdelay.should == 0
96
+ server.localhostname.should == "Example.local"
97
+ server.controlfile.should == "/etc/monitrc"
98
+ server.httpd.should be_kind_of Monit::HTTPD
99
+ end
100
+ end
101
+
102
+ describe "HTTPD" do
103
+ it "should create a new instance of Monit::HTTPD from a hash" do
104
+ hash = { "address" => nil,
105
+ "port" => 2812,
106
+ "ssl" => "0" }
107
+ httpd = Monit::HTTPD.new(hash)
108
+ httpd.address.should be_nil
109
+ httpd.port.should == 2812
110
+ httpd.ssl.should be_false
111
+ end
112
+ end
113
+
114
+ describe "Platform" do
115
+ it "should create a new instance of Monit::Platform from a hash" do
116
+ hash = { "name" => "Darwin",
117
+ "release" => "10.4.0",
118
+ "version" => "Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386",
119
+ "machine" => "i386",
120
+ "cpu" => "2",
121
+ "memory" => "4194304" }
122
+ platform = Monit::Platform.new(hash)
123
+ platform.name.should == "Darwin"
124
+ platform.release.should == "10.4.0"
125
+ 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"
126
+ platform.machine.should == "i386"
127
+ platform.cpu.should == 2
128
+ platform.memory.should == 4194304
129
+ end
130
+ end
131
+
132
+ describe "Service" do
133
+ let(:service) do
134
+ hash = { "collected_sec" => "1283946152",
135
+ "collected_usec" => "309973",
136
+ "name" => "Example.local",
137
+ "status" => "0",
138
+ "status_hint" => "0",
139
+ "monitor" => "1",
140
+ "monitormode" => "0",
141
+ "pendingaction" => "0",
142
+ "groups" => { "name" => "server" },
143
+ "system" => { "load" => { "avg01" => "0.28",
144
+ "avg05" => "0.43",
145
+ "avg15" => "0.48" },
146
+ "cpu" => { "user" => "10.0",
147
+ "system" => "4.1" },
148
+ "memory" => { "percent" => "44.1",
149
+ "kilobyte" => "1850152" }
150
+ },
151
+ "type" => "5" }
152
+ Monit::Service.new(hash)
153
+ end
154
+
155
+ it "should create a new instance of Monit::Platform from a hash" do
156
+ service.should be_kind_of OpenStruct
157
+ service.should be_kind_of Monit::Service
158
+ service.collected_sec.should == "1283946152"
159
+ service.collected_usec.should == "309973"
160
+ service.name.should == "Example.local"
161
+ service.status.should == "0"
162
+ service.status_hint.should == "0"
163
+ service.monitor.should == "1"
164
+ service.monitormode.should == "0"
165
+ service.pendingaction.should == "0"
166
+ service.groups.should be_kind_of Hash
167
+ service.system.should be_kind_of Hash
168
+ service.service_type.should == "5"
169
+ end
170
+
171
+ describe "#start!" do
172
+ it "sends :start to #do" do
173
+ service.stub(:do).with(:start)
174
+ service.start!
175
+ end
176
+ end
177
+
178
+ describe "#stop!" do
179
+ it "sends :stop to #do" do
180
+ service.stub(:do).with(:stop)
181
+ service.stop!
182
+ end
183
+ end
184
+
185
+ describe "#restart!" do
186
+ it "sends :restart to #do" do
187
+ service.stub(:do).with(:restart)
188
+ service.restart!
189
+ end
190
+ end
191
+
192
+ describe "#monitor!" do
193
+ it "sends :monitor to #do" do
194
+ service.stub(:do).with(:monitor)
195
+ service.monitor!
196
+ end
197
+ end
198
+
199
+ describe "#unmonitor!" do
200
+ it "sends :unmonitor to #do" do
201
+ service.stub(:do).with(:unmonitor)
202
+ service.unmonitor!
203
+ end
204
+ end
205
+
206
+ describe "#do" do
207
+ it "returns true if the response code is 2xx" do
208
+ stub_request(:any, /localhost/).to_return(:status => 200)
209
+ service.do(:start).should == true
210
+ stub_request(:any, /localhost/).to_return(:status => 201)
211
+ service.do(:start).should == true
212
+ end
213
+
214
+ it "returns false if the response code is not 2xx" do
215
+ stub_request(:any, /localhost/).to_return(:status => 500)
216
+ service.do(:start).should == false
217
+ stub_request(:any, /localhost/).to_return(:status => 400)
218
+ service.do(:start).should == false
219
+ stub_request(:any, /localhost/).to_return(:status => 302)
220
+ service.do(:start).should == false
221
+ end
222
+ end
223
+ end
224
+ end