healthChecks.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 hypen
  11. 'version' => '1.0.0', // SemVer of plugin
  12. 'image' => 'plugins/images/healthchecksio.png', // 1:1 non transparent image for plugin
  13. 'settings' => true, // does plugin need a settings page? true or false
  14. 'homepage' => false // Is plugin for use on homepage? true or false
  15. );
  16. // INCLUDE/REQUIRE FILES
  17. // PLUGIN FUNCTIONS
  18. function healthCheckTest($url)
  19. {
  20. $success = false;
  21. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true, 'redirects' => 1);
  22. $headers = array('Token' => $GLOBALS['organizrAPI']);
  23. $url = qualifyURL($url);
  24. $response = Requests::get($url, $headers, $options);
  25. if ($response->success) {
  26. $success = true;
  27. }
  28. if ($response->status_code == 200) {
  29. $success = true;
  30. }
  31. return $success;
  32. }
  33. function healthCheckUUID($uuid, $pass = false)
  34. {
  35. if (!$uuid || !$pass || $GLOBALS['HEALTHCHECKS-PingURL'] == '') {
  36. return false;
  37. }
  38. $url = qualifyURL($GLOBALS['HEALTHCHECKS-PingURL']);
  39. $uuid = '/' . $uuid;
  40. $path = !$pass ? '/fail' : '';
  41. $response = Requests::get($url . $uuid . $path, [], []);
  42. return $response;
  43. }
  44. function healthCheckRun()
  45. {
  46. $continue = $GLOBALS['HEALTHCHECKS-all-items'] !== '' ? $GLOBALS['HEALTHCHECKS-all-items'] : false;
  47. if ($continue && $GLOBALS['HEALTHCHECKS-enabled'] && !empty($GLOBALS['HEALTHCHECKS-PingURL']) && qualifyRequest($GLOBALS['HEALTHCHECKS-Auth-include'])) {
  48. $allItems = [];
  49. foreach ($GLOBALS['HEALTHCHECKS-all-items'] as $k => $v) {
  50. if ($k !== false) {
  51. foreach ($v as $item) {
  52. $allItems[$k][$item['label']] = $item['value'];
  53. }
  54. }
  55. }
  56. foreach ($allItems as $k => $v) {
  57. if ($v['Enabled'] == 'false') {
  58. unset($allItems[$k]);
  59. }
  60. if (!$v['UUID']) {
  61. unset($allItems[$k]);
  62. }
  63. }
  64. foreach ($allItems as $k => $v) {
  65. $testLocal = $v['Internal URL'] !== '' ?? false;
  66. $testExternal = $v['External URL'] !== '' ?? false;
  67. $testBoth = ($testLocal && $testExternal) ?? false;
  68. $pass = false;
  69. if ($testLocal) {
  70. $allItems[$k]['results']['internal'] = (healthCheckTest($v['Internal URL'])) ? 'Success' : 'Error';
  71. }
  72. if ($testExternal) {
  73. $allItems[$k]['results']['external'] = (healthCheckTest($v['External URL'])) ? 'Success' : 'Error';
  74. }
  75. if ($testBoth) {
  76. if ($allItems[$k]['results']['external'] == 'Success' && $allItems[$k]['results']['internal'] == 'Success') {
  77. $pass = true;
  78. }
  79. } elseif ($testLocal) {
  80. if ($allItems[$k]['results']['internal'] == 'Success') {
  81. $pass = true;
  82. }
  83. } elseif ($testExternal) {
  84. if ($allItems[$k]['results']['external'] == 'Success') {
  85. $pass = true;
  86. }
  87. }
  88. healthCheckUUID($v['UUID'], 'true');
  89. }
  90. return $allItems;
  91. } else {
  92. 'No Access';
  93. }
  94. }
  95. /* GET HEALTHCHECK SETTINGS */
  96. function healthCheckGetSettings()
  97. {
  98. return array(
  99. 'Options' => array(
  100. array(
  101. 'type' => 'select',
  102. 'name' => 'HEALTHCHECKS-Auth-include',
  103. 'label' => 'Minimum Authentication',
  104. 'value' => $GLOBALS['HEALTHCHECKS-Auth-include'],
  105. 'options' => groupSelect()
  106. ),
  107. array(
  108. 'type' => 'input',
  109. 'name' => 'HEALTHCHECKS-PingURL',
  110. 'label' => 'URL',
  111. 'value' => $GLOBALS['HEALTHCHECKS-PingURL'],
  112. 'help' => 'URL for HealthChecks Ping',
  113. 'placeholder' => 'HealthChecks Ping URL'
  114. ),
  115. ),
  116. 'Services' => array(
  117. array(
  118. 'type' => 'arrayMultiple',
  119. 'name' => 'HEALTHCHECKS-all-items',
  120. 'label' => 'Services',
  121. 'value' => $GLOBALS['HEALTHCHECKS-all-items']
  122. )
  123. )
  124. );
  125. }