Advertisement
Guest User

Untitled

a guest
May 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. <?php
  2.  
  3. // SOURCE: https://github.com/nerdbaggy/StatusPage/issues/44
  4.  
  5. namespace NerdBaggy\StatusPage;
  6.  
  7. class statusPage
  8. {
  9.  
  10. public function getChecks($action = null)
  11. {
  12. $cache = phpFastCache();
  13.  
  14. $allChecks = $cache->get('statuspage-allChecks');
  15. if ($allChecks === null) {
  16.  
  17. if ($action === 'update'){
  18. $this->updateCache(true);
  19. }else{
  20. $this->updateCache(false);
  21. }
  22. $allChecks = $cache->get('statuspage-allChecks');
  23. }
  24.  
  25. $needsUpdated = false;
  26. if(count($allChecks)){
  27. foreach ($allChecks as $key => $cid) {
  28. $allCheckInfo[] = $cache->get('statuspage-' . $cid);
  29. if (count($allCheckInfo[0]['logs']) === 0 && $action === 'update'){
  30. $needsUpdated = true;
  31. unset($allCheckInfo);
  32. break;
  33. }
  34. }
  35. }
  36.  
  37. if ($needsUpdated){
  38. $this->updateCache(true);
  39.  
  40. foreach ($allChecks as $key => $cid) {
  41. $allCheckInfo[] = $cache->get('statuspage-' . $cid);
  42. }
  43. }
  44. return $allCheckInfo;
  45. }
  46.  
  47. public function updateCache($action)
  48. {
  49. date_default_timezone_set("UTC");
  50. $cache = phpFastCache();
  51. $checksArray = $this->getChecksJson($action);
  52. $excludedMonitors = unserialize(constant('excludedMonitors'));
  53.  
  54. if(count($checksArray['monitors'])){
  55. foreach ($checksArray['monitors'] as $key => $check) {
  56. if (!in_array($check['id'], $excludedMonitors)) {
  57.  
  58.  
  59. $allCheckID[] = $check['id'];
  60. $fixedResponseTimes = array();
  61. $fixedEventTime = array();
  62.  
  63. if (is_array($check['response_times'])) {
  64.  
  65. foreach ($check['response_times'] as $key => $restime) {
  66. $fixedResponseTimes[] = array(
  67. 'datetime' => date("Y-m-d G:i:s", $restime['datetime']),
  68. 'value' => $restime['value']
  69. );
  70. }
  71.  
  72. }
  73.  
  74. if (!is_null($check['logs'])){
  75.  
  76. foreach ($check['logs'] as $key => $dt) {
  77. $fixedEventTime[] = array(
  78. 'actualTime' => date("m/d/Y G:i:s", $dt['datetime']),
  79. 'type' => $dt['type'],
  80. 'datetime' => $dt['datetime'],
  81. 'duration' => intval($dt['duration'])
  82. );
  83. }
  84.  
  85. }
  86.  
  87.  
  88. $tempCheck = array(
  89. 'id' => $check['id'],
  90. 'name' => html_entity_decode($check['friendly_name']),
  91. 'type' => $check['type'],
  92. 'interval' => $check['interval'],
  93. 'status' => $check['status'],
  94. 'allUpTimeRatio' => $check['all_time_uptime_ratio'],
  95. 'customUptimeRatio' => explode("-", $check['custom_uptime_ratio']),
  96. 'log' => $fixedEventTime,
  97. 'responseTime' => $fixedResponseTimes,
  98. 'timezone' => intval($checksArray['timezone']),
  99. 'currentTime' => time() + (intval($checksArray['timezone']))*60
  100. );
  101. $cache->set('statuspage-' . $check['id'], $tempCheck, constant('cacheTime'));
  102. }
  103. }
  104. }
  105. $cache->set('statuspage-allChecks', $allCheckID, constant('cacheTime'));
  106. }
  107.  
  108. public function getTableHeaders()
  109. {
  110. foreach (unserialize(constant('historyDaysNames')) as $key => $historyDaysName) {
  111. $headToSend[] = $historyDaysName;
  112. }
  113. $headToSend[] = 'Total';
  114. return $headToSend;
  115. }
  116.  
  117. public function padIt($checks)
  118. {
  119. return 'StatusPage(' . json_encode($checks) . ')';
  120. }
  121.  
  122. private function getChecksJson($action)
  123. {
  124. $apiKey = constant('apiKey');
  125. $historyDay = constant('historyDay');
  126.  
  127. // $url = "https://api.uptimerobot.com/getMonitors?apikey=$apiKey&format=json&noJsonCallback=1&customUptimeRatio=$historyDay";
  128. $url = "https://api.uptimerobot.com/v2/getMonitors";
  129. $fields = "api_key=$apiKey&format=json&custom_uptime_ratios=$historyDay&all_time_uptime_ratio=1";
  130.  
  131. if ($action){
  132.  
  133. $fields .= '&logs=1&logs_limit=20&response_times=1&response_times_average=30&timezone=1';
  134. }
  135.  
  136. if (constant('includedMonitors') != '') {
  137. $monitors = constant('includedMonitors');
  138. $fields .= "&monitors=$monitors";
  139. }
  140.  
  141. if (constant('searchMonitors') != '') {
  142. $search = constant('searchMonitors');
  143. $fields .= "&search=$search";
  144. }
  145.  
  146. $curl = curl_init();
  147. /*curl_setopt_array($curl, array(
  148. CURLOPT_RETURNTRANSFER => 1,
  149. CURLOPT_URL => $url,
  150. CURLOPT_USERAGENT => 'UptimeRobot Public Status Page',
  151. CURLOPT_CONNECTTIMEOUT => 10
  152. ));*/
  153. curl_setopt_array($curl, array(
  154. CURLOPT_URL => $url,
  155. CURLOPT_RETURNTRANSFER => true,
  156. CURLOPT_ENCODING => "",
  157. CURLOPT_MAXREDIRS => 10,
  158. CURLOPT_TIMEOUT => 30,
  159. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  160. CURLOPT_CUSTOMREQUEST => "POST",
  161. CURLOPT_POSTFIELDS => $fields, //"api_key=enterYourAPIKeyHere&format=json&logs=1",
  162. CURLOPT_HTTPHEADER => array(
  163. "cache-control: no-cache",
  164. "content-type: application/x-www-form-urlencoded"
  165. ),
  166. ));
  167. $checks = json_decode(curl_exec($curl), TRUE);
  168. //Checks to make sure curl is happy
  169. if (curl_errno($curl)) {
  170. return False;
  171. }
  172. curl_close($curl);
  173. //Checks to make sure UptimeRobot didn't return any errors
  174. if ($checks['stat'] != 'ok') {
  175. error_log('UptimeRobot API Error - ' . $checks['message']);
  176. return False;
  177. }
  178. return $checks;
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement