5.02021-11-21T21:59:49ZTemplates/ModulesTCP port monitoringTCP port monitoring## Overview
The following template is utilizing Zabbix agent to discover TCP ports in listening state.
Underneath the hood it will use the command:
```
ss --tcp --listening --numeric
```
This means the remote commands must be enabled. Or you need to convert it as an UserParameter. See discovery section for details.
After that, it will use JavaScript preprocessing step to transform the output suitable for discovery.
By default, the item will check port every 5 minutes. If the port is not reachable for 5 consecutive checks (25 minutes) it is a called a "Disaster".
Explaining video: <https://www.youtube.com/watch?v=BwQwWXoGzGM>
Cheers!
Templates/ModulesTCP portListening TCP port discoverysystem.run["ss --tcp --listening --numeric"]1d{#PORT}{$TCP.PORT.NOT_MATCHES}NOT_MATCHES_REGEXA{#PORT}{$TCP.PORT.MATCHES}BInstall UserParameter for zabbix-agent:
echo 'UserParameter=ss.tcp.listening,netstat --tcp --listening --numeric-ports' | sudo tee /etc/zabbix/zabbix_agentd.d/ss.tcp.listening.conf
Install UserParameter for zabbix-agent2:
echo 'UserParameter=ss.tcp.listening,netstat --tcp --listening --numeric-ports' | sudo tee /etc/zabbix/zabbix_agent2.d/ss.tcp.listening.conf
Restart Zabbix agent
systemctl restart zabbix-agent
systemctl restart zabbix-agent2
In this section replace Key:
system.run["ss --tcp --listening --numeric"]
with:
netstat.tcp.listeningPort {#PORT} is listeningZABBIX_ACTIVEnet.tcp.listen[{#PORT}]5mTCP portService state{max(#5)}=0Port {#PORT} is down for some timeDISASTERYESJAVASCRIPTvar lld = [];
var lines = value.match(/:[0-9]+ /gm).reduce(function(a,b){if(a.indexOf(b) < 0)a.push(b);return a;},[]).join("\n").replace(/:/g,"").replace(/ /g,"").split("\n");
var lines_num = lines.length;
for (i = 0; i < lines_num; i++)
{
var row = {};
row["{#PORT}"] = lines[i]
lld.push(row);
}
return JSON.stringify(lld);{$TCP.PORT.MATCHES}.*{$TCP.PORT.NOT_MATCHES}^\s$Service state0Down1Up