Skip to content

Instantly share code, notes, and snippets.

@jmcastagnetto
Last active August 29, 2015 14:04
Show Gist options
  • Save jmcastagnetto/48587be03741c7da66d0 to your computer and use it in GitHub Desktop.
Save jmcastagnetto/48587be03741c7da66d0 to your computer and use it in GitHub Desktop.
Script to use Pingdom's custom HTTP check, inspired by the one described in: http://jonsview.com/how-i-use-pingdoms-http-custom-feature

Script to be used with Pingdom's custom HTTP check.

Inspired by the code described in http://jonsview.com/how-i-use-pingdoms-http-custom-feature

Warning:

As of 20114-08-07, if you are using gzip compression in your server, creating a check pointing to an URL with the output of this script will fail, as the code been used by Pingdom does not yet grok compression.

The solution (at least for Apache), is to explicitly exclude from compression the path to the script in your server config, e.g.:

SetEnvIf REQUEST_URI ^/path/to/script/pingdom.php$ no-gzip dont-vary

After that it should work as intended.

License: BSD 2-Clause (http://opensource.org/licenses/BSD-2-Clause)

Copyright (c) 2014, Jesus M. Castagnetto All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-- schema
CREATE TABLE errors (
"timestamp" INTEGER NOT NULL,
"errormsg" TEXT,
"responsetime" REAL
);
CREATE UNIQUE INDEX "errorlogindex" on errors (timestamp ASC, errormsg ASC, responsetime ASC);
<?php
// (c) 2014, Jesus M. Castagnetto
// License: BSD 2-Clause (http://opensource.org/licenses/BSD-2-Clause)
// Thresholds
define('SWAP_THRESHOLD', 512); // 512 MB of swap usage
define('HD_THRESHOLD', 90); // 90% of space used
define('HD_MOUNT', '\/dev\/sda1'); // filesystem mount to check
// define('HD_MOUNT', '\/$/'); // in case you are checking "/"
define('LOADAVG5_THRESHOLD', 2.0); // the 5 minute CPU load average
define('DB_DSN', 'mysql:host=localhost;dbname=testdb');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_CPU_THRESHOLD', 90); // 90% CPU usage by database
// sqlite db, see "errolog.sql"
define("ERRORLOG_DSN", "sqlite:/path/to/errorlog.sqlite");
// checks each item against a threshold
function item_check($command, $threshold, $check) {
$item = (float) trim(`$command`);
if ($item > $threshold) {
return $check." OVER THRESHOLD";
} else {
return "";
}
}
// creates the final status string
function build_status($msgarr) {
$status = "";
foreach ($msgarr as $msg) {
if ("" !== $msg) {
$status .= ",$msg";
}
}
if ("" === $status) {
return "OK";
} else {
return substr($status, 1);
}
}
$start = microtime(true);
// check swap usage
$swap = item_check("free -m | awk '/^Swap:/{ print $3 }'",
SWAP_THRESHOLD, "SWAP USAGE");
// check load average
$loadavg = item_check("uptime | awk -F ', ' '{ print $5 }'",
LOADAVG5_THRESHOLD, "LOAD AVERAGE");
// check disk usage
$disk = item_check("df -h | awk '/".HD_MOUNT."/{print substr($5, 0, length($5))}'",
HD_THRESHOLD, "DISK USAGE");
// in case you are checking "/"
// $disk = item_check("df -h | awk '/".HD_MOUNT."/{print substr($4, 0, length($4))}'",
// HD_THRESHOLD, "DISK USAGE");
// check connectivity
$dbconnect = "";
try {
$dbc = new PDO(DB_DSN, DB_USER, DB_PASSWORD);
} catch (PDOException $e) {
$dbconnect = "ERROR CONNECTING TO DATABASE";
}
// check MySQL cpu usage
$dbcpu = item_check("top -b -n 1 | awk '/mysql/ {print $9}' | sort -nr | head -1",
DB_CPU_THRESHOLD, "CPU USAGE BY DATABASE");
$status = build_status(array($swap, $loadavg, $disk, $dbconnect, $dbcpu));
$responsetime = round(1000*(microtime(true) - $start), 3);
// save errorlog to sqlite db
if("OK" !== $status) {
$errdb = new PDO(ERRORLOG_DSN);
$sql = "insert into errors values(".time().",'$status', $responsetime);";
$errdb->exec($sql);
}
$xml = <<<EOS
<?xml version="1.0" encoding="UTF-8"?>
<pingdom_http_custom_check>
<status>$status</status>
<response_time>$responsetime</response_time>
</pingdom_http_custom_check>
EOS;
header('Content-Type: application/xml');
echo $xml;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment