diff --git a/README.md b/README.md index ee0587f..5b973c1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Performance data is also enhanced to extract performance data compliant to Nagio Usage: ``` -check_json -u|--url -a|--attribute [ -c|--critical ] [ -w|--warning ] [ -p|--perfvars ] [ -t|--timeout ] [ -d|--divisor ] [ -T|--contenttype ] [ --ignoressl ] [ -h|--help ] +check_json -u|--url -a|--attribute [ -c|--critical ] [ -w|--warning ] [ -p|--perfvars ] [ -o|--outputvars ] [ -t|--timeout ] [ -d|--divisor ] [ -T|--contenttype ] [ --ignoressl ] [ -h|--help ] ``` Example: diff --git a/check_json.pl b/check_json.pl index 3171052..ea5ff90 100755 --- a/check_json.pl +++ b/check_json.pl @@ -11,16 +11,18 @@ my $np = Nagios::Plugin->new( usage => "Usage: %s -u|--url -a|--attribute " . "[ -c|--critical ] [ -w|--warning ] " . "[ -p|--perfvars ] " + . "[ -o|--outputvars ] " . "[ -t|--timeout ] " . "[ -d|--divisor ] " . "[ -T|--contenttype ] " . "[ --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),