背景说明
zabbix通过自定义key"domain.discovery"发现域名(Json格式),然后自动生成监控项,监控项通过自定义key"https"获取域名证书有效期,若少于30天则出发告警。
文本说明
名称 | 作用 |
---|---|
domain.txt | 域名列表,存放需要监控的域名 |
get_domain.sh | zabbix通过这个脚本发现域名 |
https.sh | zabbix通过这个脚本获取域名证书有效期 |
实施步骤
- 在/etc/zabbix/scritps/下创建domain.txt、get_domain.sh、https.sh这三个文件(/etc/zabbix/scritps/可以是任意目录位置,我是放在/etc/zabbix/scripts下方便管理)
- 创建zabbix自定义监控项
#获取域名domain.txt中的域名
UserParameter=domain.discovery,/etc/zabbix/scripts/get_domain.sh
#检测域名证书过期时间
UserParameter=https[*], /etc/zabbix/scripts/https.sh $1
-
在zabbix配置自动发现规则
image.png
-
在zabbix配置自动生成监控项
image.png
-
在zabbix配置告警触发器
image.png
-
成功
image.png
附件
domain.txt
junode1.juzix.net
junode2.juzix.net
junode3.juzix.net
junode4.juzix.net
get_domain.sh
#!/bin/bash
lastline=`tail -1 /etc/zabbix/scripts/domains.txt`
echo "{\"data\":["
for i in `cat /etc/zabbix/scripts/domains.txt`
do
if [ "$i" != "$lastline" ];then
echo " {\"{#DOMAIN}\":\"$i\"},"
else
echo " {\"{#DOMAIN}\":\"$i\"}"
fi
done
echo "]}"
https.sh
#/bin/bash
#################Version Info#######################
#Author: Liang
#Create Date:2019-08-31
#Attention: 通过域名获取证书的过期时间
####################################################
#获取证书时间
end_time=`echo |openssl s_client -connect $1:443 2>/dev/null|openssl x509 -noout -dates|awk -F= '/After/{print $2}'`
#把证书时间转换为时间戳
end_timestamp=`date +%s -d "$end_time"`
#获取今天的时间戳
today=`date +%s`
#计算还有证书几天过期
date=$((($end_timestamp - $today)/86400))
echo $date
网友评论