Skip to content

Instantly share code, notes, and snippets.

@ptz0n
Created August 8, 2012 20:05
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 ptz0n/3298107 to your computer and use it in GitHub Desktop.
Save ptz0n/3298107 to your computer and use it in GitHub Desktop.
Pingdom last down check
<?php
$apiKey = 'foo123bar';
$username = 'you@domain.com';
$password = 'yourpassword';
$check = 123;
// Init cURL
$curl = curl_init();
// Set target URL
curl_setopt($curl, CURLOPT_URL, 'https://api.pingdom.com/api/2.0/checks/'.$check);
// Set the desired HTTP method (GET is default, see the documentation for each request)
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
// Set user (email) and password
curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password);
// Add a http header containing the application key (see the Authentication section of this document)
curl_setopt($curl, CURLOPT_HTTPHEADER, array('App-Key: '.$apiKey));
// Ask cURL to return the result as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute the request and decode the json result into an associative array
$response = json_decode(curl_exec($curl),true);
// Check for errors returned by the API
if (isset($response['error'])) {
print 'Error: ' . $response['error']['errormessage'] . "\n";
exit;
}
// Fetch the list of checks from the response
$check = $response['check'];
echo $check['name'], ' was down ';
echo duration(time()-$check['lasterrortime']), ' ago.';
// Seconds to readable
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment