4
0

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