痛点:公司隔三岔五就申请台服务器,端口运行起来还要手动添加配置触发器,这种重复的工作。。。。。我日了个寂寞。。
解决:使用zabbix自动发现规则DDL实现自动发现端口监控报警。
一、客户端配置
编写脚本自定义key值
mkdir -p /etc/zabbix/zabbix_agentd.d/script && cd /etc/zabbix/zabbix_agentd.d/script
[root@backup script]# vim discovery_tcp_port.sh
#!/bin/bash
portarray=(`ss -tnlp |awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
length=${#portarray[@]}
function zabbix_json(){
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"
}
function check(){
#ss -tnlp |grep 'users' |awk -F ':' "/:$1/ {print \$NF}" |cut -d',' -f1 |sed 's@[(,"]@@g' |uniq
ss -tnlp |grep 'users' |awk -F ':' "/:$1/ {print \$NF}" |grep -oE '\".*\"' |sort |uniq |head -n1 |sed 's/"//g' |awk -F ',' '{print $1}'
}
[[ $1 = check ]] \
&& $* \
|| zabbix_json
执行脚本
[root@backup script]# sh discovery_tcp_port.sh
{
"data":[
{"{#TCP_PORT}":"10050"},
{"{#TCP_PORT}":"1815"},
{"{#TCP_PORT}":"22"},
{"{#TCP_PORT}":"25"},
{"{#TCP_PORT}":"781"},
{"{#TCP_PORT}":"873"}
]
}
脚本授权
[root@backup script]# chmod a+x discovery_tcp_port.sh
[root@backup script]# chown zabbix:zabbix discovery_tcp_port.sh
自定义key
[root@backup script]# tail -1 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=discovery.tcpport[*],/etc/zabbix/zabbix_agentd.d/script/discovery_tcp_port.sh $1 $2
重启zabbix-agent
[root@backup script]# service zabbix-agent restart
二、服务端
服务器zabbix-get测试
[root@zabbix02 ~]# zabbix_get -s 192.168.31.8 -k discovery.tcpport
{
"data":[
{"{#TCP_PORT}":"10050"},
{"{#TCP_PORT}":"1815"},
{"{#TCP_PORT}":"22"},
{"{#TCP_PORT}":"25"},
{"{#TCP_PORT}":"781"},
{"{#TCP_PORT}":"873"}
]
}
三、zabbix-web页面配置
1、创建模板
2、创建自动发现规则
3、创建监控项原型、
4、配置触发器类型
完成!
0-------
停掉22端口
有问题请加博主微信进行沟通!
全部评论