Skip to content

Commit 13d19c2

Browse files
committed
Add a plugin to check if Redis is up and running.
This script come from this Git repository https://github.com/mabitt/mab-nagios-plugins
1 parent d54dd06 commit 13d19c2

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

check_redis.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/usr/bin/env sh
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 2 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
PROGNAME=$(basename $0)
18+
VERSION='Version 0.1.0'
19+
AUTHOR='MAB ([email protected]), Based on Mike Adolphs (http://www.matejunkie.com/) check_nginx.sh code'
20+
21+
hostname='127.0.0.1'
22+
port=6379
23+
output_dir='/tmp'
24+
secure=0
25+
26+
print_version() {
27+
echo "$VERSION $AUTHOR"
28+
}
29+
30+
print_help() {
31+
print_version $PROGNAME $VERSION
32+
echo ""
33+
echo "$PROGNAME is a Nagios plugin to check redis. (http://redis.io)"
34+
echo "It also parses the redis info output to get used memory, changes "
35+
echo "current connections, etc..."
36+
echo ""
37+
echo "$PROGNAME -H localhost -P 6379 -o /tmp [-w INT] [-c INT]"
38+
echo ""
39+
echo "Options:"
40+
echo " -H/--hostname)"
41+
echo " Defines the hostname. Default is: localhost"
42+
echo " -P/--port)"
43+
echo " Defines the port. Default is: 80"
44+
echo " -p/--password)"
45+
echo " Name of the server's status page defined in the location"
46+
echo " directive of your nginx configuration. Default is:"
47+
echo " -o/--output-directory)"
48+
echo " Specifies where to write the tmp-file that the check creates."
49+
echo " Default is: /tmp"
50+
echo " -w/--warning)"
51+
echo " Sets a warning level for used_memory. Default is: off"
52+
echo " -c/--critical)"
53+
echo " Sets a critical level for used_memory. Default is:"
54+
echo " off"
55+
exit 3
56+
}
57+
58+
while test -n "$1"; do
59+
case "$1" in
60+
-help|-h)
61+
print_help
62+
exit 3
63+
;;
64+
--version|-v)
65+
print_version $PROGNAME $VERSION
66+
exit 3
67+
;;
68+
--hostname|-H)
69+
hostname=$2
70+
shift
71+
;;
72+
--port|-P)
73+
port=$2
74+
shift
75+
;;
76+
--password|-p)
77+
secure=1
78+
password=$2
79+
;;
80+
--output-directory|-o)
81+
output_dir=$2
82+
shift
83+
;;
84+
--warning|-w)
85+
warning=$2
86+
shift
87+
;;
88+
--critical|-c)
89+
critical=$2
90+
shift
91+
;;
92+
*)
93+
echo "Unknown argument: $1"
94+
print_help
95+
exit 3
96+
;;
97+
esac
98+
shift
99+
done
100+
101+
get_wcdiff() {
102+
if [ ! -z "$warning" -a ! -z "$critical" ]
103+
then
104+
wclvls=1
105+
if [ ${warning} -gt ${critical} ]
106+
then
107+
wcdiff=1
108+
fi
109+
elif [ ! -z "$warning" -a -z "$critical" ]
110+
then
111+
wcdiff=2
112+
elif [ -z "$warning" -a ! -z "$critical" ]
113+
then
114+
wcdiff=3
115+
fi
116+
}
117+
118+
val_wcdiff() {
119+
if [ "$wcdiff" = 1 ]
120+
then
121+
echo "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"
122+
exit 3
123+
elif [ "$wcdiff" = 2 ]
124+
then
125+
echo "Please also set a critical value when you want to use warning/critical thresholds!"
126+
exit 3
127+
elif [ "$wcdiff" = 3 ]
128+
then
129+
echo "Please also set a warning value when you want to use warning/critical thresholds!"
130+
exit 3
131+
fi
132+
}
133+
134+
check_pid() {
135+
if [ -f "$path_pid/$name_pid" ]
136+
then
137+
retval=0
138+
else
139+
retval=1
140+
fi
141+
}
142+
143+
get_status() {
144+
filename=${output_dir}/${PROGNAME}-${hostname}.1
145+
if [ "$secure" = 1 ]
146+
then
147+
redis-cli -h $hostname -p $port -a $password info > ${filename}
148+
else
149+
redis-cli -h $hostname -p $port info > ${filename}
150+
fi
151+
}
152+
153+
get_vals() {
154+
used_memory=$(grep used_memory ${filename} | grep -v human | awk -F: '{print $2}' | tr -d '\r')
155+
used_memory_human=$(grep used_memory_human ${filename} | awk -F: '{print $2}' | tr -d '\r')
156+
changes_since_last_save=$(grep changes_since_last_save ${filename} | awk -F: '{print $2}' | tr -d '\r')
157+
connected_clients=$(grep connected_clients ${filename} | awk -F: '{print $2}' | tr -d '\r')
158+
connected_slaves=$(grep connected_slaves ${filename} | awk -F: '{print $2}' | tr -d '\r')
159+
uptime_in_days=$(grep uptime_in_days ${filename} | awk -F: '{print $2}' | tr -d '\r')
160+
db0keys=$(grep db0 ${output_dir}/$PROGNAME-${hostname}.1 | awk -F, '{print $1}' | awk -F= '{print $2}' | tr -d '\r')
161+
db0expires=$(grep db0 ${output_dir}/$PROGNAME-${hostname}.1 | awk -F, '{print $2}' | awk -F= '{print $2}' | tr -d '\r')
162+
163+
rm -f ${filename}
164+
}
165+
166+
do_output() {
167+
output="Redis is using \
168+
$used_memory_human of RAM; \
169+
$uptime_in_days days up; \
170+
$changes_since_last_save Changes; \
171+
$connected_clients Clients; \
172+
$connected_slaves Slaves; \
173+
DB0 ($db0keys keys $db0expires expires)"
174+
}
175+
176+
do_perfdata() {
177+
perfdata="'Memory'=$used_memory_human 'Clients'=$connected_clients 'DB0.keys'=$db0keys"
178+
}
179+
180+
# Here we go!
181+
get_wcdiff
182+
val_wcdiff
183+
get_status
184+
185+
if [ ! -s "$filename" ]; then
186+
echo "CRITICAL - Could not connect to server"
187+
exit 2
188+
else
189+
get_vals
190+
if [ -z "$used_memory_human" ]; then
191+
echo "CRITICAL - Error parsing server output"
192+
exit 2
193+
else
194+
do_output
195+
do_perfdata
196+
fi
197+
fi
198+
199+
if [ -n "$warning" -a -n "$critical" ]
200+
then
201+
if [ "$used_memory" -ge "$warning" -a "$used_memory" -lt "$critical" ]
202+
then
203+
echo "WARNING - ${output} | ${perfdata}"
204+
exit 2
205+
elif [ "$used_memory" -ge "$critical" ]
206+
then
207+
echo "CRITICAL - ${output} | ${perfdata}"
208+
exit 2
209+
else
210+
echo "OK - ${output} | ${perfdata}"
211+
exit 0
212+
fi
213+
else
214+
echo "OK - ${output} | ${perfdata}"
215+
exit 0
216+
fi

0 commit comments

Comments
 (0)