利用apc的snmp实现停电自动关机

  • ap9631 管理卡,提供snmp管理协议;
  • 客户端需要安装net-snmp-utils;
  • oid的获取使用APC的MIB和snmp browser
  • InputPhase=AC供电状态;
  • BatteryRunTimeRemaining=电池剩余供电时间(低于5分钟就关机了,snmp出来是timeticks)

PS:

timeticks / 100 = seconds
timeticks / 6000 = minutes
timeticks / 360000 = hours
timeticks / 8640000 = days
ups.sh
#!/bin/bash
#MRCO 2017-07-14
upsAdvInputLineVoltage=.1.3.6.1.4.1.318.1.1.1.3.2.1.0
BatteryRunTimeRemaining=.1.3.6.1.4.1.318.1.1.1.2.2.3.0
ip=10.1.120.75
threshold=30000
shut=60
 
# main
which snmpget > /dev/null 2>&1
if [[ $? == 0 ]];then
    # get the ups status.
    upsAdvInputLineVoltage=`snmpget -Oqvt -v 1 -c public ${ip} ${upsAdvInputLineVoltage}`
    BatteryRunTimeRemaining=`snmpget -Oqvt -v 1 -c public ${ip} ${BatteryRunTimeRemaining}`
    TimeRemaining=`echo $(($BatteryRunTimeRemaining/6000))`
    # react
    if [[ $upsAdvInputLineVoltage == 0 ]];then
        echo "Utility Power DOWN, $TimeRemaining minutes remaining..." | wall
        if [[ $BatteryRunTimeRemaining -lt $threshold ]];then
            echo "SYSTEM WILL SHUTING DOWN IN $shut SECONDS..." | wall
            sleep $shut
            #linux
            shutdown -h now
            #unix
            #sync;sync;sync;poweroff
        fi
    fi
fi
  • homelab/ups/利用apc_snmp实现停电自动关机.txt
  • 最后更改: 2019/04/16 18:31
  • (外部编辑)