uptime_kuma.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Exception\GuzzleException;
  4. trait UptimeKumaHomepageItem
  5. {
  6. private static Client $kumaClient;
  7. public function uptimeKumaSettingsArray($infoOnly = false)
  8. {
  9. $homepageInformation = [
  10. 'name' => 'UptimeKuma',
  11. 'enabled' => true,
  12. 'image' => 'plugins/images/tabs/kuma.png',
  13. 'category' => 'Monitor',
  14. 'settingsArray' => __FUNCTION__
  15. ];
  16. if ($infoOnly) {
  17. return $homepageInformation;
  18. }
  19. $homepageSettings = [
  20. 'debug' => true,
  21. 'settings' => [
  22. 'Enable' => [
  23. $this->settingsOption('enable', 'homepageUptimeKumaEnabled'),
  24. $this->settingsOption('auth', 'homepageUptimeKumaAuth'),
  25. ],
  26. 'Connection' => [
  27. $this->settingsOption('url', 'uptimeKumaURL', ['help' => 'URL for Uptime Kuma e.g. http://kuma:3001 (no trailing slash)', 'placeholder' => 'http://kuma:3001']),
  28. $this->settingsOption('token', 'uptimeKumaToken'),
  29. ],
  30. 'Options' => [
  31. $this->settingsOption('refresh', 'homepageUptimeKumaRefresh'),
  32. $this->settingsOption('title', 'homepageUptimeKumaHeader'),
  33. $this->settingsOption('toggle-title', 'homepageUptimeKumaHeaderToggle'),
  34. ],
  35. ]
  36. ];
  37. return array_merge($homepageInformation, $homepageSettings);
  38. }
  39. public function uptimeKumaHomepagePermissions($key = null)
  40. {
  41. $permissions = [
  42. 'main' => [
  43. 'enabled' => [
  44. 'homepageUptimeKumaEnabled'
  45. ],
  46. 'auth' => [
  47. 'homepageUptimeKumaAuth'
  48. ],
  49. 'not_empty' => [
  50. 'uptimeKumaURL',
  51. 'uptimeKumaToken',
  52. ]
  53. ]
  54. ];
  55. return $this->homepageCheckKeyPermissions($key, $permissions);
  56. }
  57. public function homepageOrderUptimeKuma()
  58. {
  59. if ($this->homepageItemPermissions($this->uptimeKumaHomepagePermissions('main'))) {
  60. return '
  61. <div id="' . __FUNCTION__ . '">
  62. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Uptime Kuma...</h2></div>
  63. <script>
  64. // Uptime Kuma
  65. homepageUptimeKuma("' . $this->config['homepageUptimeKumaRefresh'] . '");
  66. // End Uptime Kuma
  67. </script>
  68. </div>
  69. ';
  70. }
  71. }
  72. public function getUptimeKumaHomepageData()
  73. {
  74. if (!$this->homepageItemPermissions($this->uptimeKumaHomepagePermissions('main'), true)) {
  75. return false;
  76. }
  77. $api = [];
  78. $url = $this->qualifyURL($this->config['uptimeKumaURL']);
  79. try {
  80. $response = $this->getKumaClient($url, $this->config['uptimeKumaToken'])->get('/metrics');
  81. $body = $response->getBody()->getContents();
  82. $body = explode(PHP_EOL, $body);
  83. $body = array_filter($body, function (string $item) {
  84. return str_starts_with($item, 'monitor_status');
  85. });
  86. $body = array_map(function (string $item) {
  87. try {
  88. return $this->parseUptimeKumaStatus($item);
  89. } catch (Exception $e) {
  90. // do nothing when monitor is disabled
  91. }
  92. }, $body);
  93. $api = array_values(array_filter($body));
  94. } catch (GuzzleException $e) {
  95. $this->setLoggerChannel('UptimeKuma')->error($e);
  96. $this->setAPIResponse('error', $e->getMessage(), 401);
  97. return false;
  98. };
  99. $api = isset($api) ? $api : false;
  100. $this->setAPIResponse('success', null, 200, $api);
  101. return $api;
  102. }
  103. private function getKumaClient(string $url, string $token): Client
  104. {
  105. if (!isset(static::$kumaClient)) {
  106. static::$kumaClient = new Client([
  107. 'base_uri' => $url,
  108. 'headers' => [
  109. 'Authorization' => sprintf("Basic %s", base64_encode(':'.$token)),
  110. ],
  111. ]);
  112. }
  113. return static::$kumaClient;
  114. }
  115. private function parseUptimeKumaStatus(string $status): array
  116. {
  117. if (substr($status, -1) === '2') {
  118. throw new Exception("monitor diasbled");
  119. }
  120. $up = (substr($status, -1)) == '0' ? false : true;
  121. $status = substr($status, 15);
  122. $status = substr($status, 0, -4);
  123. $status = explode(',', $status);
  124. $data = [
  125. 'name' => $this->getStringBetweenQuotes($status[0]),
  126. 'url' => $this->getStringBetweenQuotes($status[2]),
  127. 'type' => $this->getStringBetweenQuotes($status[1]),
  128. 'status' => $up,
  129. ];
  130. return $data;
  131. }
  132. private function getStringBetweenQuotes(string $input): string
  133. {
  134. if (preg_match('/"(.*?)"/', $input, $match) == 1) {
  135. return $match[1];
  136. }
  137. return '';
  138. }
  139. }