Skip to content

Commit 2f35ef3

Browse files
committed
2 parents 2fa8e61 + 625c4b1 commit 2f35ef3

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#! /usr/bin/env ruby
2+
# encoding: UTF-8
3+
4+
# check-gpg-expiration
5+
#
6+
# DESCRIPTION:
7+
# This will check if given GPG key ID is about to expire.
8+
# Optionally you can specify the GPG homedir
9+
#
10+
# OUTPUT:
11+
# plain text
12+
# Defaults: CRITICAL if key ID is about to expire in 30 days
13+
# WARNING if key ID is about expire in 60 days
14+
#
15+
# PLATFORMS:
16+
# Linux
17+
#
18+
# DEPENDENCIES:
19+
# gem: sensu-plugin
20+
# gem: json
21+
# gem: csv
22+
# gem: open3
23+
# gem: date
24+
# gem: time
25+
# GPG
26+
#
27+
# USAGE:
28+
# check-gpg-expiration.rb -i <GPG_KEY_ID>
29+
# check-gpg-expiration.rb -w 7 -c 2 -i <GPG_KEY_ID>
30+
#
31+
# LICENSE:
32+
# Yasser Nabi yassersaleemi@gmail.com
33+
# Released under the same terms as Sensu (the MIT license); see LICENSE
34+
# for details.
35+
36+
require 'sensu-plugin/check/cli'
37+
require 'rubygems' if RUBY_VERSION < '1.9.0'
38+
require 'csv'
39+
require 'open3'
40+
require 'date'
41+
require 'time'
42+
43+
# Use to see if any processes require a restart
44+
class CheckGpgExpire < Sensu::Plugin::Check::CLI
45+
option :warn,
46+
short: '-w WARN',
47+
default: 60
48+
49+
option :crit,
50+
short: '-c CRIT',
51+
default: 30
52+
53+
option :homedir,
54+
short: '-h GPG_HOMEDIR',
55+
long: '--homedir GPG_HOMEDIR'
56+
57+
option :id,
58+
short: '-i GPG_KEY_ID',
59+
long: '--id GPG_KEY_ID',
60+
required: true
61+
62+
GPG = '/usr/bin/gpg'
63+
64+
# Set things up
65+
def initialize
66+
super
67+
end
68+
69+
# Helper method that returns the number of days since epoch
70+
# from a given epoch parameter
71+
def days_since_epoch(e)
72+
epoch = Date.new(1970, 1, 1)
73+
d = Time.at(e).to_date
74+
(d - epoch).to_i
75+
end
76+
77+
# Run the GPG command and return the expiration date in epoch
78+
def key_expire
79+
return_val = [false, nil]
80+
args = ['--with-colons', '--fixed-list-mode', '--list-key', config[:id]]
81+
gpg_cmd = config[:homedir].nil? ? [GPG] + args : [GPG, "--homedir #{config[:homedir]}"] + args
82+
cmd_out, cmd_err, status = Open3.capture3 gpg_cmd.join ' '
83+
if status.success?
84+
CSV.parse(cmd_out, col_sep: ':') do |row|
85+
return_val = [true, row[6].to_i] if row[0] == 'pub'
86+
end
87+
else
88+
return_val = [false, cmd_err]
89+
end
90+
return_val
91+
end
92+
93+
# Check the GPG key expiration against today
94+
def check_gpg
95+
today_epoch = Date.today.to_time.to_i
96+
success, out = key_expire
97+
return_val = success ? days_since_epoch(out) - days_since_epoch(today_epoch) : out
98+
return_val.to_s
99+
end
100+
101+
def run
102+
output = check_gpg
103+
case output
104+
when /^-\d+$/
105+
message "Key #{config[:id]} has expired!"
106+
critical
107+
when /^\d+$/
108+
message "Key #{config[:id]} has #{output} day(s) until it expires"
109+
warning if output.to_i <= config[:warn] && output.to_i > config[:crit]
110+
critical if output.to_i <= config[:crit]
111+
ok
112+
else
113+
message output
114+
unknown
115+
end
116+
end
117+
end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# springboot-metrics
4+
#
5+
# DESCRIPTION:
6+
# get metrics from Spring Boot 1.2.x application using actuator endpoints
7+
#
8+
# OUTPUT:
9+
# plain text
10+
#
11+
# PLATFORMS:
12+
# Linux
13+
#
14+
# DEPENDENCIES:
15+
# gem: sensu-plugin
16+
# gem: json
17+
# gem: uri
18+
#
19+
# USAGE:
20+
#
21+
# All metrics:
22+
# springboot-metrics.rb --host=192.168.1.1 &
23+
# --port=8081 &
24+
# --username=admin --password=secret &
25+
# --path=/metrics --counters --gauges
26+
# Exclude counters and gauges:
27+
# springboot-metrics.rb --host=192.168.1.1 &
28+
# --port=8081 &
29+
# --username=admin --password=secret --path=/metrics
30+
#
31+
# NOTES:
32+
# Check with Spring Boot 1.2.0 actuator endpoints
33+
#
34+
# LICENSE:
35+
# Copyright 2014 Victor Pechorin <dev@pechorina.net>
36+
# Released under the same terms as Sensu (the MIT license); see LICENSE
37+
# for details.
38+
#
39+
40+
require 'rubygems' if RUBY_VERSION < '1.9.0'
41+
require 'sensu-plugin/metric/cli'
42+
require 'net/http'
43+
require 'net/https'
44+
require 'json'
45+
require 'uri'
46+
47+
class SpringBootMetrics < Sensu::Plugin::Metric::CLI::Graphite
48+
option :host,
49+
short: '-h HOST',
50+
long: '--host HOST',
51+
description: 'Your spring boot actuator endpoint',
52+
required: true,
53+
default: 'localhost'
54+
55+
option :port,
56+
short: '-P PORT',
57+
long: '--port PORT',
58+
description: 'Your app port',
59+
required: true,
60+
default: 8080
61+
62+
option :username,
63+
short: '-u USERNAME',
64+
long: '--username USERNAME',
65+
description: 'Your app username',
66+
required: false
67+
68+
option :password,
69+
short: '-p PASSWORD',
70+
long: '--password PASSWORD',
71+
description: 'Your app password',
72+
required: false
73+
74+
option :path,
75+
short: '-e PATH',
76+
long: '--path PATH',
77+
description: 'Metrics endpoint path',
78+
required: true,
79+
default: '/metrics'
80+
81+
option :scheme,
82+
description: 'Metric naming scheme, text to prepend to metric',
83+
short: '-s SCHEME',
84+
long: '--scheme SCHEME',
85+
required: true,
86+
default: "#{Socket.gethostname}.springboot_metrics"
87+
88+
option :counters,
89+
description: 'Include counters',
90+
short: '-c',
91+
long: '--counters',
92+
boolean: true,
93+
default: false
94+
95+
option :gauges,
96+
description: 'Include gauges',
97+
short: '-g',
98+
long: '--gauges',
99+
boolean: true,
100+
default: false
101+
102+
def json_valid?(str)
103+
JSON.parse(str)
104+
return true
105+
rescue JSON::ParserError
106+
return false
107+
end
108+
109+
def run
110+
endpoint = "http://#{config[:host]}:#{config[:port]}"
111+
url = URI.parse(endpoint)
112+
113+
begin
114+
res = Net::HTTP.start(url.host, url.port) do |http|
115+
req = Net::HTTP::Get.new(config[:path])
116+
if config[:username] && config[:password]
117+
req.basic_auth(config[:username], config[:password])
118+
end
119+
http.request(req)
120+
end
121+
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
122+
EOFError, Net::HTTPBadResponse,
123+
Net::HTTPHeaderSyntaxError, Net::ProtocolError,
124+
Errno::ECONNREFUSED => e
125+
critical e
126+
end
127+
128+
if json_valid?(res.body)
129+
json = JSON.parse(res.body)
130+
json.each do |key, val|
131+
if key.to_s.match(/^counter\.(.+)/)
132+
output(config[:scheme] + '.' + key, val) if config[:counters]
133+
elsif key.to_s.match(/^gauge\.(.+)/)
134+
output(config[:scheme] + '.' + key, val) if config[:gauges]
135+
else
136+
output(config[:scheme] + '.' + key, val)
137+
end
138+
end
139+
else
140+
critical 'Response contains invalid JSON'
141+
end
142+
143+
ok
144+
end
145+
end

