ansible/roles/aten_pdu/files/aten-mqtt-publish.sh

54 lines
1.5 KiB
Bash

#!/bin/sh
set -eu
umask 077
community="public"
mqtt_send() {
topic="$1"
value="$2"
tlsdir="$(openssl version -d | sed -e 's/^OPENSSLDIR: "\(.\+\)"$/\1/')"
mosquitto_pub -h mqtt02.home.foo.sh -t "$topic" -m "$value" \
--cafile "${tlsdir}/certs/ca.crt" \
--key "${tlsdir}/private/$(hostname -f).key" \
--cert "${tlsdir}/certs/$(hostname -f).crt"
}
snmp_get() {
host="$1"
key="$2"
snmpget -v 1 -c "$community" "$host" -Oqv -m ATEN-PE-CFG "$key" | tr -d '"'
}
# only run script if first vrrp interface is in master state
for state in /run/keepalived/*.state ; do
if [ "$(cat "$state")" != "MASTER" ]; then
exit 0
fi
break
done
ldapsearch -Q -LLL "(&(objectClass=device)(description=Aten PE*))" cn | \
awk '{ if ($1 == "cn:") print $2 }' | while read -r name
do
location="$(snmp_get "$name" RFC1213-MIB::sysLocation.0 | \
tr '[:upper:]' '[:lower:]' | tr ' ' '_')"
snmpwalk -v 1 -c "$community" "$name" -Oq \
-m ATEN-PE-CFG ATEN-PE-CFG::outletName | while read -r port device
do
port="$(echo "$port" | cut -d '.' -f 2)"
device="$(echo "$device" | tr -d '"')"
case "$device" in
"N/A"|"00 "|"unused")
continue
;;
esac
for key in Current Power Voltage ; do
topic="home/${location}/${device}/$(echo "$key" | tr '[:upper:]' '[:lower:]')"
value="$(snmp_get "$name" "ATEN-PE-CFG::outlet${key}.${port}")"
mqtt_send "$topic" "$value"
done
done
done