Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 2f4c665

Browse files
committed
Add serverspec test Sensu plugin
1 parent c55bb3d commit 2f4c665

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# Check Serverspec tests plugin
4+
# ===
5+
#
6+
# Runs http://serverspec.org/ spec tests against your servers.
7+
# Fails with a critical if tests are failing.
8+
#
9+
# Examples:
10+
#
11+
# # Run entire suite of testd
12+
# check-serverspec -d /etc/my_tests_dir
13+
#
14+
# # Run only one set of tests
15+
# check-serverspec -d /etc/my_tests_dir -t spec/test_one
16+
17+
require 'rubygems' if RUBY_VERSION < '1.9.0'
18+
require 'json'
19+
require 'socket'
20+
require 'serverspec'
21+
require 'sensu-plugin/check/cli'
22+
23+
class CheckServerspec < Sensu::Plugin::Check::CLI
24+
25+
option :tests_dir,
26+
:short => '-d /tmp/dir',
27+
:long => '--tests-dir /tmp/dir',
28+
:required => true
29+
30+
option :spec_tests,
31+
:short => '-t spec/test',
32+
:long => '--spec-tests spec/test',
33+
:default => nil
34+
35+
option :handler,
36+
:short => '-l HANDLER',
37+
:long => '--handler HANDLER',
38+
:default => 'default'
39+
40+
def sensu_client_socket(msg)
41+
u = UDPSocket.new
42+
u.send(msg + "\n", 0, '127.0.0.1', 3030)
43+
end
44+
45+
def send_ok(check_name, msg)
46+
d = { 'name' => check_name, 'status' => 0, 'output' => 'OK: ' + msg, 'handler' => config[:handler] }
47+
sensu_client_socket d.to_json
48+
end
49+
50+
def send_warning(check_name, msg)
51+
d = { 'name' => check_name, 'status' => 1, 'output' => 'WARNING: ' + msg, 'handler' => config[:handler] }
52+
sensu_client_socket d.to_json
53+
end
54+
55+
def send_critical(check_name, msg)
56+
d = { 'name' => check_name, 'status' => 2, 'output' => 'CRITICAL: ' + msg, 'handler' => config[:handler] }
57+
sensu_client_socket d.to_json
58+
end
59+
60+
def run
61+
serverspec_results = `cd #{config[:tests_dir]} ; ruby -S rspec #{config[:spec_tests]} --format json`
62+
parsed = JSON.parse(serverspec_results)
63+
64+
parsed["examples"].each do |serverspec_test|
65+
test_name = serverspec_test["file_path"].split('/')[-1] + "_" + serverspec_test["line_number"].to_s
66+
output = serverspec_test["full_description"].gsub!(/\"/, '')
67+
68+
if serverspec_test["status"] == "passed"
69+
send_ok(
70+
test_name
71+
output
72+
)
73+
else
74+
send_warning(
75+
test_name
76+
output
77+
)
78+
end
79+
80+
end
81+
82+
puts parsed["summary_line"]
83+
failures = parsed["summary_line"].split[2]
84+
if failures != '0'
85+
exit_with(
86+
:critical,
87+
parsed["summary_line"]
88+
)
89+
else
90+
exit_with(
91+
:ok,
92+
parsed["summary_line"]
93+
)
94+
end
95+
96+
end
97+
98+
def exit_with(sym, message)
99+
case sym
100+
when :ok
101+
ok message
102+
when :critical
103+
critical message
104+
else
105+
unknown message
106+
end
107+
end
108+
109+
end

0 commit comments

Comments
 (0)