cpupssmon.sh 4.14 KB
Newer Older
Curt Dennis's avatar
Curt Dennis committed
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
#!/bin/bash

#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=$(echo $involts | grep -o '[0-9]*')
    outvolts=$(echo $involts | grep -o '[0-9]*')
    runtime=$(echo $runtime | grep -o '[0-9]*')
    capacity=$(echo $capacity | grep -o '[0-9]*')
    loadwatt=$(echo $loadwatt | grep -o '[0-9]*')
    loadpercent=$(echo $loadpercent | grep -o '[0-9]*')
    
    echo "state: $state"
    echo "supply: $supply"
    echo "involts: $involts"
    echo "outvolts: $outvolts"
    echo "runtime: $runtime"
    echo "capacity: $capacity"
    echo "loadwatt: $loadwatt"
    echo "loadpercent: $loadpercent"
    echo "interaction: $interaction"
    
    #Finally we can write it to the database
    curl -i -XPOST 'http://10.0.0.11:8086/write?db=home' --data-binary "ups_data,host=host1,sensor=battcharge value=$capacity"
    curl -i -XPOST 'http://10.0.0.11:8086/write?db=home' --data-binary "ups_data,host=host1,sensor=battload value=$loadpercent"
    curl -i -XPOST 'http://10.0.0.11:8086/write?db=home' --data-binary "ups_data,host=host1,sensor=battloadwatt value=$loadwatt"
    curl -i -XPOST 'http://10.0.0.11:8086/write?db=home' --data-binary "ups_data,host=host1,sensor=inputvoltage value=$involts"
    curl -i -XPOST 'http://10.0.0.11:8086/write?db=home' --data-binary "ups_data,host=host1,sensor=runtime value=$runtime"
    
    #Wait for a bit before checking again
    sleep "$sleeptime"
    
done