-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeliver_nrpe.py
executable file
·152 lines (115 loc) · 5.58 KB
/
deliver_nrpe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
Requires nagios-nrpe-plugin
"""
import subprocess
import argparse
import logging
import os.path
import os
import configparser
import itertools
import re
import ncheck
DEFAULT_NRPE = "/etc/nagios/nrpe.cfg"
CHECK_NRPE = "/usr/lib/nagios/plugins/check_nrpe"
RMAP = {"OK": {"code": 0},
"WARNING": {"code": 1},
"CRITICAL": {"code": 2},
"UNKNOWN": {"code": 3}}
def read_nrpe_configuration(config_file=DEFAULT_NRPE, running_configuration={}, **kwargs):
config = configparser.ConfigParser(interpolation=None)
logger = logging.getLogger("read_nrpe_configuration")
with open(config_file) as nrpe_cfg_fobj:
config.read_file(itertools.chain(["[nrpe]"], nrpe_cfg_fobj), source=config_file)
new_items = config._sections["nrpe"]
delivery_path = kwargs.get("delivery_path", new_items.get("delivery_path", None))
delivery_profile = kwargs.get("delivery_profile", new_items.get("delivery_profile", None))
logger.debug(new_items)
for k, v in new_items.items():
if k == "include":
# Include Single File
logger.debug("Processing File : {}".format(v))
new_items, ipath, iprofile = read_nrpe_configuration(config_file=v,
running_configuration=new_items)
if ipath is not None:
delivery_path = ipath
if iprofile is not None:
delivery_profile = iprofile
elif k == "include_dir":
# Walk Dir and Include Files
logger.debug("Processing Dir : {}".format(v))
for (dirpath, dirnames, filenames) in os.walk(v):
for singlefile in filenames:
this_file = dirpath + "/" + singlefile
logger.debug("Processing File : {}".format(this_file))
new_items, dpath, dprofile = read_nrpe_configuration(config_file=this_file,
running_configuration=new_items)
if dpath is not None:
delivery_path = dpath
if dprofile is not None:
delivery_profile = dprofile
logger.debug("Deliver Path & Profile {} {}".format(delivery_path, delivery_profile))
return {**new_items, **running_configuration}, delivery_path, delivery_profile
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="append_const", help="Verbosity Controls",
const=1, default=[])
parser.add_argument("-n", "--nrped", type=str, help="Location of NRPED Config", default=DEFAULT_NRPE)
parser.add_argument("-H", "--overridehost", help="Override Host Name", default="host")
parser.add_argument("-b", "--bin", help="Check NRPE Binary", default=CHECK_NRPE)
parser.add_argument("-c", "--check", help="Run this Specific Check", default=False)
parser.add_argument("-d", "--delivery_path", help="Directory/Bucket to Deliver Checks too", default=None)
parser.add_argument("-p", "--aws_profile", help="If delivery to AWS which profile to use", default=None)
parser.add_argument("-C", "--confirm", help="Confirm desire to store", default=False, action="store_true")
args = parser.parse_args()
VERBOSE = len(args.verbose)
EXTRA_MODULES = ["boto3", "urllib3", "botocore"]
extra_level = logging.ERROR
if VERBOSE == 0:
logging.basicConfig(level=logging.ERROR)
extra_level = logging.ERROR
elif VERBOSE == 1:
logging.basicConfig(level=logging.WARNING)
extra_level = logging.ERROR
elif VERBOSE == 2:
logging.basicConfig(level=logging.INFO)
extra_level = logging.WARNING
elif VERBOSE == 3:
logging.basicConfig(level=logging.DEBUG)
extra_level = logging.INFO
elif VERBOSE >= 4:
logging.basicConfig(level=logging.DEBUG)
extra_level = logging.DEBUG
for mod in EXTRA_MODULES:
logging.getLogger(mod).setLevel(extra_level)
logger = logging.getLogger("check_delivered.py")
logger.error("tst")
if args.check is not False:
process_checks = [args.check]
else:
# Process Nagios for Checks
running_config, delivery_path, delivery_profile = read_nrpe_configuration(config_file=args.nrped, running_configuration={})
if args.delivery_path != None:
delivery_path = args.delivery_path
elif args.aws_profile != None:
delivery_profile = args.aws_profile
logger.debug(running_config)
logger.debug("Delivery Settings: {}, {}, {}".format(args.confirm, delivery_path, delivery_profile))
all_checks = []
for k, v in running_config.items():
match = re.search("command\[(.+)\]", k)
if match is not None:
check_name = match.group(1)
logger.debug("Found Check Named {} : {}".format(check_name, v))
try:
run = subprocess.run(v, shell="/bin/bash", capture_output=True)
except Exception as run_error:
logger.error("Unable to Run Check {}".format(v))
else:
this_response = {"code" : int(run.returncode), "string": run.stdout.decode()}
logger.debug(this_response)
this_nrpe = ncheck.NCheck(response=this_response, check=check_name,
profile=delivery_profile,
path=delivery_path,
do_storage=args.confirm)