-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis_
executable file
·288 lines (246 loc) · 9.29 KB
/
redis_
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/perl -w
#
## Copyright (C) 2009 Gleb Voronich <http://stanly.net.ua/>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; version 2 dated June,
## 1991.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
##
## $Log$
##
## Based on Redis module code v0.08 2009/from http://svn.rot13.org/index.cgi/Redis
##
## Installation process:
##
## 1. Download the plugin to your plugins directory (e.g. /usr/share/munin/plugins)
## 2. Create 3 symlinks at the directory that us used by munin for plugins detection (e.g. /etc/munin/plugins): redis_connected_clients, redis_per_sec and and redis_used_memory
## 3. Edit plugin-conf.d/munin-node if it is needed (env.host and env.port variables are accepted; set env.password for password protected Redis server)
## 4. Restart munin-node service
##
## Magic Markers
#%# family=auto
#%# capabilities=autoconf suggest
use strict;
use IO::Socket::INET;
use Switch;
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef;
my $sock = &get_conn();
my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );
my $autoconf = ( defined $ARGV[0] and $ARGV[0] eq "autoconf" );
if ( $autoconf ) {
if ( defined( $sock ) ) {
print "yes\n";
exit 0;
} else {
print "no (unable to connect to $HOST\[:$PORT\])\n";
exit 0;
}
}
my $suggest = ( defined $ARGV[0] and $ARGV[0] eq "suggest" );
if ( $suggest ) {
if ( defined( $sock ) ) {
my @plugins = ('connected_clients', 'key_ratio', 'keys_per_sec', 'per_sec', 'used_keys', 'used_memory');
foreach my $plugin (@plugins) {
print "$plugin\n";
}
exit 0;
} else {
print "no (unable to connect to $HOST\[:$PORT\])\n";
exit 0;
}
}
my $hash=&get_info();
$0 =~ s/(.+)redis_//g;
switch ($0) {
case "connected_clients" {
if ( $config ) {
my $maxclients= get_config("maxclients")->{"maxclients"};
print "graph_title Connected clients\n";
print "graph_vlabel Connected clients\n";
print "graph_category redis\n";
print "graph_args -l 0\n";
print "connected_clients.line $maxclients:ff0000:Limit\n";
print "connected_clients.label connected clients\n";
exit 0;
}
print "connected_clients.value " . $hash->{'connected_clients'} . "\n";
}
case "keys_per_sec" {
if ( $config ) {
print "graph_title Keys Per Second\n";
print "graph_vlabel per \${graph_period}\n";
print "graph_category redis\n";
print "graph_args -l 0\n";
print "hits.label hits\n";
print "hits.type COUNTER\n";
print "misses.label misses\n";
print "misses.type COUNTER\n";
print "expired.label expirations\n";
print "expired.type COUNTER\n";
print "evictions.label evictions\n";
print "evictions.type COUNTER\n";
exit 0;
}
print "hits.value " . $hash->{'keyspace_hits'} . "\n";
print "misses.value " . $hash->{'keyspace_misses'} . "\n";
print "expired.value " . $hash->{'expired_keys'} . "\n";
print "evictions.value " . $hash->{'evicted_keys'} . "\n";
}
case "key_ratio" {
if ( $config ) {
print "graph_title Key Hit vs Miss Ratio\n";
print "graph_vlabel per \${graph_period}\n";
print "graph_category redis\n";
print "graph_args -u 100 -l 0 -r --base 1000\n";
print "hitratio.label hit ratio\n";
print "hitratio.type GAUGE\n";
print "hitratio.draw AREA\n";
print "missratio.label miss ratio\n";
print "missratio.type GAUGE\n";
print "missratio.draw STACK\n";
exit 0;
}
my $total = $hash->{'keyspace_hits'} + $hash->{'keyspace_misses'};
my $hitratio = 0;
my $missratio = 0;
if ($total > 0) {
$hitratio = $hash->{'keyspace_hits'} / $total * 100;
$missratio = $hash->{'keyspace_misses'} / $total * 100;
}
printf("hitratio.value %.2f\n", $hitratio);
printf("missratio.value %.2f\n", $missratio);
}
case "per_sec" {
if ( $config ) {
print "graph_title Per second\n";
print "graph_vlabel per \${graph_period}\n";
print "graph_category redis\n";
print "graph_args -l 0\n";
print "requests.label requests\n";
print "requests.type COUNTER\n";
print "connections.label connections\n";
print "connections.type COUNTER\n";
exit 0;
}
print "requests.value ". $hash->{'total_commands_processed'} ."\n";
print "connections.value ". $hash->{'total_connections_received'} ."\n";
}
case "used_memory" {
if ( $config ) {
my $maxmemory = get_config("maxmemory")->{"maxmemory"};
print "graph_title Used memory\n";
print "graph_vlabel Used memory\n";
print "graph_category redis\n";
print "graph_args -l 0 --base 1024\n";
print "used_memory.line $maxmemory:ff0000:Limit\n";
print "used_memory.label used memory\n";
print "used_memory_peak.label used memory in peak\n";
print "used_memory_rss.label Resident set size memory usage\n";
exit 0;
}
print "used_memory.value ". $hash->{'used_memory'} ."\n";
print "used_memory_rss.value ". $hash->{'used_memory_rss'} ."\n";
print "used_memory_peak.value ". $hash->{'used_memory_peak'} ."\n";
}
case "used_keys" {
my $dbs;
foreach my $key (keys %{$hash}) {
if ( $key =~ /^db\d+$/ && $hash->{$key} =~ /keys=(\d+),expires=(\d+)/ ) {
$dbs->{$key} = [ $1, $2 ];
}
}
if ( $config ) {
print "graph_title Used keys\n";
print "graph_vlabel Used keys\n";
print "graph_category redis\n";
print "graph_args -l 0\n";
foreach my $db (keys %{$dbs}) {
printf "%s_keys.label %s keys\n", $db, $db;
printf "%s_expires.label %s expires\n", $db, $db;
}
exit 0;
}
foreach my $db (keys %{$dbs}) {
printf "%s_keys.value %d\n", $db, $dbs->{$db}[0];
printf "%s_expires.value %d\n", $db, $dbs->{$db}[1];
}
}
case "replication_status" {
if ( $config ) {
print "graph_title Replication Status\n";
print "graph_vlabel Seconds of last interaction\n";
print "graph_category redis\n";
print "graph_args -l 0\n";
print "master_last_io_seconds_ago.warning 0:20\n";
print "master_last_io_seconds_ago.critical 0:60\n";
print "master_last_io_seconds_ago.label Seconds since the last interaction with master\n";
exit 0;
}
print "master_last_io_seconds_ago.value ". $hash->{'master_last_io_seconds_ago'} ."\n";
}
}
close ($sock);
sub get_conn {
my $sock = IO::Socket::INET->new(
PeerAddr => $HOST,
PeerPort => $PORT,
Timeout => 10,
Proto => 'tcp'
);
if ( defined( $PASSWORD ) ) {
print $sock "AUTH ", $PASSWORD, "\r\n";
my $result = <$sock> || die "can't read socket: $!";
}
return $sock;
}
sub get_info{
print $sock "INFO\r\n";
my $result = <$sock> || die "can't read socket: $!";
my $rep;
# +2 characters for \r\n at end of the data block
read($sock, $rep, substr($result,1)+2) || die "can't read from socket: $!";
my $hash;
foreach (split(/\r\n/, substr($rep, 0, -2))) {
my ($key,$val) = split(/:/, $_, 2);
if (defined($key)) {
$hash->{$key} = $val;
}
}
return $hash;
}
# This subroutine returns configuration matched to supplied as object
sub get_config{
print $sock "*3\r\n\$6\r\nCONFIG\r\n\$3\r\nGET\r\n\$".length($_[0])."\r\n".$_[0]."\r\n";
# Response will look like like
# *2\r\n$9\r\nmaxmemory\r\n$10\r\n3221225472\r\n
my $type = <$sock> || die "can't read socket: $!";
my $conf;
if( substr($type,0,1) ne "*" ) {
return $conf;
}
my $count=substr($type,1);
my ( $namesize, $name, $valuesize, $value );
while ( $count > 1 ){
$count=$count-2;
$namesize=<$sock>;
read($sock, $name, substr($namesize,1)+2) || die "can't read from socket: $!";
$valuesize=<$sock>;
read($sock, $value, substr($valuesize,1)+2) || die "can't read from socket: $!";
$conf->{substr($name, 0, -2)}=substr($value, 0, -2);
}
return $conf;
}
# vim: ft=perl ai ts=4 sw=4 et: