Nagios module for monitoring smsdlog for modem-connectivity

#!/usr/bin/php
<?php

function secondsToTime($inputSeconds)
{
        $secondsInAMinute = 60;
        $secondsInAnHour  = 60 * $secondsInAMinute;
        $secondsInADay    = 24 * $secondsInAnHour;

        // extract days
        $days = floor($inputSeconds / $secondsInADay);

        // extract hours
        $hourSeconds = $inputSeconds % $secondsInADay;
        $hours = floor($hourSeconds / $secondsInAnHour);

        // extract minutes
        $minuteSeconds = $hourSeconds % $secondsInAnHour;
        $minutes = floor($minuteSeconds / $secondsInAMinute);

        // extract the remaining seconds
        $remainingSeconds = $minuteSeconds % $secondsInAMinute;
        $seconds = ceil($remainingSeconds);

        $res = array();

        if ($days > 0)
                $ret[] = "$days days";

        if ($hours > 0)
                $ret[] = "$hours hours";

        if ($minutes > 0)
                $ret[] = "$minutes minutes";

        if ($seconds > 0)
                $ret[] = "$seconds seconds";

        // return the final array
        $obj = array(
                'd' => (int) $days,
                'h' => (int) $hours,
                'm' => (int) $minutes,
                's' => (int) $seconds,
        );
        if (empty($ret))
                return "0 seconds";
        else
                return implode(", ",$ret);
}

date_default_timezone_set('UTC');
$cmd = exec("grep 'Signal Strength' /var/log/smstools/smsd.log | /usr/bin/tail -n 1");
if (!$cmd)
{
        print("CRITICAL: No lifesign from modem in current logfile\n");
        die(2);
}

list ($year, $month, $day, $hour, $min, $sec) = preg_split("/:|-| /", explode(",", $cmd)[0] );
$ts = mktime($hour, $min, $sec, $month, $day, $year);
$diff = time() - $ts;

if ($diff < 30)
{
        print ("OK: Last lifesign from modem ".secondsToTime($diff)." ago\n");
        die(0);
}
else
{
        print ("CRITICAL: Last lifesign from modem ".secondsToTime($diff)." ago\n");
        die(2);
}
Dette indlæg blev udgivet i Nagios, Uncategorized. Bogmærk permalinket.