-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_omd.py
executable file
·166 lines (144 loc) · 5.58 KB
/
check_omd.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
check_omd.py - a script for checking a particular
OMD site status
2018 By Christian Stankowic
<info at cstan dot io>
https://github.com/stdevel/check_omd
"""
from optparse import OptionParser
import subprocess
import io
import logging
__version__ = "1.1.1"
"""
str: Program version
"""
LOGGER = logging.getLogger('check_omd')
"""
logging: Logger instance
"""
def get_site_status():
"""
Retrieves a particular site's status
"""
#get username
proc = subprocess.Popen("whoami", stdout=subprocess.PIPE)
site = proc.stdout.read().rstrip().decode("utf-8")
LOGGER.debug("It seems like I'm OMD site '%s'", site)
#get OMD site status
cmd = ['omd', 'status', '-b']
LOGGER.debug("running command '%s'", cmd)
proc = subprocess.Popen(
cmd, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
res, err = proc.communicate()
err = err.decode('utf-8')
if err:
if "no such site" in err:
print("UNKNOWN: unable to check site: '{0}' - did you miss " \
"running this plugin as OMD site user?".format(err.rstrip()))
else:
print("UNKNOWN: unable to check site: '{0}'".format(err.rstrip()))
exit(3)
if res:
#try to find out whether omd was executed as root
if res.count(bytes("OVERALL", "utf-8")) > 1:
print("UNKOWN: unable to check site, it seems this plugin is " \
"executed as root (use OMD site context!)")
exit(3)
#check all services
fail_srvs = []
warn_srvs = []
restarted_srvs = []
LOGGER.debug("Got result '%s'", res)
for line in io.StringIO(res.decode('utf-8')):
service = line.rstrip().split(" ")[0]
status = line.rstrip().split(" ")[1]
if service not in OPTIONS.exclude:
#check service
if status != "0":
if service in OPTIONS.warning:
LOGGER.debug(
"%s service marked for warning has failed" \
" state (%s)", service, status
)
warn_srvs.append(service)
else:
if OPTIONS.heal:
cmd = ['omd', 'restart', service]
LOGGER.debug("running command '%s'", cmd)
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res2, err2 = proc.communicate()
print("{}".format(res2.rstrip().decode("utf-8")))
restarted_srvs.append(service)
else:
fail_srvs.append(service)
LOGGER.debug(
"%s service has failed state " \
"(%s)", service, status
)
else:
LOGGER.debug(
"Ignoring '%s' as it's blacklisted.", service
)
if OPTIONS.heal:
if len(restarted_srvs) > 0:
print("WARNING: Restarted services on site '{0}': '{1}'".format(site, ' '.join(restarted_srvs)))
exit(1)
else:
exit(0)
if len(fail_srvs) == 0 and len(warn_srvs) == 0:
print("OK: OMD site '{0}' services are running.".format(site))
exit(0)
elif len(fail_srvs) > 0:
print("CRITICAL: OMD site '{0}' has failed service(s): " \
"'{1}'".format(site, ' '.join(fail_srvs)))
exit(2)
else:
print("WARNING: OMD site '{0}' has service(s) in warning state: " \
"'{1}'".format(site, ' '.join(warn_srvs)))
exit(1)
if __name__ == "__main__":
#define description, version and load parser
DESC = '''%prog is used to check a particular OMD site status. By default,
the script only checks a site's overall status. It is also possible to exclude
particular services and only check the remaining services (e.g. rrdcached,
npcd, icinga, apache, crontab).
Checkout the GitHub page for updates: https://github.com/stdevel/check_omd'''
PARSER = OptionParser(description=DESC, version=__version__)
#-d / --debug
PARSER.add_option(
"-d", "--debug", dest="debug", default=False, action="store_true",
help="enable debugging outputs (default: no)"
)
#-e / --exclude
PARSER.add_option(
"-x", "--exclude", dest="exclude", default=["OVERALL"],
action="append", metavar="SERVICE", help="defines one or more " \
"services that should be excluded (default: none)"
)
#-w / --warning
PARSER.add_option(
"-w", "--warning", dest="warning", default=[""], action="append",
metavar="SERVICE", help="defines one or more services that only " \
"should throw a warning if not running (useful for fragile stuff " \
"like npcd, default: none)"
)
#-H/ --heal
PARSER.add_option(
"-H", "--heal", dest="heal", default=False, action="store_true",
help="automatically restarts the services that are not running (default: no)"
)
#parse arguments
(OPTIONS, ARGS) = PARSER.parse_args()
#set logging level
logging.basicConfig()
if OPTIONS.debug:
LOGGER.setLevel(logging.DEBUG)
else:
LOGGER.setLevel(logging.ERROR)
LOGGER.debug("OPTIONS: %s", OPTIONS)
#check site status
get_site_status()