plugin.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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' => 'api/plugins/healthChecks/logo.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. 'FYI' => array(
  24. array(
  25. 'type' => 'html',
  26. 'label' => '',
  27. 'override' => 12,
  28. 'html' => '
  29. <div class="row">
  30. <div class="col-lg-12">
  31. <div class="panel panel-info">
  32. <div class="panel-heading">
  33. <span lang="en">ATTENTION</span>
  34. </div>
  35. <div class="panel-wrapper collapse in" aria-expanded="true">
  36. <div class="panel-body">
  37. <h4 lang="en">Once this plugin is setup, you will need to setup a CRON job</h4>
  38. <br/>
  39. <span>
  40. <h4><b lang="en">CRON Job URL</b></h4>
  41. <code>' . $this->getServerPath() . 'api/v2/plugins/healthchecks/run</code><br/>
  42. <h5><b lang="en">Frequency</b></h5>
  43. <span lang="en">As often as you like - i.e. every 1 minute</span>
  44. </span>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. '
  51. )
  52. ),
  53. 'Options' => array(
  54. array(
  55. 'type' => 'select',
  56. 'name' => 'HEALTHCHECKS-Auth-include',
  57. 'label' => 'Minimum Authentication',
  58. 'value' => $this->config['HEALTHCHECKS-Auth-include'],
  59. 'options' => $this->groupSelect()
  60. ),
  61. array(
  62. 'type' => 'input',
  63. 'name' => 'HEALTHCHECKS-PingURL',
  64. 'label' => 'URL',
  65. 'value' => $this->config['HEALTHCHECKS-PingURL'],
  66. 'help' => 'URL for HealthChecks Ping',
  67. 'placeholder' => 'HealthChecks Ping URL'
  68. ),
  69. ),
  70. 'Services' => array(
  71. array(
  72. 'type' => 'arrayMultiple',
  73. 'name' => 'HEALTHCHECKS-all-items',
  74. 'label' => 'Services',
  75. 'value' => $this->config['HEALTHCHECKS-all-items']
  76. )
  77. )
  78. );
  79. }
  80. public function _healthCheckPluginTest($url)
  81. {
  82. $success = false;
  83. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 10);
  84. $headers = array('Token' => $this->config['organizrAPI']);
  85. $url = $this->qualifyURL($url);
  86. try {
  87. $response = Requests::get($url, $headers, $options);
  88. if ($response->success) {
  89. $success = true;
  90. }
  91. if ($response->status_code == 200) {
  92. $success = true;
  93. }
  94. } catch (Requests_Exception $e) {
  95. $this->writeLog('error', 'HealthChecks Plugin - Error: ' . $e->getMessage(), 'SYSTEM');
  96. return false;
  97. }
  98. return $success;
  99. }
  100. public function _healthCheckPluginUUID($uuid, $pass = false)
  101. {
  102. if (!$uuid || $this->config['HEALTHCHECKS-PingURL'] == '') {
  103. return false;
  104. }
  105. $url = $this->qualifyURL($this->config['HEALTHCHECKS-PingURL']);
  106. $uuid = '/' . $uuid;
  107. $path = !$pass ? '/fail' : '';
  108. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  109. return Requests::get($url . $uuid . $path, [], $options);
  110. }
  111. public function _healthCheckPluginRun()
  112. {
  113. $continue = $this->config['HEALTHCHECKS-all-items'] !== '' ? $this->config['HEALTHCHECKS-all-items'] : false;
  114. if (!$continue) {
  115. $this->setAPIResponse('error', 'No items are setup', 409);
  116. }
  117. if ($continue && $this->config['HEALTHCHECKS-enabled'] && !empty($this->config['HEALTHCHECKS-PingURL']) && $this->qualifyRequest($this->config['HEALTHCHECKS-Auth-include'])) {
  118. $allItems = [];
  119. foreach ($this->config['HEALTHCHECKS-all-items'] as $k => $v) {
  120. if ($k !== false) {
  121. foreach ($v as $item) {
  122. $allItems[$k][$item['label']] = $item['value'];
  123. }
  124. }
  125. }
  126. foreach ($allItems as $k => $v) {
  127. if ($v['Enabled'] == false) {
  128. unset($allItems[$k]);
  129. }
  130. if (!$v['UUID']) {
  131. unset($allItems[$k]);
  132. }
  133. }
  134. $limit = 30;
  135. if (!empty($allItems)) {
  136. $limit = count($allItems) * 20;
  137. }
  138. set_time_limit($limit);
  139. foreach ($allItems as $k => $v) {
  140. $testLocal = $v['Internal URL'] !== '' ?? false;
  141. $testExternal = $v['External URL'] !== '' ?? false;
  142. $testBoth = ($testLocal && $testExternal) ?? false;
  143. $pass = false;
  144. if ($testLocal) {
  145. $allItems[$k]['results']['internal'] = ($this->_healthCheckPluginTest($v['Internal URL'])) ? 'Success' : 'Error';
  146. }
  147. if ($testExternal) {
  148. if (($testBoth && $allItems[$k]['results']['internal'] == 'Error') || !$testBoth) {
  149. $allItems[$k]['results']['external'] = ($this->_healthCheckPluginTest($v['External URL'])) ? 'Success' : 'Error';
  150. } else {
  151. $allItems[$k]['results']['external'] = 'Not needed';
  152. }
  153. }
  154. if ($testBoth) {
  155. if ($allItems[$k]['results']['external'] == 'Success' || $allItems[$k]['results']['internal'] == 'Success') {
  156. $pass = true;
  157. }
  158. } elseif ($testLocal) {
  159. if ($allItems[$k]['results']['internal'] == 'Success') {
  160. $pass = true;
  161. }
  162. } elseif ($testExternal) {
  163. if ($allItems[$k]['results']['external'] == 'Success') {
  164. $pass = true;
  165. }
  166. }
  167. $this->_healthCheckPluginUUID($v['UUID'], $pass);
  168. }
  169. $this->setAPIResponse('success', null, 200, $allItems);
  170. } else {
  171. $this->setAPIResponse('error', 'User does not have access', 401);
  172. }
  173. }
  174. }