packer-builds

Packer Builds for LXC and Libvirt
git clone https://git.in0rdr.ch/packer-builds.git
Log | Files | Refs | README

commit 6e3f7b0e0137a68b2b076f475c77b7b74d5ec220
parent 9b6479b4a734af45f465c577b0d6c215201e09d4
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Sun, 26 May 2024 18:49:24 +0200

feat(gatus): add collectd script

This script exposes the collectd metrics from the unix socket
(collectd unix socket plugin) as valid http response.

We need the script primarily, because the collectd unix socket plugin
does not return a proper http response. This script fixes that behavior
so it is a proper http response. Nginx will refuse to return the
response from the unix upstream otherwise. Unfortunately, the Nginx
add_header directive would only add the header starting from Nginx
server (to the end client).

The script returns the latest metrics in json format.

The cgi script can be used in companion with an Nginx server. Gatus can
fetch the interal http endpoint :9090 to fetch the latest metrics in
json format.

Diffstat:
Mscripts/gatus.sh | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/scripts/gatus.sh b/scripts/gatus.sh @@ -19,7 +19,7 @@ useradd -M -r gatus # Update system apt-get install -y apt-utils apt-get update && apt-get upgrade -y -apt-get install -y curl nginx nfs-common +apt-get install -y curl nginx nfs-common fcgiwrap socat # Install Go (https://go.dev/doc/install) cd $GATUS_BUILD_DIR @@ -127,6 +127,18 @@ server { proxy_set_header X-Forwarded-Proto \$scheme; } } + +server { + listen 127.0.0.1:9090; + + # expose the collectd metrics + location /collectd { + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/local/bin/collectd.sh; + fastcgi_param QUERY_STRING $query_string; + fastcgi_pass unix:/run/fcgiwrap.socket; + } +} EOF # Allow Gatus to read config file @@ -144,3 +156,50 @@ ExecStartPre= ExecStartPre=/usr/bin/sh -c 'sleep 5 && mount -a' ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;' EOF + +# Add collectd script to return stats as HTTP response +cat <<EOF > /usr/local/bin/collectd.sh +#!/usr/bin/env bash +# +# Depends: +# - fcgiwrap +# - socat +# +# https://landchad.net/cgi + +COLLECTD_SOCK=/collectd/query.sock +#echo "LISTVAL" | socat - UNIX-CLIENT:$COLLECTD_SOCK + +# We need the script primarily, because the collectd unix socket plugin does +# not return a proper http response. This line here fixes that behavior so it +# is a proper http response. Nginx will refuse to return the response from the +# unix upstream otherwise. Unfortunately, the Nginx add_header directive would +# only add the header starting from Nginx server (to the end client). +echo "Content-type: text/json" +echo + +function get_df() { + free_e=$(echo "GETVAL $1/percent_bytes-free" | socat - UNIX-CLIENT:/$COLLECTD_SOCK | tail -1 | cut -d '=' -f2) + + # convert from scientific notation + printf "%.0f\n" "$free_e" +} + +function md_status(){ + md_status_e=$(echo "GETVAL turris/exec-$1/md_disks-degraded" | socat - UNIX-CLIENT:/$COLLECTD_SOCK | tail -1 | cut -d '=' -f2) + # convert from scientific notation + printf "%.0f\n" "$md_status_e" +} + +df_srv=$(get_df "turris/df-srv") +df_nfs=$(get_df "turris/df-srv-nfs") +md_status=$(md_status "md127") + +cat << EOF +{ + "srv_free_percent":"$df_srv", + "nfs_free_percent":"$df_nfs", + "md_degraded":"$md_status" +} +EOF +chmod +x /usr/local/bin/collectd.sh