healthChecks.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // PLUGIN INFORMATION
  3. $GLOBALS['plugins'][]['healthChecks'] = array( // Plugin Name
  4. 'name' => 'HealthChecks', // Plugin Name
  5. 'author' => 'CauseFX', // Who wrote the plugin
  6. 'category' => 'Utilities', // One to Two Word Description
  7. 'link' => '', // Link to plugin info
  8. 'license' => 'personal,business', // License Type use , for multiple
  9. 'idPrefix' => 'HEALTHCHECKS', // html element id prefix
  10. 'configPrefix' => 'HEALTHCHECKS', // config file prefix for array items without the hyphen
  11. 'version' => '1.0.0', // SemVer of plugin
  12. 'image' => 'plugins/images/healthchecksio.png', // 1:1 non transparent image for plugin
  13. 'settings' => true, // does plugin need a settings modal?
  14. 'bind' => false, // use default bind to make settings page - true or false
  15. 'api' => false, // api route for settings page
  16. 'homepage' => false // Is plugin for use on homepage? true or false
  17. );
  18. class HealthChecks extends Organizr
  19. {
  20. public function _healthCheckPluginGetSettings()
  21. {
  22. return array(
  23. 'Options' => array(
  24. array(
  25. 'type' => 'select',
  26. 'name' => 'HEALTHCHECKS-Auth-include',
  27. 'label' => 'Minimum Authentication',
  28. 'value' => $this->config['HEALTHCHECKS-Auth-include'],
  29. 'options' => $this->groupSelect()
  30. ),
  31. array(
  32. 'type' => 'input',
  33. 'name' => 'HEALTHCHECKS-PingURL',
  34. 'label' => 'URL',
  35. 'value' => $this->config['HEALTHCHECKS-PingURL'],
  36. 'help' => 'URL for HealthChecks Ping',
  37. 'placeholder' => 'HealthChecks Ping URL'
  38. ),
  39. ),
  40. 'Services' => array(
  41. array(
  42. 'type' => 'arrayMultiple',
  43. 'name' => 'HEALTHCHECKS-all-items',
  44. 'label' => 'Services',
  45. 'value' => $this->config['HEALTHCHECKS-all-items']
  46. )
  47. )
  48. );
  49. }
  50. public function _healthCheckPluginTest($url)
  51. {
  52. $success = false;
  53. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 1);
  54. $headers = array('Token' => $this->config['organizrAPI']);
  55. $url = $this->qualifyURL($url);
  56. try {
  57. $response = Requests::get($url, $headers, $options);
  58. if ($response->success) {
  59. $success = true;
  60. }
  61. if ($response->status_code == 200) {
  62. $success = true;
  63. }
  64. } catch (Requests_Exception $e) {
  65. $this->writeLog('error', 'HealthChecks Plugin - Error: ' . $e->getMessage(), 'SYSTEM');
  66. return false;
  67. }
  68. return $success;
  69. }
  70. public function _healthCheckPluginUUID($uuid, $pass = false)
  71. {
  72. if (!$uuid || !$pass || $this->config['HEALTHCHECKS-PingURL'] == '') {
  73. return false;
  74. }
  75. $url = $this->qualifyURL($this->config['HEALTHCHECKS-PingURL']);
  76. $uuid = '/' . $uuid;
  77. $path = !$pass ? '/fail' : '';
  78. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  79. return Requests::get($url . $uuid . $path, [], $options);
  80. }
  81. public function _healthCheckPluginRun()
  82. {
  83. $continue = $this->config['HEALTHCHECKS-all-items'] !== '' ? $this->config['HEALTHCHECKS-all-items'] : false;
  84. if (!$continue) {
  85. $this->setAPIResponse('error', 'No items are setup', 409);
  86. }
  87. if ($continue && $this->config['HEALTHCHECKS-enabled'] && !empty($this->config['HEALTHCHECKS-PingURL']) && $this->qualifyRequest($this->config['HEALTHCHECKS-Auth-include'])) {
  88. $allItems = [];
  89. foreach ($this->config['HEALTHCHECKS-all-items'] as $k => $v) {
  90. if ($k !== false) {
  91. foreach ($v as $item) {
  92. $allItems[$k][$item['label']] = $item['value'];
  93. }
  94. }
  95. }
  96. foreach ($allItems as $k => $v) {
  97. if ($v['Enabled'] == false) {
  98. unset($allItems[$k]);
  99. }
  100. if (!$v['UUID']) {
  101. unset($allItems[$k]);
  102. }
  103. }
  104. foreach ($allItems as $k => $v) {
  105. $testLocal = $v['Internal URL'] !== '' ?? false;
  106. $testExternal = $v['External URL'] !== '' ?? false;
  107. $testBoth = ($testLocal && $testExternal) ?? false;
  108. $pass = false;
  109. if ($testLocal) {
  110. $allItems[$k]['results']['internal'] = ($this->_healthCheckPluginTest($v['Internal URL'])) ? 'Success' : 'Error';
  111. }
  112. if ($testExternal) {
  113. if (($testBoth && $allItems[$k]['results']['internal'] == 'Error') || !$testBoth) {
  114. $allItems[$k]['results']['external'] = ($this->_healthCheckPluginTest($v['External URL'])) ? 'Success' : 'Error';
  115. } else {
  116. $allItems[$k]['results']['external'] = 'Not needed';
  117. }
  118. }
  119. if ($testBoth) {
  120. if ($allItems[$k]['results']['external'] == 'Success' || $allItems[$k]['results']['internal'] == 'Success') {
  121. $pass = true;
  122. }
  123. } elseif ($testLocal) {
  124. if ($allItems[$k]['results']['internal'] == 'Success') {
  125. $pass = true;
  126. }
  127. } elseif ($testExternal) {
  128. if ($allItems[$k]['results']['external'] == 'Success') {
  129. $pass = true;
  130. }
  131. }
  132. $this->_healthCheckPluginUUID($v['UUID'], 'true');
  133. }
  134. $this->setAPIResponse('success', null, 200, $allItems);
  135. } else {
  136. $this->setAPIResponse('error', 'User does not have access', 401);
  137. }
  138. }
  139. }