92 lines
2.3 KiB
Bash
92 lines
2.3 KiB
Bash
#!/bin/sh
|
|
|
|
set -eu
|
|
umask 077
|
|
|
|
community="public"
|
|
|
|
if [ "${1:-}" = "-n" ]; then
|
|
_noop=true
|
|
else
|
|
_noop=false
|
|
fi
|
|
|
|
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 l | awk '
|
|
{
|
|
if ($1 == "cn:") {
|
|
cn = $2
|
|
}
|
|
if ($1 == "l:") {
|
|
l = substr($0, 3)
|
|
}
|
|
if ($0 == "" && cn != "" && l != "") {
|
|
print cn l
|
|
cn = ""
|
|
l = ""
|
|
}
|
|
}
|
|
' | while read -r name location
|
|
do
|
|
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
|
|
if device_name="$(ldapsearch -Q -LLL cn="${device}.*" cn | awk "
|
|
{
|
|
if (\$1 == \"cn:\") {
|
|
if (name) {
|
|
exit 1
|
|
}
|
|
name=\$2
|
|
}
|
|
} END {
|
|
if (!name) {
|
|
exit 1
|
|
}
|
|
print name
|
|
}
|
|
")" ; then
|
|
device="$device_name"
|
|
fi
|
|
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}")"
|
|
if $_noop ; then
|
|
echo "${topic} -> ${value}"
|
|
else
|
|
mqtt_send "$topic" "$value"
|
|
fi
|
|
done
|
|
done
|
|
done
|