forked from liquidat/nagios-icinga-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_puppetagent
executable file
·132 lines (107 loc) · 4.08 KB
/
check_puppetagent
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
#! /usr/bin/python
# NRPE plugin to check if Puppet agent works properly.
# Extracts the age from last_run_summary.yaml and also verifies
# the syntax of the yaml file.
#
# Copyright 2013 Roland Wolters, credativ GmbH
#
# Version 20130905
#
# 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 sys
import os
import datetime
import argparse
import yaml
summary = '/var/lib/puppet/state/last_run_summary.yaml'
def ok(message):
print 'OK: %s' % message
sys.exit(0)
def warning(message):
print 'WARN: %s' % message
sys.exit(1)
def critical(message):
print 'CRIT: %s' % message
sys.exit(2)
def unknown(message):
print 'UNKNOWN: %s' % message
sys.exit(3)
def time_prettyfy(secs):
"""Returns a human readable time description"""
days = secs // 86400
hours = (secs % 86400) // 3600
minutes = (secs % 3600) // 60
seconds = secs % 60
message = '%d seconds ago' % seconds
if minutes:
message = '%d minutes and %s' % (minutes, message)
if hours:
message = '%d hours, %s' % (hours, message)
if days:
message = '%d days, %s' % (days, message)
return message
def optionsparser():
parser = argparse.ArgumentParser()
parser.add_argument("-w","--warn", dest="warn", type=int, default=1200, help="seconds after last Puppet run which issues a warning")
parser.add_argument("-c","--crit", type=int, dest="crit", default=2700, help="seconds after last Puppet run which are critical")
arguments = parser.parse_args()
try:
warn_seconds = arguments.warn
crit_seconds = arguments.crit
except Exception, exc:
unknown('Unable to parse arguments: %s' % exc)
if not os.path.isfile(summary):
# Should perhaps be CRIT
warning('Puppet has never run, no %s found.' % summary)
return warn_seconds,crit_seconds
def getyamlfile(summary):
"""Get time information from yaml file"""
summaryfile = open(summary)
summarymap = yaml.safe_load(summaryfile)
summaryfile.close()
# Check if the yaml file has correct entries
# If "events" is missing, the puppet run failed for some reason.
# Also, we check the yaml file for failures puppet detected itself.
try:
failures = summarymap["events"]["failure"]
except Exception, exc:
critical('Yaml file not properly formatted, last puppet run failed.')
if failures:
critical('Puppet status file lists failures.')
return summarymap
def main():
warn_seconds,crit_seconds = optionsparser()
yaml_map = getyamlfile(summary)
# Check last_run time on summary file
now = datetime.datetime.now()
lastruntime = datetime.datetime.fromtimestamp(int(yaml_map["time"]["last_run"]))
diff_time = now - lastruntime
diff_sec = diff_time.total_seconds()
lastrun = 'Puppet was last run %s' % time_prettyfy(diff_sec)
if lastruntime > now:
unknown('Puppet summary file modified in the future!')
elif diff_sec > crit_seconds:
critical(lastrun)
elif diff_sec > warn_seconds:
warning(lastrun)
else:
ok(lastrun)
if __name__ == "__main__":
main()