-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-dcv.py
158 lines (135 loc) · 5.05 KB
/
check-dcv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# SPDX-License-Identifier: GPL-3.0-or-later
# License-Filename: LICENSE
# -----------------------------------------------------------------------------
# Authors :
#
# - Yann 'Ze' Richard <yann.richard à univ-rennes2.fr> - DSI Université Rennes 2
#
# -----------------------------------------------------------------------------
import argparse
import time
import logging
import os
import sys
import traceback
from datetime import datetime
from datetime import date
from cert_manager import DCV
from cert_manager import Client
defaultLogLevel = logging.DEBUG
mylogger = logging.getLogger(__name__)
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
def debug_factory(logger, debug_level):
"""
Decorate logger in order to add custom levels for Nagios
"""
def custom_debug(msg, *args, **kwargs):
if logger.level >= debug_level:
return
logger._log(debug_level, msg, args, kwargs)
return custom_debug
def get_args():
parser = argparse.ArgumentParser(
description = "Check Sectigo DCV expiration",
epilog = ''' Sources : https://github.com/DSI-Universite-Rennes2/check-sectigo-dcv.git
'''
)
parser._optionals.title = "Options"
parser.add_argument('-w', '--warning', required=False, help='warning delay in days', dest='warn', type=int, default=30)
parser.add_argument('-c', '--critical', required=False, help='critical delay in days', dest='crit', type=int, default=15)
parser.add_argument('-v', '--verbose', required=False, help='enable verbose output', dest='verbose', action='store_true')
args = parser.parse_args()
return args
def getDomainExpireIn(days):
SClient = Client(
base_url = os.getenv('SECTIGO_API_BASEURL'),
login_uri = os.getenv('SECTIGO_API_LOGINURL'),
username = os.getenv('SECTIGO_API_USER'),
password = os.getenv('SECTIGO_API_PASS'),
)
dcv = DCV(client=SClient)
res = dcv.search(expiresIn=days)
domains = []
for domain in res:
domainName = domain["domain"]
dcvStatus = domain["dcvStatus"]
dcvOrderStatus = domain["dcvOrderStatus"]
dcvMethod = domain["dcvMethod"]
expirationDate = domain["expirationDate"]
expirationDT = datetime.strptime(expirationDate, '%Y-%m-%d').date()
expireIn = expirationDT - date.today()
# Ignore wildcard subdomains
if domainName.startswith('*.'):
continue
info = {
'domain': domainName,
'dcvStatus': dcvStatus,
'dcvOrderStatus': dcvOrderStatus,
'dcvMethod': dcvMethod,
'expirationDate': expirationDate,
'expireInDays': expireIn.days,
}
domains.append(info)
return domains
def main():
# Handling arguments
args = get_args()
warn = args.warn
crit = args.crit
verbose = args.verbose
# Logging settings
if verbose:
log_level = logging.DEBUG
else:
log_level = logging.INFO
# Add custom level unknown
logging.addLevelName(logging.DEBUG+1, 'UNKOWN')
setattr(mylogger, 'unkown', debug_factory(mylogger, logging.DEBUG+1))
# Change INFO LevelName to OK
logging.addLevelName(logging.INFO, 'OK')
# Setting output format for Nagios
logging.basicConfig(stream=sys.stdout, format='%(levelname)s - %(message)s', level=log_level)
if warn < crit:
mylogger.unkown(f'Error : warning value must be greater than critical ( {warn} > {crit} )')
sys.exit(UNKNOWN)
# ------------------------------------------------------------------------ #
# checking DCV
try:
domains = getDomainExpireIn(warn)
nagiosExit = OK
nagiosMessage = f'No domain will expire within {warn} days'
warnDomains = []
critDomains = []
if domains:
nagiosExit = WARNING
delayAlert = warn
for d in domains:
if d['expireInDays'] <= crit:
nagiosExit = CRITICAL
delayAlert = crit
critDomains.append('{} ({})'.format(d['domain'], d['expireInDays']))
else:
warnDomains.append('{} ({})'.format(d['domain'], d['expireInDays']))
listOfDomains = ', '.join(critDomains + warnDomains)
nagiosMessage = f'{len(domains)} domain is about to expire within {delayAlert} days : {listOfDomains}'
if nagiosExit == WARNING:
mylogger.warning(nagiosMessage)
else:
mylogger.critical(nagiosMessage)
else:
mylogger.info("%s" % nagiosMessage)
sys.exit(nagiosExit)
except Exception as ex:
mylogger.unkown(f'Exception raised: {ex}')
traceback.print_exc()
sys.exit(UNKNOWN)
finally:
sys.exit(nagiosExit)
if __name__ == "__main__":
main()