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/

loops

Example: foreach, for, while, break and continue, on the processes of a Linux.

  • example
  • basics
  • loops
  • foreach
  • while
  • ssh

by banshee1 installation1 version

v1Latest

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

Keen 1.0.0 · 46 lines
// Loops: foreach, for and while, and break and continue, which leave a loop or skip to its next turn.
// The collector counts the processes of a Linux by state, from ps.
need('protocol:ssh');

output('zombies', 'number', array('label' => 'Zombie processes'));
output('running', 'number', array('label' => 'Running processes'));
output('stopped', 'text', array('label' => 'A process is stopped'));

watch.above('zombies', 'Zombie processes', 'zombies', array('warning' => 5));
watch.state('stopped', 'Stopped process', 'stopped', array('warning' => array('yes'), 'enabled' => false));

// One line per process, its state first: "Ss", "R+", "Z", "T"...
$ps = ssh.run('ps -e -o stat=');
$states = text.lines($ps['output']);

// foreach: one turn per element of the list. continue goes straight to the next turn.
$zombies = 0;
foreach ($states as $state) {
    if (!text.like($state, 'Z*')) {
        continue;
    }
    $zombies = $zombies + 1;
}
value('zombies', $zombies);

// for: a counter from a start to an end; here, the rank of each line, from 0.
$running = 0;
for ($i = 0; $i < list.count($states); $i = $i + 1) {
    if (text.like($states[$i], 'R*')) {
        $running = $running + 1;
    }
}
value('running', $running);

// while: turns as long as its condition holds. break leaves the loop at once: one stopped process
// found, there is no need to read the others.
$stopped = 'no';
$i = 0;
while ($i < list.count($states)) {
    if (text.like($states[$i], 'T*')) {
        $stopped = 'yes';
        break;
    }
    $i = $i + 1;
}
value('stopped', $stopped);