#!/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 MIKROTIK-MIB "$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=MikroTik *))" cn | \ awk '{ if ($1 == "cn:") print $2 }' | while read -r name do snmpwalk -v 1 -c "$community" "$name" -Oq -m MIKROTIK-MIB \ MIKROTIK-MIB::mtxrPOEStatus | while read -r port status do port="$(echo "$port" | cut -d "." -f 2)" [ "$status" = "poweredOn" ] || continue device="$(snmp_get "$name" "SNMPv2-SMI::mib-2.31.1.1.1.18.${port}")" [ -z "$device" ] && continue location="$(ldapsearch -Q -LLL "(&(objectClass=device)(cn=${device}))" l | \ sed -n 's/^l: \(.\+\)/\1/p' | tr '[:upper:]' '[:lower:]' | tr ' ' '_')" [ -z "$location" ] && continue for key in Current Power Voltage ; do topic="home/${location}/${device}/$(echo "$key" | tr '[:upper:]' '[:lower:]')" value="$(snmp_get "$name" "MIKROTIK-MIB::mtxrPOE${key}.${port}")" mqtt_send "$topic" "$value" done done done