zabbix监控证书到期时间

# 获取证书过期时间脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

# 域名自动发现脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 @   Hello_worlds  阅读(178)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示