1#!/usr/bin/perl
2#
3# memcached-tool:
4#   stats/management tool for memcached.
5#
6# Author:
7#   Brad Fitzpatrick <[email protected]>
8#
9# License:
10#   public domain.  I give up all rights to this
11#   tool.  modify and copy at will.
12#
13
14use strict;
15use IO::Socket::INET;
16
17my $host = shift;
18my $mode = shift || "display";
19my ($from, $to);
20
21if ($mode eq "display") {
22    undef $mode if @ARGV;
23} elsif ($mode eq "move") {
24    $from = shift;
25    $to = shift;
26    undef $mode if $from < 6 || $from > 17;
27    undef $mode if $to   < 6 || $to   > 17;
28    print STDERR "ERROR: parameters out of range\n\n" unless $mode;
29} elsif ($mode eq 'dump') {
30    ;
31} elsif ($mode eq 'stats') {
32    ;
33} else {
34    undef $mode;
35}
36
37undef $mode if @ARGV;
38
39die
40"Usage: memcached-tool <host[:port]> [mode]\n
41       memcached-tool 10.0.0.5:11211 display    # shows slabs
42       memcached-tool 10.0.0.5:11211            # same.  (default is display)
43       memcached-tool 10.0.0.5:11211 stats      # shows general stats
44       memcached-tool 10.0.0.5:11211 move 7 9   # takes 1MB slab from class #7
45                                                # to class #9.
46
47You can only move slabs around once memory is totally allocated, and only
48once the target class is full.  (So you can't move from #6 to #9 and #7
49to #9 at the same itme, since you'd have to wait for #9 to fill from
50the first reassigned page)
51" unless $host && $mode;
52
53$host .= ":11211" unless $host =~ /:\d+/;
54
55my $sock = IO::Socket::INET->new(PeerAddr => $host,
56				 Proto    => 'tcp');
57die "Couldn't connect to $host\n" unless $sock;
58
59
60if ($mode eq "move") {
61    my $tries = 0;
62    while (1) {
63	print $sock "slabs reassign $from $to\r\n";
64	my $res = <$sock>;
65	$res =~ s/\s+//;
66	if ($res eq "DONE") {
67	    print "Success.\n";
68	    exit 0;
69	} elsif ($res eq "CANT") {
70	    print "Error: can't move from $from to $to.  Destination not yet full?  See usage docs.\n";
71	    exit;
72	} elsif ($res eq "BUSY") {
73	    if (++$tries == 3) {
74		print "Failed to move after 3 tries.  Try again later.\n";
75		exit;
76	    }
77
78	    print "Page busy, retrying...\n";
79	    sleep 1;
80	} elsif ($res =~ "CLIENT_ERROR") {
81	    print "Error: CLIENT_ERROR; perhaps slab reassignment is not enabled.\n";
82        exit;
83	}
84    }
85
86    exit;
87}
88
89if ($mode eq 'dump') {
90    my %items;
91    my $totalitems;
92
93    print $sock "stats items\r\n";
94
95    while (<$sock>) {
96        last if /^END/;
97        if (/^STAT items:(\d*):number (\d*)/) {
98            $items{$1} = $2;
99            $totalitems += $2;
100        }
101    }
102    print STDERR "Dumping memcache contents\n";
103    print STDERR "  Number of buckets: " . scalar(keys(%items)) . "\n";
104    print STDERR "  Number of items  : $totalitems\n";
105
106    foreach my $bucket (sort(keys(%items))) {
107        print STDERR "Dumping bucket $bucket - " . $items{$bucket} . " total items\n";
108        print $sock "stats cachedump $bucket $items{$bucket} 1\r\n";
109        my %keyexp;
110        while (<$sock>) {
111            last if /^END/;
112            # return format looks like this
113            # ITEM foo [6 b; 1176415152 s]
114            if (/^ITEM (\S+) \[.* (\d+) s\]/) {
115                $keyexp{$1} = $2;
116            }
117        }
118
119        foreach my $k (keys(%keyexp)) {
120            my $val;
121            print $sock "get $k\r\n";
122            my $response = <$sock>;
123            $response =~ /VALUE (\S+) (\d+) (\d+)/;
124            my $flags = $2;
125            my $len = $3;
126            read $sock, $val , $len;
127            # get the END
128            $_ = <$sock>;
129            $_ = <$sock>;
130            print "add $k $flags $keyexp{$k} $len\r\n$val\r\n";
131        }
132    }
133    exit;
134}
135
136if ($mode eq 'stats') {
137    my %items;
138
139    print $sock "stats\r\n";
140
141    while (<$sock>) {
142        last if /^END/;
143        chomp;
144        if (/^STAT\s+(\S*)\s+(.*)/) {
145            $items{$1} = $2;
146        }
147    }
148    printf ("#%-17s %5s %11s\n", $host, "Field", "Value");
149    foreach my $name (sort(keys(%items))) {
150      printf ("%24s %12s\n", $name, $items{$name});
151
152    }
153    exit;
154}
155
156# display mode:
157
158my %items;  # class -> { number, age, chunk_size, chunks_per_page,
159            #            total_pages, total_chunks, used_chunks,
160            #            free_chunks, free_chunks_end }
161
162print $sock "stats items\r\n";
163while (<$sock>) {
164    last if /^END/;
165    if (/^STAT items:(\d+):(\w+) (\d+)/) {
166	$items{$1}{$2} = $3;
167    }
168}
169
170print $sock "stats slabs\r\n";
171while (<$sock>) {
172    last if /^END/;
173    if (/^STAT (\d+):(\w+) (\d+)/) {
174	$items{$1}{$2} = $3;
175    }
176}
177
178print "  #  Item_Size   Max_age  1MB_pages Count   Full?\n";
179foreach my $n (1..40) {
180    my $it = $items{$n};
181    next if (0 == $it->{total_pages});
182    my $size = $it->{chunk_size} < 1024 ? "$it->{chunk_size} B " :
183	sprintf("%.1f kB", $it->{chunk_size} / 1024.0);
184    my $full = $it->{free_chunks_end} == 0 ? "yes" : " no";
185    printf "%3d   %8s %7d s %7d %7d %7s\n",
186                        $n, $size, $it->{age}, $it->{total_pages},
187                        $it->{number}, $full;
188}
189
190