patternMinor
Using an API to obtain JSON data and get the date string and determine if data is stale
Viewed 0 times
theobtainanddateapigetstaleusingdeterminejson
Problem
This is a nagios check that will use an API URL, get JSON data, flatten the data into a usable perl hash, and ultimately obtain a date string. Once the date is obtained, it should recognize the
Gitlab repo
```
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use LWP::UserAgent;
use Getopt::Std;
use JSON::Parse 'parse_json';
use JSON::Parse 'assert_valid_json';
use Hash::Flatten qw(:all);
use DateTime;
use DateTime::Format::Strptime;
my $plugin_name = "Nagios check_http_freshness";
my $VERSION = "1.0.0";
my $dateNowUTC = DateTime->now;
my $verbose = 0;
$Getopt::Std::STANDARD_HELP_VERSION = "true";
# nagios exit codes
use constant EXIT_OK => 0;
use constant EXIT_WARNING => 1;
use constant EXIT_CRITICAL => 2;
use constant EXIT_UNKNOWN => 3;
#parse cmd opts
my %opts;
getopts('U:K:F:u:t:w:c:z:v', \%opts);
$opts{t} = 60 unless (defined $opts{t});
$opts{w} = 12 unless (defined $opts{w});
$opts{c} = 24 unless (defined $opts{c});
$opts{F} = "%Y%m%dT%H%M%S" unless (defined $opts{F});
$opts{u} = "hours" unless (defined $opts{u});
$opts{z} = "UTC" unless (defined $opts{z});
if (not (defined $opts{U}) || not (defined $opts{K}) ) {
print "[ERROR] INVALID USAGE\n";
HELP_MESSAGE();
exit EXIT_UNKNOWN;
}
if (defined $opts{v}){$verbose = 1;}
if ($opts{w} >= $opts{c}){
print "[ERROR] Warning value must be less than critical value.\n"; HELP_MESSAGE(); exit EXIT_UNKNOWN;
}
if (not ($opts{u} eq "hours") && not ($opts{u} eq "minutes")){
print "[ERROR] Time unites must be either hours or minutes.\n"; HELP_MESSAGE(); exit EXIT_UNKNOWN;
}
# Configure the user agent and settings for the http/s request.
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla');
$ua->p
strftime format based on user input and determine the delta hours or minutes. Once the delta time is calculated, it should return critical, warning, or OK, based on the -c or -w user inputs. I just started Perl a week ago and need some code review to become better at it.Gitlab repo
```
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use LWP::UserAgent;
use Getopt::Std;
use JSON::Parse 'parse_json';
use JSON::Parse 'assert_valid_json';
use Hash::Flatten qw(:all);
use DateTime;
use DateTime::Format::Strptime;
my $plugin_name = "Nagios check_http_freshness";
my $VERSION = "1.0.0";
my $dateNowUTC = DateTime->now;
my $verbose = 0;
$Getopt::Std::STANDARD_HELP_VERSION = "true";
# nagios exit codes
use constant EXIT_OK => 0;
use constant EXIT_WARNING => 1;
use constant EXIT_CRITICAL => 2;
use constant EXIT_UNKNOWN => 3;
#parse cmd opts
my %opts;
getopts('U:K:F:u:t:w:c:z:v', \%opts);
$opts{t} = 60 unless (defined $opts{t});
$opts{w} = 12 unless (defined $opts{w});
$opts{c} = 24 unless (defined $opts{c});
$opts{F} = "%Y%m%dT%H%M%S" unless (defined $opts{F});
$opts{u} = "hours" unless (defined $opts{u});
$opts{z} = "UTC" unless (defined $opts{z});
if (not (defined $opts{U}) || not (defined $opts{K}) ) {
print "[ERROR] INVALID USAGE\n";
HELP_MESSAGE();
exit EXIT_UNKNOWN;
}
if (defined $opts{v}){$verbose = 1;}
if ($opts{w} >= $opts{c}){
print "[ERROR] Warning value must be less than critical value.\n"; HELP_MESSAGE(); exit EXIT_UNKNOWN;
}
if (not ($opts{u} eq "hours") && not ($opts{u} eq "minutes")){
print "[ERROR] Time unites must be either hours or minutes.\n"; HELP_MESSAGE(); exit EXIT_UNKNOWN;
}
# Configure the user agent and settings for the http/s request.
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla');
$ua->p
Solution
You may also want to look at the Nagios::Plugin module or it's successor, Monitoring::Plugin.
In these plugins, the OK, WARNING, CRITICAL and UNKNOWN exit statuses are exported by default, so you do not need to declare them.
To initiate a plugin, you need something like this:
Then you can add your command line options thus:
and so on. The arguments for the
You can then compare your result with the warning and critical thresholds. In essence, your
Or
Then, to exit with the correct status, you add this line:
Hope that helps!
In these plugins, the OK, WARNING, CRITICAL and UNKNOWN exit statuses are exported by default, so you do not need to declare them.
To initiate a plugin, you need something like this:
my $plugin = Nagios::Plugin->new(
shortname => $PLUGIN_NAME, # Short name of your plugin
usage => $USAGE, # Your usage message
version => $VERSION # Version number
);Then you can add your command line options thus:
$plugin->add_arg("w=i", "-w \n Warning if data exceeds this time", 12, 1);
$plugin->add_arg("c=i", "-c \n Critical if data exceeds this time", 24, 1);
$plugin->getopts();and so on. The arguments for the
add_arg() method are spec, usage, default and required.You can then compare your result with the warning and critical thresholds. In essence, your
NAGIOS_STATUS subroutine can be broken down to one simple line:my $status = $plugin->check_threshold(check => $delta_time, warning => $plugin->opts->w, critical => $plugin->opts->c);Or
my $threshold = $plugin->set_thresholds(warning => $plugin->opts->w, critical => $plugin->opts->c); # Set threshold so you can use it later
my $status = $plugin->check_threshold(check => $delta_time);Then, to exit with the correct status, you add this line:
$plugin->plugin_exit($status, sprintf("Delta time is %s", $delta_time));Hope that helps!
Code Snippets
my $plugin = Nagios::Plugin->new(
shortname => $PLUGIN_NAME, # Short name of your plugin
usage => $USAGE, # Your usage message
version => $VERSION # Version number
);$plugin->add_arg("w=i", "-w <hours>\n Warning if data exceeds this time", 12, 1);
$plugin->add_arg("c=i", "-c <hours>\n Critical if data exceeds this time", 24, 1);
$plugin->getopts();my $status = $plugin->check_threshold(check => $delta_time, warning => $plugin->opts->w, critical => $plugin->opts->c);my $threshold = $plugin->set_thresholds(warning => $plugin->opts->w, critical => $plugin->opts->c); # Set threshold so you can use it later
my $status = $plugin->check_threshold(check => $delta_time);$plugin->plugin_exit($status, sprintf("Delta time is %s", $delta_time));Context
StackExchange Code Review Q#148192, answer score: 7
Revisions (0)
No revisions yet.