我这边采用的是BK650M2-CH个人的UPS,只用于NAS使用,这里记录一下如何通过zabbix监控UPS的过程。

参考资料

https://www.haiyun.me/archives/1425.html

查看UPS的USB链接

root@zabbix-itezu:/etc/nut# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 004: ID 051d:0002 American Power Conversion Uninterruptible Power Supply  # 这是我的UPS,说明连接正常
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

安装nut

apt install nut nut-client nut-server

修改配置文件

配置文件位置/etc/nut

  • nut.conf
#添加
MODE=standalone
  • ups.conf
[ups]
driver = usbhid-ups
port = auto
  • upsd.conf
LISTEN 127.0.0.1 3493
LISTEN ::1 3493
  • upsd.users
[admin]
password = dGixWVRYqhswukfp
actions = set
actions = fsd
instcmds = ALL

[monmaster]
password = h1n0DhCusKCWlcpy
upsmon master
  • upsmon.conf
MONITOR ups 1 monmaster h1n0DhCusKCWlcpy master
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD "/sbin/upssched"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 555
POWERDOWNFLAG /etc/killpower
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM SYSLOG+WALL+EXEC
NOTIFYFLAG FSD SYSLOG+WALL+EXEC
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5

启动

systemctl restart nut-server
systemctl restart nut-client
systemctl restart nut-monitor
systemctl enable nut-server
systemctl enable nut-client
systemctl enable nut-monitor

查看是否有返回值

如下有正常的返回值,说明就可以使用了。

upsc ups@localhost
Init SSL without certificate database
battery.charge: 100
battery.charge.low: 1
battery.mfr.date: 2001/01/01
battery.runtime: 326
battery.runtime.low: 120
battery.type: PbAc
battery.voltage: 13.6
battery.voltage.nominal: 12.0
device.mfr: American Power Conversion
device.model: Back-UPS BK650M2-CH
device.serial: 9B2238A02524
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: 2.7.4
driver.version.data: APC HID 0.96
driver.version.internal: 0.41
input.sensitivity: low
input.transfer.high: 278
input.transfer.low: 160
input.voltage: 224.0
input.voltage.nominal: 220
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.firmware: 294803G -292804G 
ups.load: 52
ups.mfr: American Power Conversion
ups.mfr.date: 2022/09/23
ups.model: Back-UPS BK650M2-CH
ups.productid: 0002
ups.realpower.nominal: 390
ups.serial: 9B2238A02524
ups.status: OL
ups.test.result: Done and passed
ups.timer.reboot: 0
ups.timer.shutdown: -1
ups.vendorid: 051d

出现过的问题

connect failed: Connection failure: Connection refused  # 无法连接,发现不知道什么时候nut-server 没有了。重新安装了,在用netstat -anlput ,可以看到监控了3493 端口
root@wanglei-Default-string:/etc/nut# netstat -anlput
激活Internet连接 (服务器和已建立连接的)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      160755/systemd-reso 
tcp        0      0 127.0.0.1:3493          0.0.0.0:*               LISTEN      161155/upsd     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      101682/cupsd    
tcp        0      0 127.0.0.1:8088          0.0.0.0:*               LISTEN      110295/influxd  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      105026/sshd: /usr/s 
tcp        0      0 192.168.50.253:33496    91.189.91.38:80         TIME_WAIT   -               
tcp        0     96 192.168.50.253:22       192.168.50.250:49851    ESTABLISHED 103409/sshd: wangle 
tcp        0      0 127.0.0.1:57398         127.0.0.1:8086          ESTABLISHED 109813/telegraf   
tcp        0      0 192.168.50.253:60598    182.42.85.113:1194      ESTABLISHED 104879/openvpn  
tcp6       0      0 :::8086                 :::*                    LISTEN      110295/influxd  
tcp6       0      0 ::1:631                 :::*                    LISTEN      101682/cupsd    
tcp6       0      0 ::1:3350                :::*                    LISTEN      5303/xrdp-sesman  
tcp6       0      0 ::1:3493                :::*                    LISTEN      161155/upsd     
tcp6       0      0 :::3389                 :::*                    LISTEN      5313/xrdp       
tcp6       0      0 :::3306                 :::*                    LISTEN      109371/mysqld   
tcp6       0      0 :::22                   :::*                    LISTEN      105026/sshd: /usr/s 
tcp6       0      0 127.0.0.1:8086          127.0.0.1:57398         ESTABLISHED 110295/influxd  
udp        0      0 0.0.0.0:48676           0.0.0.0:*                           23198/avahi-daemon: 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           160755/systemd-reso 
udp        0      0 0.0.0.0:631             0.0.0.0:*                           101688/cups-browsed 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           23198/avahi-daemon: 
udp6       0      0 :::35797                :::*                                23198/avahi-daemon: 
udp6       0      0 :::5353                 :::*                                23198/avahi-daemon:

