check_graphite 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,6 +13,8 @@ check_graphite accepts the following options:
13
13
  * `-M' or `--metric`: the metric expression which will be queried, it can be an expression
14
14
  * `-F` or `--from`: time frame for which to query metrics, defaults to "30seconds"
15
15
  * `-N` or `--name`: name to give to the metric, defaults to "value"
16
+ * `-U` or `--username`: username used for basic authentication
17
+ * `-P` or `--password`: password used for basic authentication
16
18
  * `-w`: warning threshold for the metric
17
19
  * `-c`: critical threshold for the metric
18
20
  * `-t`: timeout after which the metric should be considered unknown
@@ -21,5 +21,7 @@ Gem::Specification.new do |s|
21
21
  # specify any dependencies here; for example:
22
22
  # s.add_development_dependency "rspec"
23
23
  s.add_runtime_dependency "nagios_check"
24
- s.add_runtime_dependency "http"
24
+
25
+ s.add_development_dependency "rspec"
26
+ s.add_development_dependency "fakeweb"
25
27
  end
@@ -1,3 +1,3 @@
1
1
  module CheckGraphite
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require "nagios_check"
2
2
  require "json"
3
- require "http"
3
+ require "net/http"
4
4
  require "check_graphite/version"
5
5
 
6
6
  module CheckGraphite
@@ -12,15 +12,29 @@ module CheckGraphite
12
12
  on "--metric METRIC", "-M METRIC", :mandatory
13
13
  on "--from TIMEFRAME", "-F TIMEFRAME", :default => "30seconds"
14
14
  on "--name NAME", "-N NAME", :default => :value
15
+ on "--username USERNAME", "-U USERNAME"
16
+ on "--password PASSWORD", "-P PASSWORD"
15
17
 
16
18
  enable_warning
17
19
  enable_critical
18
20
  enable_timeout
19
21
 
20
22
  def check
21
- data = Http.get "#{options.endpoint}?target=#{options.metric}&from=-#{options.from}&format=json"
22
- raise "no such metric registered" unless data.length > 0
23
- res = data.first["datapoints"].reduce({:sum => 0.0, :count => 0}) {|acc, e|
23
+ uri = URI(URI.encode("#{options.endpoint}?target=#{options.metric}&from=-#{options.from}&format=json"))
24
+ req = Net::HTTP::Get.new(uri.request_uri)
25
+
26
+ # use basic auth if username is set
27
+ if options.username
28
+ req.basic_auth options.username, options.password
29
+ end
30
+
31
+ res = Net::HTTP.start(uri.hostname, uri.port) { |http|
32
+ http.request(req)
33
+ }
34
+
35
+ res.code == "200" || raise("HTTP error code #{res.code}")
36
+
37
+ res = JSON(res.body).first["datapoints"].reduce({:sum => 0.0, :count => 0}) {|acc, e|
24
38
  if e[0]
25
39
  {:sum => acc[:sum] + e[0], :count => acc[:count] + 1}
26
40
  else
@@ -0,0 +1,57 @@
1
+ require "rspec"
2
+ require "fake_web"
3
+
4
+ require "check_graphite"
5
+
6
+ describe CheckGraphite::Command do
7
+ before do
8
+ FakeWeb.allow_net_connect = false
9
+ end
10
+
11
+ describe "it should make http requests and return data" do
12
+ before do
13
+ FakeWeb.register_uri(:get, "http://your.graphite.host/render?target=collectd.somebox.load.load.midterm&from=-30seconds&format=json",
14
+ :body => '[{"target": "default.test.boottime", "datapoints": [[1.0, 1339512060], [3.0, 1339512120]]}]',
15
+ :content_type => "application/json")
16
+ end
17
+
18
+ it "should just work" do
19
+ ARGV = %w{ -H http://your.graphite.host/render -M collectd.somebox.load.load.midterm }
20
+ c = CheckGraphite::Command.new
21
+ STDOUT.should_receive(:puts).with("OK|value=2.0;;;;")
22
+ lambda { c.run }.should raise_error SystemExit
23
+ end
24
+
25
+ it "should be critical" do
26
+ ARGV = %w{ -H http://your.graphite.host/render -M collectd.somebox.load.load.midterm -c 0 }
27
+ c = CheckGraphite::Command.new
28
+ STDOUT.should_receive(:puts).with("CRITICAL|value=2.0;;;;")
29
+ lambda { c.run }.should raise_error SystemExit
30
+ end
31
+ end
32
+
33
+ describe "it should make http requests with basic auth and return data" do
34
+ before do
35
+ FakeWeb.register_uri(:get, "http://baduser:badpass@your.graphite.host/render?target=collectd.somebox.load.load.midterm&from=-30seconds&format=json",
36
+ :body => "Unauthorized", :status => ["401", "Unauthorized"])
37
+
38
+ FakeWeb.register_uri(:get, "http://testuser:testpass@your.graphite.host/render?target=collectd.somebox.load.load.midterm&from=-30seconds&format=json",
39
+ :body => '[{"target": "default.test.boottime", "datapoints": [[1.0, 1339512060], [3.0, 1339512120]]}]',
40
+ :content_type => "application/json")
41
+ end
42
+
43
+ it "should work with valid username and password" do
44
+ ARGV = %w{ -H http://your.graphite.host/render -M collectd.somebox.load.load.midterm -U testuser -P testpass}
45
+ c = CheckGraphite::Command.new
46
+ STDOUT.should_receive(:puts).with("OK|value=2.0;;;;")
47
+ lambda { c.run }.should raise_error SystemExit
48
+ end
49
+
50
+ it "should fail with bad username and password" do
51
+ ARGV = %w{ -H http://your.graphite.host/render -M collectd.somebox.load.load.midterm -U baduser -P badpass }
52
+ c = CheckGraphite::Command.new
53
+ STDOUT.should_receive(:puts).with(/UNKNOWN: INTERNAL ERROR: HTTP error code 401/)
54
+ lambda { c.run }.should raise_error SystemExit
55
+ end
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: check_graphite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-02-10 00:00:00.000000000Z
12
+ date: 2012-06-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nagios_check
16
- requirement: &70361640493220 !ruby/object:Gem::Requirement
16
+ requirement: &21306100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,29 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70361640493220
24
+ version_requirements: *21306100
25
25
  - !ruby/object:Gem::Dependency
26
- name: http
27
- requirement: &70361640492800 !ruby/object:Gem::Requirement
26
+ name: rspec
27
+ requirement: &21305220 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: '0'
33
- type: :runtime
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *21305220
36
+ - !ruby/object:Gem::Dependency
37
+ name: fakeweb
38
+ requirement: &21304580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
34
45
  prerelease: false
35
- version_requirements: *70361640492800
46
+ version_requirements: *21304580
36
47
  description: check values from a graphite server
37
48
  email:
38
49
  - pyr@spootnik.org
@@ -49,6 +60,7 @@ files:
49
60
  - check_graphite.gemspec
50
61
  - lib/check_graphite.rb
51
62
  - lib/check_graphite/version.rb
63
+ - spec/check_graphite_spec.rb
52
64
  homepage: https://github.com/pyr/check-graphite
53
65
  licenses: []
54
66
  post_install_message:
@@ -69,8 +81,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
81
  version: '0'
70
82
  requirements: []
71
83
  rubyforge_project: check_graphite
72
- rubygems_version: 1.8.15
84
+ rubygems_version: 1.8.10
73
85
  signing_key:
74
86
  specification_version: 3
75
87
  summary: check_graphite
76
- test_files: []
88
+ test_files:
89
+ - spec/check_graphite_spec.rb