4
0

plugin.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. array(
  70. 'type' => 'switch',
  71. 'name' => 'HEALTHCHECKS-401-enabled',
  72. 'label' => '401 Error as Success',
  73. 'value' => $this->config['HEALTHCHECKS-401-enabled']
  74. ),
  75. array(
  76. 'type' => 'switch',
  77. 'name' => 'HEALTHCHECKS-403-enabled',
  78. 'label' => '403 Error as Success',
  79. 'value' => $this->config['HEALTHCHECKS-403-enabled']
  80. ),
  81. ),
  82. 'Connection' => array(
  83. array(
  84. 'type' => 'input',
  85. 'name' => 'healthChecksURL',
  86. 'label' => 'URL',
  87. 'value' => $this->config['healthChecksURL'],
  88. 'help' => 'URL for HealthChecks API',
  89. 'placeholder' => 'HealthChecks API URL'
  90. ),
  91. array(
  92. 'type' => 'password-alt',
  93. 'name' => 'healthChecksToken',
  94. 'label' => 'Token',
  95. 'value' => $this->config['healthChecksToken']
  96. ),
  97. array(
  98. 'type' => 'html',
  99. 'label' => '',
  100. 'override' => 12,
  101. 'html' => '
  102. <div class="row">
  103. <div class="col-lg-12">
  104. <div class="panel panel-danger">
  105. <div class="panel-heading">
  106. <span lang="en">ATTENTION</span>
  107. </div>
  108. <div class="panel-wrapper collapse in" aria-expanded="true">
  109. <div class="panel-body">
  110. <h4 lang="en">Please use a Full Access Token</h4>
  111. <br/>
  112. <div>
  113. <p lang="en">Do not use a Read-Only Token as that will not give a correct UUID for sending the results to HealthChecks.io</p>
  114. <p lang="en">Make sure to save before using the import button on Services tab</p>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121. '
  122. )
  123. ),
  124. 'Services' => array(
  125. array(
  126. 'type' => 'arrayMultiple',
  127. 'name' => 'HEALTHCHECKS-all-items',
  128. 'label' => 'Services',
  129. 'value' => $this->config['HEALTHCHECKS-all-items']
  130. )
  131. )
  132. );
  133. }
  134. public function _healthCheckPluginTest($url)
  135. {
  136. $success = false;
  137. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 10, 'timeout' => 60);
  138. $headers = array('Token' => $this->config['organizrAPI']);
  139. $url = $this->qualifyURL($url);
  140. try {
  141. $response = Requests::get($url, $headers, $options);
  142. if ($response->success) {
  143. $success = true;
  144. }
  145. if ($response->status_code == 200) {
  146. $success = true;
  147. }
  148. if ($this->config['HEALTHCHECKS-401-enabled']) {
  149. if ($response->status_code == 401) {
  150. $success = true;
  151. }
  152. }
  153. if ($this->config['HEALTHCHECKS-403-enabled']) {
  154. if ($response->status_code == 403) {
  155. $success = true;
  156. }
  157. }
  158. } catch (Requests_Exception $e) {
  159. $this->writeLog('error', 'HealthChecks Plugin - Error: ' . $e->getMessage(), 'SYSTEM');
  160. return false;
  161. }
  162. return $success;
  163. }
  164. public function _healthCheckSelfHostedURLValidation($url, $checkOnly = false)
  165. {
  166. $selfHosted = true;
  167. $url = $this->qualifyURL($url);
  168. if (stripos($url, 'hc-ping.com') == false) {
  169. if (stripos($url, '/ping') == false) {
  170. $url = $url . '/ping';
  171. }
  172. } else {
  173. $selfHosted = false;
  174. }
  175. return $checkOnly ? $selfHosted : $url;
  176. }
  177. public function _healthCheckPluginStartUUID($uuid)
  178. {
  179. if (!$uuid || $this->config['HEALTHCHECKS-PingURL'] == '') {
  180. return false;
  181. }
  182. $url = $this->_healthCheckSelfHostedURLValidation($this->config['HEALTHCHECKS-PingURL']);
  183. $uuid = '/' . $uuid;
  184. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  185. return Requests::get($url . $uuid . '/start', [], $options);
  186. }
  187. public function _healthCheckPluginUUID($uuid, $pass = false)
  188. {
  189. if (!$uuid || $this->config['HEALTHCHECKS-PingURL'] == '') {
  190. return false;
  191. }
  192. $url = $this->_healthCheckSelfHostedURLValidation($this->config['HEALTHCHECKS-PingURL']);
  193. $uuid = '/' . $uuid;
  194. $path = !$pass ? '/fail' : '';
  195. $options = ($this->localURL($url)) ? array('verify' => false) : array('verify' => $this->getCert());
  196. return Requests::get($url . $uuid . $path, [], $options);
  197. }
  198. public function _healthCheckPluginRun()
  199. {
  200. $continue = $this->config['HEALTHCHECKS-all-items'] !== '' ? $this->config['HEALTHCHECKS-all-items'] : false;
  201. if (!$continue) {
  202. $this->setAPIResponse('error', 'No items are setup', 409);
  203. }
  204. if ($continue && $this->config['HEALTHCHECKS-enabled'] && !empty($this->config['HEALTHCHECKS-PingURL']) && $this->qualifyRequest($this->config['HEALTHCHECKS-Auth-include'])) {
  205. $allItems = [];
  206. foreach ($this->config['HEALTHCHECKS-all-items'] as $k => $v) {
  207. if ($k !== false) {
  208. foreach ($v as $item) {
  209. $allItems[$k][$item['label']] = $item['value'];
  210. }
  211. }
  212. }
  213. foreach ($allItems as $k => $v) {
  214. if ($v['Enabled'] == false) {
  215. unset($allItems[$k]);
  216. }
  217. if (!$v['UUID']) {
  218. unset($allItems[$k]);
  219. }
  220. }
  221. $limit = 30;
  222. if (!empty($allItems)) {
  223. $limit = count($allItems) * 20;
  224. }
  225. set_time_limit($limit);
  226. foreach ($allItems as $k => $v) {
  227. $testLocal = $v['Internal URL'] !== '' ?? false;
  228. $testExternal = $v['External URL'] !== '' ?? false;
  229. $testBoth = ($testLocal && $testExternal) ?? false;
  230. $pass = false;
  231. if ($testLocal || $testExternal || $testBoth) {
  232. $this->_healthCheckPluginStartUUID($v['UUID']);
  233. }
  234. if ($testLocal) {
  235. $allItems[$k]['results']['internal'] = ($this->_healthCheckPluginTest($v['Internal URL'])) ? 'Success' : 'Error';
  236. }
  237. if ($testExternal) {
  238. if (($testBoth && $allItems[$k]['results']['internal'] == 'Error') || !$testBoth) {
  239. $allItems[$k]['results']['external'] = ($this->_healthCheckPluginTest($v['External URL'])) ? 'Success' : 'Error';
  240. } else {
  241. $allItems[$k]['results']['external'] = 'Not needed';
  242. }
  243. }
  244. if ($testBoth) {
  245. if ($allItems[$k]['results']['external'] == 'Success' || $allItems[$k]['results']['internal'] == 'Success') {
  246. $pass = true;
  247. }
  248. } elseif ($testLocal) {
  249. if ($allItems[$k]['results']['internal'] == 'Success') {
  250. $pass = true;
  251. }
  252. } elseif ($testExternal) {
  253. if ($allItems[$k]['results']['external'] == 'Success') {
  254. $pass = true;
  255. }
  256. }
  257. $this->_healthCheckPluginUUID($v['UUID'], $pass);
  258. }
  259. $this->setAPIResponse('success', null, 200, $allItems);
  260. } else {
  261. $this->setAPIResponse('error', 'User does not have access', 401);
  262. }
  263. }
  264. }