-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_puppet.py
executable file
·294 lines (224 loc) · 8.65 KB
/
check_puppet.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Contact foreman to see when was the last puppet run for a given client.
If the client reported within valid period,
check also that puppet reported a success
Few doctests, run with :
$ python -m doctest check_puppet.py -v
"""
__author__ = 'Julien Rottenberg'
__date__ = "February 2012"
__version__ = "1.0"
__credits__ = """Thanks to Foreman - http://theforeman.org/"""
from datetime import timedelta, datetime
from optparse import OptionParser, OptionGroup
import base64
import urllib2
import re
from urllib2 import HTTPError, URLError
from socket import setdefaulttimeout
try:
import simplejson as json
except ImportError:
# pylint: disable-msg=W0404
import json
def get_data(url, username, password, timeout):
"""
Initialize the connection to Foreman
Fetch data using the api
"""
request = urllib2.Request(url)
request.add_header('Content-Type', 'application/json')
request.add_header('Accept', 'application/json')
if (username and password):
b64string = base64.encodestring('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % b64string)
try:
setdefaulttimeout(timeout)
raw_out = urllib2.urlopen(request).read()
out = json.loads(raw_out)
except HTTPError:
print 'CRITICAL - Check %s does that node ever reported?' % url
raise SystemExit, 2
except URLError:
print 'CRITICAL - Error on %s Double check foreman name' % url
raise SystemExit, 2
return out
def seconds2human(my_time):
"""
Convert given duration in seconds into human readable string
>>> seconds2human(60)
'0:01:00'
>>> seconds2human(300)
'0:05:00'
>>> seconds2human(3601)
'1:00:01'
>>> seconds2human(86401)
'1 day, 0:00:01'
"""
time_delta = timedelta(seconds=my_time)
return str(time_delta)
def check_result(params, server):
"""
From the server response and input parameter
check if the puppet client report should trigger an alert
http://theforeman.org/projects/foreman/wiki/API
"""
last_report_str = server['reported_at']
report_summary = server['summary']
try:
total_report_time = server['metrics']['time']['total']
# foreman seems to have issue with sum of time for some reports
except KeyError:
total_report_time = 'N/A'
# No dateutil.parser on centos5 stock (python-dateutil.noarch)
# we know output timezone is Zulu
last_report = datetime(*map(int, re.split('[^\d]', last_report_str)[:-1]))
# see http://stackoverflow.com/a/127872 for details
now = params['now']
now_since_last_report = now - last_report
msg = 'Last report was marked as %s %s ago - took %s seconds' % (
report_summary,
now_since_last_report,
total_report_time)
if (now_since_last_report >= timedelta(minutes=params['critical'])):
status = 'CRITICAL'
elif (now_since_last_report >= timedelta(minutes=params['warning'])):
status = 'WARNING'
else:
if (report_summary != 'Success'):
status = 'WARNING'
else:
status = 'OK'
return(status, msg)
def usage():
"""
Return usage text so it can be used on failed human interactions
"""
usage_string = """
usage: %prog [options] -H SERVER -F FOREMAN_HOST -w WARNING -c CRITICAL
Make sure the last report seen by report for the given host is not too old
or exiting with an error
Warning and Critical are defined in minutes
Ex :
check_puppet.py -H server1.example.com -F foreman.example.com -w 60 -c 120
will check if the server1 has reported to foreman.example.com in the last hour
"""
return usage_string
def controller():
"""
Parse user input, fail quick if not enough parameters
"""
description = """A Nagios plugin to check if the last report
for a puppet client was successful and not too long ago."""
version = "%prog " + __version__
parser = OptionParser(description=description, usage=usage(),
version=version)
parser.set_defaults(verbose=False)
parser.add_option('-H', '--hostname', type='string',
help='Puppet client hostname')
parser.add_option('-w', '--warning', type='int', default=30,
help='Warning threshold in minutes')
parser.add_option('-c', '--critical', type='int', default=60,
help='Critical threshold in minutes')
parser.add_option('-F', '--foreman', type='string',
help='foreman host to contact')
connection = OptionGroup(parser, "Connection Options",
"Network / Authentication related options")
connection.add_option('-u', '--username', type='string',
help='Foreman username')
connection.add_option('-p', '--password', type='string',
help='Foreman password')
connection.add_option('-t', '--timeout', type='int', default=10,
help='Connection timeout in seconds')
connection.add_option('-P', '--port', type='int',
help='Foreman port',
default=80)
connection.add_option('--prefix', type='string',
help='Foreman prefix, if not installed on /',
default='/')
connection.add_option('-S', '--ssl', action="store_true",
default=False,
help='If the connection requires ssl')
parser.add_option_group(connection)
extra = OptionGroup(parser, "Extra Options")
extra.add_option('-v', action='store_true', dest='verbose',
default=False,
help='Verbose mode')
parser.add_option_group(extra)
options, arguments = parser.parse_args()
if (arguments != []):
print """Non recognized option %s
Please use --help for usage""" % arguments
print usage()
raise SystemExit, 2
if (options.hostname == None):
print "-H HOSTNAME"
print "We need the puppet client hostname to test against"
print usage()
raise SystemExit, 2
if (options.foreman == None):
print "\n-F FOREMAN_SERVER"
print "\nWe need to know which Foreman to query against"
print usage()
raise SystemExit, 2
return vars(options)
def main():
"""
Runs all the functions
"""
# Command Line Parameters
user_in = controller()
if user_in['verbose']:
def verboseprint(*args):
""" http://stackoverflow.com/a/5980173 print only when verbose ON"""
# Print each argument separately so caller doesn't need to
# stuff everything to be printed into a single string
print
for arg in args:
print arg,
print
else:
verboseprint = lambda *a: None # do-nothing function
# Validate the port based on the required protocol
if user_in['ssl']:
protocol = "https"
# Unspecified port will be 80 by default, not correct if ssl is ON
if (user_in['port'] == 80):
user_in['port'] = 443
else:
protocol = "http"
# Let's avoid the double / if we specified a prefix
if (user_in['prefix'] != '/'):
user_in['prefix'] = '/%s/' % user_in['prefix']
user_in['url'] = "%s://%s:%s%shosts/%s/%s" % (protocol,
user_in['foreman'],
user_in['port'],
user_in['prefix'],
user_in['hostname'],
'reports/last')
# Get the current UTC time, no need to get the microseconds
# we use UTC time as foreman output utc time by default
user_in['now'] = datetime.utcnow().replace(microsecond=0)
verboseprint("CLI Arguments : ", user_in)
foreman_out = get_data(user_in['url'],
user_in['username'],
user_in['password'],
user_in['timeout'])
verboseprint("Reply from server : \n%s" % json.dumps(foreman_out,
sort_keys=True,
indent=2))
status, message = check_result(user_in, foreman_out['report'])
print '%s - %s' % (status, message)
# Exit statuses recognized by Nagios
if status == 'OK':
raise SystemExit, 0
elif status == 'WARNING':
raise SystemExit, 1
elif status == 'CRITICAL':
raise SystemExit, 2
else:
raise SystemExit, 3
if __name__ == '__main__':
main()