Zabbix自动发现,监控ssl证书

  • 自动发现域名脚本

# -*- coding: utf-8 -*-

from os import path
import json


DIR_PATH = path.dirname(path.abspath(__file__))
dataFile='%s/all_domain.db' % DIR_PATH
res = {"data": []}

with open(dataFile, 'r') as read_f:
    for line in read_f:
        line_l = line.split()
        ip_dic = {"{#DOMAIN_NAME}": line_l[0]}
        res['data'].append(ip_dic)

print(json.dumps(res))

监控ssl证书有效期

# -*- coding: utf-8 -*-

import re
import sys
import time
import subprocess
from datetime import datetime
from io import StringIO
 
def get_certificate_overdue_days(domain):
    f = StringIO()
    comm = f"curl -Ivs https://{domain} --connect-timeout 10"
    result = subprocess.getstatusoutput(comm)
    f.write(result[1])
   
    try:
        m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
        start_date = m.group(1)
        expire_date = m.group(2)
        common_name = m.group(3)
        issuer = m.group(4)
    except Exception as e:
        return 0
   
    # time 字符串转时间数组
    start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
    start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
    # datetime 字符串转时间数组
    expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
    expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")
   
    # 剩余天数
    remaining = (expire_date-datetime.now()).days
    if remaining < 0:
        return 0 
    return remaining 
 
if __name__ == "__main__":
    domain = sys.argv[1] 
    remaining_days = get_certificate_overdue_days(domain)
    print(remaining_days)

你可能感兴趣的:(zabbix,zabbix)