-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupsmonitor
54 lines (47 loc) · 1.98 KB
/
upsmonitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#Script to write to InfluxDB for CyberPower UPS Satistics
#This script requires the pwrstat utility to be installed and running on the system
#Written by BarryDinpoon, modified by Nick-Vazquez
# InfluxDB v2 API - Write: https://docs.influxdata.com/influxdb/v2/api/#operation/PostWrite
#Input InfluxDB information here
#Values enclosed in double quotes ""
DBHOST=<InfluxDBHost>
#DBHOST="https://influxdb.yourdomain.com"
DBPORT=<InfluxDBPort>
ORGNAME=<OrganizationName>
BUCKET=<BucketName>
APITOKEN=<APIToken>
while true; do
# Collect the stats
STATS=$(pwrstat -status)
#Gather Values for statistics
LOAD=$(echo -e "$STATS" |grep Load | cat | awk '{print $2}')
RUNTIME=$(echo -e "$STATS" |grep Remaining | cat | awk '{print $3}')
UTILVOLT=$(echo -e "$STATS" |grep "Utility Voltage" | cat | awk '{print $3}')
OUTVOLT=$(echo -e "$STATS" |grep "Output Voltage" | cat | awk '{print $3}')
BATTCAP=$(echo -e "$STATS" |grep "Battery Capacity" | cat | awk '{print $3}')
SUPPLY=$(echo -e "$STATS" |grep "Power Supply" | cat | awk '{print $4}')
STATE=$(echo -e "$STATS" |grep "State" | cat | awk '{print $2}')
RATING=$(echo -e "$STATS" |grep "Rating Power" | cat | awk '{print $3}')
LOADPERCENT=$(echo "scale=2;($LOAD/$RATING)" | bc -l)
#Check Power Supply, 1 = Utility Power, 0 = Battery Power
if [ $SUPPLY == "Utility" ]; then
SUPPLY="1"
else
SUPPLY="0"
fi
#Check State, 1 = Normal, 0 = Other
if [ $STATE == "Normal" ]; then
STATE="1"
else
STATE="0"
fi
#Write out to InfluxDB
/usr/bin/curl -s -i -X POST "$DBHOST:$DBPORT/api/v2/write?org=$ORGNAME&bucket=$BUCKET&precision=ns" \
--header "Authorization: Token $APITOKEN" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary "power_status,host=$HOSTNAME success=0,load=$LOAD,runtime=$RUNTIME,utility_voltage=$UTILVOLT,output_voltage=$OUTVOLT,battery_capacity=$BATTCAP,power_supply=$SUPPLY,power_state=$STATE,ups_rating=$RATING,load_percentage=$LOADPERCENT" > /dev/null
#Wait 2 seconds
sleep 2s
done