kumolus-monit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 698427728e7e60f98055b187a0878662ac5c3c72
4
+ data.tar.gz: d7059ee386f7df3a2cbf18d21775bb54dae09333
5
+ SHA512:
6
+ metadata.gz: 6f5c845da2e1a071b595cab88b9ef86632859d706c8173326174da68fef1032673039c28186209294daeb93c7c0f226208047867573620c95496d2686e9e284a
7
+ data.tar.gz: 8701f4ceed93f073b7f92a8327e2224e9543127b75f3123a1795d80876db224b7f6a861a4f12a692967cffbc6547f36b5a3ecd3c3c878515542d9cf1b508b356
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kumoas@kumolus.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in kumolus-monit.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 kumoas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ Kumolus Monit Rubygem
2
+ =============
3
+
4
+ A Ruby interface for Monit.
5
+
6
+ ## Installation
7
+
8
+ Just like any other gem:
9
+
10
+ gem install kumolus-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
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kumolus/monit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "kumolus/monit/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kumolus-monit"
8
+ spec.version = Kumolus::Monit::VERSION
9
+ spec.authors = ["Kumolus"]
10
+ spec.email = ["kumoas@kumolus.com"]
11
+
12
+ spec.summary = "Connect to Kumolus Monit"
13
+ spec.description = "Retrieve server information from Monit."
14
+ spec.homepage = "http://github.com/kumolus/kumolus-monit"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_runtime_dependency "nokogiri", "~> 1.5"
38
+ spec.add_runtime_dependency "activesupport", ">= 5.1.4"
39
+ end
@@ -0,0 +1,14 @@
1
+ module Kumolus
2
+ module Monit
3
+ # The HTTPD section from the Monit XML
4
+ class HTTPD
5
+ attr_reader :address, :port, :ssl
6
+
7
+ def initialize(options = {})
8
+ @address = options["address"]
9
+ @port = options["port"]
10
+ @ssl = options["ssl"] == "1" ? true : false
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module Kumolus
2
+ module Monit
3
+ # The platform section from the Monit XML
4
+ class Platform
5
+ attr_reader :name, :release, :version, :machine, :cpu, :memory
6
+
7
+ def initialize(options = {})
8
+ @name = options["name"]
9
+ @release = options["release"]
10
+ @version = options["version"]
11
+ @machine = options["machine"]
12
+ @cpu = options["cpu"].to_i
13
+ @memory = options["memory"].to_i
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Kumolus
2
+ module Monit
3
+ # The server section from the Monit XML
4
+ class Server
5
+ attr_reader :id, :incarnation, :version, :uptime, :poll, :startdelay, :localhostname, :controlfile, :httpd
6
+
7
+ def initialize(options = {})
8
+ @id = options["id"]
9
+ @incarnation = options["incarnation"]
10
+ @version = options["version"]
11
+ @uptime = options["uptime"].to_i
12
+ @poll = options["poll"].to_i
13
+ @startdelay = options["startdelay"].to_i
14
+ @localhostname = options["localhostname"]
15
+ @controlfile = options["controlfile"]
16
+ @httpd = options["httpd"].is_a?(HTTPD) ? options["httpd"] : Kumolus::Monit::HTTPD.new(options["httpd"])
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,63 @@
1
+ require "ostruct"
2
+ module Kumolus
3
+ module Monit
4
+ # The service section from the Monit XML. Inherits from OpenStruct.
5
+ class Service < OpenStruct
6
+ TYPES = { 0 => "Filesystem", 1 => "Directory", 2 => "File", 3 => "Daemon", 4 => "Connection", 5 => "System" }
7
+
8
+ def initialize(hash = nil, options = {})
9
+ @host ||= options[:host] || "localhost"
10
+ @port ||= options[:port] || 2812
11
+ @ssl ||= options[:ssl] || false
12
+ @auth ||= options[:auth] || false
13
+ @username = options[:username]
14
+ @password = options[:password]
15
+
16
+ hash = rename_service_type(hash) if hash
17
+ super(hash)
18
+ end
19
+
20
+ def url(path)
21
+ url_params = { :host => @host, :port => @port, :path => "/#{path}" }
22
+ @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
23
+ end
24
+
25
+ [:start, :stop, :restart, :monitor, :unmonitor].each do |action|
26
+ define_method "#{action}!" do
27
+ self.do action
28
+ end
29
+ end
30
+
31
+ def do(action)
32
+ uri = self.url self.name
33
+ http = Net::HTTP.new(uri.host, uri.port)
34
+
35
+ if @ssl
36
+ http.use_ssl = true
37
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
38
+ end
39
+
40
+ request = Net::HTTP::Post.new(uri.request_uri)
41
+ request.body = "action=#{action}"
42
+
43
+ request.basic_auth(@username, @password) if @auth
44
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
45
+
46
+ begin
47
+ response = http.request(request)
48
+ !!(response.code =~ /\A2\d\d\z/)
49
+ rescue Errno::ECONNREFUSED => e
50
+ false
51
+ end
52
+ end
53
+
54
+ private
55
+ # Renames the Service type from "type" to "service_type" to avoid conflicts
56
+ def rename_service_type(hash)
57
+ hash["service_type"] = hash["type"]
58
+ hash.delete("type")
59
+ hash
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,92 @@
1
+ require "active_support/json"
2
+ require "active_support/xml_mini"
3
+ require "active_support/core_ext/hash/conversions"
4
+ require "net/http"
5
+ require "net/https"
6
+ require "uri"
7
+
8
+ module Kumolus
9
+ module Monit
10
+
11
+ class Status
12
+ ::ActiveSupport::XmlMini.backend = "Nokogiri"
13
+
14
+ attr_reader :url, :hash, :xml, :server, :platform, :services
15
+ attr_accessor :username, :auth, :host, :port, :ssl, :auth, :username
16
+ attr_writer :password
17
+
18
+ def initialize(options = {})
19
+ @host = options[:host] || "localhost"
20
+ @port = (options[:port] || 2812).to_i
21
+ @ssl = options[:ssl] || false
22
+ @auth = options[:auth] || false
23
+ @path = options[:path]
24
+ @username = options[:username]
25
+ @password = options[:password]
26
+ @services = []
27
+ end
28
+
29
+ def url
30
+ url_params = { :host => @host, :port => @port, :path => "#{@path}/_status", :query => "format=xml" }
31
+ @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
32
+ end
33
+
34
+ def get
35
+ uri = self.url
36
+ http = Net::HTTP.new(uri.host, uri.port)
37
+
38
+ if @ssl
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+ end
42
+
43
+ request = Net::HTTP::Get.new(uri.request_uri)
44
+
45
+ if @auth
46
+ request.basic_auth(@username, @password)
47
+ end
48
+
49
+ request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"
50
+
51
+ begin
52
+ response = http.request(request)
53
+ rescue Errno::ECONNREFUSED
54
+ return false
55
+ end
56
+
57
+ if (response.code =~ /\A2\d\d\z/)
58
+ @xml = response.body
59
+ return self.parse(@xml)
60
+ else
61
+ return false
62
+ end
63
+ end
64
+
65
+ def parse(xml)
66
+ @hash = Hash.from_xml(xml)
67
+ @server = Kumolus::Monit::Server.new(@hash["monit"]["server"])
68
+ @platform = Kumolus::Monit::Platform.new(@hash["monit"]["platform"])
69
+
70
+ options = {
71
+ :host => @host,
72
+ :port => @port,
73
+ :ssl => @ssl,
74
+ :auth => @auth,
75
+ :username => @username,
76
+ :password => @password
77
+ }
78
+
79
+ if @hash["monit"]["service"].is_a? Array
80
+ @services = @hash["monit"]["service"].map do |service|
81
+ Kumolus::Monit::Service.new(service, options)
82
+ end
83
+ else
84
+ @services = [Kumolus::Monit::Service.new(@hash["monit"]["service"], options)]
85
+ end
86
+ true
87
+ rescue
88
+ false
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,5 @@
1
+ module Kumolus
2
+ module Monit
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require "kumolus/monit/httpd"
2
+ require "kumolus/monit/platform"
3
+ require "kumolus/monit/server"
4
+ require "kumolus/monit/service"
5
+ require "kumolus/monit/status"
6
+ require "kumolus/monit/version"
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kumolus-monit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kumolus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 5.1.4
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 5.1.4
83
+ description: Retrieve server information from Monit.
84
+ email:
85
+ - kumoas@kumolus.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - kumolus-monit.gemspec
101
+ - lib/kumolus/monit.rb
102
+ - lib/kumolus/monit/httpd.rb
103
+ - lib/kumolus/monit/platform.rb
104
+ - lib/kumolus/monit/server.rb
105
+ - lib/kumolus/monit/service.rb
106
+ - lib/kumolus/monit/status.rb
107
+ - lib/kumolus/monit/version.rb
108
+ homepage: http://github.com/kumolus/kumolus-monit
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ allowed_push_host: https://rubygems.org/
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.11
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Connect to Kumolus Monit
133
+ test_files: []