Projet

Général

Profil

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

root / plugins / redis / redis_ @ cd58f87e

Historique | Voir | Annoter | Télécharger (6,96 ko)

1 b54782b7 Gleb Voronich
#!/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 a7be1b20 Vadim Istratov
## 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 b54782b7 Gleb Voronich
## 4. Restart munin-node service
31 016400bd Matt West
##
32
## Magic Markers
33
#%# family=auto
34
#%# capabilities=autoconf suggest
35 b54782b7 Gleb Voronich
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 a7be1b20 Vadim Istratov
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef;
43 b54782b7 Gleb Voronich
44 016400bd Matt West
my $sock = &get_conn();
45
my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );
46
my $autoconf = ( defined $ARGV[0] and $ARGV[0] eq "autoconf" );
47
if ( $autoconf ) {
48
    if ( defined( $sock ) ) {
49
        print "yes\n";
50
        exit 0;
51
    } else {
52
        print "no (unable to connect to $HOST\[:$PORT\])\n";
53
        exit 0;
54
    }
55
}
56
my $suggest = ( defined $ARGV[0] and $ARGV[0] eq "suggest" );
57
if ( $suggest ) {
58
    if ( defined( $sock ) ) {
59
        my @plugins = ('connected_clients', 'key_ratio', 'keys_per_sec', 'per_sec', 'used_keys', 'used_memory');
60
        foreach my $plugin (@plugins) {
61
            print "$plugin\n";
62
        }
63
        exit 0;
64
    } else {
65
        print "no (unable to connect to $HOST\[:$PORT\])\n";
66
        exit 0;
67
    }
68 a7be1b20 Vadim Istratov
}
69
70 b54782b7 Gleb Voronich
print $sock "INFO\r\n";
71
my $result = <$sock> || die "can't read socket: $!";
72
73
my $rep;
74
read($sock, $rep, substr($result,1)) || die "can't read from socket: $!";
75
76
my $hash;
77
foreach (split(/\r\n/, $rep)) {
78 5a489679 raszi
    my ($key,$val) = split(/:/, $_, 2);
79
    $hash->{$key} = $val;
80 b54782b7 Gleb Voronich
}
81
close ($sock);
82
83
$0 =~ s/(.+)redis_//g;
84
85
switch ($0) {
86
    case "connected_clients" {
87 5a489679 raszi
        if ( $config ) {
88 b54782b7 Gleb Voronich
            print "graph_title Connected clients\n";
89
            print "graph_vlabel Connected clients\n";
90
            print "graph_category redis\n";
91 016400bd Matt West
            print "graph_args -l 0\n";
92
            print "connected_clients.label connected clients\n";
93 b54782b7 Gleb Voronich
            exit 0;
94 5a489679 raszi
        }
95
96
        print "connected_clients.value " . $hash->{'connected_clients'} . "\n";
97 b54782b7 Gleb Voronich
    }
98
99
100 016400bd Matt West
    case "keys_per_sec" {
101
        if ( $config ) {
102
            print "graph_title Keys Per Second\n";
103
            print "graph_vlabel per \${graph_period}\n";
104
            print "graph_category redis\n";
105
            print "graph_args -l 0\n";
106
            print "hits.label hits\n";
107
            print "hits.type COUNTER\n";
108
            print "misses.label misses\n";
109
            print "misses.type COUNTER\n";
110
            print "expired.label expirations\n";
111
            print "expired.type COUNTER\n";
112
            print "evictions.label evictions\n";
113
            print "evictions.type COUNTER\n";
114
            exit 0;
115
        }
116
117
        print "hits.value " . $hash->{'keyspace_hits'} . "\n";
118
        print "misses.value " . $hash->{'keyspace_misses'} . "\n";
119
        print "expired.value " . $hash->{'expired_keys'} . "\n";
120
        print "evictions.value " . $hash->{'evicted_keys'} . "\n";
121
    }
122
123
    case "key_ratio" {
124
        if ( $config ) {
125
            print "graph_title Key Hit vs Miss Ratio\n";
126
            print "graph_vlabel per \${graph_period}\n";
127
            print "graph_category redis\n";
128
            print "graph_args -u 100 -l 0 -r --base 1000\n";
129
            print "hitratio.label hit ratio\n";
130
            print "hitratio.type GAUGE\n";
131
            print "hitratio.draw AREA\n";
132
            print "missratio.label miss ratio\n";
133
            print "missratio.type GAUGE\n";
134
            print "missratio.draw STACK\n";
135
            exit 0;
136
        }
137
            
138
        my $total = $hash->{'keyspace_hits'} + $hash->{'keyspace_misses'};
139
        printf("hitratio.value %.2f\n", $hash->{'keyspace_hits'} / $total * 100);
140
        printf("missratio.value %.2f\n", $hash->{'keyspace_misses'} / $total * 100);
141
    }
142
143
144 b54782b7 Gleb Voronich
    case "per_sec" {
145 5a489679 raszi
        if ( $config ) {
146 b54782b7 Gleb Voronich
            print "graph_title Per second\n";
147
            print "graph_vlabel per \${graph_period}\n";
148
            print "graph_category redis\n";
149 016400bd Matt West
            print "graph_args -l 0\n";
150 b54782b7 Gleb Voronich
            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 5a489679 raszi
        }
156
157
        print "requests.value ". $hash->{'total_commands_processed'} ."\n";
158
        print "connections.value ". $hash->{'total_connections_received'} ."\n";
159 b54782b7 Gleb Voronich
    }
160
161
162
    case "used_memory" {
163 5a489679 raszi
        if ( $config ) {
164 b54782b7 Gleb Voronich
            print "graph_title Used memory\n";
165
            print "graph_vlabel Used memory\n";
166
            print "graph_category redis\n";
167 016400bd Matt West
            print "graph_args -l 0\n";
168
            print "used_memory.label used memory\n";
169
            print "used_memory.draw AREA\n";
170 b54782b7 Gleb Voronich
            exit 0;
171 5a489679 raszi
        }
172
173
        print "used_memory.value ". $hash->{'used_memory'}  ."\n";
174
    }
175
    
176
    case "used_keys" {
177
        my $dbs;
178
        foreach my $key (keys %{$hash}) {
179
            if ( $key =~ /^db\d+$/ && $hash->{$key} =~ /keys=(\d+),expires=(\d+)/ ) {
180
                $dbs->{$key} = [ $1, $2 ];
181
            }
182
        }
183
184
        if ( $config ) {
185
            print "graph_title Used keys\n";
186
            print "graph_vlabel Used keys\n";
187
            print "graph_category redis\n";
188 016400bd Matt West
            print "graph_args -l 0\n";
189 5a489679 raszi
190
            foreach my $db (keys %{$dbs}) {
191
                printf "%s_keys.label %s keys\n", $db, $db;
192
                printf "%s_expires.label %s expires\n", $db, $db;
193
            }
194
195
            exit 0;
196
        }
197
198
        foreach my $db (keys %{$dbs}) {
199
            printf "%s_keys.value %d\n", $db, $dbs->{$db}[0];
200
            printf "%s_expires.value %d\n", $db, $dbs->{$db}[1];
201
        }
202 b54782b7 Gleb Voronich
    }
203
}
204 5a489679 raszi
205 016400bd Matt West
sub get_conn {
206
    my $sock = IO::Socket::INET->new(
207
        PeerAddr => $HOST,
208
        PeerPort => $PORT,
209
        Timeout => 10,
210
        Proto => 'tcp'
211
    );
212
    if ( defined( $PASSWORD )  ) {
213
        print $sock "AUTH ", $PASSWORD, "\r\n";
214
        my $result = <$sock> || die "can't read socket: $!";
215
    }
216
    return $sock;
217
}
218
219 5a489679 raszi
# vim: ft=perl ai ts=4 sw=4 et: