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 dump       # dumps keys and values
45" unless $host && $mode;
46
47$host .= ":11211" unless $host =~ /:\d+/;
48
49my $sock = IO::Socket::INET->new(PeerAddr => $host,
50                                 Proto    => 'tcp');
51die "Couldn't connect to $host\n" unless $sock;
52
53if ($mode eq 'dump') {
54    my %items;
55    my $totalitems;
56
57    print $sock "stats items\r\n";
58
59    while (<$sock>) {
60        last if /^END/;
61        if (/^STAT items:(\d*):number (\d*)/) {
62            $items{$1} = $2;
63            $totalitems += $2;
64        }
65    }
66    print STDERR "Dumping memcache contents\n";
67    print STDERR "  Number of buckets: " . scalar(keys(%items)) . "\n";
68    print STDERR "  Number of items  : $totalitems\n";
69
70    foreach my $bucket (sort(keys(%items))) {
71        print STDERR "Dumping bucket $bucket - " . $items{$bucket} . " total items\n";
72        print $sock "stats cachedump $bucket $items{$bucket}\r\n";
73        my %keyexp;
74        while (<$sock>) {
75            last if /^END/;
76            # return format looks like this
77            # ITEM foo [6 b; 1176415152 s]
78            if (/^ITEM (\S+) \[.* (\d+) s\]/) {
79                $keyexp{$1} = $2;
80            }
81        }
82
83        foreach my $k (keys(%keyexp)) {
84            print $sock "get $k\r\n";
85            my $response = <$sock>;
86            if ($response =~ /VALUE (\S+) (\d+) (\d+)/) {
87                my $flags = $2;
88                my $len = $3;
89                my $val;
90                read $sock, $val, $len;
91                print "add $k $flags $keyexp{$k} $len\r\n$val\r\n";
92                # get the END
93                $_ = <$sock>;
94                $_ = <$sock>;
95            }
96        }
97    }
98    exit;
99}
100
101if ($mode eq 'stats') {
102    my %items;
103
104    print $sock "stats\r\n";
105
106    while (<$sock>) {
107        last if /^END/;
108        chomp;
109        if (/^STAT\s+(\S*)\s+(.*)/) {
110            $items{$1} = $2;
111        }
112    }
113    printf ("#%-17s %5s %11s\n", $host, "Field", "Value");
114    foreach my $name (sort(keys(%items))) {
115        printf ("%24s %12s\n", $name, $items{$name});
116
117    }
118    exit;
119}
120
121# display mode:
122
123my %items;  # class -> { number, age, chunk_size, chunks_per_page,
124#            total_pages, total_chunks, used_chunks,
125#            free_chunks, free_chunks_end }
126
127print $sock "stats items\r\n";
128while (<$sock>) {
129    last if /^END/;
130    if (/^STAT items:(\d+):(\w+) (\d+)/) {
131        $items{$1}{$2} = $3;
132    }
133}
134
135print $sock "stats slabs\r\n";
136while (<$sock>) {
137    last if /^END/;
138    if (/^STAT (\d+):(\w+) (\d+)/) {
139        $items{$1}{$2} = $3;
140    }
141}
142
143print "  #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM\n";
144foreach my $n (1..40) {
145    my $it = $items{$n};
146    next if (0 == $it->{total_pages});
147    my $size = $it->{chunk_size} < 1024 ?
148        "$it->{chunk_size}B" :
149        sprintf("%.1fK", $it->{chunk_size} / 1024.0);
150    my $full = $it->{free_chunks_end} == 0 ? "yes" : " no";
151    printf("%3d %8s %9ds %7d %7d %7s %8d %8d %4d\n",
152           $n, $size, $it->{age}, $it->{total_pages},
153           $it->{number}, $full, $it->{evicted},
154           $it->{evicted_time}, $it->{outofmemory});
155}
156
157