zabbix监控证书到期时间

# 获取证书过期时间脚本

cat  /etc/zabbix/scripts/check-cert-expire.sh

#!/bin/bash
host=$1
port=$2
end_date=`/usr/bin/openssl s_client -servername $host -host $host -port $port -showcerts </dev/null 2>/dev/null |
  sed -n '/BEGIN CERTIFICATE/,/END CERT/p' |
  /usr/bin/openssl  x509 -text 2>/dev/null |
  sed -n 's/ *Not After : *//p'`
# openssl 检验和验证SSL证书。
# -servername $host 因一台主机存在多个证书,利用SNI特性检查
# </dev/null 定向标准输入,防止交互式程序。从/dev/null 读时,直接读出0 。
# sed -n 和p 一起使用,仅显示匹配到的部分。 //,// 区间匹配。
# openssl x509 -text 解码证书信息,包含证书的有效期。

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

# 域名自动发现脚本:

cat /etc/zabbix/scripts/sshcert_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/ssl_cert_list","r") as f:
        for sslcert in f:
            dict={}
            dict["{#DOMAINNAME}"]=sslcert.strip().split()[0]
            dict["{#PORT}"]=sslcert.strip().split()[1]
            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/ssl_cert_list
www.baidu.com 443
www.jd.com 443

# zabbix配置

cat /etc/zabbix/zabbix_agentd.conf.d/userparameter_sslcert.conf
UserParameter=sslcert_discovery,/usr/bin/python  /etc/zabbix/scripts/sshcert_discovery.py
UserParameter=sslcert.info[*],/bin/bash /etc/zabbix/scripts/check-cert-expire.sh $1 $2

 

# zabbix添加监控模版

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>5.2</version>
    <date>2021-01-06T11:40:38Z</date>
    <groups>
        <group>
            <name>Templates</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template ssl cert Information</template>
            <name>Template ssl cert Information</name>
            <groups>
                <group>
                    <name>Templates</name>
                </group>
            </groups>
            <discovery_rules>
                <discovery_rule>
                    <name>ssl cert information</name>
                    <key>sslcert_discovery</key>
                    <delay>10</delay>
                    <item_prototypes>
                        <item_prototype>
                            <name>[{#DOMAINNAME}]证书剩余时间:</name>
                            <key>sslcert.info[{#DOMAINNAME},{#PORT}]</key>
                            <delay>120s</delay>
                            <units>天</units>
                            <preprocessing>
                                <step>
                                    <type>MULTIPLIER</type>
                                    <parameters>
                                        <parameter>1</parameter>
                                    </parameters>
                                </step>
                            </preprocessing>
                            <trigger_prototypes>
                                <trigger_prototype>
                                    <expression>{last()}&lt;90</expression>
                                    <name>[{#DOMAINNAME}] 证书到期时间小于90天</name>
                                    <priority>HIGH</priority>
                                </trigger_prototype>
                            </trigger_prototypes>
                        </item_prototype>
                    </item_prototypes>
                </discovery_rule>
            </discovery_rules>
        </template>
    </templates>
</zabbix_export>

 

调整触发器时长,验证配置是否正确。

# 出发告警

 

 

 # 告警恢复

 

posted @ 2021-01-06 19:48  Hello_worlds  阅读(168)  评论(0编辑  收藏  举报