plugin.php 8.5 KB

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