banshee is under active development: collectors will keep changing, and a version you publish will likely need adjustments. For stable use, wait for version 1.0.0.

Linux

banshee/linux-collection/linux/ssh/

swap

Swap of a Linux by SSH: size, usage, and pages swapped in and out per second.

  • linux
  • ssh
  • swap
  • memory
  • alpine
  • debian
  • fedora

by banshee1 installation2 versions

Keen 1.0.0 · 46 lines
// The swap of a Linux, by SSH: its size, what it holds, and the pages going in and out per second. Pages
// read back from swap again and again say the memory is too small, far better than used swap, where the
// kernel readily stores pages nobody needs any more.
// Reads /proc/meminfo (SwapTotal, SwapFree) and /proc/vmstat (pswpin, pswpout) with cat: the kernel writes
// both the same way on Alpine, Debian and Fedora.
// A machine without swap has nothing to measure: the collector says so, rather than a green light on nothing.
need('protocol:ssh');

output('total', 'number', array('label' => 'Swap size', 'unit' => 'MiB'));
output('used', 'number', array('label' => 'Used swap', 'unit' => 'MiB'));
output('used-percent', 'number', array('label' => 'Used swap', 'unit' => '%'));
output('swap-in', 'number', array('label' => 'Pages read back from swap', 'unit' => 'pages/s'));
output('swap-out', 'number', array('label' => 'Pages written to swap', 'unit' => 'pages/s'));

watch.above('used-percent', 'Swap saturated', 'used-percent', array('warning' => 80, 'critical' => 95, 'for' => '3 runs'));
watch.above('swapping', 'Sustained swapping', 'swap-in', array('warning' => 100, 'for' => '5 min'));

// "SwapTotal:      2097148 kB" in /proc/meminfo, in KiB; "pswpin 1234" in /proc/vmstat, in pages since boot.
$read = ssh.run('cat /proc/meminfo /proc/vmstat');
$v = array();
foreach (text.lines($read['output']) as $line) {
    $m = text.match($line, '^([A-Za-z0-9_()]+):? +([0-9]+)');
    if (list.count($m) > 0) {
        $v[$m[1]] = $m[2];
    }
}
if (!list.has($v, 'SwapTotal') || !list.has($v, 'SwapFree') || !list.has($v, 'pswpin') || !list.has($v, 'pswpout')) {
    fail('misconfigured', 'SwapTotal, SwapFree, pswpin or pswpout missing from /proc/meminfo and /proc/vmstat', $read['errors']);
}
if ($v['SwapTotal'] <= 0) {
    fail('misconfigured', 'This machine has no swap (SwapTotal is 0): this probe has nothing to measure');
}

$in = counter.rate('swap-in', $v['pswpin']);
$out = counter.rate('swap-out', $v['pswpout']);
if ($in == '' || $out == '') {
    skip('First reading, or counters went backwards: swapping comes with the next run');
}

$total = $v['SwapTotal'] / 1024;
$used = ($v['SwapTotal'] - $v['SwapFree']) / 1024;
value('total', number.round($total));
value('used', number.round($used));
value('used-percent', number.round(100 * $used / $total, 1));
value('swap-in', number.round($in, 1));
value('swap-out', number.round($out, 1));