Created
June 27, 2022 07:57
-
-
Save Bo0oM/ceabdc7da239418ffc0bd033f57b7964 to your computer and use it in GitHub Desktop.
Memcached dumper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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