nagios-splunk 0.0.1 → 0.0.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.
data/lib/nagios/splunk.rb CHANGED
@@ -11,6 +11,9 @@ module Nagios
11
11
  autoload :Check, "nagios/splunk/check"
12
12
  autoload :RestClient, "nagios/splunk/rest_client"
13
13
  autoload :CLI, "nagios/splunk/cli"
14
+ autoload :Exception, "nagios/splunk/exception"
15
+ autoload :NoPoolsFound, "nagios/splunk/exception"
16
+ autoload :NoLicensesFound, "nagios/splunk/exception"
14
17
 
15
18
  end
16
19
  end
@@ -15,43 +15,117 @@ module Nagios
15
15
  @rest_client = client
16
16
  end
17
17
 
18
+ # pool usage check
19
+ # @param [String] name of the pool
20
+ # @param [Integer] warn license usage threshhold
21
+ # @param [Integer] crit license usage threshhold
22
+ # @param [String] stack_id which pool is assigned to
23
+ # @return [Array<Integer, String>] exit code and message
24
+ def pool_usage(name, warn, crit)
25
+ quota = pool_quota(name)
26
+ used_quota = find_pool_by_name(name)["used_bytes"].to_i
27
+
28
+ code = check_threshold(used_quota, quota, warn, crit)
29
+
30
+ message = "License pool '#{name}' #{STATUS[code]}: #{used_quota * 100 / quota}% of license pool capacity is used"
31
+ message << " | quota: #{quota} B; used: #{used_quota} B"
32
+ return [code, message]
33
+ end
34
+
18
35
  # license usage check
19
36
  # @param [Integer] warn license usage threshhold
20
37
  # @param [Integer] crit license usage threshhold
38
+ # @param [String] stack_id
21
39
  # @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
40
+ def license_usage(warn, crit, stack_id = "enterprise")
41
+ quota = license_quota(stack_id)
42
+ used_quota = pools_in_stack_usage(stack_id)
43
+
44
+ code = check_threshold(used_quota, quota, warn, crit)
45
+
35
46
  message = "License #{STATUS[code]}: #{used_quota * 100 / quota}% of license capacity is used"
36
47
  message << " | quota: #{quota} B; used: #{used_quota} B"
37
48
  return [code, message]
38
49
  end
39
50
 
51
+ private
52
+
53
+ # Compare usage and quota with threshold
54
+ # @param [Integer] usage
55
+ # @param [Integer] quota
56
+ # @param [Integer] warn
57
+ # @param [Integer] crit
58
+ # @return [Integer] exit code
59
+ def check_threshold(usage, quota, warn, crit)
60
+ case true
61
+ when usage > quota * crit.to_i / 100:
62
+ return 2
63
+ when usage > quota * warn.to_i / 100:
64
+ return 1
65
+ else
66
+ return 0
67
+ end
68
+ end
69
+
70
+ # Find out pool quota
71
+ # @param [String] name of the pool
72
+ # @return [Integer] quota number of bytes
73
+ def pool_quota(name)
74
+ pool = find_pool_by_name(name)
75
+ # get license quota if it is not limited
76
+ pool["quota"] == "MAX" ? license_quota(pool["stack_id"]) : pool["quota"].to_i
77
+ end
78
+
79
+ # Find out license quota
80
+ # @param [String] stack_id licesne stack
81
+ # @return [Integer] license stack quota in bytes
82
+ def license_quota(stack_id)
83
+ find_licenses(stack_id).reduce(0) { |r,l| r+=l[1]["quota"].to_i }
84
+ end
85
+
86
+ # Find out pool(s) license usage
87
+ # @param [String] stack_id which pools are assigned to
88
+ # @retunr [Integer] number of bytes used by pool(s)
89
+ def pools_in_stack_usage(stack_id)
90
+ pools = find_pools(stack_id)
91
+ pools.reduce(0) {|r,p| r+=p[1]["used_bytes"].to_i }
92
+ end
93
+
40
94
  # list of avialable licenses
95
+ # @param [String] stack_id
41
96
  # @return [Array<Hash>]
42
- def licenses
97
+ def find_licenses(stack_id)
43
98
  response = rest_client.get(LICENSE_LIST_URL)