plugins/system/check-cpu.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
#
3+
# Check CPU usage
4+
#
5+
# ===
6+
#
7+
# Examples:
8+
#
9+
# check-cpu.sh -w 85 -c 95
10+
#
11+
# Date: 2014-09-12
12+
# Author: Jun Ichikawa <jun1ka0@gmail.com>
13+
#
14+
# Released under the same terms as Sensu (the MIT license); see LICENSE
15+
# for details.
16+
17+
# get arguments
18+
while getopts 'w:c:h' OPT; do
19+
case $OPT in
20+
w) WARN=$OPTARG;;
21+
c) CRIT=$OPTARG;;
22+
h) hlp="yes";;
23+
*) unknown="yes";;
24+
esac
25+
done
26+
27+
# usage
28+
HELP="
29+
usage: $0 [ -w value -c value -p -h ]
30+
31+
-w --> Warning percentage < value
32+
-c --> Critical percentage < value
33+
-h --> print this help screen
34+
"
35+
36+
if [ "$hlp" = "yes" ]; then
37+
echo "$HELP"
38+
exit 0
39+
fi
40+
41+
cpuusage1=(`cat /proc/stat | head -1`)
42+
if [ ${#cpuusage1} -eq 0 ]; then
43+
echo "CRITICAL - CPU UNKNOWN"
44+
exit 2
45+
fi
46+
sleep 1
47+
cpuusage2=(`cat /proc/stat | head -1`)
48+
if [ ${#cpuusage2} -eq 0 ]; then
49+
echo "CRITICAL - CPU UNKNOWN"
50+
exit 2
51+
fi
52+
53+
WARN=${WARN:=90}
54+
CRIT=${CRIT:=95}
55+
56+
cpu_diff=(0)
57+
total=0
58+
usage_diff=0
59+
for i in `seq 1 9`
60+
do
61+
cpu_diff=("${cpu_diff[@]}" `echo "${cpuusage2[$i]}-${cpuusage1[$i]}" | bc`)
62+
total=`echo "$total+${cpu_diff[$i]}" | bc`
63+
if [ $i -ne "4" ]; then
64+
usage_diff=`echo "$usage_diff+${cpu_diff[$i]}" | bc`
65+
else
66+
idl=$cpu_diff[$i]
67+
fi
68+
done
69+
cpu_usage=`echo "scale=2; 100*$usage_diff/$total" | bc`
70+
71+
if [ "$(echo "${cpu_usage} > ${CRIT}" | bc)" -eq 1 ]; then
72+
echo "CPU CRITICAL - ${cpu_usage}% is greater than critical point.[${CRIT}]"
73+
exit 2
74+
fi
75+
76+
if [ "$(echo "${cpu_usage} > ${WARN}" | bc)" -eq 1 ]; then
77+
echo "CPU WARNING - ${cpu_usage}% is greater than warning point.[${WARN}]"
78+
exit 1
79+
fi
80+
81+
echo "CPU OK - Usage:${cpu_usage}"
82+
exit 0

0 commit comments

Comments
 (0)