plugin.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. 'Connection' => array(
  71. array(
  72. 'type' => 'input',
  73. 'name' => 'healthChecksURL',
  74. 'label' => 'URL',
  75. 'value' => $this->config['healthChecksURL'],
  76. 'help' => 'URL for HealthChecks API',
  77. 'placeholder' => 'HealthChecks API URL'
  78. ),
  79. array(
  80. 'type' => 'password-alt',
  81. 'name' => 'healthChecksToken',
  82. 'label' => 'Token',
  83. 'value' => $this->config['healthChecksToken']
  84. ),
  85. array(
  86. 'type' => 'html',
  87. 'label' => '',
  88. 'override' => 12,
  89. 'html' => '
  90. <div class="row">
  91. <div class="col-lg-12">
  92. <div class="panel panel-info">
  93. <div class="panel-heading">
  94. <span lang="en">ATTENTION</span>
  95. </div>
  96. <div class="panel-wrapper collapse in" aria-expanded="true">
  97. <div class="panel-body">
  98. <h4 lang="en">This is only used for the import button...</h4>
  99. <br/>
  100. <span>
  101. <span lang="en">Make sure to save before using the import button on Services tab</span>
  102. </span>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. '
  109. )
  110. ),
  111. 'Services' => array(
  112. array(
  113. 'type' => 'arrayMultiple',
  114. 'name' => 'HEALTHCHECKS-all-items',
  115. 'label' => 'Services',
  116. 'value' => $this->config['HEALTHCHECKS-all-items']
  117. )
  118. )
  119. );
  120. }
  121. public function _healthCheckPluginTest($url)
  122. {
  123. $success = false;
  124. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 10);
  125. $headers = array('Token' => $this->config['organizrAPI']);
  126. $url = $this->qualifyURL($url);
  127. try {
  128. $response = Requests::get($url, $headers, $options);
  129. if ($response->success) {
  130. $success = true;
  131. }
  132. if ($response->status_code == 200) {
  133. $success = true;
  134. }
  135. } catch (Requests_Exception $e) {
  136. $this->writeLog('error', 'HealthChecks Plugin - Error: ' . $e->getMessage(), 'SYSTEM');
  137. return false;
  138. }
  139. return $success;
  140. }
  141. public function _healthCheckPluginUUID($uuid, $pass = false)
  142. {
  143. if (!$uuid || $this->config['HEALTHCHECKS-PingURL'] == '') {
  144. return false;
  145. }
  146. $url = $this->qualifyURL($this->config['HEALTHCHECKS-PingURL']);
  147. $uuid = '/' . $uuid;
  148. $path = !$pass ? '/fail' : '';
  149. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  150. return Requests::get($url . $uuid . $path, [], $options);
  151. }
  152. public function _healthCheckPluginRun()
  153. {
  154. $continue = $this->config['HEALTHCHECKS-all-items'] !== '' ? $this->config['HEALTHCHECKS-all-items'] : false;
  155. if (!$continue) {
  156. $this->setAPIResponse('error', 'No items are setup', 409);
  157. }
  158. if ($continue && $this->config['HEALTHCHECKS-enabled'] && !empty($this->config['HEALTHCHECKS-PingURL']) && $this->qualifyRequest($this->config['HEALTHCHECKS-Auth-include'])) {
  159. $allItems = [];
  160. foreach ($this->config['HEALTHCHECKS-all-items'] as $k => $v) {
  161. if ($k !== false) {
  162. foreach ($v as $item) {
  163. $allItems[$k][$item['label']] = $item['value'];
  164. }
  165. }
  166. }
  167. foreach ($allItems as $k => $v) {
  168. if ($v['Enabled'] == false) {
  169. unset($allItems[$k]);
  170. }
  171. if (!$v['UUID']) {
  172. unset($allItems[$k]);
  173. }
  174. }
  175. $limit = 30;
  176. if (!empty($allItems)) {
  177. $limit = count($allItems) * 20;
  178. }
  179. set_time_limit($limit);
  180. foreach ($allItems as $k => $v) {
  181. $testLocal = $v['Internal URL'] !== '' ?? false;
  182. $testExternal = $v['External URL'] !== '' ?? false;
  183. $testBoth = ($testLocal && $testExternal) ?? false;
  184. $pass = false;
  185. if ($testLocal) {
  186. $allItems[$k]['results']['internal'] = ($this->_healthCheckPluginTest($v['Internal URL'])) ? 'Success' : 'Error';
  187. }
  188. if ($testExternal) {
  189. if (($testBoth && $allItems[$k]['results']['internal'] == 'Error') || !$testBoth) {
  190. $allItems[$k]['results']['external'] = ($this->_healthCheckPluginTest($v['External URL'])) ? 'Success' : 'Error';
  191. } else {
  192. $allItems[$k]['results']['external'] = 'Not needed';
  193. }
  194. }
  195. if ($testBoth) {
  196. if ($allItems[$k]['results']['external'] == 'Success' || $allItems[$k]['results']['internal'] == 'Success') {
  197. $pass = true;
  198. }
  199. } elseif ($testLocal) {
  200. if ($allItems[$k]['results']['internal'] == 'Success') {
  201. $pass = true;
  202. }
  203. } elseif ($testExternal) {
  204. if ($allItems[$k]['results']['external'] == 'Success') {
  205. $pass = true;
  206. }
  207. }
  208. $this->_healthCheckPluginUUID($v['UUID'], $pass);
  209. }
  210. $this->setAPIResponse('success', null, 200, $allItems);
  211. } else {
  212. $this->setAPIResponse('error', 'User does not have access', 401);
  213. }
  214. }
  215. }