#!/usr/local/bin/php
<?php
define('API_TOKEN', 'YOUR_API_TOKEN');
define("DOMAIN", 'YOUR_DOMAIN');
define("DOMAIN_ID", 'YOUR_DOMAINID');
$validServers = array(
't1' => 'A-t1-p.yourdomain.com',
't2' => 'A-t2-p.yourdomain.com',
't3' => 'A-t3-p.yourdomain.com',
't4' => 'A-t4-p.yourdomain.com',
't5' => 'A-t5-p.yourdomain.com',
't6' => 'A-t6-p.yourdomain.com',
't7' => 'A-t7-p.yourdomain.com',
't8' => 'A-t8-p.yourdomain.com',
);
function isSiteUp()
{
// bypass CloudFlare cache
$ch = curl_init('https://www.yourdomain.com/robots.txt?' . time());
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
);
curl_setopt_array($ch, $options);
$results = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return (@$info['http_code'] == 200);
}
function getValidServer()
{
global $validServers, $dns;
foreach ($validServers as $server)
{
// Don't count current server
if ($dns['A-primarycname.yourdomain.com']['content'] == $dns[$server]['content'])
{
continue;
}
$ch = curl_init('https://' . $dns[$server]['content'] . '/robots.txt?' . time());
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTPHEADER => array('Host: www.yourdomain.com'),
);
curl_setopt_array($ch, $options);
$results = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (@$info['http_code'])
{
return $server;
}
}
}
function _execute($endPoint, $fields, $method = 'GET')
{
// $fields['tkn'] = $apiKey;
// $fields['email'] = $email;
if ($method == 'GET' && $fields) {
$endPoint .= '?' . http_build_query($fields);
}
$ch = curl_init('https://api.cloudflare.com/client/v4/' . $endPoint);
if ($method == 'POST') {
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($fields)
));
}
elseif ($method == 'PUT')
{
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => json_encode($fields)
));
}
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => [
'Content-type: application/json',
// 'X-Auth-Key:' . API_KEY,
// 'X-Auth-Email: ' . EMAIL.
'Authorization: Bearer ' . API_TOKEN
]
));
$response = curl_exec($ch);
curl_close($ch);
return @json_decode($response, true);
}
/*
function _execute($fields)
{
$fields['tkn'] = API_KEY;
$fields['email'] = EMAIL;
$ch = curl_init('https://www.cloudflare.com/api_json.html');
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_SSL_VERIFYHOST => 0,
);
curl_setopt_array($ch, $options);
$results = curl_exec($ch);
curl_close($ch);
return json_decode($results);
}
*/
function getDns ()
{
$json = _execute(
'zones/' . DOMAIN_ID . '/dns_records',
[
'type' => 'A',
'per_page' => 100
]
);
$records = [];
if (!empty($json['result']))
{
foreach ($json['result'] as $record)
{
$records[$record['type'] . '-' . $record['name']] = $record;
}
}
return $records;
print_r ($json);
exit;
$json = _execute(array(
'a' => 'rec_load_all',
'z' => DOMAIN,
));
$records = array();
if (!empty($json->response->recs->objs))
{
foreach ($json->response->recs->objs as $record)
{
$records[$record->type . '-' . $record->name] = $record;
}
}
return $records;
}
function changeDns ($entry, $ip)
{
echo "\r\nswitching front-end web server to: " . $ip . "\r\n";
$json = _execute('zones/' . DOMAIN_ID . '/dns_records/' . $entry['id'], array(
'type' => $entry['type'],
'name' => $entry['name'],
'proxied' => $entry['proxied'],
'content' => $ip,
), 'PUT');
print_r ($json);
return;
}
$isSiteUp = isSiteUp();
$omitSelf = false;
if ($_SERVER['argv'][1] == 'omitself')
{
$hostname = trim(shell_exec('hostname'));
$omitSelf = true;
$dns = getDns();
if ($dns['A-primarycname.yourdomain.com']['content'] != $dns[$validServers[$hostname]]['content'])
{
echo "This is not front-end web server...\r\n";
exit;
}
unset($validServers[$hostname]);
}
if (!$isSiteUp || $omitSelf)
{
if (!$isSiteUp)
{
echo "Site Down...\r\n";
}
if (empty($dns))
{
$dns = getDns();
}
if (!$dns)
{
exit;
}
$validServer = getValidServer();
if ($validServer)
{
changeDns($dns['A-primarycname.yourdomain.com'], $dns[$validServer]['content']);
if ($omitSelf)
{
// Wait 10 seconds for Cloudflare to propagate change before shutting down Nginx
sleep(10);
}
}
else
{
echo "No Online Servers Found...\r\n";
}
}
else
{
echo "Site Up...\r\n";
}