Skip to content

Instantly share code, notes, and snippets.

@furious
Last active February 1, 2022 13:58
Show Gist options
  • Save furious/a5868a7d7bc79e798eca74fbdf279ea9 to your computer and use it in GitHub Desktop.
Save furious/a5868a7d7bc79e798eca74fbdf279ea9 to your computer and use it in GitHub Desktop.
OVH - Check Server Availability Tool
<?php
# ovh - check server availability tool
# author: furious
# crontab (every 5 min): */5 * * * * php -f /path/to/checkserver.php
/* CONFIG */
define('EMAIL', '[email protected]');
define('SERVERS', array('1801armada01', '1801sys12'));
define('OVH_CHECK_URL', 'http://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2');
define('LAST_CHECK_FILE', dirname(__FILE__). '/checkcache.json');
if($json = file_get_contents(LAST_CHECK_FILE)){
$cache = json_decode($json);
} else {
$cache = (object) array();
}
if($json = json_decode(file_get_contents(OVH_CHECK_URL)) and $available = $json->answer->availability){
$notify = false;
foreach ($available as $server){
$servername = $server->reference;
if(in_array($servername, SERVERS)){
$zones = array();
foreach ($server->zones as $z){
$status = $z->availability == "unavailable" ? "unavailable" : "available";
if($status != 'unavailable' and (!isset($cache->{$servername}) or in_array($z->zone, $cache->{$servername}->unavailable))){
$notify = true;
}
$zones[$status][] = $z->zone;
}
$msg = "Server: {$servername}\n";
foreach ($zones as $status => $z) $msg .= "{$status}: " . implode(', ', $z) . "\n";
echo $msg;
$cache->{$servername} = (object) $zones;
$cache->{$servername}->message = $msg;
}
}
file_put_contents(LAST_CHECK_FILE, json_encode($cache));
if(defined('EMAIL') and $notify){
$msg = array();
foreach ($cache as $server => $info) {
$msg[] = $info->message;
}
mail(EMAIL, date('[d/m/Y H:i:s]') . " | OVH - Server Availability", implode("\n\n", $msg));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment