~brad-marshall/charms/trusty/quantum-gateway/add-nrpe-checks

« back to all changes in this revision

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

  • Committer: Brad Marshall
  • Date: 2014-11-18 01:26:17 UTC
  • Revision ID: brad.marshall@canonical.com-20141118012617-3a1d69r8nqc59q1h
[bradm] Removed nagios check files that were moved to nrpe-external-master charm

Show diffs side-by-side

added added

removed removed

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