Examples
banshee/examples-collection/examples/mechanics/
wait
Example: time.wait, two readings within one pass; the CPU usage of a Linux.
by banshee1 installation1 version
Published on October 27, 2026. First version in this collection.
// time.wait: wait within a pass, inside the time the collector is allowed. Two readings of the CPU
// counters five seconds apart give the usage at once, where counter.rate waits for the next pass.
// The collector reads the CPU usage of a Linux, by SSH.
need('protocol:ssh');
output('cpu-busy', 'number', array('label' => 'CPU usage', 'unit' => '%'));
watch.above('cpu-high', 'CPU busy', 'cpu-busy', array('warning' => 80, 'critical' => 95, 'for' => '3 runs'));
// The line "cpu user nice system idle iowait irq softirq steal" of /proc/stat, in hundredths of a second
// since the start: the idle time, and the whole time.
function cpu_times($line) {
$f = text.words($line);
return array('idle' => $f[4] + $f[5], 'total' => $f[1] + $f[2] + $f[3] + $f[4] + $f[5] + $f[6] + $f[7] + $f[8]);
}
$first = ssh.run("grep '^cpu ' /proc/stat");
time.wait(5);
$second = ssh.run("grep '^cpu ' /proc/stat");
$before = cpu_times($first['output']);
$after = cpu_times($second['output']);
$total = $after['total'] - $before['total'];
if ($total <= 0) {
fail('unreachable', 'The CPU counters did not move in 5 seconds');
}
value('cpu-busy', number.round(100 * (1 - ($after['idle'] - $before['idle']) / $total), 1));