-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_time_http
executable file
·125 lines (110 loc) · 4.57 KB
/
check_time_http
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
#!/usr/bin/env python3
#
# MIT License
# Copyright (c) 2019 Dick Visser
#
# https://github.com/dnmvisser/nagios_check_time_by_http_date
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import sys
import requests
import datetime
import pytz
import email.utils
import urllib3
def nagios_exit(message, code):
print(message)
sys.exit(code)
# start with clean slate
ok_msg = []
warn_msg = []
crit_msg = []
unknown_msg = []
try:
parser = argparse.ArgumentParser(description=
'Check time offset of the HTTP "Date" response header against the local time')
parser.add_argument('--url', '-u',
help='the URL to check', required=True)
parser.add_argument('--useragent', '-a',
help='the User-Agent string to use', required=False, default='check_time_http')
# https://kantarainitiative.github.io/SAMLprofiles/saml2int.html#_clock_skew
parser.add_argument('--warn', '-w',
help='Offset to result in warning (seconds, default 180)',
required=False, type=int, default=180)
parser.add_argument('--crit', '-c',
help='Offset to result in critical (seconds, default 300)',
required=False, type=int, default=300)
parser.add_argument('--timeout', '-t',
help='Timeout of the HTTP request (seconds)',
required=False, type=int)
parser.add_argument('--verbose', '-v',
help='Return verbose output',
required=False, dest='verbose', action='store_true')
verify_parser = parser.add_mutually_exclusive_group(required=False)
verify_parser.add_argument('--verify',
dest='verify', action='store_true',
help="Verify SSL certificate (default)")
verify_parser.add_argument('--no-verify',
dest='verify', action='store_false',
help="Do not verify SSL certificate")
parser.set_defaults(verify=True)
args = parser.parse_args()
url = args.url
useragent = args.useragent
warn = args.warn
crit = args.crit
timeout = args.timeout
verify = args.verify
verbose = args.verbose
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
req = requests.head(
url,
timeout=timeout,
verify=verify,
headers={'User-Agent': useragent}
)
# Date with time zone
utc_now = pytz.utc.localize(datetime.datetime.utcnow().replace(microsecond=0))
# this is case insensitive
if 'date' in req.headers:
http_date = email.utils.parsedate_to_datetime(req.headers['date'])
offset = int((http_date - utc_now).total_seconds())
message = "Offset is {0} seconds".format(offset)
if(verbose):
message += ", URL: {0}".format(url)
message += ", local date: {0}".format(utc_now)
message += ", HTTP headers: {0}".format(req.headers)
if(abs(offset) > crit):
crit_msg.append(message)
elif(abs(offset) > warn):
warn_msg.append(message)
else:
ok_msg.append(message)
else:
unknown_msg.append("No Date header detected for {0}".format(url))
except Exception as e:
unknown_msg.append("{0}.".format(e))
# Exit with accumulated message(s)
if unknown_msg:
nagios_exit("UNKNOWN: " + ' '.join(unknown_msg), 3)
elif crit_msg:
nagios_exit("CRITICAL: " + ' '.join(crit_msg + warn_msg), 2)
elif warn_msg:
nagios_exit("WARNING: " + ' '.join(warn_msg), 1)
else:
nagios_exit("OK: " + ' '.join(ok_msg), 0)