nagios-api-client 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.irbrc ADDED
@@ -0,0 +1,4 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'nagios/api'
3
+ puts "Loaded Nagios::API"
4
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source = "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ end
@@ -0,0 +1,82 @@
1
+ Ruby Nagios API Client
2
+ ========
3
+
4
+ A ruby gem used to interact with the nagios-api (REST interface to Nagios) server:
5
+
6
+ https://github.com/xb95/nagios-api
7
+
8
+ Install
9
+ -------
10
+
11
+ gem install nagios-api-client
12
+
13
+ If you are using bundler, add this to your Gemfile:
14
+
15
+ gem "nagios-api-client", require: 'nagios/api'
16
+
17
+ If not using bundler:
18
+
19
+ require 'rubygems'
20
+ require 'nagios/api'
21
+
22
+ Examples
23
+ ------
24
+
25
+ # Create a new client to interact with the nagios-api server.
26
+ client = Nagios::API::Client.new("http://nagios-server:8181")
27
+
28
+ # Optionally set authentication credentials
29
+ client = Nagios::API::Client.new("http://nagios-server:8181", user: 'user', password: 'password')
30
+
31
+ # Find a host
32
+ host = client.hosts.find("hostname")
33
+
34
+ # Find a service on the host
35
+ service = host.services.find("servicename")
36
+
37
+ # Check the status
38
+ puts service.status
39
+
40
+ # Check the status details
41
+ puts service.status_details
42
+
43
+ # Schedule downtime
44
+ service.schedule_downtime(
45
+ author: "User 1",
46
+ comment: "This is flexible downtime",
47
+ start_time: Time.now,
48
+ fixed: false,
49
+ duration: 3600,
50
+ end_time: Time.now + 14400
51
+ )
52
+
53
+ Author
54
+ ------
55
+
56
+ Philippe Green <phil@greenviewdata.com>
57
+ Greenview Data, Inc.
58
+
59
+ License
60
+ -------
61
+
62
+ The MIT License (MIT)
63
+
64
+ Copyright (c) 2014 Greenview Data, Inc.
65
+
66
+ Permission is hereby granted, free of charge, to any person obtaining a copy
67
+ of this software and associated documentation files (the "Software"), to deal
68
+ in the Software without restriction, including without limitation the rights
69
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70
+ copies of the Software, and to permit persons to whom the Software is
71
+ furnished to do so, subject to the following conditions:
72
+
73
+ The above copyright notice and this permission notice shall be included in all
74
+ copies or substantial portions of the Software.
75
+
76
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
82
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require 'curb-fu'
2
+ require 'json'
3
+ require 'nagios/api'
4
+
5
+ module Nagios
6
+ end
@@ -0,0 +1,15 @@
1
+ require 'nagios/api/version'
2
+
3
+ module Nagios
4
+ module API
5
+ autoload :Interface, 'nagios/api/interface'
6
+ autoload :Client, 'nagios/api/client'
7
+ autoload :Service, 'nagios/api/service'
8
+ autoload :Services, 'nagios/api/service'
9
+ autoload :State, 'nagios/api/state'
10
+ autoload :Host, 'nagios/api/host'
11
+ autoload :Hosts, 'nagios/api/host'
12
+ autoload :Resource, 'nagios/api/resource'
13
+ autoload :Downtime, 'nagios/api/downtime'
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module Nagios::API
2
+ # Create a new client to interact with the nagios-api server.
3
+ #
4
+ # client = Nagios::API::Client.new("http://nagios-server:8181")
5
+ #
6
+ # Optionally set authentication credentials
7
+ #
8
+ # client = Nagios::API::Client.new("http://nagios-server:8181", user: 'user', password: 'password')
9
+ #
10
+ class Client
11
+ attr_reader :base_url, :options, :state
12
+
13
+ def initialize(base_url, extra_args = {})
14
+ @base_url = base_url
15
+ @options = extra_args
16
+ end
17
+
18
+ def api
19
+ @api ||= Nagios::API::Interface.new(options.merge(base_url: base_url))
20
+ end
21
+
22
+ def hosts
23
+ @hosts ||= Nagios::API::Hosts.new(api_client: self)
24
+ end
25
+
26
+ def state
27
+ @state ||= Nagios::API::State.new(api_client: self)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ module Nagios::API
2
+ class Downtime < Nagios::API::Resource
3
+ attr_reader :api_client, :service, :host
4
+
5
+ def initialize(args = {})
6
+ @service = args.delete(:service)
7
+ @host = args.delete(:host)
8
+
9
+ args['end_time'] = Time.at(args['end_time'].to_i)
10
+ args['start_time'] = Time.at(args['start_time'].to_i)
11
+ args['created_at'] = Time.at(args['entry_time'].to_i)
12
+
13
+ super(args)
14
+ end
15
+
16
+ def id
17
+ downtime_id
18
+ end
19
+
20
+ def cancel
21
+ result = api_client.api.post("/cancel_downtime/#{id}", {})
22
+
23
+ raise StandardError, "Unknown response canceling downtime: #{result}" unless result == "cancelled"
24
+
25
+ true
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ module Nagios::API
2
+ class Hosts
3
+ attr_reader :api_client
4
+
5
+ def initialize(args = {})
6
+ @api_client = args[:api_client]
7
+ @host_cache = {}
8
+ end
9
+
10
+ def find(hostname)
11
+ return @host_cache[hostname] if @host_cache.has_key?(hostname)
12
+
13
+ host = Host.new(api_client.api.query("/host/#{hostname}").merge(api_client: api_client))
14
+ @host_cache[hostname] = host
15
+ host
16
+ end
17
+
18
+ def reload
19
+ @host_cache = {}
20
+ end
21
+ end
22
+
23
+ class Host < Nagios::API::Resource
24
+ def services
25
+ @services ||= Nagios::API::Services.new(api_client: api_client, host: self)
26
+ end
27
+
28
+ def name
29
+ host_name
30
+ end
31
+
32
+ def state
33
+ api_client.state.for_host(name)
34
+ end
35
+
36
+ def schedule_downtime
37
+ # host
38
+ # author
39
+ # comment
40
+ # start_time
41
+ # fixed
42
+ # duration
43
+ # end_time
44
+ # services_too
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,78 @@
1
+ require 'json'
2
+ require 'curb-fu'
3
+
4
+ module Nagios::API
5
+ class Interface
6
+ class HTTPError < StandardError; end
7
+ class BackendAPIError < StandardError; end
8
+
9
+ attr_accessor :base_url, :username, :password, :use_cache
10
+
11
+ def initialize(args = {})
12
+ @base_url = args[:base_url]
13
+ @username = args[:username]
14
+ @password = args[:password]
15
+ @use_cache = args[:use_cache]
16
+ end
17
+
18
+ def query(path)
19
+ result = query_from_api(path)
20
+
21
+ return result unless use_cache
22
+ save_to_cache(path, result) if result
23
+ result ||= query_from_cache(path)
24
+ end
25
+
26
+ def memcached
27
+ @memcached ||= Memcached.new("localhost:11211")
28
+ end
29
+
30
+ def save_to_cache(path, data)
31
+ memcached.set path, data, 0
32
+ end
33
+
34
+ def query_from_cache(path)
35
+ memcached.get(path) rescue nil
36
+ end
37
+
38
+ def authentication_options
39
+ !username.nil? ? { username: username, password: password } : {}
40
+ end
41
+
42
+ def post(path, params)
43
+ args = { url: base_url + path, headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } }
44
+ args.merge!(authentication_options)
45
+ result = CurbFu.post(args, params.to_json)
46
+
47
+ if result.success?
48
+ data = JSON.parse(result.body)
49
+
50
+ if data['success']
51
+ return data['content']
52
+ else
53
+ raise BackendAPIError, data['content']
54
+ end
55
+ else
56
+ raise HTTPError, "Error communicating with API server: #{result.status}"
57
+ end
58
+ end
59
+
60
+ def query_from_api(path)
61
+ args = { url: base_url + path, headers: { Accept: 'application/json' } }
62
+ args.merge!(authentication_options)
63
+ result = CurbFu.get(args)
64
+
65
+ if result.success?
66
+ data = JSON.parse(result.body)
67
+
68
+ if data['success']
69
+ return data['content']
70
+ else
71
+ raise BackendAPIError
72
+ end
73
+ else
74
+ raise HTTPError, "Error communicating with API server: #{result.status}"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,38 @@
1
+ module Nagios::API
2
+ class Resource
3
+ attr_reader :api_client
4
+ attr_accessor :attributes
5
+
6
+ def initialize(args = {})
7
+ @api_client = args.delete(:api_client)
8
+ @attributes = {}
9
+ self.attributes = args
10
+ end
11
+
12
+ def attributes=(args = {})
13
+ args ||= {}
14
+ args.each do |key, value|
15
+ self.send(:"#{key}=", value)
16
+ end
17
+ end
18
+
19
+ def method_missing(meth, *args, &block)
20
+ if meth.to_s =~ /^(.+)=$/
21
+ @attributes[$1.to_sym] = args[0]
22
+ elsif @attributes.has_key?(meth)
23
+ @attributes[meth]
24
+ else
25
+ super # You *must* call super if you don't handle the
26
+ # method, otherwise you'll mess up Ruby's method
27
+ # lookup.
28
+ end
29
+ end
30
+
31
+ def to_hash(options = {})
32
+ hash = attributes
33
+ hash.reject! { |key, value| options[:except].include?(key.to_sym) || options[:except].include?(key.to_s) } if options[:except]
34
+ hash.reject! { |key, value| !options[:only].include?(key.to_sym) && !options[:only].include?(key.to_s) } if options[:only]
35
+ hash
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,88 @@
1
+ module Nagios::API
2
+ class Services
3
+ class NoHost < StandardError; end
4
+
5
+ attr_reader :api_client
6
+ attr_accessor :host
7
+
8
+ def initialize(args = {})
9
+ @api_client = args[:api_client]
10
+ @host = args[:host]
11
+ end
12
+
13
+ def all
14
+ raise NoHost unless host
15
+
16
+ @services ||= api_client.api.query("/service/#{host.name}").values.collect do |service_attributes|
17
+ Service.new(service_attributes.merge(api_client: api_client, host: host))
18
+ end
19
+ end
20
+
21
+ def find(name)
22
+ raise NoHost unless host
23
+
24
+ all.find { |service| service.name.downcase == name.downcase }
25
+ end
26
+
27
+ def reload
28
+ @services = nil
29
+ end
30
+ end
31
+
32
+ class Service < Nagios::API::Resource
33
+ def name
34
+ service
35
+ end
36
+
37
+ def state
38
+ return nil unless host
39
+
40
+ host.state['services'][name]
41
+ end
42
+
43
+ def downtimes
44
+ return nil unless state
45
+
46
+ dts = state['downtimes'] ? state['downtimes'].values : []
47
+ dts.collect { |dt| Nagios::API::Downtime.new(dt.merge(service: self, api_client: api_client)) }
48
+ end
49
+
50
+ # schedule_downtime arguments:
51
+ # author
52
+ # comment
53
+ # start_time
54
+ # fixed
55
+ # duration
56
+ # end_time
57
+ def schedule_downtime(params = {})
58
+ params = params.dup
59
+ params[:host] = host.name
60
+ params[:service] = name
61
+ params[:start_time] = params[:start_time].to_i if params[:start_time]
62
+ params[:end_time] = params[:end_time].to_i if params[:end_time]
63
+
64
+ result = api_client.api.post("/schedule_downtime", params)
65
+
66
+ raise StandardError, "Unknown response scheduling downtime: #{result}" unless result == "scheduled"
67
+
68
+ true
69
+ end
70
+
71
+ def status
72
+ case current_state.to_i
73
+ when 0
74
+ :ok
75
+ when 1
76
+ :warning
77
+ when 2
78
+ :critical
79
+ else
80
+ :unknown
81
+ end
82
+ end
83
+
84
+ def status_details
85
+ plugin_output
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,17 @@
1
+ module Nagios::API
2
+ class State < Nagios::API::Resource
3
+ def initialize(args = {})
4
+ super
5
+
6
+ reload
7
+ end
8
+
9
+ def reload
10
+ self.attributes = { :data => api_client.api.query("/state") }
11
+ end
12
+
13
+ def for_host(hostname)
14
+ data[hostname]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Nagios
2
+ module API
3
+ VERSION = "0.4"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nagios/api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nagios-api-client"
7
+ s.version = Nagios::API::VERSION
8
+ s.author = "Philippe Green, Greenview Data, Inc."
9
+ s.email = ["development@greenviewdata.com"]
10
+ s.homepage = "https://github.com/gdi/ruby-nagios-api-client"
11
+ s.license = "MIT"
12
+ s.summary = %q{Interface to interact with the nagios-api server}
13
+ s.description = %q{Interact with the nagios-api (REST interface to Nagios) server found at github.com/xb95/nagios-api}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- spec/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.required_ruby_version = '>= 1.9.2'
21
+
22
+ s.add_runtime_dependency "curb-fu"
23
+
24
+ s.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagios-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philippe Green, Greenview Data, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb-fu
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Interact with the nagios-api (REST interface to Nagios) server found
47
+ at github.com/xb95/nagios-api
48
+ email:
49
+ - development@greenviewdata.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .irbrc
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - lib/nagios.rb
60
+ - lib/nagios/api.rb
61
+ - lib/nagios/api/client.rb
62
+ - lib/nagios/api/downtime.rb
63
+ - lib/nagios/api/host.rb
64
+ - lib/nagios/api/interface.rb
65
+ - lib/nagios/api/resource.rb
66
+ - lib/nagios/api/service.rb
67
+ - lib/nagios/api/state.rb
68
+ - lib/nagios/api/version.rb
69
+ - nagios-api-client.gemspec
70
+ homepage: https://github.com/gdi/ruby-nagios-api-client
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.9.2
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.25
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Interface to interact with the nagios-api server
95
+ test_files: []
96
+ has_rdoc: