基于: https://www.cnblogs.com/loki717/p/7991081.html
由于存在多个端口,需要监控,重复创建zabbix监控项,转为自动发现远程端口,并测试端口状态
一、自动发现主机端口
1. hostlist.conf 主机端口映射文件
192.168.11.221;22 192.168.11.221;80
2. port_alert.sh 端口自发现脚本
4 prod-admin-02 172.19.120.37 [sa] #!/bin/bash CONF=/etc/zabbix/script/hostlist.conf Check(){ grep -vE '(^ *#|^$)' ${CONF} | grep -vE '^ *[0-9]+' &> /dev/null if [ $? -eq 0 ] then echo Error: ${CONF} Contains Invalid Port. exit 1 else portarray=($(grep -vE '(^ *#|^$)' ${CONF} | grep -E '^ *[0-9]+')) fi } PortDiscovery(){ length=${#portarray[@]} printf "{ " printf ' '""data":[" for ((i=0;i<$length;i++)) do HOST=$(echo ${portarray[$i]} | awk -F ';' '{print $1}') PORT=$(echo ${portarray[$i]} | awk -F ';' '{print $2}') printf ' {' printf ""{#TCP_HOST}":"${HOST}"," printf ""{#TCP_PORT}":"${PORT}"}" if [ $i -lt $[$length-1] ];then printf ',' fi done printf " ] " printf "} " } Port(){ Check PortDiscovery } Port
测试处理结果
# sh port_alert.sh { "data":[ {"{#TCP_HOST}":"192.168.11.221","{#TCP_PORT}":"22"}, {"{#TCP_HOST}":"192.168.11.221","{#TCP_PORT}":"80"} ] }
3.1 自定义item,用于自动发现规则
# vim /etc/zabbix/zabbix_agentd.d/userparameter.conf UserParameter=port.alert, /etc/zabbix/script/port_alert.sh
3.2 zabbix_get测试item
# zabbix_get -s 192.168.11.221 -k port.alert { "data":[ {"{#TCP_HOST}":"192.168.11.221","{#TCP_PORT}":"22"}, {"{#TCP_HOST}":"192.168.11.221","{#TCP_PORT}":"80"} ] }
二、监控主机端口状态
1. nc_port.sh 测试主机端口状态
#!/bin/bash HOST=$1 PORT=$2 nc -w 3 -z $HOST $PORT && echo 1 || echo 0
2.1. 自定义item,用于测试主机状态
# vim /etc/zabbix/zabbix_agentd.d/userparameter.conf UserParameter=mport.nc[*], /etc/zabbix/script/nc_port.sh $1 $2
2.2 zabbix_get测试item
# zabbix_get -s 172.19.129.207 -k mport.nc[192.168.11.221,80] 1
三、zabbix配置
1. 配置自动发现规则
2. 配置监控项原型
3. 配置触发器类型