~launchpad-pqm/launchpad/devel

4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
1
#!/usr/bin/python2.4
2
# Copyright 2007 Canonical Ltd.  All rights reserved.
3
4417.4.23 by Tom Haddon
Another round of comments from Andrew Bennets and RF merge
4
"""Nagios plugin for script monitoring.
5
4417.4.24 by Tom Haddon
Improved docstring for script-monitor-nagios script
6
This script is needed as separate from script-monitor.py because Nagios 
7
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
8
return codes as plugin statuses. These are:
4417.4.24 by Tom Haddon
Improved docstring for script-monitor-nagios script
9
10
    0: OK
11
    1: WARNING
12
    2: CRITICAL
13
    3: UNKNOWN
14
15
As such, it was felt more appropriate to separate out the scripts, 
16
even though there is some code duplication.
4417.4.23 by Tom Haddon
Another round of comments from Andrew Bennets and RF merge
17
"""
4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
18
19
__metaclass__ = type
20
__all__ = ['check_script']
21
22
import _pythonpath
23
24
from datetime import datetime, timedelta
25
from optparse import OptionParser
26
from time import strftime
27
import sys
28
29
from canonical.database.sqlbase import connect
30
from canonical.launchpad.scripts import db_options, logger_options, logger
31
from canonical.launchpad.scripts.scriptmonitor import check_script
32
33
34
def main():
35
    # XXX: Tom Haddon 2007-07-12 
36
    # There's a lot of untested stuff here: parsing options - 
37
    # this should be moved into a testable location.
4417.4.22 by Tom Haddon
Clean up code after comments from Andrew Bennets
38
    # Also duplicated code in scripts/script-monitor.py
4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
39
    parser = OptionParser(
40
            '%prog [options] (minutes) (host:scriptname) [host:scriptname]'
41
            )
42
    db_options(parser)
43
    logger_options(parser)
44
45
    (options, args) = parser.parse_args()
46
47
    if len(args) < 2:
48
        print "Must specify time in minutes and " \
49
            "at least one host and script"
50
        return 3
51
52
    # First argument is the number of minutes into the past
53
    # we want to look for the scripts on the specified hosts
54
    try:
55
        minutes_ago, args = int(args[0]), args[1:]
56
        start_date = datetime.now() - timedelta(minutes=minutes_ago)
57
58
        completed_from = strftime("%Y-%m-%d %H:%M:%S", start_date.timetuple())
59
        completed_to = strftime("%Y-%m-%d %H:%M:%S", datetime.now().timetuple())
60
61
        hosts_scripts = []
62
        for arg in args:
63
            try:
64
                hostname, scriptname = arg.split(':')
65
            except TypeError:
4417.4.21 by Tom Haddon
Fixed string conversion and error_found now boolean
66
                print "%r is not in the format 'host:scriptname'" % arg
4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
67
                return 3
68
            hosts_scripts.append((hostname, scriptname))
69
    except ValueError:
70
        print "Must specify time in minutes and " \
71
            "at least one host and script"
72
        return 3
73
74
    log = logger(options)
75
76
    try:
77
        log.debug("Connecting to database")
78
        con = connect(options.dbuser)
79
        error_found = False
80
        msg = []
81
        for hostname, scriptname in hosts_scripts:
82
            failure_msg = check_script(con, log, hostname, 
83
                scriptname, completed_from, completed_to)
84
            if failure_msg is not None:
85
                msg.append("%s:%s" % (hostname, scriptname))
4417.4.21 by Tom Haddon
Fixed string conversion and error_found now boolean
86
                error_found = True
4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
87
        if error_found:
88
            # Construct our return message
89
            print "Scripts failed to run: %s" % ', '.join(msg)
90
            return 2
91
        else:
92
            # Construct our return message
93
            print "All scripts ran as expected"
94
            return 0
4417.4.23 by Tom Haddon
Another round of comments from Andrew Bennets and RF merge
95
    except Exception, e:
96
        # Squeeze the exception type and stringification of the exception value
97
        # on to one line.
98
        print "Unhandled exception: %s %r" % (e.__class__.__name__, str(e))
4417.4.18 by Tom Haddon
Merged RF and added Nagios script monitoring check
99
        return 3
100
101
if __name__ == '__main__':
102
    sys.exit(main())