查看获取端口的脚本
[root@iZbp113qvinee7jxepbagqZ ~]# cat /opt/discovery_port.sh
#!/bin/bash
portarray=(`sudo netstat -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf '\n\t\t{'
printf "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
[root@iZbp113qvinee7jxepbagqZ ~]#
授权,要让zabbix用户有执行权限
[root@iZbp113qvinee7jxepbagqZ ~]# chmod +x /opt/discovery_port.sh
[root@iZbp113qvinee7jxepbagqZ ~]#
查看zabbix自定义key文件
[root@iZbp113qvinee7jxepbagqZ ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_discovery_port.conf
UserParameter=tcpportlisten,/opt/discovery_port.sh "$1"
[root@iZbp113qvinee7jxepbagqZ ~]#
重启zabbix agent
[root@iZbp146uf9ftw85i3zaol9Z ~]# service zabbix-agent restart
Shutting down Zabbix agent: [ OK ]
Starting Zabbix agent: [ OK ]
[root@iZbp146uf9ftw85i3zaol9Z ~]#
服务端测试
[root@iZbp14ipzpabr30yllskn4Z tmp]# zabbix_get -s 10.208.2.41 -k tcpportlisten
{
"data":[
{"{#TCP_PORT}":"10050"},
{"{#TCP_PORT}":"22"},
{"{#TCP_PORT}":"26891"}
]
}
[root@iZbp14ipzpabr30yllskn4Z tmp]#
在web页面中配置设置正则表达式
选择新建正则表达式
name: Tcpport Regular
Expressions:
Result is True ^(80|443|8080|1871)
创建模板
截图如下