uptime_kuma.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $this->settingsOption('switch', 'homepageUptimeKumaCompact', ['label' => 'Compact view', 'help' => 'Toggles the compact view of this homepage module']),
  35. ],
  36. ]
  37. ];
  38. return array_merge($homepageInformation, $homepageSettings);
  39. }
  40. public function uptimeKumaHomepagePermissions($key = null)
  41. {
  42. $permissions = [
  43. 'main' => [
  44. 'enabled' => [
  45. 'homepageUptimeKumaEnabled'
  46. ],
  47. 'auth' => [
  48. 'homepageUptimeKumaAuth'
  49. ],
  50. 'not_empty' => [
  51. 'uptimeKumaURL',
  52. 'uptimeKumaToken',
  53. ]
  54. ]
  55. ];
  56. return $this->homepageCheckKeyPermissions($key, $permissions);
  57. }
  58. public function homepageOrderUptimeKuma()
  59. {
  60. if ($this->homepageItemPermissions($this->uptimeKumaHomepagePermissions('main'))) {
  61. return '
  62. <div id="' . __FUNCTION__ . '">
  63. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Uptime Kuma...</h2></div>
  64. <script>
  65. // Uptime Kuma
  66. homepageUptimeKuma("' . $this->config['homepageUptimeKumaRefresh'] . '");
  67. // End Uptime Kuma
  68. </script>
  69. </div>
  70. ';
  71. }
  72. }
  73. public function getUptimeKumaHomepageData()
  74. {
  75. if (!$this->homepageItemPermissions($this->uptimeKumaHomepagePermissions('main'), true)) {
  76. return false;
  77. }
  78. $api = [];
  79. $url = $this->qualifyURL($this->config['uptimeKumaURL']);
  80. try {
  81. $metrics = (new UptimeKumaMetrics(
  82. $this->getKumaClient($url, $this->config['uptimeKumaToken'])
  83. ->get('/metrics')
  84. ->getBody()
  85. ->getContents()
  86. ))->process();
  87. $api = [
  88. 'data' => $metrics->getMonitors(),
  89. 'options' => [
  90. 'title' => $this->config['homepageUptimeKumaHeader'],
  91. 'titleToggle' => $this->config['homepageUptimeKumaHeaderToggle'],
  92. 'compact' => $this->config['homepageUptimeKumaCompact'],
  93. ]
  94. ];
  95. } catch (GuzzleException $e) {
  96. $this->setLoggerChannel('UptimeKuma')->error($e);
  97. $this->setAPIResponse('error', $e->getMessage(), 401);
  98. return false;
  99. };
  100. $api = isset($api) ? $api : false;
  101. $this->setAPIResponse('success', null, 200, $api);
  102. return $api;
  103. }
  104. private function getKumaClient(string $url, string $token): Client
  105. {
  106. if (!isset(static::$kumaClient)) {
  107. static::$kumaClient = new Client([
  108. 'base_uri' => $url,
  109. 'headers' => [
  110. 'Authorization' => sprintf("Basic %s", base64_encode(':'.$token)),
  111. ],
  112. ]);
  113. }
  114. return static::$kumaClient;
  115. }
  116. }