nagios-splunk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,19 @@
1
+ = Nagios::Splunk
2
+ Nagios splunk plugin allows to check splunk license usage
3
+
4
+ == API
5
+
6
+ client = Nagios::Splunk::RestClient.new("http://admin:changeme@localhost:8089/")
7
+ splunk = Nagios::Splunk::Check.new(client)
8
+
9
+ warn = 80 # 80%
10
+ crit = 90 # 90%
11
+
12
+ splunk.license_usage(warn, crit) # => "License OK: 50% of license capacity is used | quota: 500 MB; used: 250 MB"
13
+
14
+ == Usage:
15
+
16
+ $ check_splunk -s http://admin:changeme@localhost:8089/ -w 60 -c 80
17
+ "License OK: 50% of license capacity is used | quota: 500 MB; used: 250 MB"
18
+ $ echo $?
19
+ 0
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake/testtask'
2
+ require 'rake/gempackagetask'
3
+
4
+ task :default => :test
5
+
6
+ desc "Run tests"
7
+ Rake::TestTask.new do |t|
8
+ t.libs.push "lib"
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ gem_spec = eval(File.read("nagios-splunk.gemspec"))
14
+
15
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
16
+ pkg.gem_spec = gem_spec
17
+ end
18
+
19
+ desc "remove build files"
20
+ task :clean do
21
+ sh %Q{ rm -f pkg/*.gem }
22
+ end
data/bin/check_splunk ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'nagios/splunk'
4
+ exit Nagios::Splunk::CLI.new.run
@@ -0,0 +1,16 @@
1
+ module Nagios
2
+
3
+ STATUS = { 1 => "WARN", 2 => "CRITICAL", 0 => "OK" }
4
+
5
+ module Splunk
6
+
7
+ LOGIN_URL = "/services/auth/login"
8
+ POOL_LIST_URL = "/services/licenser/pools"
9
+ LICENSE_LIST_URL = "/services/licenser/licenses"
10
+
11
+ autoload :Check, "nagios/splunk/check"
12
+ autoload :RestClient, "nagios/splunk/rest_client"
13
+ autoload :CLI, "nagios/splunk/cli"
14
+
15
+ end
16
+ end
@@ -0,0 +1,73 @@
1
+ require 'nokogiri'
2
+
3
+ module Nagios
4
+
5
+ module Splunk
6
+
7
+ # Nagios::Splunk::Check implements different types of checks of
8
+ class Check
9
+
10
+ attr_reader :rest_client
11
+
12
+ # @param [String] server_url full server url with username and password (RFC 3986)
13
+ # example: https://user:pass@localhost:8089/
14
+ def initialize(client)
15
+ @rest_client = client
16
+ end
17
+
18
+ # license usage check
19
+ # @param [Integer] warn license usage threshhold
20
+ # @param [Integer] crit license usage threshhold
21
+ # @return [Array<Integer, String>] exit code and message
22
+ def license_usage(warn, crit)
23
+ quota = licenses.
24
+ select {|k,v| v["type"] == "enterprise" && v["status"] == "VALID" }.
25
+ reduce(0) { |r,l| r+=l[1]["quota"].to_i }
26
+ used_quota = pools.reduce(0) {|r,p| r+=p[1]["used_bytes"].to_i }
27
+ case true
28
+ when used_quota > quota * crit.to_i / 100:
29
+ code = 2
30
+ when used_quota > quota * warn.to_i / 100:
31
+ code = 1
32
+ else
33
+ code = 0
34
+ end
35
+ message = "License #{STATUS[code]}: #{used_quota * 100 / quota}% of license capacity is used"
36
+ message << " | quota: #{quota} B; used: #{used_quota} B"
37
+ return [code, message]
38
+ end
39
+
40
+ # list of avialable licenses
41
+ # @return [Array<Hash>]
42
+ def licenses
43
+ response = rest_client.get(LICENSE_LIST_URL)
44
+ response.code == "200" ? parse_data(response.body) : {}
45
+ end
46
+
47
+ # list of available pools
48
+ # @return [Array<Hash>]
49
+ def pools
50
+ response = rest_client.get(POOL_LIST_URL)
51
+ response.code == "200" ? parse_data(response.body) : {}
52
+ end
53
+
54
+ private
55
+
56
+ def parse_data(data)
57
+ doc = Nokogiri::Slop(data)
58
+ doc.remove_namespaces!
59
+ doc.search("entry").reduce(Hash.new) do |r,e|
60
+ name = e.title.content
61
+ next r if name =~ /^F+D{0,1}$/
62
+ r[name] ||= Hash.new
63
+ e.search("dict/key").each do |k|
64
+ r[name][k.attribute("name").value] = k.text
65
+ end
66
+ r
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,42 @@
1
+ require 'mixlib/cli'
2
+
3
+ module Nagios
4
+ module Splunk
5
+
6
+ class CLI
7
+ include Mixlib::CLI
8
+
9
+ option(:server_url,
10
+ :short => "-s URL",
11
+ :long => "--server URL",
12
+ :required => true,
13
+ :description => "Splunk server url",
14
+ :on => :head)
15
+
16
+ option(:warn,
17
+ :short => "-w WARN",
18
+ :long => "--warn WARN",
19
+ :default => '80',
20
+ :description => "Warn % of license capacity usage (default: 80)")
21
+
22
+ option(:crit,
23
+ :short => "-c CRIT",
24
+ :long => "--crit CRIT",
25
+ :default => '90',
26
+ :description => "Critical % of license capacity usage (default: 90)")
27
+
28
+ def run(argv = ARGV)
29
+ parse_options(argv)
30
+
31
+ client = RestClient.new(config[:server_url])
32
+ splunk = Check.new(client)
33
+
34
+ status, message = splunk.license_usage(config[:warn], config[:crit])
35
+
36
+ puts message
37
+ status
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,75 @@
1
+ require 'net/https'
2
+
3
+ module Nagios
4
+ module Splunk
5
+
6
+ # Rest client which communicates with Splunk via REST API
7
+ class RestClient
8
+
9
+ attr_reader :token
10
+
11
+ # turn on/off debugging
12
+ # @param [<True, False>]
13
+ def self.debug(value = nil)
14
+ @debug = value if value
15
+ @debug
16
+ end
17
+
18
+ # parse server url string and set up intance variables
19
+ # @param [String] server_url full server url with username and password (RFC 3986)
20
+ # example: https://user:pass@localhost:8089/
21
+ def initialize(server_url)
22
+ uri = URI.parse(server_url)
23
+ @host = uri.host
24
+ @port = uri.port
25
+ @username = uri.user
26
+ @password = uri.password
27
+ end
28
+
29
+ # send HTTP request to the server
30
+ # @param [String] url
31
+ # @return [Net::HTTPRespose]
32
+ def get(url)
33
+ login unless token
34
+ response = client.request(request(url, token))
35
+ end
36
+
37
+ private
38
+
39
+ # build GET request and include Authorization
40
+ # @param [String] url
41
+ # @param [String] token authenticate token
42
+ def request(url, token)
43
+ headers = { "Authorization" => "Splunk #{token}" }
44
+ Net::HTTP::Get.new(url, headers)
45
+ end
46
+
47
+ # build HTTP connection
48
+ # @return [Net::HTTP]
49
+ def client
50
+ @client ||= Net::HTTP.new(@host, @port)
51
+ @client.use_ssl = true
52
+ @client.set_debug_output $stderr if debug
53
+ @client
54
+ end
55
+
56
+ def debug
57
+ self.class.debug
58
+ end
59
+
60
+ # receive authentication token
61
+ def login
62
+ request = Net::HTTP::Post.new(LOGIN_URL)
63
+ request.set_form_data({"username" => @username, "password" => @password})
64
+ response = client.request(request)
65
+ if response.code == "200"
66
+ doc = Nokogiri::XML.parse(response.body)
67
+ elem = doc.search("//sessionKey").first
68
+ @token = elem.text if elem
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "nagios-splunk"
5
+ s.version = "0.0.1"
6
+ s.authors = 'Max Horbul'
7
+ s.email = ["max@gorbul.net"]
8
+ s.homepage = "https://github.com/mhorbul/nagios-splunk"
9
+ s.rubyforge_project = 'nagios-splunk'
10
+ s.summary = "Nagios Splunk plugin"
11
+ s.description = "Splunk monitoring in Nagios"
12
+
13
+ s.files = Dir['{bin/*,lib/**/*,test/*}'] +
14
+ %w{README Rakefile nagios-splunk.gemspec}
15
+
16
+ s.bindir = 'bin'
17
+ s.executables << 'check_splunk'
18
+ s.require_path = 'lib'
19
+
20
+ s.add_dependency 'nokogiri'
21
+ s.add_dependency 'mixlib-cli'
22
+
23
+ s.add_development_dependency 'minitest'
24
+ s.add_development_dependency 'rake'
25
+
26
+ s.has_rdoc = false
27
+ end
@@ -0,0 +1,12 @@
1
+ $:.push File.join(File.dirname(__FILE__), "../lib")
2
+
3
+ require 'rubygems'
4
+ require "bundler/setup"
5
+ require 'nagios/splunk'
6
+ require 'minitest/autorun'
7
+
8
+ module MiniTest
9
+ def self.fixtures_path
10
+ File.expand_path(File.join(File.dirname(__FILE__), "fixtures"))
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagios-splunk
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Max Horbul
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: mixlib-cli
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: minitest
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ description: Splunk monitoring in Nagios
77
+ email:
78
+ - max@gorbul.net
79
+ executables:
80
+ - check_splunk
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - bin/check_splunk
87
+ - lib/nagios/splunk/check.rb
88
+ - lib/nagios/splunk/cli.rb
89
+ - lib/nagios/splunk/rest_client.rb
90
+ - lib/nagios/splunk.rb
91
+ - test/test_helper.rb
92
+ - README
93
+ - Rakefile
94
+ - nagios-splunk.gemspec
95
+ homepage: https://github.com/mhorbul/nagios-splunk
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project: nagios-splunk
124
+ rubygems_version: 1.8.17
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Nagios Splunk plugin
128
+ test_files: []
129
+