diff --git a/widgets/beszel-server-stats/README.md b/widgets/beszel-server-stats/README.md index 1b0461b7..86d3cc83 100644 --- a/widgets/beszel-server-stats/README.md +++ b/widgets/beszel-server-stats/README.md @@ -4,7 +4,7 @@ This widget mimics the style of the builtin [Server Stats](https://github.com/glanceapp/glance/blob/main/docs/configuration.md#server-stats) widget, but it pulls the data from a [Beszel](https://github.com/henrygd/beszel) instance instead. The collected data is about the same, but it means needing to -run one less container if you already have beszel running to monitor your +run one less container if you already have Beszel running to monitor your servers. If you encounter any issues, please open an issue, tag me, and I’ll investigate further. @@ -27,17 +27,21 @@ Customisation can be applied using the `options:` field. See [Options](#options) > > For URLs, you **MUST** include `http://` or `https://`. > Do **NOT** include a trailing `/` at the end of URLs. +> +> For authentication use either your API token or your e-mail and password. * `BESZEL_URL` - The URL to your Beszel Hub instance, e.g., `http://:` or `https://` -* `BESZEL_TOKEN` - Your personal Beszel API Token. +* `BESZEL_TOKEN` - Your Beszel API token. +* `BESZEL_EMAIL` - Your Beszel e-mail. +* `BESZEL_PASSWORD` - Your Beszel password. -To setup Beszel API grabbing your token is required, to get it use a command -below. Remember to replace `USERNAME`, `PASSWORD` and `IP:PORT`. +To grab your Beszel API token, to use the command +below. Remember to replace ``, `` and ``. ```sh -curl -X POST "http://IP:PORT/api/collections/users/auth-with-password" \ +curl -X POST "/api/collections/users/auth-with-password?fields=token" \ -H "Content-Type: application/json" \ - -d '{"identity":"USERNAME","password":"PASSWORD"}' + -d '{"identity":"","password":""}' ``` ## Secrets @@ -56,9 +60,19 @@ Default options are: options: # Required options base-url: ${BESZEL_URL} # Your environment-variables for the URL - api-key: ${BESZEL_TOKEN} # Your environment-variables for the API token. Can be a secret as well `${secret:beszel-token}` + + # Required options for API token authentication + api-key: ${BESZEL_TOKEN} # Your environment variable for the API token. Can be a secret as well `${secret:beszel-token}` + + # Required options for email + password authentication + email: ${BESZEL_EMAIL} # Your environment variable for the e-mail. Can be a secret as well `${secret:beszel-email}` + password: ${BESZEL_PASSWORD} # Your environment variable for the password. Can be a secret as well `${secret:beszel-password}` ``` +If you want to use email and password authentication instead of an API token, +you can omit the `api-key` field empty and fill in the `email` and `password` +fields with the appropriate values. + ## Widget YAML ```yaml @@ -67,53 +81,74 @@ options: cache: 5m options: base-url: ${BESZEL_URL} + # required for API token authentication api-key: ${BESZEL_TOKEN} + # required for e-mail + password authentication + # email: ${BESZEL_EMAIL} + # password: ${BESZEL_PASSWORD} template: | {{/* Required config options */}} {{ $baseURL := .Options.StringOr "base-url" "" }} + + {{/* Required config options for API token authentication */}} {{ $apiKey := .Options.StringOr "api-key" "" }} - + + {{/* Required config options for email + password authentication */}} + {{ $email := .Options.StringOr "email" "" }} + {{ $password := .Options.StringOr "password" "" }} + {{/* Error message template */}} {{ define "errorMsg" }} -
+
ERROR
- + -
-

{{ . }}

+
+

{{ . }}

{{ end }} - + {{ define "formatGigabytes" }} - {{ $value := . }} - {{ $label := "GB" }} - {{- if lt $value 1.0 }} + {{ $value := . }} + {{ $label := "GB" }} + {{- if lt $value 1.0 }} {{ $value = mul $value 1000.0 }} {{ $label = "MB" }} - {{- else if lt $value 1000.0 }} - {{ else }} + {{- else if lt $value 1000.0 }} + {{ else }} {{ $value = div $value 1000.0 }} {{ $label = "TB" }} - {{ end }} - {{ printf "%.1f" $value }} {{ $label }} + {{ end }} + {{ printf "%.1f" $value }} {{ $label }} {{ end }} - + {{/* Check required fields */}} - {{ if or (eq $baseURL "") (eq $apiKey "") }} - {{ template "errorMsg" "Some required options are not set." }} + {{ if or (eq $baseURL "") (and (eq $apiKey "") (or (eq $email "") (eq $password ""))) }} + {{ template "errorMsg" "Some required options are not set." }} {{ else }} - - {{ $token := concat "Bearer " $apiKey }} - - {{ $systemsResponse := newRequest (print $baseURL "/api/collections/systems/records") - | withHeader "Authorization" $token - | getResponse }} - {{ $systems := $systemsResponse.JSON.Array "items" }} - - - {{ range $n, $system := $systems }} + + {{ $token := .}} + {{ if not (eq $apiKey "") }} + {{ $token = concat "Bearer " $apiKey }} + {{ else }} + {{ $authResponse := newRequest (print $baseURL "/api/collections/users/auth-with-password") + | withParameter "fields" "token" + | withHeader "Content-Type" "application/json" + | withStringBody (printf `{"identity": "%s", "password": "%s"}` $email $password) + | getResponse }} + {{ $jwtToken := $authResponse.JSON.String "token" }} + {{ $token = concat "Bearer " $jwtToken }} + {{ end }} + + {{ $systemsResponse := newRequest (print $baseURL "/api/collections/systems/records") + | withHeader "Authorization" $token + | getResponse }} + {{ $systems := $systemsResponse.JSON.Array "items" }} + + + {{ range $n, $system := $systems }} {{ $status := $system.String "status" }} - + {{ $systemStatsRequest := newRequest (print $baseURL "/api/collections/system_stats/records") | withHeader "Authorization" $token | withParameter "sort" "-created" @@ -121,194 +156,195 @@ options: | withParameter "perPage" "1" | withParameter "filter" (print "type='1m'&&system='" ($system.String "id") "'") | getResponse }} - + {{ $systemStatItems := $systemStatsRequest.JSON.Array "items" }} - + {{ $hostname := $system.String "name" }} {{ $uptimeSec := $system.Float "info.u" }} - + {{ $systemTemp := $system.Float "info.dt"}} - + {{ $cpuLoad := $system.Float "info.cpu" }} - {{ $cpuLoad1m := $system.Float "info.l1" }} - {{ $cpuLoad15m := $system.Float "info.l15" }} - + {{ $cpuLoad1m := $system.Float "info.la.0" }} + {{ $cpuLoad15m := $system.Float "info.la.2" }} + {{ $memoryUsedPercent := $system.Float "info.mp" }} - + {{ $rootUsedPercent := $system.Float "info.dp" }} - + {{ $hasStats := false }} {{ $systemStats := "" }} - + {{ $memoryTotalGb := 0.0 }} {{ $memoryUsedGb := 0.0 }} - {{ $swapTotalGb := 0.0 }} + {{ $swapTotalGb := 0.0 }} {{ $swapUsedGb := 0.0 }} {{ $swapUsedPercent := 0.0 }} {{ $rootTotalGb := 0.0 }} {{ $rootUsedGb := 0.0 }} - + {{ $maxDiskUsedPercent := $rootUsedPercent }} + + {{ range $key, $efs := ($system.Get "info.efs").Map }} + {{ if gt ($efs.Float) $maxDiskUsedPercent }} + {{ $maxDiskUsedPercent = $efs.Float }} + {{ end }} + {{ end }} + {{ if gt (len $systemStatItems) 0 }} - {{ $hasStats = true }} - {{ $systemStats = index $systemStatItems 0 }} - - {{ $memoryTotalGb = $systemStats.Float "stats.m" }} - {{ $memoryUsedGb = $systemStats.Float "stats.mu" }} - - {{ $swapTotalGb = $systemStats.Float "stats.s" }} - {{ $swapUsedGb = $systemStats.Float "stats.su" }} - {{ $swapUsedPercent = mul (div $swapUsedGb $swapTotalGb) 100.0 }} - - {{ $rootTotalGb = $systemStats.Float "stats.d" }} - {{ $rootUsedGb = $systemStats.Float "stats.du" }} + {{ $hasStats = true }} + {{ $systemStats = index $systemStatItems 0 }} + + {{ $memoryTotalGb = $systemStats.Float "stats.m" }} + {{ $memoryUsedGb = $systemStats.Float "stats.mu" }} + + {{ $swapTotalGb = $systemStats.Float "stats.s" }} + {{ $swapUsedGb = $systemStats.Float "stats.su" }} + {{ $swapUsedPercent = mul (div $swapUsedGb $swapTotalGb) 100.0 }} + + {{ $rootTotalGb = $systemStats.Float "stats.d" }} + {{ $rootUsedGb = $systemStats.Float "stats.du" }} {{ end }} - +
-
+
-
{{ $hostname }}
-
+
{{ $hostname }}
+
{{ if eq $status "up" }} - {{ printf "%.1f" (mul $uptimeSec 0.000011574) }}d uptime + {{ printf "%.1f" (mul $uptimeSec 0.000011574) }}d uptime {{ else }} - unreachable + unreachable {{ end }} -
-
-
- {{- if eq $status "up" }} -
-
-
Kernel
-
-
{{ $system.String "info.k" }}
-
-
CPU
-
-
{{ $system.String "info.m" }}
-
-
- {{- end }} - +
+
+ - + +
-
- -
+ +
-
+
CPU
{{- if ge $systemTemp 80.0 }} - + {{- end }} -
{{ $cpuLoad }} %
-
-
+
{{ printf "%.1f" $cpuLoad }} %
+
+
-
+
1M AVG
{{ printf "%.1f" $cpuLoad1m }} %
-
-
+
+
15M AVG
{{ printf "%.1f" $cpuLoad15m }} %
-
-
+
+
TEMP C
{{ printf "%.1f" $systemTemp }} °
-
+
- +
-
-
+
+
+
-
- +
-
+
RAM
-
{{ $memoryUsedPercent }} %
-
-
+
{{ printf "%.1f" $memoryUsedPercent }} %
+
+
{{- if $hasStats }}
-
+
RAM
- {{ template "formatGigabytes" $memoryUsedGb }} / {{ template "formatGigabytes" $memoryTotalGb }} + {{ template "formatGigabytes" $memoryUsedGb }} / {{ template "formatGigabytes" $memoryTotalGb }}
-
- {{- if gt $swapTotalGb 0.0 }} -
+
+ {{ printf "%5.1f" $memoryUsedPercent }}% +
+ {{- if gt $swapTotalGb 0.0 }} +
SWAP
- {{ template "formatGigabytes" $swapUsedGb }} / {{ template "formatGigabytes" $swapTotalGb }} + {{ template "formatGigabytes" $swapUsedGb }} / {{ template "formatGigabytes" $swapTotalGb }} +
+
+ {{ printf "%5.1f" $swapUsedPercent }}%
-
- {{- end }} + {{- end }}
{{- end }}
-
- {{- if gt $swapTotalGb 0.0 }} -
- {{- end }} +
+ {{- if gt $swapTotalGb 0.0 }} +
+ {{- end }} +
-
- +
-
+
DISK
-
{{ $rootUsedPercent }} %
-
-
+
{{ printf "%.1f" $maxDiskUsedPercent }} %
+
+
{{- if $hasStats }}
-
    +
    • -
      /
      -
      -
      +
      /
      +
      +
      {{ template "formatGigabytes" $rootUsedGb }} / {{ template "formatGigabytes" $rootTotalGb }} -
      +
      +
      + {{ printf "%5.1f" $rootUsedPercent }}%
    • - {{ range $key, $efs := ($systemStats.Get "stats.efs").Map }} + {{ range $key, $efs := ($systemStats.Get "stats.efs").Map }}
    • -
      {{ $key }}
      -
      -
      +
      {{ $key }}
      +
      +
      {{ template "formatGigabytes" (($efs.Get "du").Float) }} / {{ template "formatGigabytes" (($efs.Get "d").Float) }} -
      +
      +
      + {{ printf "%5.1f" (mul 100 (div (($efs.Get "du").Float) (($efs.Get "d").Float))) }}%
    • - {{ end }} -
    + {{ end }} +
{{- end }}
-
- {{ range $key, $efs := ($system.Get "info.efs").Map }} +
+ {{ range $key, $efs := ($system.Get "info.efs").Map }} {{ $percent := $efs.Float }}
- {{ end }} + {{ end }} +
-
- -
+ +
- {{ end }} + {{ end }} {{ end }} ```