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.

Examples

banshee/examples-collection/examples/basics/

functions

Example: functions of the author, parameters and return, on the memory of a Linux.

  • example
  • basics
  • functions
  • memory
  • ssh

by banshee1 installation1 version

v1Latest

Published on October 27, 2026. First version in this collection.

Keen 1.0.0 · 37 lines
// Functions: a piece of code written once and called several times. A function sees only its
// parameters, never the variables of the body, and gives back a value with return.
// The collector reads the memory and the swap of a Linux from /proc/meminfo.
need('protocol:ssh');

output('memory-used', 'number', array('label' => 'Memory used', 'unit' => '%'));
output('swap-used', 'number', array('label' => 'Swap used', 'unit' => '%'));

watch.above('memory-full', 'Memory almost full', 'memory-used', array('warning' => 90, 'critical' => 98));

// The value in kB of one line of /proc/meminfo, "MemTotal:  8049932 kB".
function kilobytes($meminfo, $name) {
    foreach (text.lines($meminfo) as $line) {
        $words = text.words($line);
        if ($words[0] == $name . ':') {
            return $words[1];
        }
    }
    fail('misconfigured', 'No ' . $name . ' in /proc/meminfo');
}

// The share of $part in $total, in percent, one decimal; 0 when there is no total (a host without swap).
function percent($part, $total) {
    if ($total == 0) {
        return 0;
    }
    return number.round(100 * $part / $total, 1);
}

$meminfo = ssh.run('cat /proc/meminfo');
$memory_total = kilobytes($meminfo['output'], 'MemTotal');
$memory_free = kilobytes($meminfo['output'], 'MemAvailable');
$swap_total = kilobytes($meminfo['output'], 'SwapTotal');
$swap_free = kilobytes($meminfo['output'], 'SwapFree');

value('memory-used', percent($memory_total - $memory_free, $memory_total));
value('swap-used', percent($swap_total - $swap_free, $swap_total));