healthchecks.php 4.2 KB

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