Projet

Général

Profil

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

root / plugins / redis / redis_ @ 9f85e0ae

Historique | Voir | Annoter | Télécharger (7,17 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 285c11cd iborodikhin
    if (defined($key)) {
80
        $hash->{$key} = $val;
81
    }
82 b54782b7 Gleb Voronich
}
83
close ($sock);
84
85
$0 =~ s/(.+)redis_//g;
86
87
switch ($0) {
88
    case "connected_clients" {
89 5a489679 raszi
        if ( $config ) {
90 b54782b7 Gleb Voronich
            print "graph_title Connected clients\n";
91
            print "graph_vlabel Connected clients\n";
92
            print "graph_category redis\n";
93 016400bd Matt West
            print "graph_args -l 0\n";
94
            print "connected_clients.label connected clients\n";
95 b54782b7 Gleb Voronich
            exit 0;
96 5a489679 raszi
        }
97
98
        print "connected_clients.value " . $hash->{'connected_clients'} . "\n";
99 b54782b7 Gleb Voronich
    }
100
101
102 016400bd Matt West
    case "keys_per_sec" {
103
        if ( $config ) {
104
            print "graph_title Keys Per Second\n";
105
            print "graph_vlabel per \${graph_period}\n";
106
            print "graph_category redis\n";
107
            print "graph_args -l 0\n";
108
            print "hits.label hits\n";
109
            print "hits.type COUNTER\n";
110
            print "misses.label misses\n";
111
            print "misses.type COUNTER\n";
112
            print "expired.label expirations\n";
113
            print "expired.type COUNTER\n";
114
            print "evictions.label evictions\n";
115
            print "evictions.type COUNTER\n";
116
            exit 0;
117
        }
118
119
        print "hits.value " . $hash->{'keyspace_hits'} . "\n";
120
        print "misses.value " . $hash->{'keyspace_misses'} . "\n";
121
        print "expired.value " . $hash->{'expired_keys'} . "\n";
122
        print "evictions.value " . $hash->{'evicted_keys'} . "\n";
123
    }
124
125
    case "key_ratio" {
126
        if ( $config ) {
127
            print "graph_title Key Hit vs Miss Ratio\n";
128
            print "graph_vlabel per \${graph_period}\n";
129
            print "graph_category redis\n";
130
            print "graph_args -u 100 -l 0 -r --base 1000\n";
131
            print "hitratio.label hit ratio\n";
132
            print "hitratio.type GAUGE\n";
133
            print "hitratio.draw AREA\n";
134
            print "missratio.label miss ratio\n";
135
            print "missratio.type GAUGE\n";
136
            print "missratio.draw STACK\n";
137
            exit 0;
138
        }
139
            
140
        my $total = $hash->{'keyspace_hits'} + $hash->{'keyspace_misses'};
141 285c11cd iborodikhin
        my $hitratio = 0;
142
        my $missratio = 0;
143
        if ($total > 0) {
144
            $hitratio = $hash->{'keyspace_hits'} / $total * 100;
145
            $missratio = $hash->{'keyspace_misses'} / $total * 100;
146
        }
147
        printf("hitratio.value %.2f\n", $hitratio);
148
        printf("missratio.value %.2f\n", $missratio);
149 016400bd Matt West
    }
150
151
152 b54782b7 Gleb Voronich
    case "per_sec" {
153 5a489679 raszi
        if ( $config ) {
154 b54782b7 Gleb Voronich
            print "graph_title Per second\n";
155
            print "graph_vlabel per \${graph_period}\n";
156
            print "graph_category redis\n";
157 016400bd Matt West
            print "graph_args -l 0\n";
158 b54782b7 Gleb Voronich
            print "requests.label requests\n";
159
            print "requests.type COUNTER\n";
160
            print "connections.label connections\n";
161
            print "connections.type COUNTER\n";
162
            exit 0;
163 5a489679 raszi
        }
164
165
        print "requests.value ". $hash->{'total_commands_processed'} ."\n";
166
        print "connections.value ". $hash->{'total_connections_received'} ."\n";
167 b54782b7 Gleb Voronich
    }
168
169
170
    case "used_memory" {
171 5a489679 raszi
        if ( $config ) {
172 b54782b7 Gleb Voronich
            print "graph_title Used memory\n";
173
            print "graph_vlabel Used memory\n";
174
            print "graph_category redis\n";
175 b6b77ef9 Michael Renner
            print "graph_args -l 0 --base 1024\n";
176 016400bd Matt West
            print "used_memory.label used memory\n";
177
            print "used_memory.draw AREA\n";
178 b54782b7 Gleb Voronich
            exit 0;
179 5a489679 raszi
        }
180
181
        print "used_memory.value ". $hash->{'used_memory'}  ."\n";
182
    }
183
    
184
    case "used_keys" {
185
        my $dbs;
186
        foreach my $key (keys %{$hash}) {
187
            if ( $key =~ /^db\d+$/ && $hash->{$key} =~ /keys=(\d+),expires=(\d+)/ ) {
188
                $dbs->{$key} = [ $1, $2 ];
189
            }
190
        }
191
192
        if ( $config ) {
193
            print "graph_title Used keys\n";
194
            print "graph_vlabel Used keys\n";
195
            print "graph_category redis\n";
196 016400bd Matt West
            print "graph_args -l 0\n";
197 5a489679 raszi
198
            foreach my $db (keys %{$dbs}) {
199
                printf "%s_keys.label %s keys\n", $db, $db;
200
                printf "%s_expires.label %s expires\n", $db, $db;
201
            }
202
203
            exit 0;
204
        }
205
206
        foreach my $db (keys %{$dbs}) {
207
            printf "%s_keys.value %d\n", $db, $dbs->{$db}[0];
208
            printf "%s_expires.value %d\n", $db, $dbs->{$db}[1];
209
        }
210 b54782b7 Gleb Voronich
    }
211
}
212 5a489679 raszi
213 016400bd Matt West
sub get_conn {
214
    my $sock = IO::Socket::INET->new(
215
        PeerAddr => $HOST,
216
        PeerPort => $PORT,
217
        Timeout => 10,
218
        Proto => 'tcp'
219
    );
220
    if ( defined( $PASSWORD )  ) {
221
        print $sock "AUTH ", $PASSWORD, "\r\n";
222
        my $result = <$sock> || die "can't read socket: $!";
223
    }
224
    return $sock;
225
}
226
227 5a489679 raszi
# vim: ft=perl ai ts=4 sw=4 et: