healthchecks.php 4.1 KB

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