Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'vendor/autoload.php';
- function redisStatus($status = null) {
- Predis\Autoloader::register();
- $client = new Predis\Client('tcp://127.0.0.1:6379', ['parameters' => ['password' => 'null']]);
- $storeStatus = $client->get('status_np');
- echo $status . PHP_EOL;
- if ($storeStatus === $status) {
- return;
- } else {
- $client->set('status_np', $status);
- slack($status, 'check');
- }
- }
- function slack($message, $channel)
- {
- $token = $_SERVER['TOKEN'];
- $ch = curl_init("https://slack.com/api/chat.postMessage");
- $data = http_build_query([
- "token" => $token,
- "channel" => $channel, //"#mychannel",
- "text" => $message, //"Hello, Foo-Bar channel message.",
- "username" => "ExternalHealthCheckBot",
- ]);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- #slack('test', 'check');
- function healthCheck() {
- $url = $_SERVER['CHECK_URL'];
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER,
- ['Content-Type: application/json']
- );
- $result = curl_exec($ch);
- if (!curl_errno($ch)) {
- switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
- case 200: #OK
- redisStatus('Server running');
- break;
- default:
- $str = json_encode(['code' => $http_code, 'response' => $result]);
- redisStatus($str);
- }
- } else {
- redisStatus(curl_error($ch));
- }
- curl_close($ch);
- }
- healthCheck();
- exit('finish'.PHP_EOL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement