Advertisement
aivavic

ping

Jan 28th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. require 'vendor/autoload.php';
  2. function redisStatus($status = null) {
  3. Predis\Autoloader::register();
  4. $client = new Predis\Client('tcp://127.0.0.1:6379', ['parameters' => ['password' => 'null']]);
  5. $storeStatus = $client->get('status_np');
  6. echo $status . PHP_EOL;
  7. if ($storeStatus === $status) {
  8. return;
  9. } else {
  10. $client->set('status_np', $status);
  11. slack($status, 'check');
  12. }
  13. }
  14.  
  15. function slack($message, $channel)
  16. {
  17. $token = $_SERVER['TOKEN'];
  18. $ch = curl_init("https://slack.com/api/chat.postMessage");
  19. $data = http_build_query([
  20. "token" => $token,
  21. "channel" => $channel, //"#mychannel",
  22. "text" => $message, //"Hello, Foo-Bar channel message.",
  23. "username" => "ExternalHealthCheckBot",
  24. ]);
  25. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  26. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  28. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  29. $result = curl_exec($ch);
  30. curl_close($ch);
  31.  
  32. return $result;
  33. }
  34. #slack('test', 'check');
  35. function healthCheck() {
  36. $url = $_SERVER['CHECK_URL'];
  37. $ch = curl_init($url);
  38. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  39. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  41. curl_setopt($ch, CURLOPT_HTTPHEADER,
  42. ['Content-Type: application/json']
  43. );
  44. $result = curl_exec($ch);
  45.  
  46.  
  47. if (!curl_errno($ch)) {
  48. switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
  49. case 200: #OK
  50. redisStatus('Server running');
  51. break;
  52. default:
  53. $str = json_encode(['code' => $http_code, 'response' => $result]);
  54. redisStatus($str);
  55. }
  56. } else {
  57. redisStatus(curl_error($ch));
  58. }
  59. curl_close($ch);
  60. }
  61.  
  62. healthCheck();
  63. exit('finish'.PHP_EOL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement