zabbix 监控域名到期时间
cat userparameter_http.conf
UserParameter=http_discovery,/usr/bin/python /etc/zabbix/scripts/base/http_discovery.py UserParameter=http.info[*],/bin/bash /etc/zabbix/scripts/base/check-http-expire.sh $1
cat /etc/zabbix/scripts/base/http_list
baidu.com 443
cat /etc/zabbix/scripts/base/http_discovery.py
#!/usr/bin/env python #coding:utf-8 import os import sys import json #这个函数主要是构造出一个特定格式的字典,用于zabbix def ssl_cert_discovery(): web_list=[] web_dict={"data":None} with open("/etc/zabbix/scripts/base/http_list","r") as f: for sslcert in f: dict={} dict["{#DOMAINNAME}"]=sslcert.strip().split()[0] dict["{#PORT}"]=sslcert.strip().split()[1] dict["{#OLDNS}"]=sslcert.strip().split()[2] web_list.append(dict) web_dict["data"]=web_list jsonStr = json.dumps(web_dict,indent=4) return jsonStr if __name__ == "__main__": print ssl_cert_discovery()
cat /etc/zabbix/scripts/base/check-http-expire.sh
#!/bin/bash host=$1 #end_date=`whois -H $host|grep "Registry Expiry Date"|awk '{print $NF}'` end_date=`whois -H $host|egrep "Registry Expiry Date|Expiration Time"|awk '{print $3,$4}'|sed 's#Date: ##g'` #echo "$end_date" if [ -n "$end_date" ] then end_date_seconds=`date '+%s' --date "$end_date"` now_seconds=`date '+%s'` echo "($end_date_seconds-$now_seconds)/24/3600" | bc fi
模板
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>5.2</version> <date>2022-08-12T04:53:53Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template http expire date</template> <name>Template http expire date</name> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>域名过期剩余时间</name> </application> </applications> <discovery_rules> <discovery_rule> <name>域名到期_Discovery</name> <type>ZABBIX_ACTIVE</type> <key>http_discovery</key> <delay>24h</delay> <lifetime>8h</lifetime> <item_prototypes> <item_prototype> <name>[{#DOMAINNAME}]域名过期剩余时间:</name> <type>ZABBIX_ACTIVE</type> <key>http.info[{#DOMAINNAME},{#PORT}]</key> <delay>24h</delay> <history>0</history> <trends>7d</trends> <units>天</units> <applications> <application> <name>域名过期剩余时间</name> </application> </applications> <preprocessing> <step> <type>MULTIPLIER</type> <parameters> <parameter>1</parameter> </parameters> </step> </preprocessing> <trigger_prototypes> <trigger_prototype> <expression>{last()}<33</expression> <name>[{#DOMAINNAME}] 域名过期剩余时间小于33天</name> <priority>HIGH</priority> <manual_close>YES</manual_close> </trigger_prototype> </trigger_prototypes> </item_prototype> </item_prototypes> </discovery_rule> </discovery_rules> </template> </templates> </zabbix_export>