monit 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg/*
3
3
  .bundle
4
4
  .yardoc
5
5
  doc/*
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -1,11 +1,10 @@
1
- before_install:
2
- - gem install bundler
3
1
  script: "bundle exec rake spec"
4
2
  language: ruby
5
3
  rvm:
6
4
  - 1.8.7
7
5
  - 1.9.2
8
6
  - 1.9.3
7
+ - 2.0.0
9
8
  - jruby-18mode
10
9
  - jruby-19mode
11
10
  - rbx-18mode
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :gemcutter
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in monit.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -11,20 +11,25 @@ Just like any other gem:
11
11
 
12
12
  ## Usage
13
13
 
14
- status = Monit::Status.new({ :host => "monit.myserver.com",
15
- :auth => true,
16
- :username => "foo",
14
+ status = Monit::Status.new({ :host => "monit.myserver.com",
15
+ :auth => true,
16
+ :username => "foo",
17
17
  :password => "bar" })
18
18
  status.get # => true
19
19
  status.platform # => <Monit::Platform:0x00000003673dd0 ... >
20
20
  status.platform.cpu # => 2
21
21
  status.platform.memory # => 4057712
22
22
 
23
+ # => start/stop/restart/monitor/unmonitor
24
+ status.services.each do |service|
25
+ service.stop!
26
+ end
27
+
23
28
  For more options see the API documentation
24
29
 
25
30
  ## Compatibility and Build Status
26
31
 
27
- [![Build Status](https://secure.travis-ci.org/k33l0r/monit.png)](http://travis-ci.org/k33l0r/monit)
32
+ [![Build Status](https://travis-ci.org/k33l0r/monit.png)](https://travis-ci.org/k33l0r/monit) [![Code Climate](https://codeclimate.com/github/k33l0r/monit.png)](https://codeclimate.com/github/k33l0r/monit)
28
33
 
29
34
  The gem is only compatible with Ruby 1.8.7 and above, including JRuby 1.6+.
30
35
  For the build status, check [Travis CI][travis].
@@ -33,6 +38,6 @@ For the build status, check [Travis CI][travis].
33
38
 
34
39
  ## License and copyright
35
40
 
36
- Copyright (c) 2010 - 2012 Matias Korhonen & contributors
41
+ Copyright © 2010 - 2013 Matias Korhonen & contributors
37
42
 
38
43
  Licensed under the MIT license, see the LICENSE file for details.
@@ -4,13 +4,70 @@ module Monit
4
4
  class Service < OpenStruct
5
5
  TYPES = { 0 => "Filesystem", 1 => "Directory", 2 => "File", 3 => "Daemon", 4 => "Connection", 5 => "System" }
6
6
 
7
- def initialize(hash = nil)
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
+
8
15
  hash = rename_service_type(hash) if hash
9
16
  super(hash)
10
17
  end
11
18
 
12
- private
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
+ def start!
25
+ self.do :start
26
+ end
27
+
28
+ def stop!
29
+ self.do :stop
30
+ end
31
+
32
+ def restart!
33
+ self.do :restart
34
+ end
35
+
36
+ def monitor!
37
+ self.do :monitor
38
+ end
13
39
 
40
+ def unmonitor!
41
+ self.do :unmonitor
42
+ end
43
+
44
+ def do(action)
45
+ uri = self.url self.name
46
+ http = Net::HTTP.new(uri.host, uri.port)
47
+
48
+ if @ssl
49
+ http.use_ssl = true
50
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
51
+ end
52
+
53
+ request = Net::HTTP::Post.new(uri.request_uri)
54
+ request.body = "action=#{action}"
55
+
56
+ if @auth
57
+ request.basic_auth(@username, @password)
58
+ end
59
+
60
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
61
+
62
+ begin
63
+ response = http.request(request)
64
+ !!(response.code =~ /\A2\d\d\z/)
65
+ rescue Errno::ECONNREFUSED => e
66
+ false
67
+ end
68
+ end
69
+
70
+ private
14
71
  # Renames the Service type from "type" to "service_type" to avoid conflicts
15
72
  def rename_service_type(hash)
16
73
  hash["service_type"] = hash["type"]
@@ -1,4 +1,5 @@
1
1
  #require "crack/xml"
2
+ require "active_support/json"
2
3
  require "active_support/xml_mini"
3
4
  require "active_support/core_ext/hash/conversions"
4
5
  require "net/http"
@@ -61,7 +62,7 @@ module Monit
61
62
 
62
63
  response = http.request(request)
63
64
 
64
- if response.code == "200"
65
+ if (response.code =~ /\A2\d\d\z/)
65
66
  @xml = response.body
66
67
  return self.parse(@xml)
67
68
  else
@@ -74,12 +75,21 @@ module Monit
74
75
  @hash = Hash.from_xml(xml)
75
76
  @server = Server.new(@hash["monit"]["server"])
76
77
  @platform = Platform.new(@hash["monit"]["platform"])
78
+
79
+ options = {
80
+ :host => @host,
81
+ :port => @port,
82
+ :ssl => @ssl,
83
+ :auth => @auth,
84
+ :username => @username,
85
+ :password => @password }
86
+
77
87
  if @hash["monit"]["service"].is_a? Array
78
88
  @services = @hash["monit"]["service"].map do |service|
79
- Service.new(service)
89
+ Service.new(service, options)
80
90
  end
81
91
  else
82
- @services = [Service.new(@hash["monit"]["service"])]
92
+ @services = [Service.new(@hash["monit"]["service"], options)]
83
93
  end
84
94
  true
85
95
  rescue
@@ -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,79 @@
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
+ def start
25
+ return self.do :start
26
+ end
27
+
28
+ def stop
29
+ return self.do :stop
30
+ end
31
+
32
+ def restart
33
+ return self.do :restart
34
+ end
35
+
36
+ def monitor
37
+ return self.do :monitor
38
+ end
39
+
40
+ def unmonitor
41
+ return self.do :unmonitor
42
+ end
43
+
44
+ def do(action)
45
+ uri = self.url self.name
46
+ http = Net::HTTP.new(uri.host, uri.port)
47
+
48
+ if @ssl
49
+ http.use_ssl = true
50
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
51
+ end
52
+
53
+ request = Net::HTTP::Post.new(uri.request_uri)
54
+ request.body = "action=#{action}"
55
+
56
+ if @auth
57
+ request.basic_auth(@username, @password)
58
+ end
59
+
60
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
61
+
62
+ response = http.request(request)
63
+
64
+ if response.code == "200"
65
+ return true
66
+ else
67
+ return false
68
+ end
69
+ end
70
+
71
+ private
72
+ # Renames the Service type from "type" to "service_type" to avoid conflicts
73
+ def rename_service_type(hash)
74
+ hash["service_type"] = hash["type"]
75
+ hash.delete("type")
76
+ hash
77
+ end
78
+ end
79
+ end
@@ -1,3 +1,3 @@
1
1
  module Monit
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,7 +4,7 @@ require "monit/version"
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.authors = ["Matias Korhonen"]
7
- gem.email = ["matias@kiskolabgem.com"]
7
+ gem.email = ["me@matiaskorhonen.fi"]
8
8
  gem.homepage = "http://github.com/k33l0r/monit"
9
9
  gem.summary = "Connect to Monit"
10
10
  gem.description = "Retrieve server information from Monit."
@@ -19,7 +19,8 @@ Gem::Specification.new do |gem|
19
19
  gem.add_development_dependency "rake"
20
20
  gem.add_development_dependency "bundler"
21
21
  gem.add_development_dependency "rspec", "~> 2.9.0"
22
+ gem.add_development_dependency "webmock", "~> 1.11.0"
22
23
 
23
24
  gem.add_runtime_dependency "nokogiri", "~> 1.5.2"
24
- gem.add_runtime_dependency "activesupport", "~> 3.2.2"
25
+ gem.add_runtime_dependency "activesupport", "~> 3.2.12"
25
26
  end
@@ -5,7 +5,7 @@ SMALLISH_STATUS_PATH = File.expand_path("../samples/smallish_status.xml", __FIL
5
5
  LARGISH_STATUS_PATH = File.expand_path("../samples/largish_status.xml", __FILE__)
6
6
 
7
7
  describe Monit do
8
- context "Status" do
8
+ describe "Status" do
9
9
  it "should be possible to instatiate it with no options" do
10
10
  @status = Monit::Status.new
11
11
  @status.should be_kind_of Monit::Status
@@ -73,7 +73,7 @@ describe Monit do
73
73
  end
74
74
  end
75
75
 
76
- context "Server" do
76
+ describe "Server" do
77
77
  it "should create a new instance of Monit::Server from a hash" do
78
78
  hash = { "id" => "52255a0b8999c46c98de9697a8daef67",
79
79
  "incarnation" => "1283946152",
@@ -99,7 +99,7 @@ describe Monit do
99
99
  end
100
100
  end
101
101
 
102
- context "HTTPD" do
102
+ describe "HTTPD" do
103
103
  it "should create a new instance of Monit::HTTPD from a hash" do
104
104
  hash = { "address" => nil,
105
105
  "port" => "2812",
@@ -111,7 +111,7 @@ describe Monit do
111
111
  end
112
112
  end
113
113
 
114
- context "Platform" do
114
+ describe "Platform" do
115
115
  it "should create a new instance of Monit::Platform from a hash" do
116
116
  hash = { "name" => "Darwin",
117
117
  "release" => "10.4.0",
@@ -129,8 +129,8 @@ describe Monit do
129
129
  end
130
130
  end
131
131
 
132
- context "Service" do
133
- it "should create a new instance of Monit::Platform from a hash" do
132
+ describe "Service" do
133
+ let(:service) do
134
134
  hash = { "collected_sec" => "1283946152",
135
135
  "collected_usec" => "309973",
136
136
  "name" => "Example.local",
@@ -149,7 +149,10 @@ describe Monit do
149
149
  "kilobyte" => "1850152" }
150
150
  },
151
151
  "type" => "5" }
152
- service = Monit::Service.new(hash)
152
+ Monit::Service.new(hash)
153
+ end
154
+
155
+ it "should create a new instance of Monit::Platform from a hash" do
153
156
  service.should be_kind_of OpenStruct
154
157
  service.should be_kind_of Monit::Service
155
158
  service.collected_sec.should == "1283946152"
@@ -164,5 +167,58 @@ describe Monit do
164
167
  service.system.should be_kind_of Hash
165
168
  service.service_type.should == "5"
166
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
167
223
  end
168
224
  end
@@ -3,4 +3,5 @@ require "bundler"
3
3
  Bundler.setup
4
4
 
5
5
  require "rspec"
6
+ require "webmock/rspec"
6
7
  require "monit"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-18 00:00:00.000000000 Z
12
+ date: 2013-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70169996912740 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70169996912740
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: bundler
27
- requirement: &70169996912320 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70169996912320
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec
38
- requirement: &70169996911820 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,31 @@ dependencies:
43
53
  version: 2.9.0
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70169996911820
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.9.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.11.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.11.0
47
78
  - !ruby/object:Gem::Dependency
48
79
  name: nokogiri
49
- requirement: &70169996911260 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
50
81
  none: false
51
82
  requirements:
52
83
  - - ~>
@@ -54,29 +85,39 @@ dependencies:
54
85
  version: 1.5.2
55
86
  type: :runtime
56
87
  prerelease: false
57
- version_requirements: *70169996911260
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.5.2
58
94
  - !ruby/object:Gem::Dependency
59
95
  name: activesupport
60
- requirement: &70169996910660 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
61
97
  none: false
62
98
  requirements:
63
99
  - - ~>
64
100
  - !ruby/object:Gem::Version
65
- version: 3.2.2
101
+ version: 3.2.12
66
102
  type: :runtime
67
103
  prerelease: false
68
- version_requirements: *70169996910660
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 3.2.12
69
110
  description: Retrieve server information from Monit.
70
111
  email:
71
- - matias@kiskolabgem.com
112
+ - me@matiaskorhonen.fi
72
113
  executables: []
73
114
  extensions: []
74
115
  extra_rdoc_files: []
75
116
  files:
76
117
  - .gitignore
118
+ - .rspec
77
119
  - .travis.yml
78
120
  - Gemfile
79
- - Gemfile.lock
80
121
  - LICENSE
81
122
  - README.md
82
123
  - Rakefile
@@ -86,6 +127,10 @@ files:
86
127
  - lib/monit/server.rb
87
128
  - lib/monit/service.rb
88
129
  - lib/monit/status.rb
130
+ - lib/monit/status/httpd.rb
131
+ - lib/monit/status/platform.rb
132
+ - lib/monit/status/server.rb
133
+ - lib/monit/status/service.rb
89
134
  - lib/monit/version.rb
90
135
  - monit.gemspec
91
136
  - spec/monit_spec.rb
@@ -106,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
151
  version: '0'
107
152
  segments:
108
153
  - 0
109
- hash: 3171063330755817441
154
+ hash: -2454781035396715959
110
155
  required_rubygems_version: !ruby/object:Gem::Requirement
111
156
  none: false
112
157
  requirements:
@@ -115,10 +160,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
160
  version: '0'
116
161
  segments:
117
162
  - 0
118
- hash: 3171063330755817441
163
+ hash: -2454781035396715959
119
164
  requirements: []
120
165
  rubyforge_project:
121
- rubygems_version: 1.8.15
166
+ rubygems_version: 1.8.25
122
167
  signing_key:
123
168
  specification_version: 3
124
169
  summary: Connect to Monit
@@ -1,36 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- monit (0.2.2)
5
- activesupport (~> 3.2.2)
6
- nokogiri (~> 1.5.2)
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- activesupport (3.2.3)
12
- i18n (~> 0.6)
13
- multi_json (~> 1.0)
14
- diff-lcs (1.1.3)
15
- i18n (0.6.0)
16
- multi_json (1.3.4)
17
- nokogiri (1.5.2)
18
- rake (0.8.7)
19
- rspec (2.9.0)
20
- rspec-core (~> 2.9.0)
21
- rspec-expectations (~> 2.9.0)
22
- rspec-mocks (~> 2.9.0)
23
- rspec-core (2.9.0)
24
- rspec-expectations (2.9.0)
25
- diff-lcs (~> 1.1.3)
26
- rspec-mocks (2.9.0)
27
-
28
- PLATFORMS
29
- java
30
- ruby
31
-
32
- DEPENDENCIES
33
- bundler
34
- monit!
35
- rake
36
- rspec (~> 2.9.0)