healthchecks.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageHealthChecksEnabled'),
  21. $this->settingsOption('auth', 'homepageHealthChecksAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'healthChecksURL'),
  25. $this->settingsOption('multiple-token', 'healthChecksToken'),
  26. $this->settingsOption('disable-cert-check', 'healthChecksDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'healthChecksUseCustomCertificate'),
  28. ],
  29. 'Misc Options' => [
  30. $this->settingsOption('multiple', 'healthChecksTags', ['label' => 'Tags', 'help' => 'Pull only checks with this tag - Blank for all', 'placeholder' => 'Multiple tags using CSV - tag1,tag2']),
  31. $this->settingsOption('refresh', 'homepageHealthChecksRefresh'),
  32. $this->settingsOption('switch', 'homepageHealthChecksShowDesc', ['label' => 'Show Description']),
  33. $this->settingsOption('switch', 'homepageHealthChecksShowTags', ['label' => 'Show Tags']),
  34. ],
  35. ]
  36. ];
  37. return array_merge($homepageInformation, $homepageSettings);
  38. }
  39. public function healthChecksHomepagePermissions($key = null)
  40. {
  41. $permissions = [
  42. 'main' => [
  43. 'enabled' => [
  44. 'homepageHealthChecksEnabled'
  45. ],
  46. 'auth' => [
  47. 'homepageHealthChecksAuth'
  48. ],
  49. 'not_empty' => [
  50. 'healthChecksURL',
  51. 'healthChecksToken'
  52. ]
  53. ]
  54. ];
  55. return $this->homepageCheckKeyPermissions($key, $permissions);
  56. }
  57. public function homepageOrderhealthchecks()
  58. {
  59. if ($this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'))) {
  60. return '
  61. <div id="' . __FUNCTION__ . '">
  62. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Health Checks...</h2></div>
  63. <script>
  64. // Health Checks
  65. homepageHealthChecks("' . $this->config['healthChecksTags'] . '","' . $this->config['homepageHealthChecksRefresh'] . '");
  66. // End Health Checks
  67. </script>
  68. </div>
  69. ';
  70. }
  71. }
  72. public function getHealthChecks($tags = null)
  73. {
  74. if (!$this->homepageItemPermissions($this->healthChecksHomepagePermissions('main'), true)) {
  75. return false;
  76. }
  77. $api['content']['checks'] = array();
  78. $tags = ($tags) ? $this->healthChecksTags($tags) : '';
  79. $healthChecks = explode(',', $this->config['healthChecksToken']);
  80. foreach ($healthChecks as $token) {
  81. $url = $this->qualifyURL($this->config['healthChecksURL']) . '/' . $tags;
  82. try {
  83. $headers = array('X-Api-Key' => $token);
  84. $options = $this->requestOptions($url, $this->config['homepageHealthChecksRefresh'], $this->config['healthChecksDisableCertCheck'], $this->config['healthChecksUseCustomCertificate']);
  85. $response = Requests::get($url, $headers, $options);
  86. if ($response->success) {
  87. $healthResults = json_decode($response->body, true);
  88. $api['content']['checks'] = array_merge($api['content']['checks'], $healthResults['checks']);
  89. }
  90. } catch (Requests_Exception $e) {
  91. $this->writeLog('error', 'HealthChecks Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  92. };
  93. }
  94. usort($api['content']['checks'], function ($a, $b) {
  95. return $a['status'] <=> $b['status'];
  96. });
  97. $api['options'] = [
  98. 'desc' => $this->config['homepageHealthChecksShowDesc'],
  99. 'tags' => $this->config['homepageHealthChecksShowTags'],
  100. ];
  101. $api['content']['checks'] = isset($api['content']['checks']) ? $api['content']['checks'] : false;
  102. $this->setAPIResponse('success', null, 200, $api);
  103. return $api;
  104. }
  105. public function healthChecksTags($tags)
  106. {
  107. $return = '?tag=';
  108. if (!$tags) {
  109. return '';
  110. } elseif ($tags == '*') {
  111. return '';
  112. } else {
  113. if (strpos($tags, ',') !== false) {
  114. $list = explode(',', $tags);
  115. return $return . implode("&tag=", $list);
  116. } else {
  117. return $return . $tags;
  118. }
  119. }
  120. }
  121. }