#!/bin/bash
#定义变量 IP_LIST=(“192.168.1.1” “192.168.1.2” “192.168.1.3”) COMMUNITY=“public” OUTPUT_FILE=“ups_metrics.prom”
#遍历设备列表 for IP in \({IP_LIST[@]} do #获取UPS信息 UPS_NAME=\)(snmpget -v2c -c \(COMMUNITY \)IP sysName.0 | awk ‘{print \(4}') UPS_MODEL=\)(snmpget -v2c -c \(COMMUNITY \)IP hrDeviceDescr.\((snmpwalk -v2c -c \)COMMUNITY \(IP | grep 'hrDeviceType' | awk '{print \)1}’ | cut -d ‘.’ -f 2) | awk ‘{print substr(\(0, index(\)0,\(4))}') UPS_STATUS=\)(snmpget -v2c -c \(COMMUNITY \)IP upsBasicOutputStatus.0 | awk ‘{print substr(\(4, 1, length(\)4)-1)}’) UPS_LOAD=\((snmpget -v2c -c \)COMMUNITY \(IP upsAdvBatteryCapacity.\)(snmpwalk -v2c -c \(COMMUNITY \)IP | grep ‘upsAdvBatteryCapacity’ | awk ‘{print \(1}' | cut -d '.' -f 2) | awk '{print substr(\)4, 1, length($4)-1)}’)
#输出Prometheus Metrics格式的指标数据到文件中 echo “# HELP ups_name The name of the UPS device” >>\(OUTPUT_FILE echo "# TYPE ups_name gauge" >>\)OUTPUT_FILE echo “ups_name{ip=\”\(IP\",name=\"\)UPS_NAME\“} 1” >>$OUTPUT_FILE
echo “# HELP ups_model The model of the UPS device” >>\(OUTPUT_FILE echo "# TYPE ups_model gauge" >>\)OUTPUT_FILE echo “ups_model{ip=\”\(IP\",model=\"\)UPS_MODEL\“} 1” >>$OUTPUT_FILE
echo “# HELP ups_status The status of the UPS output (1=on, 2=off, 3=rebooting)” >>\(OUTPUT_FILE echo "# TYPE ups_status gauge" >>\)OUTPUT_FILE echo “ups_status{ip=\”\(IP\",status=\"\)UPS_STATUS\“} 1” >>$OUTPUT_FILE
echo “# HELP ups_load The current load percentage of the UPS battery” >>\(OUTPUT_FILE echo "# TYPE ups_load gauge" >>\)OUTPUT_FILE echo “ups_load{ip=\”\(IP\"} \)UPS_LOAD” >>$OUTPUT_FILE
done
echo “Metrics collection completed.”