根据公司要求,要求用zabbix对域名的ssl证书进行到期监控
直接上代码
1 #!/usr/bin/env python3 2 from urllib3.contrib import pyopenssl 3 from datetime import datetime 4 from sys import argv 5 6 7 class SSL_Check: 8 9 def __init__(self, url): 10 self.url = url 11 12 @property 13 def get_str_time(self): 14 x509 = pyopenssl.OpenSSL.crypto.load_certificate(pyopenssl.OpenSSL.crypto.FILETYPE_PEM, 15 pyopenssl.ssl.get_server_certificate((self.url, 443))) 16 return x509.get_notAfter().decode()[0:-1] 17 @property 18 def get_ssl_time(self): 19 ssl_time = datetime.strptime(self.get_str_time,'%Y%m%d%H%M%S') 20 return (ssl_time - datetime.now()).days 21 22 23 if __name__ == '__main__': 24 try: 25 url = argv[1] 26 ssl_chenk=SSL_Check(url) 27 print(ssl_chenk.get_ssl_time) 28 except Exception as e: 29 print('')
修改zabbix-agent的配置文件
1 vim /etc/zabbix/zabbix_agentd.conf 2 UserParameter=ssl_check[*],/etc/zabbix/script/ssl_check.py $1
登陆zabbix页面
配置-->主机-->监控项-->创建监控项
最后根据需求设置触发器证书还有多长时间报警提醒