zabbix配置

root@zabbix-itezu:/opt/zabbix_scripts# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/ups.conf 
UserParameter=ups.check[*], sudo /opt/zabbix_scripts/monitor_ups.sh  $1

monitor_ups.sh 内容

#!/bin/bash
#####################################################################
###  用途:用于监控 UPS 的状态                                      ###
###  执行方式: 由zabbix调用执行                                    ###
###  执行周期: 半个小时或者1个小时执行一次                          ###
###  Author: wanglei                                              ###
###  Date: 2022-08-17                                             ###
###  Ver:  1.1                                                    ###
###  Email: wanglei@adwetec.com                                   ###
###                                                               ###
#####################################################################
#CMD='/usr/bin/upsc ups@localhost 2>&1 | grep -v "^Init SSL"' 
CMD='/usr/bin/upsc' #ups@localhost 2>&1 | grep -v "^Init SSL"' 
Usage(){

    echo "Usage: `basename $0` [ battery.charge | device.model | ups.serial ... ]"
    echo "battery.charge: ups 的电压"
    echo "device.model: ups 产品名称"
    exit 1

}


if [ $# -lt 1 ]
then 
  Usage
fi

case "$1" in
    battery.charge)
       ## 当前充电量百分比
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "battery.charge:" |awk '{print $2}'
    ;;
    battery.runtime)
       ## 电池可用多长时间,分钟
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "battery.runtime:" |awk '{print $2}'
    ;;
    battery.voltage)
       ## 电池的当前电压,浮点数
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "battery.voltage:" |awk '{print $2}'
    ;;
    battery.voltage.nominal)
       ## 电池的正常电压
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "battery.voltage.nominal:" |awk '{print $2}'
    ;;
    device.model)
       ## UPS的型号
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "device.model:" |awk '{print $2,$3}'
    ;;
    device.serial)
       ## UPS 序列号,也可用ups.serial
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "device.serial:" |awk '{print $2}'
    ;;
    driver.version)
       ## 驱动的版本,字符
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "driver.version:" |awk '{print $2}'
    ;;
    input.transfer.high)
       ## 最高输入电压
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "input.transfer.high:" |awk '{print $2}'
    ;;
    input.transfer.low)
       ## 最低输入电压
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "input.transfer.low:" |awk '{print $2}'
    ;;
    input.voltage)
       ## 现在输入电压
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "input.voltage:" |awk '{print $2}'
    ;;
    input.voltage.nominal)
       ## 正常输入电压
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "input.voltage.nominal:" |awk '{print $2}'
    ;;
    ups.firmware)
       ## UPS 固件版本,字符类型
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.firmware:" |awk '{print $2,$3}'
    ;;
    ups.mfr.date)
       ## UPS 制造时间,字符类型
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.mfr.date:" |awk '{print $2}'
    ;;
    ups.mfr)
       ## UPS 厂商,字符类型
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.mfr:" | awk -F ':'  '{print $2}'
    ;;
    ups.status)
       ## UPS 状态,分为OL(在线),OL CHRG(充电中),OB DISCHRG(使用电池)
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.status:" |awk '{print $2,$3}'
    ;;
    ups.load)
       ## UPS 负载
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.load:" |awk '{print $2,$3}'
    ;;
    ups.realpower.nominal)
       ## UPS 总瓦数
       $CMD ups@localhost 2>&1 | grep -v "^Init SSL"|grep "ups.realpower.nominal:" |awk '{print $2,$3}'
    ;;
    *)
       Usage
    ;;
esac

添加sudo权限

给zabbix用户添加 monitor_ups.sh的sudo权限

root@zabbix-itezu:/opt/zabbix_scripts# cat /etc/sudoers.d/zabbix 
zabbix  ALL=(ALL) NOPASSWD: /opt/zabbix_scripts/monitor_raid.sh,  /opt/MegaRAID/MegaCli/MegaCli64, /opt/zabbix_scripts/monitor_ups.sh