forked from c-kr/check_json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_json.pl
executable file
·102 lines (85 loc) · 2.72 KB
/
check_json.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env perl
use warnings;
use strict;
use LWP::UserAgent;
use JSON 'decode_json';
use Nagios::Plugin;
use Data::Dumper;
my $np = Nagios::Plugin->new(
usage => "Usage: %s [ -v|--verbose ] [-U <URL>] [-t <timeout>] "
. "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] "
. "[ -a | --attribute ] <attribute>",
version => '0.1',
blurb => 'Nagios plugin to check JSON attributes via http(s)',
extra => "\nExample: \n"
. "check_json.pl -U http://192.168.5.10:9332/local_stats -a '{shares}->{dead}' -w :5 -c :10",
url => 'https://github.com/c-kr/check_json',
plugin => 'check_json',
timeout => 15,
);
# add valid command line options and build them into your usage/help documentation.
$np->add_arg(
spec => 'URL|U=s',
help => '-U, --URL http://192.168.5.10:9332/local_stats',
required => 1,
);
$np->add_arg(
spec => 'attribute|a=s',
help => '-a, --attribute {shares}->{dead}',
required => 1,
);
$np->add_arg(
spec => 'divisor|D=i',
help => '-D, --divisor 1000000',
);
$np->add_arg(
spec => 'warning|w=s',
help => '-w, --warning INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ',
);
$np->add_arg(
spec => 'critical|c=s',
help => '-c, --critical INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ',
);
# Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts;
## GET URL
my $ua = LWP::UserAgent->new;
$ua->agent('check_json/0.1');
$ua->default_header('Accept' => 'application/json');
$ua->protocols_allowed( [ 'http', 'https'] );
$ua->parse_head(0);
$ua->timeout($np->opts->timeout);
my $response = ($ua->get($np->opts->URL));
if ($response->is_success) {
if (!($response->header("content-type") =~ 'application/json')) {
$np->nagios_exit(UNKNOWN,"Content type is not JSON: ".$response->header("content-type"));
}
} else {
$np->nagios_exit(CRITICAL, "Connection failed: ".$response->status_line);
}
my $json_response = decode_json($response->content);
if ($np->opts->verbose) { (print Dumper ($json_response))};
my $value;
my $exec = '$value = $json_response->'.$np->opts->attribute;
if ($np->opts->verbose) {print "EXEC is: $exec \n"};
eval $exec;
if (!defined $value) {
$np->nagios_exit(UNKNOWN, "No value received");
}
if (defined $np->opts->divisor) {
$value = $value/$np->opts->divisor;
}
my $result = $np->check_threshold($value);
$np->add_perfdata(
label => 'value',
value => $value,
threshold => $np->threshold(),
);
$np->nagios_exit(
return_code => $result,
message => $value,
);