Skip to content

Instantly share code, notes, and snippets.

@KyleGobel
Created July 8, 2014 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleGobel/be9efb7177ddb2d36007 to your computer and use it in GitHub Desktop.
Save KyleGobel/be9efb7177ddb2d36007 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use warnings;
use strict;
use Getopt::Std;
use LWP::UserAgent;
use JSON 'decode_json';
my $plugin_name = "Tracker Status check_tracker_status_json";
my $VERSION = "1.00";
# getopt module config
$Getopt::Std::STANDARD_HELP_VERSION = 1;
# nagios exit codes
use constant EXIT_OK => 0;
use constant EXIT_WARNING => 1;
use constant EXIT_CRITICAL => 2;
use constant EXIT_UNKNOWN => 3;
my $status = EXIT_UNKNOWN;
#parse cmd opts
my %opts;
getopts('vU:t:d:', \%opts);
$opts{t} = 5 unless (defined $opts{t});
if (not (defined $opts{U}) ) {
print "ERROR: INVALID USAGE\n";
HELP_MESSAGE();
exit $status;
}
my $ua = LWP::UserAgent->new;
$ua->agent('Redirect Bot ' . $VERSION);
$ua->protocols_allowed( [ 'http', 'https'] );
$ua->parse_head(0);
$ua->timeout($opts{t});
my $response = $ua->get($opts{U});
if ( index($response->header("content-type"), 'application/json') == -1 )
{
print "Expected content-type to be application/json, got ", $response->header("content-type");
exit EXIT_CRITICAL;
}
my $json_response;
eval {
$json_response = decode_json($response->content);
print "JSON repsonse decoded successfully.\n";
$status = EXIT_OK;
if ($json_response->{'ControllerEnviornment'} ne "prd")
{
print "ControllerEnviornment is not prd!\nValue Found: $json_response->{'ControllerEnviornment'}\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'ClickProcessorServiceRunning'} ne 1)
{
print "Click Processor is not running!\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'ConversionProcessorServiceRunning'} ne 1)
{
print "Conversion Processor is not running!\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'EventProcessorServiceRunning'} ne 1)
{
print "Event Processor is not running!\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'ClickProcessorEnviornment'} ne "prd")
{
print "Click Processor Enviornment is not prd!\nValue Found: $json_response->{'ClickProcessorEnviornment'}\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'ConversionProcessorEnviornment'} ne "prd")
{
print "Conversion Processor Enviornment is not prd!\nValue Found: $json_response->{'ConversionProcessorEnviornment'}\n";
$status = EXIT_CRITICAL;
}
if ($json_response->{'EventProcessorEnviornment'} ne "prd")
{
print "Event Processor Enviornment is not prd!\nValue Found: $json_response->{'EventProcessorEnviornment'}\n";
$status = EXIT_CRITICAL;
}
exit $status;
} or do {
print "Unable to decode JSON, invalid response?";
exit EXIT_CRITICAL;
};
sub HELP_MESSAGE
{
print <<EOHELP
Retrieve an http/s url and checks its application type is application/json and the response content decodes properly into JSON.
Checks that the tracker parameters are correct
--help shows this message
--version shows version information
USAGE: $0 -U http://my.url.com
-U URL to retrieve (http or https)
-t Timeout in seconds to wait for the URL to load (default 60)
EOHELP
;
}
sub VERSION_MESSAGE
{
print <<EOVM
$plugin_name v. $VERSION
EOVM
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment