forked from danodemano/monitoring-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupsmon.sh
132 lines (118 loc) · 4.03 KB
/
upsmon.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh
#This script gets the current power information
#from the main UPS
#The time we are going to sleep between readings
sleeptime=30
#Prepare to start the loop and warn the user
echo "Press [CTRL+C] to stop..."
while :
do
#Get the current dump of the stats
upsstats=`pwrstat -status`
#Lets try to find the lines we are looking for
while read -r line; do
#Check if we have the line we are looking for
if [[ $line == *"State."* ]]
then
state=$line
fi
if [[ $line == *"Power Supply by"* ]]
then
supply=$line
fi
if [[ $line == *"Utility Voltage"* ]]
then
involts=$line
fi
if [[ $line == *"Output Voltage"* ]]
then
outvolts=$line
fi
if [[ $line == *"Battery Capacity"* ]]
then
capacity=$line
fi
if [[ $line == *"Remaining Runtime"* ]]
then
runtime=$line
fi
if [[ $line == *"Load."* ]]
then
load=$line
fi
if [[ $line == *"Line Interaction"* ]]
then
interaction=$line
fi
done <<< "$upsstats"
#Remove the long string of .s
state=$(echo $state | tr -s '[.]')
supply=$(echo $supply | tr -s '[.]')
involts=$(echo $involts | tr -s '[.]')
outvolts=$(echo $outvolts | tr -s '[.]')
capacity=$(echo $capacity | tr -s '[.]')
runtime=$(echo $runtime | tr -s '[.]')
load=$(echo $load | tr -s '[.]')
interaction=$(echo $interaction | tr -s '[.]')
#Lets parse out thevalues from the strings
#First split on the .
IFS='.' read -ra statearr <<< "$state"
state=${statearr[1]}
IFS='.' read -ra supplyarr <<< "$supply"
supply=${supplyarr[1]}
IFS='.' read -ra involtsarr <<< "$involts"
involts=${involtsarr[1]}
IFS='.' read -ra outvoltsarr <<< "$outvolts"
outvolts=${outvoltsarr[1]}
IFS='.' read -ra capacityarr <<< "$capacity"
capacity=${capacityarr[1]}
IFS='.' read -ra runtimearr <<< "$runtime"
runtime=${runtimearr[1]}
IFS='.' read -ra loadarr <<< "$load"
load=${loadarr[1]}
IFS='.' read -ra interactionarr <<< "$interaction"
interaction=${interactionarr[1]}
#We need an extra split for the load
IFS='(' read -ra loadarr <<< "$load"
loadwatt=${loadarr[0]}
loadpercent=${loadarr[1]}
#Remove unneeded spaces
state=$(echo $state | xargs)
supply=$(echo $supply | xargs)
involts=$(echo $involts | xargs)
outvolts=$(echo $outvolts | xargs)
capacity=$(echo $capacity | xargs)
runtime=$(echo $runtime | xargs)
loadwatt=$(echo $loadwatt | xargs)
loadpercent=$(echo $loadpercent | xargs)
interaction=$(echo $interaction | xargs)
#Now we just strip off some of the unneeded
#info from the end of the strings
involts=${involts::-2}
outvolts=${outvolts::-2}
runtime=${runtime::-4}
capacity=${capacity::-2}
loadwatt=${loadwatt::-5}
loadpercent=${loadpercent::-3}
echo "$state"
echo "$supply"
echo "$involts"
echo "$outvolts"
echo "$runtime"
echo "$capacity"
echo "$loadwatt"
echo "$loadpercent"
echo "$interaction"
#Finally we can write it to the database
#curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=state value=$state"
#curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=supply value=$supply"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=involts value=$involts"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=outvolts value=$outvolts"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=runtime value=$runtime"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=capacity value=$capacity"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=loadwatt value=$loadwatt"
curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=loadpercent value=$loadpercent"
#curl -i -XPOST 'http://localhost:8086/write?db=home' --data-binary "ups_stats,ups=primary_stack,metric=interaction value=$interaction"
#Wait for a bit before checking again
sleep "$sleeptime"
done