10637.3.1
by Guilherme Salgado
Use the default python version instead of a hard-coded version |
1 |
#!/usr/bin/python -S
|
8687.15.22
by Karl Fogel
Add the copyright header block to the remaining .py files. |
2 |
#
|
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
3 |
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
|
8687.15.22
by Karl Fogel
Add the copyright header block to the remaining .py files. |
4 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
5 |
|
4417.4.23
by Tom Haddon
Another round of comments from Andrew Bennets and RF merge |
6 |
"""Nagios plugin for script monitoring.
|
7 |
||
10303.1.1
by Gary Poster
use newest version of zc.buildout |
8 |
This script is needed as separate from script-monitor.py because Nagios
|
4417.4.24
by Tom Haddon
Improved docstring for script-monitor-nagios script |
9 |
only understands one line of returned text, and interprets specific
|
4417.4.23
by Tom Haddon
Another round of comments from Andrew Bennets and RF merge |
10 |
return codes as plugin statuses. These are:
|
4417.4.24
by Tom Haddon
Improved docstring for script-monitor-nagios script |
11 |
|
12 |
0: OK
|
|
13 |
1: WARNING
|
|
14 |
2: CRITICAL
|
|
15 |
3: UNKNOWN
|
|
16 |
||
10303.1.1
by Gary Poster
use newest version of zc.buildout |
17 |
As such, it was felt more appropriate to separate out the scripts,
|
4417.4.24
by Tom Haddon
Improved docstring for script-monitor-nagios script |
18 |
even though there is some code duplication.
|
4417.4.23
by Tom Haddon
Another round of comments from Andrew Bennets and RF merge |
19 |
"""
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
20 |
|
21 |
__metaclass__ = type |
|
22 |
__all__ = ['check_script'] |
|
23 |
||
14606.3.4
by William Grant
Replace canonical.database usage everywhere, and format-imports. |
24 |
import _pythonpath |
25 |
||
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
26 |
from datetime import ( |
27 |
datetime, |
|
28 |
timedelta, |
|
29 |
)
|
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
30 |
from optparse import OptionParser |
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
31 |
import sys |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
32 |
from time import strftime |
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
33 |
|
14606.3.4
by William Grant
Replace canonical.database usage everywhere, and format-imports. |
34 |
from lp.scripts.scriptmonitor import check_script |
35 |
from lp.services.database.sqlbase import connect |
|
14565.2.15
by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts. |
36 |
from lp.services.scripts import ( |
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
37 |
db_options, |
38 |
logger, |
|
39 |
logger_options, |
|
40 |
)
|
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
41 |
|
42 |
||
43 |
def main(): |
|
10303.1.1
by Gary Poster
use newest version of zc.buildout |
44 |
# XXX: Tom Haddon 2007-07-12
|
45 |
# There's a lot of untested stuff here: parsing options -
|
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
46 |
# this should be moved into a testable location.
|
4417.4.22
by Tom Haddon
Clean up code after comments from Andrew Bennets |
47 |
# Also duplicated code in scripts/script-monitor.py
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
48 |
parser = OptionParser( |
49 |
'%prog [options] (minutes) (host:scriptname) [host:scriptname]'
|
|
50 |
)
|
|
51 |
db_options(parser) |
|
52 |
logger_options(parser) |
|
53 |
||
54 |
(options, args) = parser.parse_args() |
|
55 |
||
56 |
if len(args) < 2: |
|
57 |
print "Must specify time in minutes and " \ |
|
58 |
"at least one host and script"
|
|
59 |
return 3 |
|
60 |
||
61 |
# First argument is the number of minutes into the past
|
|
62 |
# we want to look for the scripts on the specified hosts
|
|
63 |
try: |
|
64 |
minutes_ago, args = int(args[0]), args[1:] |
|
65 |
start_date = datetime.now() - timedelta(minutes=minutes_ago) |
|
66 |
||
67 |
completed_from = strftime("%Y-%m-%d %H:%M:%S", start_date.timetuple()) |
|
14027.3.1
by Jeroen Vermeulen
Fix lots of lint in recently-changed files. |
68 |
completed_to = strftime( |
69 |
"%Y-%m-%d %H:%M:%S", datetime.now().timetuple()) |
|
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
70 |
|
71 |
hosts_scripts = [] |
|
72 |
for arg in args: |
|
73 |
try: |
|
74 |
hostname, scriptname = arg.split(':') |
|
75 |
except TypeError: |
|
4417.4.21
by Tom Haddon
Fixed string conversion and error_found now boolean |
76 |
print "%r is not in the format 'host:scriptname'" % arg |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
77 |
return 3 |
78 |
hosts_scripts.append((hostname, scriptname)) |
|
79 |
except ValueError: |
|
80 |
print "Must specify time in minutes and " \ |
|
81 |
"at least one host and script"
|
|
82 |
return 3 |
|
83 |
||
84 |
log = logger(options) |
|
85 |
||
86 |
try: |
|
87 |
log.debug("Connecting to database") |
|
13879.1.3
by William Grant
Drop now-obsolete connect(user) args. |
88 |
con = connect() |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
89 |
error_found = False |
90 |
msg = [] |
|
91 |
for hostname, scriptname in hosts_scripts: |
|
10303.1.1
by Gary Poster
use newest version of zc.buildout |
92 |
failure_msg = check_script(con, log, hostname, |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
93 |
scriptname, completed_from, completed_to) |
94 |
if failure_msg is not None: |
|
95 |
msg.append("%s:%s" % (hostname, scriptname)) |
|
4417.4.21
by Tom Haddon
Fixed string conversion and error_found now boolean |
96 |
error_found = True |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
97 |
if error_found: |
98 |
# Construct our return message
|
|
99 |
print "Scripts failed to run: %s" % ', '.join(msg) |
|
100 |
return 2 |
|
101 |
else: |
|
102 |
# Construct our return message
|
|
103 |
print "All scripts ran as expected" |
|
104 |
return 0 |
|
4417.4.23
by Tom Haddon
Another round of comments from Andrew Bennets and RF merge |
105 |
except Exception, e: |
14027.3.1
by Jeroen Vermeulen
Fix lots of lint in recently-changed files. |
106 |
# Squeeze the exception type and stringification of the exception
|
107 |
# value on to one line.
|
|
4417.4.23
by Tom Haddon
Another round of comments from Andrew Bennets and RF merge |
108 |
print "Unhandled exception: %s %r" % (e.__class__.__name__, str(e)) |
4417.4.18
by Tom Haddon
Merged RF and added Nagios script monitoring check |
109 |
return 3 |
110 |
||
111 |
if __name__ == '__main__': |
|
112 |
sys.exit(main()) |