Projet

Général

Profil

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

root / plugins / redis / redis_ @ 8fef176a

Historique | Voir | Annoter | Télécharger (8,85 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 Switch;
39

    
40
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
41
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
42
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef;
43
my $TITLE_PREFIX = exists $ENV{'title_prefix'} ? $ENV{'title_prefix'} . ": " : "";
44

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

    
71
my $hash=&get_info();
72

    
73
$0 =~ s/(.+)redis_//g;
74

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

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

    
91

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

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

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

    
141

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

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

    
159

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

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

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

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

    
198
            exit 0;
199
        }
200

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

    
208
close ($sock);
209

    
210
sub get_conn {
211
    my $sock = IO::Socket::INET->new(
212
        PeerAddr => $HOST,
213
        PeerPort => $PORT,
214
        Timeout => 10,
215
        Proto => 'tcp'
216
    );
217
    if ( defined( $PASSWORD )  ) {
218
        print $sock "AUTH ", $PASSWORD, "\r\n";
219
        my $result = <$sock> || die "can't read socket: $!";
220
    }
221
    return $sock;
222
}
223

    
224
sub get_info{
225
    print $sock "INFO\r\n";
226
    my $result = <$sock> || die "can't read socket: $!";
227

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

    
232
    my $hash;
233
    foreach (split(/\r\n/, substr($rep, 0, -2))) {
234
        my ($key,$val) = split(/:/, $_, 2);
235
        if (defined($key)) {
236
            $hash->{$key} = $val;
237
        }
238
    }
239
    return $hash;
240
}
241

    
242
# This subroutine returns configuration matched to supplied as object
243
sub get_config{
244

    
245
    print $sock "*3\r\n\$6\r\nCONFIG\r\n\$3\r\nGET\r\n\$".length($_[0])."\r\n".$_[0]."\r\n";
246
    # Response will look like like
247
    # *2\r\n$9\r\nmaxmemory\r\n$10\r\n3221225472\r\n
248

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

    
251
    my $conf;
252
    if( substr($type,0,1) ne "*" ) {
253
        return $conf;
254
    }
255

    
256
    my $count=substr($type,1);
257

    
258
    my ( $namesize, $name, $valuesize, $value );
259
    while ( $count > 1 ){
260
        $count=$count-2;
261

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

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

    
268
        $conf->{substr($name, 0, -2)}=substr($value, 0, -2);
269
    }
270

    
271
    return $conf;
272
}
273

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