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.1
by Tom Haddon
First draft of script monitoring |
5 |
|
6 |
"""Monitor scripts."""
|
|
7 |
||
8 |
__metaclass__ = type |
|
4417.4.9
by Tom Haddon
Checking command line options |
9 |
__all__ = ['check_script'] |
4417.4.1
by Tom Haddon
First draft of script monitoring |
10 |
|
14606.3.4
by William Grant
Replace canonical.database usage everywhere, and format-imports. |
11 |
import _pythonpath |
12 |
||
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
13 |
from datetime import ( |
14 |
datetime, |
|
15 |
timedelta, |
|
16 |
)
|
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
17 |
from email.MIMEText import MIMEText |
4417.4.1
by Tom Haddon
First draft of script monitoring |
18 |
from optparse import OptionParser |
4417.4.14
by Tom Haddon
Using own email to send out notifications |
19 |
import smtplib |
4417.4.1
by Tom Haddon
First draft of script monitoring |
20 |
import sys |
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
21 |
from time import strftime |
22 |
||
14606.3.4
by William Grant
Replace canonical.database usage everywhere, and format-imports. |
23 |
from lp.scripts.scriptmonitor import check_script |
24 |
from lp.services.database.sqlbase import connect |
|
14565.2.15
by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts. |
25 |
from lp.services.scripts import ( |
14027.3.4
by Jeroen Vermeulen
Automated import fixes and copyright updates. |
26 |
db_options, |
27 |
logger, |
|
28 |
logger_options, |
|
29 |
)
|
|
4417.4.12
by Tom Haddon
Basic tests (not working) for script monitoring |
30 |
|
4417.4.1
by Tom Haddon
First draft of script monitoring |
31 |
|
32 |
def main(): |
|
10303.1.1
by Gary Poster
use newest version of zc.buildout |
33 |
# XXX: Tom Haddon 2007-07-12
|
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
34 |
# There's a lot of untested stuff here: parsing options and sending
|
35 |
# emails - this should be moved into a testable location.
|
|
4417.4.22
by Tom Haddon
Clean up code after comments from Andrew Bennets |
36 |
# Also duplicated code in scripts/script-monitor-nagios.py
|
4417.4.1
by Tom Haddon
First draft of script monitoring |
37 |
parser = OptionParser( |
4417.4.9
by Tom Haddon
Checking command line options |
38 |
'%prog [options] (minutes) (host:scriptname) [host:scriptname]'
|
4417.4.1
by Tom Haddon
First draft of script monitoring |
39 |
)
|
40 |
db_options(parser) |
|
41 |
logger_options(parser) |
|
42 |
||
43 |
(options, args) = parser.parse_args() |
|
44 |
||
4417.4.9
by Tom Haddon
Checking command line options |
45 |
if len(args) < 2: |
46 |
parser.error("Must specify at time in minutes and " |
|
47 |
"at least one host and script") |
|
4417.4.1
by Tom Haddon
First draft of script monitoring |
48 |
|
4417.4.6
by Tom Haddon
Cleaning up formatting |
49 |
# First argument is the number of minutes into the past
|
50 |
# we want to look for the scripts on the specified hosts
|
|
4417.4.9
by Tom Haddon
Checking command line options |
51 |
try: |
52 |
minutes_ago, args = int(args[0]), args[1:] |
|
53 |
start_date = datetime.now() - timedelta(minutes=minutes_ago) |
|
54 |
||
55 |
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. |
56 |
completed_to = strftime( |
57 |
"%Y-%m-%d %H:%M:%S", datetime.now().timetuple()) |
|
4417.4.9
by Tom Haddon
Checking command line options |
58 |
|
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
59 |
hosts_scripts = [] |
4417.4.9
by Tom Haddon
Checking command line options |
60 |
for arg in args: |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
61 |
try: |
62 |
hostname, scriptname = arg.split(':') |
|
63 |
except TypeError: |
|
64 |
parser.error( |
|
65 |
"%r is not in the format 'host:scriptname'" % (arg,)) |
|
66 |
hosts_scripts.append((hostname, scriptname)) |
|
67 |
except ValueError: |
|
4417.4.11
by Tom Haddon
Use MAX(date_completed) as cleaner that ORDER BY and LIMIT, clearer error messages and formatting |
68 |
parser.error("Must specify time in minutes and " |
4417.4.9
by Tom Haddon
Checking command line options |
69 |
"at least one host and script") |
4417.4.1
by Tom Haddon
First draft of script monitoring |
70 |
|
71 |
log = logger(options) |
|
72 |
||
73 |
try: |
|
74 |
log.debug("Connecting to database") |
|
13879.1.3
by William Grant
Drop now-obsolete connect(user) args. |
75 |
con = connect() |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
76 |
error_found = False |
4417.4.14
by Tom Haddon
Using own email to send out notifications |
77 |
msg, subj = [], [] |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
78 |
for hostname, scriptname in hosts_scripts: |
10303.1.1
by Gary Poster
use newest version of zc.buildout |
79 |
failure_msg = check_script(con, log, hostname, |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
80 |
scriptname, completed_from, completed_to) |
81 |
if failure_msg is not None: |
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
82 |
msg.append(failure_msg) |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
83 |
subj.append("%s:%s" % (hostname, scriptname)) |
4417.4.11
by Tom Haddon
Use MAX(date_completed) as cleaner that ORDER BY and LIMIT, clearer error messages and formatting |
84 |
error_found = 2 |
4417.4.14
by Tom Haddon
Using own email to send out notifications |
85 |
if error_found: |
14027.3.1
by Jeroen Vermeulen
Fix lots of lint in recently-changed files. |
86 |
# Construct our email.
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
87 |
msg = MIMEText('\n'.join(msg)) |
88 |
msg['Subject'] = "Scripts failed to run: %s" % ", ".join(subj) |
|
5046.1.1
by Tom Haddon
Better from address for script monitor |
89 |
msg['From'] = 'script-failures@launchpad.net' |
90 |
msg['Reply-To'] = 'launchpad@lists.canonical.com' |
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
91 |
msg['To'] = 'launchpad@lists.canonical.com' |
10303.1.1
by Gary Poster
use newest version of zc.buildout |
92 |
|
14027.3.1
by Jeroen Vermeulen
Fix lots of lint in recently-changed files. |
93 |
# Send out the email.
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
94 |
smtp = smtplib.SMTP() |
95 |
smtp.connect() |
|
14027.3.1
by Jeroen Vermeulen
Fix lots of lint in recently-changed files. |
96 |
smtp.sendmail( |
97 |
'script-failures@launchpad.net', |
|
98 |
['launchpad@lists.canonical.com'], msg.as_string()) |
|
4417.4.14
by Tom Haddon
Using own email to send out notifications |
99 |
smtp.close() |
4417.4.16
by Tom Haddon
Using sqlvalues, commenting on untested parts, and cleaning up |
100 |
return 2 |
4417.4.1
by Tom Haddon
First draft of script monitoring |
101 |
except: |
102 |
log.exception("Unhandled exception") |
|
103 |
return 1 |
|
104 |
||
105 |
if __name__ == '__main__': |
|
106 |
sys.exit(main()) |