Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Php wrapper script that can be used for nagios checks with an AWS ec2 instance
#!/usr/bin/php
<?php
# see https://gist.github.com/BobBurns/bc16d6e45fc01e61138f026c18187b00
# for a more efficient script using the aws php sdk
# wrapper script that can be used for nagios checks with aws ec2 instance
#
# Must have Instance Id $dim_value
# Requires aws cli configured with Access Keys
#
# build command string
# aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2014-04-08T23:18:00 --end-time 2014-04-09T23:18:00 --period 3600 --namespace AWS/EC2 --statistics Maximum --dimensions Name=InstanceId,Value=i-abcdef
date_default_timezone_set ("UTC");
$end_time = date('c');
$date = date_create(date('c'));
date_sub($date, date_interval_create_from_date_string('15 minute'));
$start_time = date_format($date, 'c');
$metric_name = "CPUUtilization";
$time = date("c");
$period = "60";
$statistics = "Average";
$dim_name = "InstanceId";
$dim_value = "i-abcdefg";
$exec_path = "/usr/local/bin/aws";
$aws_command = $exec_path . " cloudwatch get-metric-statistics --metric-name " . $metric_name . " --start-time " . $start_time . " --end-time " . $end_time . " --period " . $period . " --namespace AWS/EC2 --statistics " . $statistics . " --dimensions Name=" . $dim_name . ",Value=" . $dim_value;
###########################################################################################
# get comand line arguements
$options = getopt("w:c:");
if (!$options['w'] || !$options['c'])
{
echo "Usage: $argv[0] -w<warning range> -c<critical range>\n";
exit (-1);
}
$warnings = explode(':', $options['w']);
if (count($warnings) == 2)
{
$warn_low = $warnings[0];
$warn_high = $warnings[1];
} else
{
$warn_low = 0;
$warn_high = $warnings[0];
}
$criticals = explode(':', $options['c']);
if (count($criticals) == 2)
{
$crit_low = $criticals[0];
$crit_high = $criticals[1];
} else
{
$crit_low = 0;
$crit_high = $criticals[0];
}
########################################################################################
#call aws cloudwatch
$json_result = shell_exec($aws_command);
$result = json_decode($json_result, true);
# get cpu average
$result_count = count($result['Datapoints']) - 1;
if ($result_count < 0) { $result_count = 0; }
$cpu_avg = $result['Datapoints'][$result_count]['Average'];
# compare cpu average with argv
# and exit
# TODO write timer event to return UNKNOWN if timeout > 3 sec
if ($cpu_avg > $crit_high || $cpu_avg < $crit_low)
{
echo "*CRITICAL* - Cpu Utilization: " . $cpu_avg . "% | CpuUtilization%=" . $cpu_avg;
echo "\n";
exit (2);
}
if ($cpu_avg > $warn_high || $cpu_avg < $warn_low)
{
echo "*WARNING* - Cpu Utilization: " . $cpu_avg . "% | CpuUtilization%=" . $cpu_avg;
echo "\n";
exit (1);
}
echo "OK - Cpu Utilization: " . $cpu_avg . "% | CpuUtilization%=" . $cpu_avg;
echo "\n";
exit (0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.