客户端配置
1.脚本
[root@es1 home]# cat /home/port_service.sh
#!/bin/bash
#by Mr.lu
#su root
portarray=(`sudo -u root /usr/bin/netstat -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
length=${#portarray[@]}
printf "{
"
printf ' '""data":["
for ((i=0;i<$length;i++))
do
printf '
{'
printf ""{#TCP_PORT}":"${portarray[$i]}","
printf ""{#TCP_NAME}":"`sudo -u root /usr/bin/netstat -ntpl|awk '{if($4~/:'${portarray[$i]}'$/) print $7}'|awk -F '/' '{print $2}'|uniq|awk '{print $1}'`"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "
]
"
printf "}
"
2. zabbix agnet 配置文件
[root@es1 home]# cat /etc/zabbix/zabbix_agentd.d/port_service.conf
UnsafeUserParameters=1
UserParameter=tcpportlisten,/home/port_service.sh "$1"
3. 权限
/etc/sudoers
增加:
zabbix ALL = NOPASSWD: ALL
4.重启zabbix-agent
服务端配置
1.增加模板(如增加到现有模板,直接点自动发现)
1.自动发现配置
注意:键值要与zabbix 客户端配置一样
2.监控原型
注意:变量名要与脚本里一样
3.报警