-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_redis.php
180 lines (154 loc) · 3.55 KB
/
check_redis.php
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
#!/usr/bin/env php
<?php
#
# Check redis status
#
# Usage: check_redis [-h host] [-p port] [-a auth] [-C connected_clients | -M used_memory] [-w warning] [-c critical]
# -h redis host
# -p redis port, eg: 6379
# -C Number of clients connected to Redis
# -M Memory used by the Redis server
# -w WARNING Warning value
# -c CRITICAL Critical value
# -H Display this screen
#
# (c) 2018, Dave Modis <[email protected]>
# https://github.com/davemodis/nagios-plugins
#
$host = '127.0.0.1';
$port = 6379;
$auth = false;
$warning = 0;
$critical = 0;
$mode = 0;
for($i = 1; $i < count($argv); $i++)
{
switch ($argv[$i]) {
case '-h':
$host = $argv[$i+1];
break;
case '-p':
$port = (int)$argv[$i+1];
break;
case '-a':
$auth = $argv[$i+1];
break;
case '-w':
$warning = (float)$argv[$i+1];
break;
case '-c':
$critical = (float)$argv[$i+1];
break;
case '-C':
if(!$mode)
$mode = 1;
break;
case '-M':
if(!$mode)
$mode = 2;
// case '-L':
// if(!$mode)
// $mode = 3;
// break;
break;
case '-H':
die('Usage: check_redis [-h host] [-p port] [-C connected_clients | -L latency | -M used_memory] [-w warning] [-c critical]
-h redis host
-p redis port, eg: 6379
-C Number of clients connected to Redis
-M Memory used by the Redis server
-w WARNING Warning value or 0
-c CRITICAL Critical value or 0
-H Display this screen
');
// -L Average time it takes Redis to respond to a query
break;
}
}
if(!$mode)
{
echo("UNKNOWN - Mode is not set\n");
exit(3);
}
if( $warning > $critical )
{
echo("UNKNOWN - warning ($warning) can't be greater than critical ($critical)\n");
exit(3);
}
$redis = new Redis();
$status = @$redis->connect($host, $port);
if( !$status )
{
echo("CRITICAL - could not connect to redis $host:$port\n");
exit(2);
}
if( $auth )
{
$status = $redis->auth($auth);
if( !$status )
{
echo("CRITICAL - auth fail\n");
exit(2);
}
}
switch ($mode) {
case 1:
$info = 'Clients';
break;
case 2:
$info = 'Memory';
break;
}
try{
$status = $redis->rawCommand('INFO', $info);
}
catch(RedisException $e)
{
echo("CRITICAL - ".$e->getMessage()."\n");
exit(2);
}
$status = explode("\n", $status);
array_shift($status);
$gigabite = 1073741824;
foreach ($status as $s) {
$s = explode(':', $s);
if($mode === 1)
{
$s[1] = trim($s[1]);
if( $critical && $s[0] === 'connected_clients' && $critical < (int)$s[1] )
{
echo("CRITICAL - connected_clients {$s[1]} greater then {$critical}\n");
exit(2);
}
if( $warning && $s[0] === 'connected_clients' && $warning < (int)$s[1] )
{
echo("WARNING - connected_clients {$s[1]} greater then {$warning}\n");
exit(1);
}
if( $s[0] === 'connected_clients' )
{
echo("OK - connected_clients {$s[1]}\n");
exit(0);
}
}
if($mode === 2)
{
if( $critical && $s[0] === 'used_memory' && $critical * $gigabite < (int)$s[1])
{
echo("CRITICAL - used_memory ".number_format(((int)$s[1]/$gigabite),2)."G greater then {$critical}G\n");
exit(2);
}
if( $warning && $s[0] === 'used_memory' && $warning * $gigabite < (int)$s[1])
{
echo("WARNING - used_memory ".number_format(((int)$s[1]/$gigabite),2)."G greater then {$warning}G\n");
exit(1);
}
if( $s[0] === 'used_memory_human' )
{
echo("OK - used_memory {$s[1]}\n");
exit(0);
}
}
}
echo("UNKNOWN - Mode is not set\n");
exit(3);