1
0
mirror of https://github.com/c-kr/check_json.git synced 2024-11-23 19:03:49 +01:00

Add new option outputvars to print JSON values in status message by rodo #6

This commit is contained in:
Christopher Kreft 2014-05-12 10:48:30 +02:00
parent 326f1c8af9
commit a4529419db
2 changed files with 29 additions and 4 deletions

View File

@ -9,7 +9,7 @@ Performance data is also enhanced to extract performance data compliant to Nagio
Usage: Usage:
``` ```
check_json -u|--url <URL> -a|--attribute <attribute> [ -c|--critical <threshold> ] [ -w|--warning <threshold> ] [ -p|--perfvars <fields> ] [ -t|--timeout <timeout> ] [ -d|--divisor <divisor> ] [ -T|--contenttype <content-type> ] [ --ignoressl ] [ -h|--help ] check_json -u|--url <URL> -a|--attribute <attribute> [ -c|--critical <threshold> ] [ -w|--warning <threshold> ] [ -p|--perfvars <fields> ] [ -o|--outputvars <fields> ] [ -t|--timeout <timeout> ] [ -d|--divisor <divisor> ] [ -T|--contenttype <content-type> ] [ --ignoressl ] [ -h|--help ]
``` ```
Example: Example:

View File

@ -11,16 +11,18 @@ my $np = Nagios::Plugin->new(
usage => "Usage: %s -u|--url <URL> -a|--attribute <attribute> " usage => "Usage: %s -u|--url <URL> -a|--attribute <attribute> "
. "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] " . "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] "
. "[ -p|--perfvars <fields> ] " . "[ -p|--perfvars <fields> ] "
. "[ -o|--outputvars <fields> ] "
. "[ -t|--timeout <timeout> ] " . "[ -t|--timeout <timeout> ] "
. "[ -d|--divisor <divisor> ] " . "[ -d|--divisor <divisor> ] "
. "[ -T|--contenttype <content-type> ] " . "[ -T|--contenttype <content-type> ] "
. "[ --ignoressl ] " . "[ --ignoressl ] "
. "[ -h|--help ] ", . "[ -h|--help ] ",
version => '0.3', version => '0.4',
blurb => 'Nagios plugin to check JSON attributes via http(s)', blurb => 'Nagios plugin to check JSON attributes via http(s)',
extra => "\nExample: \n" extra => "\nExample: \n"
. "check_json.pl --url http://192.168.5.10:9332/local_stats --attribute '{shares}->{dead}' " . "check_json.pl --url http://192.168.5.10:9332/local_stats --attribute '{shares}->{dead}' "
. "--warning :5 --critical :10 --perfvars '{shares}->{dead},{shares}->{live}'", . "--warning :5 --critical :10 --perfvars '{shares}->{dead},{shares}->{live}' "
. "--outputvars '{status_message}'",
url => 'https://github.com/c-kr/check_json', url => 'https://github.com/c-kr/check_json',
plugin => 'check_json', plugin => 'check_json',
timeout => 15, timeout => 15,
@ -33,21 +35,25 @@ $np->add_arg(
help => '-u, --url http://192.168.5.10:9332/local_stats', help => '-u, --url http://192.168.5.10:9332/local_stats',
required => 1, required => 1,
); );
$np->add_arg( $np->add_arg(
spec => 'attribute|a=s', spec => 'attribute|a=s',
help => '-a, --attribute {shares}->{dead}', help => '-a, --attribute {shares}->{dead}',
required => 1, required => 1,
); );
$np->add_arg( $np->add_arg(
spec => 'divisor|d=i', spec => 'divisor|d=i',
help => '-d, --divisor 1000000', help => '-d, --divisor 1000000',
); );
$np->add_arg( $np->add_arg(
spec => 'warning|w=s', spec => 'warning|w=s',
help => '-w, --warning INTEGER:INTEGER . See ' help => '-w, --warning INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT ' . 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ', . 'for the threshold format. ',
); );
$np->add_arg( $np->add_arg(
spec => 'critical|c=s', spec => 'critical|c=s',
help => '-c, --critical INTEGER:INTEGER . See ' help => '-c, --critical INTEGER:INTEGER . See '
@ -61,18 +67,24 @@ $np->add_arg(
. "CSV list of fields from JSON response to include in perfdata " . "CSV list of fields from JSON response to include in perfdata "
); );
$np->add_arg(
spec => 'outputvars|o=s',
help => "-o, --outputvars eg. '{status_message}'\n "
. "CSV list of fields output in status message, same syntax as perfvars"
);
$np->add_arg( $np->add_arg(
spec => 'contenttype|T=s', spec => 'contenttype|T=s',
default => 'application/json', default => 'application/json',
help => "-T, --contenttype application/json \n " help => "-T, --contenttype application/json \n "
. "Content-type accepted if different from application/json ", . "Content-type accepted if different from application/json ",
); );
$np->add_arg( $np->add_arg(
spec => 'ignoressl', spec => 'ignoressl',
help => "--ignoressl\n Ignore bad ssl certificates", help => "--ignoressl\n Ignore bad ssl certificates",
); );
## Parse @ARGV and process standard arguments (e.g. usage, help, version) ## Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts; $np->getopts;
if ($np->opts->verbose) { (print Dumper ($np))}; if ($np->opts->verbose) { (print Dumper ($np))};
@ -154,6 +166,19 @@ if ($np->opts->perfvars) {
} }
} }
# output some vars in message
if ($np->opts->outputvars) {
foreach my $key (split(',', $np->opts->outputvars)) {
# use last element of key as label
my $label = (split('->', $key))[-1];
# make label ascii compatible
$label =~ s/[^a-zA-Z0-9_-]//g;
my $perf_value;
$perf_value = eval '$json_response->'.$key;
push(@statusmsg, "$label: $perf_value");
}
}
$np->nagios_exit( $np->nagios_exit(
return_code => $result, return_code => $result,
message => join(', ', @statusmsg), message => join(', ', @statusmsg),