44
- response.code == "200" ? parse_data(response.body) : {}
99
+ result = response.code == "200" ? parse_data(response.body) : {}
100
+ # find all VALID licenses by stack_id
101
+ result.reject! {|k,v| v["stack_id"] != stack_id || v["status"] != "VALID" }
102
+ raise NoLicensesFound.new("No licenses are found in stack '#{stack_id}'") if result.empty?
103
+ result
45
104
  end
46
105
 
47
106
  # list of available pools
107
+ # @param [String] stack_id
48
108
  # @return [Array<Hash>]
49
- def pools
109
+ def find_pools(stack_id = nil)
50
110
  response = rest_client.get(POOL_LIST_URL)
51
- response.code == "200" ? parse_data(response.body) : {}
111
+ result = response.code == "200" ? parse_data(response.body) : {}
112
+ # find all license pools by stack_id
113
+ result.reject! {|k,v| v["stack_id"] != stack_id } unless stack_id.nil?
114
+ raise NoPoolsFound.new("No pools are found in stack '#{stack_id}'") if result.empty?
115
+ result
52
116
  end
53
117
 
54
- private
118
+ # Find pool by name
119
+ # param [String] name of the pool
120
+ # param [String] stack_id which pool is assigned to
121
+ # @return [Hash] pool
122
+ def find_pool_by_name(name)
123
+ pools = find_pools
124
+ result = pools.find { |k,v| k == name } # => ["pool-name", { "param1" => "value1" }]
125
+ raise NoPoolsFound.new("No pool named '#{name}' is found") if result.nil?
126
+ # return hash of parameters for the pool
127
+ result[1]
128
+ end
55
129
 
56
130
  def parse_data(data)
57
131
  doc = Nokogiri::Slop(data)
@@ -7,8 +7,8 @@ module Nagios
7
7
  include Mixlib::CLI
8
8
 
9
9
  option(:server_url,
10
- :short => "-s URL",
11
- :long => "--server URL",
10
+ :short => "-u URL",
11
+ :long => "--server-url URL",
12
12
  :required => true,
13
13
  :description => "Splunk server url",
14
14
  :on => :head)
@@ -25,13 +25,33 @@ module Nagios
25
25
  :default => '90',
26
26
  :description => "Critical % of license capacity usage (default: 90)")
27
27
 
28
+ option(:pool,
29
+ :short => "-p POOL",
30
+ :long => "--pool-name POOL",
31
+ :description => "Pool name wich usage is being checked.")
32
+
33
+ option(:stack_id,
34
+ :short => "-s STACK_ID",
35
+ :long => "--stack-id STACK_ID",
36
+ :default => 'enterprise',
37
+ :description => "License Stack ID.")
38
+
28
39
  def run(argv = ARGV)
29
40
  parse_options(argv)
30
41
 
31
42
  client = RestClient.new(config[:server_url])
32
43
  splunk = Check.new(client)
33
44
 
34
- status, message = splunk.license_usage(config[:warn], config[:crit])
45
+ begin
46
+ if config[:pool]
47
+ status, message = splunk.pool_usage(config[:pool], config[:warn], config[:crit])
48
+ else
49
+ status, message = splunk.license_usage(config[:warn], config[:crit], config[:stack_id])
50
+ end
51
+ rescue Nagios::Splunk::Exception => e
52
+ message = e.message
53
+ status = 3
54
+ end
35
55
 
36
56
  puts message
37
57
  status
@@ -0,0 +1,12 @@
1
+ module Nagios
2
+ module Splunk
3
+ class Exception < ::Exception
4
+ end
5
+
6
+ class NoLicensesFound < Exception
7
+ end
8
+
9
+ class NoPoolsFound < Exception
10
+ end
11
+ end
12
+ end
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "nagios-splunk"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.authors = 'Max Horbul'
7
7
  s.email = ["max@gorbul.net"]
8
8
  s.homepage = "https://github.com/mhorbul/nagios-splunk"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios-splunk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Max Horbul
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-12 00:00:00 Z
18
+ date: 2012-07-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: nokogiri
@@ -86,6 +86,7 @@ files:
86
86
  - bin/check_splunk
87
87
  - lib/nagios/splunk/check.rb
88
88
  - lib/nagios/splunk/cli.rb
89
+ - lib/nagios/splunk/exception.rb
89
90
  - lib/nagios/splunk/rest_client.rb
90
91
  - lib/nagios/splunk.rb
91
92
  - test/test_helper.rb