Last active
March 24, 2017 16:31
-
-
Save kaerimichi/5dfb4e42a3ede4a705fbcde6f9683051 to your computer and use it in GitHub Desktop.
Simple URL health check queue based on HTTP status code (using PHP and cURL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$urlsFile = file_get_contents('./healthcheck.txt'); | |
$urls = split("\n", $urlsFile); | |
$filteredList = array_filter($urls, function ($entry) { | |
return !empty($entry) && substr($entry, 0, 1) != '#'; | |
}); | |
$failedRequests = (bool) array_reduce($filteredList, function ($acc, $entry) { | |
$url = split(' ', $entry)[0]; | |
$expectedCode = split(' ', $entry)[1]; | |
$statusCode = (int) exec('curl -s -o /dev/null -w "%{http_code}" ' . $url); | |
echo "{$url} >>> {$statusCode}/{$expectedCode}\n"; | |
if ($expectedCode != $statusCode) $acc++; | |
return $acc; | |
}, 0); | |
if ($failedRequests) exit(1); | |
exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the script will finish with 0 if all URL status codes have matched the expected code | |
https://google.com 200 | |
https://github.com 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment