-
Notifications
You must be signed in to change notification settings - Fork 22
/
memcached_
executable file
·486 lines (397 loc) · 12 KB
/
memcached_
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/perl
#
=head1 MEMCACHED
Memcached - A Plugin to monitor Memcached Servers
=head1 MUNIN CONFIGURATION
[memcached_*]
env.host 127.0.0.1 *default*
env.port 11211 *default*
=head2 MUNIN ENVIRONMENT CONFIGURATION EXPLANATION
host = host we are going to monitor
port = port we are connecting to, in order to gather stats
=head1 NODE CONFIGURATION
Please make sure you can telnet to your memcache servers and issue the
following commands: stats
Available Graphs contained in this Plugin
bytes => This graphs the current network traffic in and out
commands => This graphs the current commands being issued to the memcache machine.
conns => This graphs the current, max connections as well as avg conns per sec avg conns per sec is derived from total_conns / uptime.
evictions => This graphs the current evictions on the node.
items => This graphs the current items and total items in the memcached node.
memory => This graphs the current and max memory allocation.
The following example holds true for all graphing options in this plugin.
Example: ln -s /usr/share/munin/plugins/memcached_ /etc/munin/plugins/memcached_bytes
=head1 ACKNOWLEDGEMENTS
Thanks to dormando for putting up with me ;)
=head1 AUTHOR
Matt West < https://github.com/mhwest13/Memcached-Munin-Plugin >
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf suggest
=cut
use strict;
use IO::Socket;
my $host = $ENV{host} || "127.0.0.1";
my $port = $ENV{port} || 11211;
my $connection;
my %stats;
# This hash contains the information contained in two memcache commands
# stats and stats settings.
# So I was trying to figure out how to build this up, and looking at some good examples
# I decided to use the format, or for the most part, the format from the mysql_ munin plugin
# for Innodb by Kjell-Magne Ãierud, it just spoke ease of flexibility especially with multigraphs
# thanks btw ;)
#
# %graphs is a container for all of the graph definition information. In here is where you'll
# find the configuration information for munin's graphing procedure.
# Format:
#
# $graph{graph_name} => {
# config => {
# # You'll find keys and values stored here for graph manipulation
# },
# datasrc => [
# # Name: name given to data value
# # Attr: Attribute for given value
# { name => 'Name', (Attr) },
# { ... },
# ],
# }
my %graphs;
$graphs{items} = {
config => {
args => '--base 1000 --lower-limit 0',
vlabel => 'Items in Memcached',
category => 'memcached',
title => 'Items',
info => 'This graph shows the number of items in use by memcached',
},
datasrc => [
{ name => 'curr_items', label => 'Current Items', min => '0' },
{
name => 'total_items',
label => 'New Items',
min => '0',
type => 'DERIVE'
},
],
};
$graphs{memory} = {
config => {
args => '--base 1024 --lower-limit 0',
vlabel => 'Bytes Used',
category => 'memcached',
title => 'Memory Usage',
info => 'This graph shows the memory consumption of memcached',
},
datasrc => [
{
name => 'limit_maxbytes',
draw => 'AREA',
label => 'Maximum Bytes Allocated',
min => '0'
},
{
name => 'bytes',
draw => 'AREA',
label => 'Current Bytes Used',
min => '0'
},
],
};
$graphs{bytes} = {
config => {
args => '--base 1000',
vlabel => 'bits in (-) / out (+)',
title => 'Network Traffic',
category => 'memcached',
info =>
'This graph shows the network traffic in (-) / out (+) of the machine',
order => 'bytes_read bytes_written',
},
datasrc => [
{
name => 'bytes_read',
type => 'DERIVE',
label => 'Network Traffic coming in (-)',
graph => 'no',
cdef => 'bytes_read,8,*',
min => '0'
},
{
name => 'bytes_written',
type => 'DERIVE',
label => 'Traffic in (-) / out (+)',
negative => 'bytes_read',
cdef => 'bytes_written,8,*',
min => '0'
},
],
};
$graphs{conns} = {
config => {
args => '--base 1000 --lower-limit 0',
vlabel => 'Connections per ${graph_period}',
category => 'memcached',
title => 'Connections',
info =>
'This graph shows the number of connections being handled by memcached',
order => 'curr_conns avg_conns',
},
datasrc => [
{ name => 'curr_conns', label => 'Current Connections', min => '0' },
{ name => 'avg_conns', label => 'Avg Connections', min => '0' },
],
};
$graphs{commands} = {
config => {
args => '--base 1000 --lower-limit 0',
vlabel => 'Commands per ${graph_period}',
category => 'memcached',
title => 'Commands',
info =>
'This graph shows the number of commands being handled by memcached',
},
datasrc => [
{
name => 'cmd_get',
type => 'DERIVE',
label => 'Gets',
info => 'Cumulative number of retrieval reqs',
min => '0'
},
{
name => 'cmd_set',
type => 'DERIVE',
label => 'Sets',
info => 'Cumulative number of storage reqs',
min => '0'
},
{
name => 'get_hits',
type => 'DERIVE',
label => 'Get Hits',
info => 'Number of keys that were requested and found',
min => '0'
},
{
name => 'get_misses',
type => 'DERIVE',
label => 'Get Misses',
info => 'Number of keys there were requested and not found',
min => '0'
},
],
};
$graphs{evictions} = {
config => {
args => '--base 1000 --lower-limit 0',
vlabel => 'Evictions per ${graph_period}',
category => 'memcached',
title => 'Evictions',
info => 'This graph shows the number of evictions per second',
},
datasrc => [
{
name => 'evictions',
label => 'Evictions',
info => 'Cumulative Evictions Across All Slabs',
type => 'DERIVE',
min => '0'
},
],
};
##
#### Config Check ####
##
if ( defined $ARGV[0] && $ARGV[0] eq 'config' ) {
$0 =~ /(?:([^\/]+)_)?memcached_(.+)$/;
my $prefix = $1 ? $1 : '';
my $plugin = $2;
die 'Unknown Plugin Specified: ' . ( $plugin ? $plugin : '' )
unless $graphs{$plugin};
# We need to fetch the stats before we do any config, cause its needed for multigraph
fetch_stats();
# Now lets go ahead and print out our config.
do_config( $prefix, $plugin );
exit 0;
}
##
#### Autoconf Check ####
##
if ( defined $ARGV[0] && $ARGV[0] eq 'autoconf' ) {
# Lets attempt to connect to memcached
my $s = get_conn();
# Lets check that we did connect to memcached
if ( defined($s) ) {
print "yes\n";
exit 0;
}
else {
print "no (unable to connect to $connection)\n";
exit 0;
}
}
##
#### Suggest Check ####
##
if ( defined $ARGV[0] && $ARGV[0] eq 'suggest' ) {
# Lets attempt to connect to memcached
my $s = get_conn();
# Lets check that we did connect to memcached
if ( defined($s) ) {
my @rootplugins =
( 'bytes', 'conns', 'commands', 'evictions', 'items', 'memory' );
foreach my $plugin (@rootplugins) {
print "$plugin\n";
}
exit 0;
}
else {
print "no (unable to connect to $connection)\n";
exit 0;
}
}
##
#### Well We aren't running (auto)config/suggest so lets print some stats ####
##
fetch_output();
##
#### Subroutines for printing info gathered from memcached ####
##
##
#### This subroutine performs the bulk processing for printing statistics.
##
sub fetch_output {
$0 =~ /(?:([^\/]+)_)?memcached_(.+)$/;
my $prefix = $1 ? $1 : '';
my $plugin = $2;
die 'Unknown Plugin Specified: ' . ( $plugin ? $plugin : '' )
unless $graphs{$plugin};
# Well we need to actually fetch the stats before we do anything to them.
fetch_stats();
# Now lets go ahead and print out our output.
print_root_output($plugin);
return;
}
##
#### This subroutine is for the root non-multigraph graphs which render on the main node page ####
##
sub print_root_output {
my ($plugin) = (@_);
my $graph = $graphs{$plugin};
#print "graph memcached_$plugin\n";
if ( $plugin ne 'conns' ) {
foreach my $dsrc ( @{ $graph->{datasrc} } ) {
my %datasrc = %$dsrc;
while ( my ( $key, $value ) = each(%datasrc) ) {
next if ( $key ne 'name' );
my $output = $stats{$value};
print "$dsrc->{name}.value $output\n";
}
}
}
else {
my $output;
foreach my $dsrc ( @{ $graph->{datasrc} } ) {
my %datasrc = %$dsrc;
while ( my ( $key, $value ) = each(%datasrc) ) {
if ( $value eq 'curr_conns' ) {
$output = $stats{curr_connections};
}
elsif ( $value eq 'avg_conns' ) {
$output = sprintf( "%02d",
$stats{total_connections} / $stats{uptime} );
}
else {
next;
}
print "$dsrc->{name}.value $output\n";
}
}
}
return;
}
##
#### Subroutines for printing out config information for graphs ####
##
##
#### This subroutine does the bulk printing the config info per graph ####
##
sub do_config {
my ( $prefix, $plugin ) = (@_);
print_root_config( $prefix, $plugin );
return;
}
##
#### This subroutine is for the config info for non multigraph graphs which render on the main node page ####
##
sub print_root_config {
my ( $prefix, $plugin ) = (@_);
die 'Unknown Plugin Specified: ' . ( $plugin ? $plugin : '' )
unless $graphs{$plugin};
my $graph = $graphs{$plugin};
my %graphconf = %{ $graph->{config} };
#print "graph memcached_$plugin\n";
while ( my ( $key, $value ) = each(%graphconf) ) {
if ( $key eq 'title' ) {
if ($prefix) {
print "graph_$key " . ucfirst($prefix) . " $value\n";
}
else {
print "graph_$key $value\n";
}
}
else {
print "graph_$key $value\n";
}
}
foreach my $dsrc ( @{ $graph->{datasrc} } ) {
my %datasrc = %$dsrc;
while ( my ( $key, $value ) = each(%datasrc) ) {
next if ( $key eq 'name' );
print "$dsrc->{name}.$key $value\n";
}
}
return;
}
##
#### This subroutine returns a socket connection ####
##
sub get_conn {
my $s = undef;
# check if we want to use sockets instead of tcp
my ($sock) = ( $host =~ /unix:\/\/(.+)$/ );
if ($sock) {
$connection = "unix:\/\/$sock";
$s = IO::Socket::UNIX->new( Peer => $sock );
}
else {
$connection = "$host:$port";
$s = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port,
Timeout => 10,
);
}
return $s;
}
##
#### This subroutine actually performs the data fetch for us ####
#### These commands do not lock up Memcache at all ####
##
sub fetch_stats {
my $s = get_conn();
die "Error: Unable to Connect to $connection\n" unless $s;
print $s "stats\r\n";
while ( my $line = <$s> ) {
if ( $line =~ /STAT\s(.+?)\s(\d+)/ ) {
my ( $skey, $svalue ) = ( $1, $2 );
$stats{$skey} = $svalue;
}
last if $line =~ /^END/;
}
}