Examples
banshee/examples-collection/examples/basics/
conditions
Example: if, elseif, else, comparisons and && || !, on the swap of a Linux.
by banshee1 installation1 version
Published on October 27, 2026. First version in this collection.
// Conditions: if, elseif and else, and the operators that compare (== != < > <= >=) and combine
// (&& || !). The collector reads the swap of a Linux and sums up its use in one word.
need('protocol:ssh');
output('swap', 'text', array('label' => 'Use of the swap'));
watch.state('swap-heavy', 'Swap heavily used', 'swap', array('warning' => array('heavy')));
// Two lines, "SwapTotal: 2097148 kB" and "SwapFree: 1048572 kB".
$read = ssh.run("grep -e '^SwapTotal:' -e '^SwapFree:' /proc/meminfo");
$lines = text.lines($read['output']);
if (list.count($lines) != 2) {
fail('misconfigured', 'No swap figures in /proc/meminfo', $read['output']);
}
$total_words = text.words($lines[0]);
$free_words = text.words($lines[1]);
$total = $total_words[1];
$free = $free_words[1];
// || : one side true is enough. ! turns true into false, and false into true.
if (!text.is_number($total) || !text.is_number($free)) {
fail('misconfigured', 'No swap figures in /proc/meminfo', $read['output']);
}
// The first condition that holds wins, and else takes the rest.
// && : both sides must be true; the right side is read only when the left one is.
if ($total == 0) {
$swap = 'none';
} elseif ($free == $total) {
$swap = 'unused';
} elseif (100 * ($total - $free) / $total >= 50 && $free < 1048576) {
$swap = 'heavy';
} else {
$swap = 'light';
}
value('swap', $swap);