healthChecks.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 page? true or false
  14. 'homepage' => false // Is plugin for use on homepage? true or false
  15. );
  16. class HealthChecks extends Organizr
  17. {
  18. public function _healthCheckPluginGetSettings()
  19. {
  20. return array(
  21. 'Options' => array(
  22. array(
  23. 'type' => 'select',
  24. 'name' => 'HEALTHCHECKS-Auth-include',
  25. 'label' => 'Minimum Authentication',
  26. 'value' => $this->config['HEALTHCHECKS-Auth-include'],
  27. 'options' => $this->groupSelect()
  28. ),
  29. array(
  30. 'type' => 'input',
  31. 'name' => 'HEALTHCHECKS-PingURL',
  32. 'label' => 'URL',
  33. 'value' => $this->config['HEALTHCHECKS-PingURL'],
  34. 'help' => 'URL for HealthChecks Ping',
  35. 'placeholder' => 'HealthChecks Ping URL'
  36. ),
  37. ),
  38. 'Services' => array(
  39. array(
  40. 'type' => 'arrayMultiple',
  41. 'name' => 'HEALTHCHECKS-all-items',
  42. 'label' => 'Services',
  43. 'value' => $this->config['HEALTHCHECKS-all-items']
  44. )
  45. )
  46. );
  47. }
  48. public function _healthCheckPluginTest($url)
  49. {
  50. $success = false;
  51. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 1);
  52. $headers = array('Token' => $this->config['organizrAPI']);
  53. $url = $this->qualifyURL($url);
  54. try {
  55. $response = Requests::get($url, $headers, $options);
  56. if ($response->success) {
  57. $success = true;
  58. }
  59. if ($response->status_code == 200) {
  60. $success = true;
  61. }
  62. } catch (Requests_Exception $e) {
  63. $this->writeLog('error', 'HealthChecks Plugin - Error: ' . $e->getMessage(), 'SYSTEM');
  64. return false;
  65. }
  66. return $success;
  67. }
  68. public function _healthCheckPluginUUID($uuid, $pass = false)
  69. {
  70. if (!$uuid || !$pass || $this->config['HEALTHCHECKS-PingURL'] == '') {
  71. return false;
  72. }
  73. $url = $this->qualifyURL($this->config['HEALTHCHECKS-PingURL']);
  74. $uuid = '/' . $uuid;
  75. $path = !$pass ? '/fail' : '';
  76. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  77. return Requests::get($url . $uuid . $path, [], $options);
  78. }
  79. public function _healthCheckPluginRun()
  80. {
  81. $continue = $this->config['HEALTHCHECKS-all-items'] !== '' ? $this->config['HEALTHCHECKS-all-items'] : false;
  82. if (!$continue) {
  83. $this->setAPIResponse('error', 'No items are setup', 409);
  84. }
  85. if ($continue && $this->config['HEALTHCHECKS-enabled'] && !empty($this->config['HEALTHCHECKS-PingURL']) && $this->qualifyRequest($this->config['HEALTHCHECKS-Auth-include'])) {
  86. $allItems = [];
  87. foreach ($this->config['HEALTHCHECKS-all-items'] as $k => $v) {
  88. if ($k !== false) {
  89. foreach ($v as $item) {
  90. $allItems[$k][$item['label']] = $item['value'];
  91. }
  92. }
  93. }
  94. foreach ($allItems as $k => $v) {
  95. if ($v['Enabled'] == false) {
  96. unset($allItems[$k]);
  97. }
  98. if (!$v['UUID']) {
  99. unset($allItems[$k]);
  100. }
  101. }
  102. foreach ($allItems as $k => $v) {
  103. $testLocal = $v['Internal URL'] !== '' ?? false;
  104. $testExternal = $v['External URL'] !== '' ?? false;
  105. $testBoth = ($testLocal && $testExternal) ?? false;
  106. $pass = false;
  107. if ($testLocal) {
  108. $allItems[$k]['results']['internal'] = ($this->_healthCheckPluginTest($v['Internal URL'])) ? 'Success' : 'Error';
  109. }
  110. if ($testExternal) {
  111. if (($testBoth && $allItems[$k]['results']['internal'] == 'Error') || !$testBoth) {
  112. $allItems[$k]['results']['external'] = ($this->_healthCheckPluginTest($v['External URL'])) ? 'Success' : 'Error';
  113. } else {
  114. $allItems[$k]['results']['external'] = 'Not needed';
  115. }
  116. }
  117. if ($testBoth) {
  118. if ($allItems[$k]['results']['external'] == 'Success' || $allItems[$k]['results']['internal'] == 'Success') {
  119. $pass = true;
  120. }
  121. } elseif ($testLocal) {
  122. if ($allItems[$k]['results']['internal'] == 'Success') {
  123. $pass = true;
  124. }
  125. } elseif ($testExternal) {
  126. if ($allItems[$k]['results']['external'] == 'Success') {
  127. $pass = true;
  128. }
  129. }
  130. $this->_healthCheckPluginUUID($v['UUID'], 'true');
  131. }
  132. $this->setAPIResponse('success', null, 200, $allItems);
  133. } else {
  134. $this->setAPIResponse('error', 'User does not have access', 401);
  135. }
  136. }
  137. }