mirror of
https://github.com/c-kr/check_json.git
synced 2024-11-23 10:53:47 +01:00
Add new option outputvars to print JSON values in status message by rodo #6
This commit is contained in:
parent
326f1c8af9
commit
a4529419db
@ -9,7 +9,7 @@ Performance data is also enhanced to extract performance data compliant to Nagio
|
||||
|
||||
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:
|
||||
|
@ -11,16 +11,18 @@ my $np = Nagios::Plugin->new(
|
||||
usage => "Usage: %s -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 ] ",
|
||||
version => '0.3',
|
||||
version => '0.4',
|
||||
blurb => 'Nagios plugin to check JSON attributes via http(s)',
|
||||
extra => "\nExample: \n"
|
||||
. "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',
|
||||
plugin => 'check_json',
|
||||
timeout => 15,
|
||||
@ -33,21 +35,25 @@ $np->add_arg(
|
||||
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 '
|
||||
@ -61,18 +67,24 @@ $np->add_arg(
|
||||
. "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(
|
||||
spec => 'contenttype|T=s',
|
||||
default => 'application/json',
|
||||
help => "-T, --contenttype application/json \n "
|
||||
. "Content-type accepted if different from application/json ",
|
||||
);
|
||||
|
||||
$np->add_arg(
|
||||
spec => 'ignoressl',
|
||||
help => "--ignoressl\n Ignore bad ssl certificates",
|
||||
);
|
||||
|
||||
|
||||
## Parse @ARGV and process standard arguments (e.g. usage, help, version)
|
||||
$np->getopts;
|
||||
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(
|
||||
return_code => $result,
|
||||
message => join(', ', @statusmsg),
|
||||
|
Loading…
Reference in New Issue
Block a user