继上一篇主机自动发现
得,还有服务的端口要监控
一个个去添加listen监控tcp的话不现实啊,还是也搞自动发现吧
分割下来也是2步啊
第一步脚本丢zabbix-agent下产生自定义键值
第二步不就是zabbix-server添加自动发现绑定这个键值咯
端口port自动发现
客户端新建查询端口脚本discoverport.sh
[root@new-jenkins scripts]# pwd
/etc/zabbix/scripts
[root@new-jenkins scripts]# vim discoverport.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 'ntt{'
printf ""{#TCP_PORT}":"${portarray[$i]}"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "nt]n"
printf "}n"
脚本不复杂哈,就是用Netstat过滤出来在监听的端口,然后循环输出
丢了,安装netstat命令为
yum install net-tools -y
增加执行权限
[root@new-jenkins scripts]# chmod +x discoverport.sh [root@new-jenkins scripts]# ll total 4 -rwxr-xr-x. 1 root root 407 Mar 10 16:50 discoverport.sh [root@new-jenkins scripts]#
客户端增加discovery的key键值,最后一行啊
[root@new-jenkins zabbix]# pwd /etc/zabbix [root@new-jenkins zabbix]# vim zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=192.168.3.111 ServerActive=192.168.3.111 Hostname=system.hostname Include=/etc/zabbix/zabbix_agentd.d/*.conf UnsafeUserParameters=1 UserParameter=tcpportlisten,/etc/zabbix/scripts/discoverport.sh "$1"
重启zabbix_agentd服务,
[root@new-jenkins zabbix]# systemctl restart zabbix-agent
然后去server端测试获取key
有点尴尬啊,报错了:
别慌大兄弟,这个是执行上面那个自动发现脚本没权限,需要sudo输入密码,是我们在操作过程中并没有输入,简单就去3.158给zabbix用户添加sudo权限即可。
vim /etc/sudoers zabbix ALL=(ALL) NOPASSWD:ALL
再次zabbix-server获取键值:
现在看到3.158这台自动发现的主机应用的模板是:主机模板 (Template OS Linux)
点击它进去
创建新的监控项原型
名称:tcp port $1
键值:net.tcp.listen[{#TCP_PORT}]
名称:Tcp port{#TCP_PORT}Status
表达式:***{Template OS Linux:net.tcp.listen[{#TCP_PORT}].last(0)}<>1***
名称:tcp port {#TCP_PORT}
然后绑定监控项



