healthchecks.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. trait HealthChecksHomepageItem
  3. {
  4. public function healthChecksSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'HealthChecks',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/healthchecks.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = array(
  17. 'settings' => array(
  18. 'Enable' => array(
  19. array(
  20. 'type' => 'switch',
  21. 'name' => 'homepageHealthChecksEnabled',
  22. 'label' => 'Enable',
  23. 'value' => $this->config['homepageHealthChecksEnabled']
  24. ),
  25. array(
  26. 'type' => 'select',
  27. 'name' => 'homepageHealthChecksAuth',
  28. 'label' => 'Minimum Authentication',
  29. 'value' => $this->config['homepageHealthChecksAuth'],
  30. 'options' => $this->groupOptions
  31. )
  32. ),
  33. 'Connection' => array(
  34. array(
  35. 'type' => 'input',
  36. 'name' => 'healthChecksURL',
  37. 'label' => 'URL',
  38. 'value' => $this->config['healthChecksURL'],
  39. 'help' => 'URL for HealthChecks API',
  40. 'placeholder' => 'HealthChecks API URL'
  41. ),
  42. array(
  43. 'type' => 'password-alt',
  44. 'name' => 'healthChecksToken',
  45. 'label' => 'Token',
  46. 'value' => $this->config['healthChecksToken']
  47. )
  48. ),
  49. 'Misc Options' => array(
  50. array(
  51. 'type' => 'input',
  52. 'name' => 'healthChecksTags',
  53. 'label' => 'Tags',
  54. 'value' => $this->config['healthChecksTags'],
  55. 'help' => 'Pull only checks with this tag - Blank for all',
  56. 'placeholder' => 'Multiple tags using CSV - tag1,tag2'
  57. ),
  58. array(
  59. 'type' => 'select',
  60. 'name' => 'homepageHealthChecksRefresh',
  61. 'label' => 'Refresh Seconds',
  62. 'value' => $this->config['homepageHealthChecksRefresh'],
  63. 'options' => $this->timeOptions()
  64. ),
  65. array(
  66. 'type' => 'switch',
  67. 'name' => 'homepageHealthChecksShowDesc',
  68. 'label' => 'Show Description',
  69. 'value' => $this->config['homepageHealthChecksShowDesc'],
  70. ),
  71. array(
  72. 'type' => 'switch',
  73. 'name' => 'homepageHealthChecksShowTags',
  74. 'label' => 'Show Tags',
  75. 'value' => $this->config['homepageHealthChecksShowTags'],
  76. ),
  77. ),
  78. )
  79. );
  80. return array_merge($homepageInformation, $homepageSettings);
  81. }
  82. public function healthChecksHomepagePermissions($key = null)
  83. {
  84. $permissions = [
  85. 'main' => [
  86. 'enabled' => [
  87. 'homepageHealthChecksEnabled'
  88. ],
  89. 'auth' => [
  90. 'homepageHealthChecksAuth'
  91. ],
  92. 'not_empty' => [
  93. 'healthChecksURL',
  94. 'healthChecksToken'
  95. ]
  96. ]
  97. ];
  98. if (array_key_exists($key, $permissions)) {
  99. return $permissions[$key];
  100. } elseif ($key == 'all') {
  101. return $permissions;
  102. } else {
  103. return [];
  104. }
  105. }
  106. public function homepageOrderhealthchecks()
  107. {
  108. if ($this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'))) {
  109. return '
  110. <div id="' . __FUNCTION__ . '">
  111. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Health Checks...</h2></div>
  112. <script>
  113. // Health Checks
  114. homepageHealthChecks("' . $this->config['healthChecksTags'] . '","' . $this->config['homepageHealthChecksRefresh'] . '");
  115. // End Health Checks
  116. </script>
  117. </div>
  118. ';
  119. }
  120. }
  121. public function getHealthChecks($tags = null)
  122. {
  123. if (!$this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'), true)) {
  124. return false;
  125. }
  126. $api['content']['checks'] = array();
  127. $tags = ($tags) ? $this->healthChecksTags($tags) : '';
  128. $healthChecks = explode(',', $this->config['healthChecksToken']);
  129. foreach ($healthChecks as $token) {
  130. $url = $this->qualifyURL($this->config['healthChecksURL']) . '/' . $tags;
  131. try {
  132. $headers = array('X-Api-Key' => $token);
  133. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  134. $response = Requests::get($url, $headers, $options);
  135. if ($response->success) {
  136. $healthResults = json_decode($response->body, true);
  137. $api['content']['checks'] = array_merge($api['content']['checks'], $healthResults['checks']);
  138. }
  139. } catch (Requests_Exception $e) {
  140. $this->writeLog('error', 'HealthChecks Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  141. };
  142. }
  143. usort($api['content']['checks'], function ($a, $b) {
  144. return $a['status'] <=> $b['status'];
  145. });
  146. $api['options'] = [
  147. 'desc' => $this->config['homepageHealthChecksShowDesc'],
  148. 'tags' => $this->config['homepageHealthChecksShowTags'],
  149. ];
  150. $api['content']['checks'] = isset($api['content']['checks']) ? $api['content']['checks'] : false;
  151. $this->setAPIResponse('success', null, 200, $api);
  152. return $api;
  153. }
  154. public function healthChecksTags($tags)
  155. {
  156. $return = '?tag=';
  157. if (!$tags) {
  158. return '';
  159. } elseif ($tags == '*') {
  160. return '';
  161. } else {
  162. if (strpos($tags, ',') !== false) {
  163. $list = explode(',', $tags);
  164. return $return . implode("&tag=", $list);
  165. } else {
  166. return $return . $tags;
  167. }
  168. }
  169. }
  170. }