Skip to content

Instantly share code, notes, and snippets.

@stwalkerster
Created March 12, 2013 02:36
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 stwalkerster/5139853 to your computer and use it in GitHub Desktop.
Save stwalkerster/5139853 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
$statusfile = "/var/local/nagios-update-check";
$data = array();
$ret = 0;
$updates = exec("/usr/lib/update-notifier/apt-check 2>&1", $data, $ret);
$up = explode(";", $updates);
$maxupdates = 15;
if($ret != 0)
{
echo "Apt UNKNOWN: apt-check gave return code $ret.\n";
exit(3);
}
if( $up[0] == 0 )
{
if(is_file($statusfile)) unlink($statusfile);
echo "Apt OK: Nothing to update.\n";
exit(0);
}
if( $up[0] <= $maxupdates && $up[1] == 0 )
{
if(is_file($statusfile)) unlink($statusfile);
echo "Apt OK: $up[0] updates are available.\n";
exit(0);
}
if( $up[0] > $maxupdates && $up[1] == 0)
{
if(is_file($statusfile)) {
if(date_diff(new DateTime("@" . filectime($statusfile)), new DateTime())->d >= 1) {
echo "Apt WARNING: $up[0] updates are available.\n";
exit(1);
}
else {
echo "Apt OKish: $up[0] updates are available.\n";
exit(0);
}
}
else {
touch($statusfile);
echo "Apt OKish: $up[0] updates are available.\n";
exit(0);
}
}
if( $up[0] != 0 && $up[1] != 0)
{
if(is_file($statusfile)) {
if(date_diff(new DateTime("@" . filectime($statusfile)), new DateTime())->d >= 1) {
echo "Apt CRITICAL: $up[1] security updates are available, $up[0] total.\n";
exit(2);
}
else {
echo "Apt OKish: $up[1] security updates are available, $up[0] total.\n";
exit(0);
}
}
else {
touch($statusfile);
echo "Apt OKish: $up[1] security updates are available, $up[0] total.\n";
exit(0);
}
}
echo "Apt UNKNOWN: unspecified error.\n";
exit(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment