Skip to content

Instantly share code, notes, and snippets.

@Bo0oM
Created June 27, 2022 07:57
Show Gist options
  • Save Bo0oM/ceabdc7da239418ffc0bd033f57b7964 to your computer and use it in GitHub Desktop.
Save Bo0oM/ceabdc7da239418ffc0bd033f57b7964 to your computer and use it in GitHub Desktop.
Memcached dumper
#!/usr/bin/env php
<?php
if (! isset($argv[1])) {
echo 'Usage: memcached-dumper.php <server>'.PHP_EOL;
exit;
}
$memcache = new Memcache;
$memcache->connect($argv[1]);
$slabs = $memcache->getExtendedStats('slabs');
$items = $memcache->getExtendedStats('items');
$data = array();
foreach ($slabs as $server => $server_slabs) {
foreach ($server_slabs as $slab_id => $slab_metadata) {
if (is_int($slab_id)) {
$cache_dump = $memcache->getExtendedStats('cachedump', $slab_id);
foreach ($cache_dump as $cache_server => $entries) {
if ($entries !== false) {
foreach ($entries as $key => $value) {
$data[$key] = array(
'key' => $key,
'server' => $cache_server,
'slab_id' => $slab_id,
'value' => $memcache->get($key),
'age' => $items[$cache_server]['items'][$slab_id]['age'],
);
}
}
}
}
}
}
$output = '<!DOCTYPE html>';
$output .= '<html><head><title>Memcached dump</title>';
$output .= '<style>';
$output .= 'body { font-size: 0.9em; max-width: 1024px; }';
$output .= 'td { border: 1px solid #ccc; margin: 0; }';
$output .= 'table { margin: 0; width: 100%; max-width: 1024px; }';
$output .= '</style>';
$output .= '</head><body>';
$output .= '<table>';
$output .= '<tr>';
$output .= '<th>Key</th>';
$output .= '<th>SlabID</th>';
$output .= '<th>Age</th>';
$output .= '<th>Value</th>';
$output .= '</tr>';
foreach ($data as $values) {
$output .= '<tr>';
$output .= '<td>'.$values['key'].'</td>';
$output .= '<td>'.$values['slab_id'].'</td>';
$output .= '<td>'.$values['age'].'</td>';
$output .= '<td><pre style="overflow: auto; height: 150px; width: 900px;">'.var_export($values['value'], true).'</pre></td>';
$output .= '</tr>';
}
$output .= '</table></body></html>';
file_put_contents('memcached-dumper.html', $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment