mirror of
https://github.com/c-kr/check_json.git
synced 2024-11-23 19:03:49 +01:00
Added support for multiple attribute checks in same call and wildcard output
This commit is contained in:
parent
a4529419db
commit
045219b9bb
@ -8,8 +8,8 @@ use Nagios::Plugin;
|
|||||||
use Data::Dumper;
|
use Data::Dumper;
|
||||||
|
|
||||||
my $np = Nagios::Plugin->new(
|
my $np = Nagios::Plugin->new(
|
||||||
usage => "Usage: %s -u|--url <URL> -a|--attribute <attribute> "
|
usage => "Usage: %s -u|--url <URL> -a|--attributes <attributes> "
|
||||||
. "[ -c|--critical <threshold> ] [ -w|--warning <threshold> ] "
|
. "[ -c|--critical <thresholds> ] [ -w|--warning <thresholds> ] "
|
||||||
. "[ -p|--perfvars <fields> ] "
|
. "[ -p|--perfvars <fields> ] "
|
||||||
. "[ -o|--outputvars <fields> ] "
|
. "[ -o|--outputvars <fields> ] "
|
||||||
. "[ -t|--timeout <timeout> ] "
|
. "[ -t|--timeout <timeout> ] "
|
||||||
@ -20,7 +20,7 @@ my $np = Nagios::Plugin->new(
|
|||||||
version => '0.4',
|
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 --attributes '{shares}->{dead}' "
|
||||||
. "--warning :5 --critical :10 --perfvars '{shares}->{dead},{shares}->{live}' "
|
. "--warning :5 --critical :10 --perfvars '{shares}->{dead},{shares}->{live}' "
|
||||||
. "--outputvars '{status_message}'",
|
. "--outputvars '{status_message}'",
|
||||||
url => 'https://github.com/c-kr/check_json',
|
url => 'https://github.com/c-kr/check_json',
|
||||||
@ -37,8 +37,8 @@ $np->add_arg(
|
|||||||
);
|
);
|
||||||
|
|
||||||
$np->add_arg(
|
$np->add_arg(
|
||||||
spec => 'attribute|a=s',
|
spec => 'attributes|a=s',
|
||||||
help => '-a, --attribute {shares}->{dead}',
|
help => '-a, --attributes {shares}->{dead},{shares}->{uptime}',
|
||||||
required => 1,
|
required => 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -63,13 +63,13 @@ $np->add_arg(
|
|||||||
|
|
||||||
$np->add_arg(
|
$np->add_arg(
|
||||||
spec => 'perfvars|p=s',
|
spec => 'perfvars|p=s',
|
||||||
help => "-p, --perfvars eg. '{shares}->{dead},{shares}->{live}'\n "
|
help => "-p, --perfvars eg. '* or {shares}->{dead},{shares}->{live}'\n "
|
||||||
. "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(
|
$np->add_arg(
|
||||||
spec => 'outputvars|o=s',
|
spec => 'outputvars|o=s',
|
||||||
help => "-o, --outputvars eg. '{status_message}'\n "
|
help => "-o, --outputvars eg. '* or {status_message}'\n "
|
||||||
. "CSV list of fields output in status message, same syntax as perfvars"
|
. "CSV list of fields output in status message, same syntax as perfvars"
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -118,8 +118,19 @@ if ($response->is_success) {
|
|||||||
my $json_response = decode_json($response->content);
|
my $json_response = decode_json($response->content);
|
||||||
if ($np->opts->verbose) { (print Dumper ($json_response))};
|
if ($np->opts->verbose) { (print Dumper ($json_response))};
|
||||||
|
|
||||||
|
my @attributes = split(',', $np->opts->attributes);
|
||||||
|
my @warning = split(',', $np->opts->warning);
|
||||||
|
my @critical = split(',', $np->opts->critical);
|
||||||
|
my @divisor = $np->opts->divisor ? split(',',$np->opts->divisor) : () ;
|
||||||
|
my %attributes = map { $attributes[$_] => { warning => $warning[$_] , critical => $critical[$_], divisor => ($divisor[$_] or 0) } } 0..$#attributes;
|
||||||
|
|
||||||
|
my %check_value;
|
||||||
my $check_value;
|
my $check_value;
|
||||||
my $check_value_str = '$check_value = $json_response->'.$np->opts->attribute;
|
my $result = -1;
|
||||||
|
|
||||||
|
foreach my $attribute (sort keys %attributes){
|
||||||
|
my $check_value;
|
||||||
|
my $check_value_str = '$check_value = $json_response->'.$attribute;
|
||||||
|
|
||||||
if ($np->opts->verbose) { (print Dumper ($check_value_str))};
|
if ($np->opts->verbose) { (print Dumper ($check_value_str))};
|
||||||
eval $check_value_str;
|
eval $check_value_str;
|
||||||
@ -128,17 +139,26 @@ if (!defined $check_value) {
|
|||||||
$np->nagios_exit(UNKNOWN, "No value received");
|
$np->nagios_exit(UNKNOWN, "No value received");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined $np->opts->divisor) {
|
if ($attributes{$attribute}{'divisor'}) {
|
||||||
$check_value = $check_value/$np->opts->divisor;
|
$check_value = $check_value/$attributes{$attribute}{'divisor'};
|
||||||
}
|
}
|
||||||
|
|
||||||
my $result = $np->check_threshold($check_value);
|
my $resultTmp = $np->check_threshold(
|
||||||
|
check => $check_value,
|
||||||
|
warning => $attributes{$attribute}{'warning'},
|
||||||
|
critical => $attributes{$attribute}{'critical'}
|
||||||
|
);
|
||||||
|
$result = $resultTmp if $result < $resultTmp;
|
||||||
|
|
||||||
|
$attributes{$attribute}{'check_value'}=$check_value;
|
||||||
|
}
|
||||||
|
|
||||||
my @statusmsg;
|
my @statusmsg;
|
||||||
|
|
||||||
|
|
||||||
# routine to add perfdata from JSON response based on a loop of keys given in perfvals (csv)
|
# routine to add perfdata from JSON response based on a loop of keys given in perfvals (csv)
|
||||||
if ($np->opts->perfvars) {
|
if ($np->opts->perfvars) {
|
||||||
foreach my $key (split(',', $np->opts->perfvars)) {
|
foreach my $key ($np->opts->perfvars eq '*' ? map { "{$_}"} sort keys $json_response : split(',', $np->opts->perfvars)) {
|
||||||
# use last element of key as label
|
# use last element of key as label
|
||||||
my $label = (split('->', $key))[-1];
|
my $label = (split('->', $key))[-1];
|
||||||
# make label ascii compatible
|
# make label ascii compatible
|
||||||
@ -148,12 +168,12 @@ if ($np->opts->perfvars) {
|
|||||||
if ($np->opts->verbose) { print Dumper ("JSON key: ".$label.", JSON val: " . $perf_value) };
|
if ($np->opts->verbose) { print Dumper ("JSON key: ".$label.", JSON val: " . $perf_value) };
|
||||||
if ( defined($perf_value) ) {
|
if ( defined($perf_value) ) {
|
||||||
# add threshold if attribute option matches key
|
# add threshold if attribute option matches key
|
||||||
if ($key eq $np->opts->attribute) {
|
if ($attributes{$key}) {
|
||||||
push(@statusmsg, "$label: $check_value");
|
push(@statusmsg, "$label: $attributes{$key}{'check_value'}");
|
||||||
$np->add_perfdata(
|
$np->add_perfdata(
|
||||||
label => lc $label,
|
label => lc $label,
|
||||||
value => $check_value,
|
value => $attributes{$key}{'check_value'},
|
||||||
threshold => $np->threshold(),
|
threshold => $np->set_thresholds( warning => $attributes{$key}{'warning'}, critical => $attributes{$key}{'critical'}),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
push(@statusmsg, "$label: $perf_value");
|
push(@statusmsg, "$label: $perf_value");
|
||||||
@ -168,7 +188,7 @@ if ($np->opts->perfvars) {
|
|||||||
|
|
||||||
# output some vars in message
|
# output some vars in message
|
||||||
if ($np->opts->outputvars) {
|
if ($np->opts->outputvars) {
|
||||||
foreach my $key (split(',', $np->opts->outputvars)) {
|
foreach my $key ($np->opts->outputvars eq '*' ? map { "{$_}"} sort keys $json_response : split(',', $np->opts->outputvars)) {
|
||||||
# use last element of key as label
|
# use last element of key as label
|
||||||
my $label = (split('->', $key))[-1];
|
my $label = (split('->', $key))[-1];
|
||||||
# make label ascii compatible
|
# make label ascii compatible
|
||||||
@ -183,4 +203,3 @@ $np->nagios_exit(
|
|||||||
return_code => $result,
|
return_code => $result,
|
||||||
message => join(', ', @statusmsg),
|
message => join(', ', @statusmsg),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user