Leider liefert telegraf zwar viele Daten an influxDB, aber keine Informationen zur aktuellen Distribution oder anstehender Update, erst recht nicht getrennt nach regulär, security.
Auf dedizierten Servern (z.B. RevProxy) lasse ich ein kleines Script laufen, dass die Daten per MQTT an edomi liefert und per influxDB zu grafana. Für Proxmox fehlte mir das noch, weil ich dies für auch für alle LXC und VM abfragen wollte.
Das folgende Script liefert genau dies für Proxmox Host, dynamisch alle LXC + VM und als Gesamtsumme im influxDB2-line format, um es per telegraf an influxDB2 und MQTT zu senden. Zum Beispiel:
Code:
update,host=HAL-proxmox regular=0i,security=1i,dist="Debian GNU/Linux 11 (bullseye)" update,host=HAL-ubuntu-LTS regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="103",object="lxc" update,host=HAL-edomi-dev regular=14i,security=5i,dist="Rocky Linux 8.6 (Green Obsidian)",vmid="20231",object="lxc" update,host=HAL-edomi-P1 regular=14i,security=5i,dist="Rocky Linux 8.6 (Green Obsidian)",vmid="20232",object="lxc" update,host=HAL-TVH regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="30018",object="lxc" update,host=HAL-30 regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="30019",object="lxc" update,host=HAL-pihole regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="30096",object="lxc" update,host=HAL-pihole-IOT regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="80096",object="lxc" update,host=HAL-AVAHI regular=0i,security=0i,dist="Ubuntu 18.04.5 LTS",vmid="99098",object="lxc" update,host=HAL-11 regular=0i,security=0i,dist="Ubuntu 22.04.1 LTS",vmid="10018",object="qemu" update,host=HAL-proxmox_SUM regular=28i,security=11i

