==== Check_certif_zabbix_sender ==== #!/usr/bin/python3 #*.* encoding: utf-8 *.* Version = 1.2 get_help = """ *** Script test Certificat *** Auteur: TLG - [MGI] Modification: DDEHOS - [MGI] Date: 21/11/2018 Version: """ + str(Version) + """ Description: Retourne les informations des certificats. Dependances: * nmap Utilisation: check_cert -u mail.musee-orsay.fr -p 993 : Retourne des info sur le Certificat check_cert --help / -h : Affiche ce message check_cert --verbose / -v Version: 21/11/2018 : TLG - On génère le script """ ### Variables ### Modules import sys import os import socket from datetime import datetime ### Fonctions def zabbix_send(_url, _port, _key, _value): # On donne une valeur a Zabbix #result = os.system("/usr/bin/zabbix_sender -z epmo-ux-supervision.epmo.local -s " + str(socket.gethostname()) + " -k " + _key + " -o '" + str(_value) + "' > /dev/null") result = os.system("/usr/bin/zabbix_sender -z epmo-ux-superv.epmo.local -s " + _url + "-" + str(_port) + " -k " + _key + " -o \"" + str(_value) + "\" > /dev/null") return result def get_certificat(_url, _port): # On retourne les informations le certificat subject = "" expiration = "" certificat = os.popen('nmap -sC ' + _url + ' -p ' + str(_port) + '|grep -E "(Not valid after|Subject:)"').read() try: subject = certificat.split('\n')[0].split('Subject: ')[1] except IndexError: pass try: expiration = certificat.split('\n')[1].split("after: ")[1] except IndexError: pass return subject, expiration def get_timestamp(_date): # On transforme la date sous format '2018-11-22T01:54:55' en timestamp try: if _date: datetime_cert = datetime.strptime(_date, '%Y-%m-%dT%H:%M:%S') result = datetime_cert.timestamp() - datetime.now().timestamp() return round(result / 60 / 60 /24) else: print("[ERROR] - Pas de date détecté") return 0 except ValueError: print("[ERROR] - La date " + _date + " n'est pas au format '%Y-%m-%dT%H:%M:%S'") print("Pour corriger cela, veuillez changer le format de la date dans la fonction get_timestamp(_date)") print("pour que le format soit le même") pass def split_subject(_subject): # On split le sujet et on le retourne en dictionnaire result = {} if _subject: for value in _subject.split('/'): info = value.split('=') result[info[0]] = info[1] else: print("[ERROR] - Pas d'information sur le certificat détecté") return result ### Debut du code # On affiche l'aide s'il n'y a pas d'option if len(sys.argv) == 1 or sys.argv.count('--help') or sys.argv.count('-h'): print(get_help) exit(0) try: url = sys.argv[sys.argv.index('-u') + 1] port = sys.argv[sys.argv.index('-p') + 1] except: print("[ERROR] - Paramètre incorrect ou manquant (-u / -p)") print(get_help) exit(1) # On récupère le certificat try: certificat = get_certificat(url, port) except: print("[ERROR] - Impossible de récupérer le certificat") print(get_help) exit(1) # On traite les données cert_timestamp = get_timestamp(certificat[1]) cert_subject = split_subject(certificat[0]) # On envoie les informations à Zabbix result = 0 if certificat[1]: result += zabbix_send(url, port, "expiration", str(cert_timestamp)) for info in cert_subject.keys(): result += zabbix_send(url, port, info, cert_subject[info]) if result > 0: print("[ERROR] - Erreurs lors de l'envoie des informations à Zabbix") ====