~gnuoy/charms/trusty/ceilometer/add-nrpe-checks

« back to all changes in this revision

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

  • Committer: Brad Marshall
  • Date: 2014-11-17 02:23:41 UTC
  • Revision ID: brad.marshall@canonical.com-20141117022341-idoy7icvhn1uxrjo
[bradm] Tweaked nagios checks to use functions to pull out services, added checks for sysv init style daemons, added in icehouse daemons, ran pep8 over the whole thing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
#                                       m
 
4
#  mmmm   m   m  mmmm   mmmm    mmm   mm#mm
 
5
#  #" "#  #   #  #" "#  #" "#  #"  #    #
 
6
#  #   #  #   #  #   #  #   #  #""""    #
 
7
#  ##m#"  "mm"#  ##m#"  ##m#"  "#mm"    "mm
 
8
#  #             #      #
 
9
#  "             "      "
 
10
# This file is managed by puppet.  Do not make local changes.
 
11
 
 
12
#
 
13
# Copyright 2014 Canonical Ltd.
 
14
#
 
15
# Author: Jacek Nykis <jacek.nykis@canonical.com>
 
16
#
 
17
 
 
18
import re
 
19
import nagios_plugin
 
20
 
 
21
 
 
22
def parse_args():
 
23
    import argparse
 
24
 
 
25
    parser = argparse.ArgumentParser(
 
26
        description='Read file and return nagios status based on its content',
 
27
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
 
28
    parser.add_argument('-f', '--status-file', required=True,
 
29
                        help='Status file path')
 
30
    parser.add_argument('-c', '--critical-text', default='CRITICAL',
 
31
                        help='String indicating critical status')
 
32
    parser.add_argument('-w', '--warning-text', default='WARNING',
 
33
                        help='String indicating warning status')
 
34
    parser.add_argument('-o', '--ok-text', default='OK',
 
35
                        help='String indicating OK status')
 
36
    parser.add_argument('-u', '--unknown-text', default='UNKNOWN',
 
37
                        help='String indicating unknown status')
 
38
    return parser.parse_args()
 
39
 
 
40
 
 
41
def check_status(args):
 
42
    nagios_plugin.check_file_freshness(args.status_file, 43200)
 
43
 
 
44
    with open(args.status_file, "r") as f:
 
45
        content = [l.strip() for l in f.readlines()]
 
46
 
 
47
    for line in content:
 
48
        if re.search(args.critical_text, line):
 
49
            raise nagios_plugin.CriticalError(line)
 
50
        elif re.search(args.warning_text, line):
 
51
            raise nagios_plugin.WarnError(line)
 
52
        elif re.search(args.unknown_text, line):
 
53
            raise nagios_plugin.UnknownError(line)
 
54
        else:
 
55
            print line
 
56
 
 
57
 
 
58
if __name__ == '__main__':
 
59
    args = parse_args()
 
60
    nagios_plugin.try_check(check_status, args)