Bleibt nur noch die Frage, wie man aus dem via MQTT kommenden JSON eine dynamische Liste machen kann, statt wie im folgenden Screenshot nur statisch...siehe anderes Thema Unbenannt.jpg Unbenannt2.jpg
Code:
sudo nano /etc/telegraf/script/upgrade_check_guest.sh
Code:
#!/usr/bin/env bash
REG_SUM=()
SEC_SUM=()
REG=()
SEC=()
OUTPUT=""
check_update_lxc () {
HOST=$(sudo pct list | grep $1 | awk '{print $3}')
DIST=$(sudo lxc-attach -n $1 -- cat /etc/*release | grep "\PRETTY" | sed -e 's/[^"]*"\(.*\)/"\1/g')
ID_LIKE=$(sudo lxc-attach -n $1 -- cat /etc/*release | grep "\ID_LIKE" | sed -e 's/.*=\(.*\)/\1/' | sed 's/"//g')
case "$ID_LIKE" in
debian)
sudo lxc-attach -n $1 -- apt-get update -qq
REG=$(sudo lxc-attach -n $1 -- apt list --upgradable 2>/dev/null | grep "\/stable" | grep -v "\-security" | wc -l)
SEC=$(sudo lxc-attach -n $1 -- apt list --upgradable 2>/dev/null | grep "\/stable" | grep "\-security" | wc -l)
let "REG_SUM+=${REG}"
let "SEC_SUM+=${SEC}"
;;
"rhel centos fedora")
sudo lxc-attach -n $1 -- yum --quiet check-update 1> /dev/null
ALL=$(sudo lxc-attach -n $1 -- yum check-update | grep "\.el" | wc -l)
SEC=$(sudo lxc-attach -n $1 -- yum check-update --security | grep "\.el" | wc -l)
let "REG=${ALL}-${SEC}"
let "REG_SUM+=${REG}"
let "SEC_SUM+=${SEC}"
;;
esac
OUTPUT+="update,host=${HOST} regular=${REG}i,security=${SEC}i,dist=${DIST},vmid=\"${1}\",object=\"lxc\""$'\n'
}
check_update_qemu () {
HOST=$(sudo qm guest exec $1 "hostname" | jq -r '."out-data"')
DIST=$(sudo qm guest exec $1 cat /etc/*release | jq -r '."out-data"' | grep "\PRETTY" | sed -e 's/[^"]*"\(.*\)/"\1/g')
ID_LIKE=$(sudo qm guest exec $1 cat /etc/*release | jq -r '."out-data"' | grep "\ID_LIKE" | sed -e 's/.*=\(.*\)/\1/' | sed 's/"//g')
case "$ID_LIKE" in
debian)
dummy=$(sudo qm guest exec $1 "apt-get" -- "update" "-qq")
REG=$(sudo qm guest exec $1 "apt" -- "list" "--upgradable" | jq -r '."out-data"' | grep "\/stable" | grep -v "\-security" | wc -l)
SEC=$(sudo qm guest exec $1 "apt" -- "list" "--upgradable" | jq -r '."out-data"' | grep "\/stable" | grep "\-security" | wc -l)
let "REG_SUM+=${REG}"
let "SEC_SUM+=${SEC}"
;;
"rhel centos fedora")
dummy=$(sudo qm guest exec $1 "yum" -- "--quiet" "check-update" "1> /dev/null")
ALL=$(sudo qm guest exec $1 "yum" -- "check-update" | jq -r '."out-data"' | grep "\.el" | wc -l)
SEC=$(sudo qm guest exec $1 "yum" -- "check-update" "--security" | jq -r '."out-data"' | grep "\.el" | wc -l)
let "REG=${ALL}-${SEC}"
let "REG_SUM+=${REG}"
let "SEC_SUM+=${SEC}"
;;
esac
OUTPUT+="update,host=${HOST} regular=${REG}i,security=${SEC}i,dist=${DIST},vmid=\"${1}\",object=\"qemu\""$'\n'
}
# A) Check upgrade for the Host
# regular updates | security updates | hostname | distribution/version
sudo apt-get update -qq
REG=$(sudo apt list --upgradable 2>/dev/null | grep "\/stable" | grep -v "\-security" | wc -l)
SEC=$(sudo apt list --upgradable 2>/dev/null | grep "\/stable" | grep "\-security" | wc -l)
let "REG_SUM+=${REG}"
let "SEC_SUM+=${SEC}"
HOST1=$(hostname)
DIST=$(sudo cat /etc/*release | grep "\PRETTY" | sed -e 's/[^"]*"\(.*\)/"\1/g')
OUTPUT+="update,host=${HOST1} regular=${REG}i,security=${SEC}i,dist=${DIST}"$'\n '
## B) Check upgrades for all active LXC containers
for cx in `sudo lxc-ls --active --line`; do
# echo -e "LXC:$cx"
check_update_lxc $cx
done
## C) Ckeck upgrades for all active VM/QEMU
for vm in $(sudo qm list | grep "\running" | awk '{print $1}'); do
# echo -e "VM:$vm"
check_update_qemu $vm
done
## D) SUM over all
OUTPUT+="update,host=${HOST1}_SUM regular=${REG_SUM}i,security=${SEC_SUM}i"$'\n'
## Final output with \n for influxDB line protocol (Lines separated by the newline character \n represent a single point in InfluxDB)
# For MQTT-outbound processor option 'batch=true' is needed
echo "$OUTPUT"
Code:
sudo nano /etc/telegraf/telegraf.conf
Code:
[[inputs.exec]] alias = "update_check" interval = "3600s" commands = [ "/etc/telegraf/script/upgrade_check_guest.sh" ] timeout = "120s" name_override = "update" data_format = "influx" [[outputs.influxdb_v2]] urls = <DEININFLUXDB2> token = <DEINTOKEN> organization = "orga" bucket = "technik" insecure_skip_verify = true precision = "s" namepass = ["update"] [[outputs.mqtt]] servers = <DEINMQTT> namepass = ["update"] topic_prefix = "status" qos = 2 username = <USER> password = <DEINPW> insecure_skip_verify = true batch = true data_format = "json"
Code:
sudo visudo -f /etc/sudoers.d/telegraf
Code:
telegraf ALL=NOPASSWD:/usr/bin/apt-get update *,/usr/bin/apt list *,/usr/bin/cat /etc/*release, /usr/bin/lxc-ls *, /usr/sbin/pct list, /usr/bin/lxc-attach *,/usr/sbin/qm guest exec*,/usr/sbin/qm list




Einen Kommentar schreiben: