Network
banshee/network-collection/network/tcp/
ports
Whether TCP ports of a host accept a connection, and how fast, to see a service down or a port closed by a firewall.
by banshee1 installation2 versions
v2Latest
Published on October 28, 2026. One naming convention across collections; documentation completed.
// Whether TCP ports of a host accept a connection, the way check_tcp reads them: for each port of the list,
// the poller opens a connection to the address of the host and closes it at once, sending nothing.
// A port that refuses the connection, or does not answer within protocol:tcp:timeout, is closed: "Port
// closed" raises on it. Each port is an instance, named by its number.
need('protocol:tcp');
$wanted = variable('tcp:ports', 'text', array(
'label' => 'Ports',
'help' => 'The TCP ports to open, separated by commas, for instance 22, 443, 3389.',
));
per('port', 'Ports');
output('open', 'text', array('label' => 'Port open', 'per' => 'port'));
output('connect-time', 'number', array('label' => 'Connection time', 'unit' => 'ms', 'per' => 'port'));
watch.state('closed', 'Port closed', 'open', array('critical' => array('no')));
watch.above('slow', 'Slow connection', 'connect-time', array('warning' => 200, 'critical' => 1000, 'enabled' => false));
// The list is read whole before any connection: a port written wrong stops the collector, rather than
// leave a port unwatched without a word. A port written twice is opened once.
$ports = array();
foreach (text.split($wanted, ',') as $item) {
$port = text.trim($item);
if ($port != '') {
if (list.count(text.match($port, '^[1-9][0-9]{0,4}$')) == 0) {
fail('misconfigured', 'Invalid port in the list', '"' . $port . '" is not a TCP port, from 1 to 65535');
}
$number = $port + 0;
if ($number > 65535) {
fail('misconfigured', 'Invalid port in the list', '"' . $port . '" is not a TCP port, from 1 to 65535');
}
$ports[$port] = $number;
}
}
if (list.count($ports) == 0) {
fail('misconfigured', 'No port in the list', 'Set the ports to open on the probe, for instance 22, 443.');
}
// A closed port has no connection time: only its state is returned.
foreach ($ports as $name => $number) {
$try = tcp.connect($number);
if ($try['open']) {
value('open', 'yes', $name);
value('connect-time', $try['ms'], $name);
} else {
value('open', 'no', $name);
}
}