Downdetector.co.uk Nagios Script

This is a PHP based script for monitoring Downdetector.co.uk on Nagios. It doesn’t use the paid API and just pulls the data it needs from the webpage. This means it is free to use.

To use it just run PHP SCRIPTNAME SERVICE WarningLevel
example: PHP check_downdetector.php facebook 100

<?php
$service = $argv[1];
$reportswarning = $argv[2];
error_reporting(0);
$url = "https://downdetector.co.uk/status/$service/";


exec("curl $url > /tmp/$service.txt");

$d=0;
$handle = fopen("/tmp/$service.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
            if (strpos($line, "data: [")) {
                        if ($d == "1") { $lastreport = $last; }

                    $d++;
                    }
            if (strpos($line, "x: ")) {

                    if ($d == "1" ) {
                            /// do something with reports
                            $reports = $reports . $line;
                            $last = $line;
                    }

                    if ($d == "2" ) {
                    /// do something with baseline
                            $baseline = $baseline . $line;
                            $last = $line;
            }


            }
            //$i++;
    }
    $lastbaseline = $last;
    fclose($handle);
}
$piecesa = explode(",", $lastreport);
$lastreport = $piecesa[1];
$lastreport = str_replace("y: ", "", $lastreport);
$lastreport = str_replace(" }", "", $lastreport);
$lastreport = str_replace(" ", "", $lastreport);
$piecesb = explode(",", $lastbaseline);
$lastbaseline = $piecesb[1];
$lastbaseline = str_replace("y: ", "", $lastbaseline);
$lastbaseline = str_replace(" }", "", $lastbaseline);
$lastbaseline = str_replace(" ", "", $lastbaseline);
unlink("/tmp/$service.txt");
if ($reportswarning != "") { $warning = $reportswarning;  } else { $warning = $lastbaseline; }
if ($lastreport > $warning) {
        echo "WARNING: $service is in a problem state with $lastreport reports and baseline of $lastbaseline warning: $warning | reports=$lastreport";
        exit(1);
} else {
        echo "OK: $service reports are within limits  with $lastreport reports and baseline of $lastbaseline  warning: $warning | reports=$lastreport";
        exit(0);
}

?>