summaryrefslogtreecommitdiff
path: root/usr-lib-nagios-plugins/check_apcupsd
blob: b68b82311705bb6bd7c6139b791e53dc360908d4 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/sh
#
# check_apcupsd 1.2
# Nagios plugin to monitor APC Smart-UPSes using apcupsd.
#
# Copyright (c) 2008 Martin Toft <mt@martintoft.dk>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#
# Example configuration
#
# commands.cfg:
#
# define command {
#         command_name check_apcupsd
#         command_line $USER1$/check_apcupsd -w $ARG2$ -c $ARG3$ $ARG1$
#         }
# 
# define command {
#         command_name check_apcupsd_no_notify
#         command_line $USER1$/check_apcupsd $ARG1$
#         }
#
# ups1.cfg:
#
# define service {
#         use generic-service
#         host_name ups1
#         service_description CHARGE
#         check_command check_apcupsd!bcharge!95!50
#         }
# 
# define service {
#         use generic-service
#         host_name ups1
#         service_description TEMP
#         check_command check_apcupsd!itemp!35!40
#         }
# 
# define service {
#         use generic-service
#         host_name ups1
#         service_description LOAD
#         check_command check_apcupsd!loadpct!70!85
#         }
# 
# define service {
#         use generic-service
#         host_name ups1
#         service_description TIMELEFT
#         check_command check_apcupsd_no_notify!timeleft
#         }

APCACCESS=/sbin/apcaccess

usage()
{
    echo "usage: check_apcupsd [-c critical_value] [-h hostname] [-p port]"
    echo -n "                     [-w warning_value] "
    echo "<bcharge|itemp|loadpct|timeleft>"
    echo
    echo "hostname and port defaults to localhost and 3551, respectively."
    echo
    echo "checks:"
    echo "    bcharge  = battery charge, measured in percent."
    echo "    itemp    = internal temperature, measured in degree Celcius."
    echo "    loadpct  = load percent, measured in percent (do'h!)."
    echo "    timeleft = time left with current battery charge and load,"
    echo "               measured in minutes."
    exit 3
}

HOSTNAME=localhost
PORT=3551

while getopts c:h:p:w: OPTNAME; do
    case "$OPTNAME" in
    h)
	HOSTNAME="$OPTARG"
	;;
    p)
	PORT="$OPTARG"
	;;
    w)
	WARNVAL="$OPTARG"
	;;
    c)
	CRITVAL="$OPTARG"
	;;
    *)
	usage
	;;
    esac
done

ARG="$@"
while [ $OPTIND -gt 1 ]; do
    ARG=`echo "$ARG" | sed 's/^[^ ][^ ]* *//'`
    OPTIND=$(($OPTIND-1))
done

if [ "$ARG" != "bcharge" -a "$ARG" != "itemp" -a "$ARG" != "loadpct" \
    -a "$ARG" != "timeleft" ]; then
    usage
fi

if [ "`echo $PORT | grep '^[0-9][0-9]*$'`" = "" ]; then
    echo "Error: port must be a positive integer!"
    exit 3
fi

if [ "$WARNVAL" != "" -a "`echo $WARNVAL | grep '^[0-9][0-9]*$'`" = "" ]; then
    echo "Error: warning_value must be a positive integer!"
    exit 3
fi

if [ "$CRITVAL" != "" -a "`echo $CRITVAL | grep '^[0-9][0-9]*$'`" = "" ]; then
    echo "Error: critical_value must be a positive integer!"
    exit 3
fi

if [ "$WARNVAL" != "" -a "$CRITVAL" != "" ]; then
    if [ "$ARG" = "bcharge" -o "$ARG" = "timeleft" ]; then
	if [ $WARNVAL -le $CRITVAL ]; then
	    echo "Error: warning_value must be greater than critical_value!"
	    exit 3
	fi
    else
	if [ $WARNVAL -ge $CRITVAL ]; then
	    echo "Error: warning_value must be less than critical_value!"
	    exit 3
	fi
    fi
fi

if [ ! -x "$APCACCESS" ]; then
    echo "Error: $APCACCESS must exist and be executable!"
    exit 3
fi

$APCACCESS status $HOSTNAME:$PORT > /dev/null
if [ $? -ne 0 ]; then
    # The error message from apcaccess will do fine.
    exit 3
fi

VALUE=`$APCACCESS status $HOSTNAME:$PORT | grep -i ^$ARG | \
    sed 's/.*:  *\([0-9.][0-9.]*\)[^0-9.].*/\1/'`
if [ "$VALUE" != "0" ]; then
    VALUE=`echo $VALUE | sed 's/^0*//'`
fi
ROUNDED=`echo $VALUE | sed 's/\..*//'`

case "$ARG" in
bcharge)
    if [ "$CRITVAL" != "" ]; then
	if [ $ROUNDED -lt $CRITVAL ]; then
	    echo "CRITICAL - Battery Charge: ${VALUE}%"
	    exit 2
	fi
    fi
    if [ "$WARNVAL" != "" ]; then
	if [ $ROUNDED -lt $WARNVAL ]; then
	    echo "WARNING - Battery Charge: ${VALUE}%"
	    exit 1
	fi
    fi
    echo "OK - Battery Charge: ${VALUE}%"
    ;;
itemp)
    if [ "$CRITVAL" != "" ]; then
	if [ $ROUNDED -ge $CRITVAL ]; then
	    echo "CRITICAL - Internal Temperature: $VALUE C"
	    exit 2
	fi
    fi
    if [ "$WARNVAL" != "" ]; then
	if [ $ROUNDED -ge $WARNVAL ]; then
	    echo "WARNING - Internal Temperature: $VALUE C"
	    exit 1
	fi
    fi
    echo "OK - Internal Temperature: $VALUE C"
    ;;
loadpct)
    if [ "$CRITVAL" != "" ]; then
	if [ $ROUNDED -ge $CRITVAL ]; then
	    echo "CRITICAL - Load: ${VALUE}%"
	    exit 2
	fi
    fi
    if [ "$WARNVAL" != "" ]; then
	if [ $ROUNDED -ge $WARNVAL ]; then
	    echo "WARNING - Load: ${VALUE}%"
	    exit 1
	fi
    fi
    echo "OK - Load: ${VALUE}%"
    ;;
timeleft)
    if [ "$CRITVAL" != "" ]; then
	if [ $ROUNDED -lt $CRITVAL ]; then
	    echo "CRITICAL - Time Left: $VALUE Minutes"
	    exit 2
	fi
    fi
    if [ "$WARNVAL" != "" ]; then
	if [ $ROUNDED -lt $WARNVAL ]; then
	    echo "WARNING - Time Left: $VALUE Minutes"
	    exit 1
	fi
    fi
    echo "OK - Time Left: $VALUE Minutes"
    ;;
esac

exit 0