~gnuoy/charms/trusty/openstack-dashboard/amulet

« back to all changes in this revision

Viewing changes to files/nrpe-external-master/nagios_plugin.py

  • Committer: Brad Marshall
  • Date: 2014-11-17 03:54:24 UTC
  • mto: This revision was merged to the branch mainline in revision 48.
  • Revision ID: brad.marshall@canonical.com-20141117035424-cn3v9xlvysbuxv5q
[bradm] Add Added sysvinit daemon monitoring, use services() instead of hard coded daemon list, pep8 fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#                                       m
 
3
#  mmmm   m   m  mmmm   mmmm    mmm   mm#mm
 
4
#  #" "#  #   #  #" "#  #" "#  #"  #    #
 
5
#  #   #  #   #  #   #  #   #  #""""    #
 
6
#  ##m#"  "mm"#  ##m#"  ##m#"  "#mm"    "mm
 
7
#  #             #      #
 
8
#  "             "      "
 
9
# This file is managed by puppet.  Do not make local changes.
 
10
 
 
11
# Copyright (C) 2005, 2006, 2007, 2012  James Troup <james.troup@canonical.com>
 
12
 
 
13
import os
 
14
import stat
 
15
import time
 
16
import traceback
 
17
import sys
 
18
 
 
19
 
 
20
################################################################################
 
21
 
 
22
class CriticalError(Exception):
 
23
    """This indicates a critical error."""
 
24
    pass
 
25
 
 
26
 
 
27
class WarnError(Exception):
 
28
    """This indicates a warning condition."""
 
29
    pass
 
30
 
 
31
 
 
32
class UnknownError(Exception):
 
33
    """This indicates a unknown error was encountered."""
 
34
    pass
 
35
 
 
36
 
 
37
def try_check(function, *args, **kwargs):
 
38
    """Perform a check with error/warn/unknown handling."""
 
39
    try:
 
40
        function(*args, **kwargs)
 
41
    except UnknownError, msg:
 
42
        print msg
 
43
        sys.exit(3)
 
44
    except CriticalError, msg:
 
45
        print msg
 
46
        sys.exit(2)
 
47
    except WarnError, msg:
 
48
        print msg
 
49
        sys.exit(1)
 
50
    except:
 
51
        print "%s raised unknown exception '%s'" % (function, sys.exc_info()[0])
 
52
        print '=' * 60
 
53
        traceback.print_exc(file=sys.stdout)
 
54
        print '=' * 60
 
55
        sys.exit(3)
 
56
 
 
57
 
 
58
################################################################################
 
59
 
 
60
def check_file_freshness(filename, newer_than=600):
 
61
    """Check a file exists, is readable and is newer than <n> seconds (where <n> defaults to 600)."""
 
62
    # First check the file exists and is readable
 
63
    if not os.path.exists(filename):
 
64
        raise CriticalError("%s: does not exist." % (filename))
 
65
    if os.access(filename, os.R_OK) == 0:
 
66
        raise CriticalError("%s: is not readable." % (filename))
 
67
 
 
68
    # Then ensure the file is up-to-date enough
 
69
    mtime = os.stat(filename)[stat.ST_MTIME]
 
70
    last_modified = time.time() - mtime
 
71
    if last_modified > newer_than:
 
72
        raise CriticalError("%s: was last modified on %s and is too old (> %s seconds)."
 
73
                            % (filename, time.ctime(mtime), newer_than))
 
74
    if last_modified < 0:
 
75
        raise CriticalError("%s: was last modified on %s which is in the future."
 
76
                            % (filename, time.ctime(mtime)))
 
77
 
 
78
################################################################################