healthchecks.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ),
  66. )
  67. );
  68. return array_merge($homepageInformation, $homepageSettings);
  69. }
  70. public function healthChecksHomepagePermissions($key = null)
  71. {
  72. $permissions = [
  73. 'main' => [
  74. 'enabled' => [
  75. 'homepageHealthChecksEnabled'
  76. ],
  77. 'auth' => [
  78. 'homepageHealthChecksAuth'
  79. ],
  80. 'not_empty' => [
  81. 'healthChecksURL',
  82. 'healthChecksToken'
  83. ]
  84. ]
  85. ];
  86. if (array_key_exists($key, $permissions)) {
  87. return $permissions[$key];
  88. } elseif ($key == 'all') {
  89. return $permissions;
  90. } else {
  91. return [];
  92. }
  93. }
  94. public function homepageOrderhealthchecks()
  95. {
  96. if ($this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'))) {
  97. return '
  98. <div id="' . __FUNCTION__ . '">
  99. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Health Checks...</h2></div>
  100. <script>
  101. // Health Checks
  102. homepageHealthChecks("' . $this->config['healthChecksTags'] . '","' . $this->config['homepageHealthChecksRefresh'] . '");
  103. // End Health Checks
  104. </script>
  105. </div>
  106. ';
  107. }
  108. }
  109. public function getHealthChecks($tags = null)
  110. {
  111. if (!$this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'), true)) {
  112. return false;
  113. }
  114. $api['content']['checks'] = array();
  115. $tags = ($tags) ? $this->healthChecksTags($tags) : '';
  116. $healthChecks = explode(',', $this->config['healthChecksToken']);
  117. foreach ($healthChecks as $token) {
  118. $url = $this->qualifyURL($this->config['healthChecksURL']) . '/' . $tags;
  119. try {
  120. $headers = array('X-Api-Key' => $token);
  121. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  122. $response = Requests::get($url, $headers, $options);
  123. if ($response->success) {
  124. $healthResults = json_decode($response->body, true);
  125. $api['content']['checks'] = array_merge($api['content']['checks'], $healthResults['checks']);
  126. }
  127. } catch (Requests_Exception $e) {
  128. $this->writeLog('error', 'HealthChecks Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  129. };
  130. }
  131. usort($api['content']['checks'], function ($a, $b) {
  132. return $a['status'] <=> $b['status'];
  133. });
  134. $api['content']['checks'] = isset($api['content']['checks']) ? $api['content']['checks'] : false;
  135. $this->setAPIResponse('success', null, 200, $api);
  136. return $api;
  137. }
  138. public function healthChecksTags($tags)
  139. {
  140. $return = '?tag=';
  141. if (!$tags) {
  142. return '';
  143. } elseif ($tags == '*') {
  144. return '';
  145. } else {
  146. if (strpos($tags, ',') !== false) {
  147. $list = explode(',', $tags);
  148. return $return . implode("&tag=", $list);
  149. } else {
  150. return $return . $tags;
  151. }
  152. }
  153. }
  154. }