Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / redis / redis_ @ f41b6861

Historique | Voir | Annoter | Télécharger (9,12 ko)

1
#!/usr/bin/perl -w
2

    
3
#
4
## Copyright (C) 2009 Gleb Voronich <http://stanly.net.ua/>
5
##
6
## This program is free software; you can redistribute it and/or
7
## modify it under the terms of the GNU General Public License
8
## as published by the Free Software Foundation; version 2 dated June,
9
## 1991.
10
##
11
## This program is distributed in the hope that it will be useful,
12
## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
## GNU General Public License for more details.
15
##
16
## You should have received a copy of the GNU General Public License
17
## along with this program; if not, write to the Free Software
18
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
##
20
##
21
## $Log$
22
##
23
## Based on Redis module code v0.08 2009/from http://svn.rot13.org/index.cgi/Redis
24
##
25
## Installation process:
26
##
27
## 1. Download the plugin to your plugins directory (e.g. /usr/share/munin/plugins)
28
## 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
29
## 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)
30
## 4. Restart munin-node service
31
##
32
## Magic Markers
33
#%# family=auto
34
#%# capabilities=autoconf suggest
35

    
36
use strict;
37
use IO::Socket::INET;
38
use IO::Socket::UNIX;
39
use Switch;
40

    
41
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
42
my $UNIX_SOCKET = exists $ENV{'unixsocket'} ? $ENV{'unixsocket'} : ''; # path to Redis Unix sock file
43
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
44
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef;
45
my $TITLE_PREFIX = exists $ENV{'title_prefix'} ? $ENV{'title_prefix'} . ": " : "";
46

    
47
my $sock = &get_conn();
48
my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );
49
my $autoconf = ( defined $ARGV[0] and $ARGV[0] eq "autoconf" );
50
if ( $autoconf ) {
51
    if ( defined( $sock ) ) {
52
        print "yes\n";
53
        exit 0;
54
    } else {
55
        print "no (unable to connect to $HOST\[:$PORT\])\n";
56
        exit 0;
57
    }
58
}
59
my $suggest = ( defined $ARGV[0] and $ARGV[0] eq "suggest" );
60
if ( $suggest ) {
61
    if ( defined( $sock ) ) {
62
        my @plugins = ('connected_clients', 'key_ratio', 'keys_per_sec', 'per_sec', 'used_keys', 'used_memory');
63
        foreach my $plugin (@plugins) {
64
            print "$plugin\n";
65
        }
66
        exit 0;
67
    } else {
68
        print "no (unable to connect to $HOST\[:$PORT\])\n";
69
        exit 0;
70
    }
71
}
72

    
73
my $hash=&get_info();
74

    
75
$0 =~ s/(.+)redis_//g;
76

    
77
switch ($0) {
78
    case "connected_clients" {
79
        if ( $config ) {
80
            my $maxclients= get_config("maxclients")->{"maxclients"};
81
            print "graph_title ${TITLE_PREFIX}Connected clients\n";
82
            print "graph_vlabel Connected clients\n";
83
            print "graph_category search\n";
84
            print "graph_args -l 0\n";
85
            print "connected_clients.line $maxclients:ff0000:Limit\n";
86
            print "connected_clients.label connected clients\n";
87
            exit 0;
88
        }
89

    
90
        print "connected_clients.value " . $hash->{'connected_clients'} . "\n";
91
    }
92

    
93

    
94
    case "keys_per_sec" {
95
        if ( $config ) {
96
            print "graph_title ${TITLE_PREFIX}Keys Per Second\n";
97
            print "graph_vlabel per \${graph_period}\n";
98
            print "graph_category search\n";
99
            print "graph_args -l 0\n";
100
            print "hits.label hits\n";
101
            print "hits.type COUNTER\n";
102
            print "misses.label misses\n";
103
            print "misses.type COUNTER\n";
104
            print "expired.label expirations\n";
105
            print "expired.type COUNTER\n";
106
            print "evictions.label evictions\n";
107
            print "evictions.type COUNTER\n";
108
            exit 0;
109
        }
110

    
111
        print "hits.value " . $hash->{'keyspace_hits'} . "\n";
112
        print "misses.value " . $hash->{'keyspace_misses'} . "\n";
113
        print "expired.value " . $hash->{'expired_keys'} . "\n";
114
        print "evictions.value " . $hash->{'evicted_keys'} . "\n";
115
    }
116

    
117
    case "key_ratio" {
118
        if ( $config ) {
119
            print "graph_title ${TITLE_PREFIX}Key Hit vs Miss Ratio\n";
120
            print "graph_vlabel per \${graph_period}\n";
121
            print "graph_category search\n";
122
            print "graph_args -u 100 -l 0 -r --base 1000\n";
123
            print "hitratio.label hit ratio\n";
124
            print "hitratio.type GAUGE\n";
125
            print "hitratio.draw AREA\n";
126
            print "missratio.label miss ratio\n";
127
            print "missratio.type GAUGE\n";
128
            print "missratio.draw STACK\n";
129
            exit 0;
130
        }
131
            
132
        my $total = $hash->{'keyspace_hits'} + $hash->{'keyspace_misses'};
133
        my $hitratio = 0;
134
        my $missratio = 0;
135
        if ($total > 0) {
136
            $hitratio = $hash->{'keyspace_hits'} / $total * 100;
137
            $missratio = $hash->{'keyspace_misses'} / $total * 100;
138
        }
139
        printf("hitratio.value %.2f\n", $hitratio);
140
        printf("missratio.value %.2f\n", $missratio);
141
    }
142

    
143

    
144
    case "per_sec" {
145
        if ( $config ) {
146
            print "graph_title ${TITLE_PREFIX}Per second\n";
147
            print "graph_vlabel per \${graph_period}\n";
148
            print "graph_category search\n";
149
            print "graph_args -l 0\n";
150
            print "requests.label requests\n";
151
            print "requests.type COUNTER\n";
152
            print "connections.label connections\n";
153
            print "connections.type COUNTER\n";
154
            exit 0;
155
        }
156

    
157
        print "requests.value ". $hash->{'total_commands_processed'} ."\n";
158
        print "connections.value ". $hash->{'total_connections_received'} ."\n";
159
    }
160

    
161

    
162
    case "used_memory" {
163
        if ( $config ) {
164
            my $maxmemory = get_config("maxmemory")->{"maxmemory"};
165
            print "graph_title ${TITLE_PREFIX}Used memory\n";
166
            print "graph_vlabel Used memory\n";
167
            print "graph_category search\n";
168
            print "graph_args -l 0 --base 1024\n";
169
            print "used_memory.line $maxmemory:ff0000:Limit\n";
170
            print "used_memory.label used memory\n";
171
            print "used_memory_peak.label used memory in peak\n";
172
            print "used_memory_rss.label Resident set size memory usage\n";
173
            exit 0;
174
        }
175

    
176
        print "used_memory.value ". $hash->{'used_memory'}  ."\n";
177
        print "used_memory_rss.value ". $hash->{'used_memory_rss'}  ."\n";
178
        print "used_memory_peak.value ". $hash->{'used_memory_peak'}  ."\n";
179
    }
180
    
181
    case "used_keys" {
182
        my $dbs;
183
        foreach my $key (keys %{$hash}) {
184
            if ( $key =~ /^db\d+$/ && $hash->{$key} =~ /keys=(\d+),expires=(\d+)/ ) {
185
                $dbs->{$key} = [ $1, $2 ];
186
            }
187
        }
188

    
189
        if ( $config ) {
190
            print "graph_title ${TITLE_PREFIX}Used keys\n";
191
            print "graph_vlabel Used keys\n";
192
            print "graph_category search\n";
193
            print "graph_args -l 0\n";
194

    
195
            foreach my $db (keys %{$dbs}) {
196
                printf "%s_keys.label %s keys\n", $db, $db;
197
                printf "%s_expires.label %s expires\n", $db, $db;
198
            }
199

    
200
            exit 0;
201
        }
202

    
203
        foreach my $db (keys %{$dbs}) {
204
            printf "%s_keys.value %d\n", $db, $dbs->{$db}[0];
205
            printf "%s_expires.value %d\n", $db, $dbs->{$db}[1];
206
        }
207
    }
208
}
209

    
210
close ($sock);
211

    
212
sub get_conn {
213
    
214
    my $sock;
215
    
216
    if( $UNIX_SOCKET && -S $UNIX_SOCKET ){
217
	
218
	$sock = IO::Socket::UNIX->new(
219
		Type => SOCK_STREAM(),
220
		Peer => $UNIX_SOCKET,
221
	);
222
	
223
    }else{
224
	
225
	$sock = IO::Socket::INET->new(
226
		PeerAddr => $HOST,
227
		PeerPort => $PORT,
228
		Timeout => 10,
229
		Proto => 'tcp'
230
	);
231
    }
232
    
233
    if ( defined( $PASSWORD )  ) {
234
        print $sock "AUTH ", $PASSWORD, "\r\n";
235
        my $result = <$sock> || die "can't read socket: $!";
236
    }
237
    return $sock;
238
}
239

    
240
sub get_info{
241
    print $sock "INFO\r\n";
242
    my $result = <$sock> || die "can't read socket: $!";
243

    
244
    my $rep;
245
    # +2 characters for \r\n at end of the data block
246
    read($sock, $rep, substr($result,1)+2) || die "can't read from socket: $!";
247

    
248
    my $hash;
249
    foreach (split(/\r\n/, substr($rep, 0, -2))) {
250
        my ($key,$val) = split(/:/, $_, 2);
251
        if (defined($key)) {
252
            $hash->{$key} = $val;
253
        }
254
    }
255
    return $hash;
256
}
257

    
258
# This subroutine returns configuration matched to supplied as object
259
sub get_config{
260

    
261
    print $sock "*3\r\n\$6\r\nCONFIG\r\n\$3\r\nGET\r\n\$".length($_[0])."\r\n".$_[0]."\r\n";
262
    # Response will look like like
263
    # *2\r\n$9\r\nmaxmemory\r\n$10\r\n3221225472\r\n
264

    
265
    my $type = <$sock> || die "can't read socket: $!";
266

    
267
    my $conf;
268
    if( substr($type,0,1) ne "*" ) {
269
        return $conf;
270
    }
271

    
272
    my $count=substr($type,1);
273

    
274
    my ( $namesize, $name, $valuesize, $value );
275
    while ( $count > 1 ){
276
        $count=$count-2;
277

    
278
        $namesize=<$sock>;
279
        read($sock, $name, substr($namesize,1)+2) || die "can't read from socket: $!";
280

    
281
        $valuesize=<$sock>;
282
        read($sock, $value, substr($valuesize,1)+2) || die "can't read from socket: $!";
283

    
284
        $conf->{substr($name, 0, -2)}=substr($value, 0, -2);
285
    }
286

    
287
    return $conf;
288
}
289

    
290
# vim: ft=perl ai ts=4 sw=4 et: