Skip to content

Instantly share code, notes, and snippets.

@jk
Last active December 23, 2015 12:19
Show Gist options
  • Select an option

  • Save jk/6634220 to your computer and use it in GitHub Desktop.

Select an option

Save jk/6634220 to your computer and use it in GitHub Desktop.
A script to check if a count.ly instance is still running. Was written for a monit task/job.
#!/bin/php
<?php
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Create a handler function
function my_assert_handler($file, $line, $code)
{
header('500 FAILED');
echo '500 FAILED',PHP_EOL;
echo "Line $line",PHP_EOL,"Condition: ".$code,PHP_EOL;
die();
}
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
$urls[] = 'https://countly.example.com/i'; // Write API
$urls[] = 'https://countly.example.com/o'; // Read API
ob_start();
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // accept all SSL certs
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
assert('$http_status == 400', 'HTTP status differs from the expected 400');
assert('strpos($response, \'Missing parameter\') > 0', "Can't find the phrase \"Missing parameter\" in the response");
$json = json_decode($response);
assert('json_last_error() == JSON_ERROR_NONE', 'Can\'t parse response into JSON');
}
header("200 SUCCESSFUL");
echo "all_systems_running",PHP_EOL;
ob_flush();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment