-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fastchecker.py
151 lines (122 loc) · 3.87 KB
/
fastchecker.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
#!/usr/bin/env python
#
# Copyright (C) 2018-2019 Mehdi Abaakouk <sileht@sileht.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import daiquiri
import functools
import logging
import mock
import monotonic
import multiprocessing
import os
import cmk
import cmk.utils.log
import cmk_base
import cmk_base.checking as checking
import cmk_base.check_api as check_api
import cmk_base.discovery as discovery
import cmk_base.config as config
import cmk_base.ip_lookup as ip_lookup
import cmk_base.data_sources as data_sources
import cmk_base.item_state as item_state
import flask
cmk.utils.log.setup_console_logging()
LOG = daiquiri.getLogger(__name__)
PIDFILE = os.environ['PIDFILE']
SITENAME = os.environ['SITENAME']
SITEPATH = os.environ['SITEPATH']
TMPPATH = os.environ['FASTCHECKER_TMPPATH']
LOGPATH = os.environ['FASTCHECKER_LOGPATH']
daiquiri.setup(
level=logging.INFO,
outputs=[
daiquiri.output.STDERR,
]
)
class StopWatch(object):
def __init__(self):
self._started_at = monotonic.monotonic()
def elapsed(self):
return max(0.0, monotonic.monotonic() - self._started_at)
class FakeStdout(object):
def __init__(self):
self.data = b""
def write(self, data):
self.data += data
def input_output_fixup(func):
@functools.wraps(func)
def wrapped(hostname):
hostname = str(hostname)
with mock.patch('cmk_base.console.sys.stdout', new=FakeStdout()) as out:
exit_status = func(hostname)
if exit_status is not None:
return "%s\n%s" % (exit_status, out.data)
else:
return out.data
return wrapped
def wsgi():
LOG.info("Loading check_mk module...")
watch = StopWatch()
config.load_all_checks(check_api.get_check_api_context)
config.load()
count = len(config.check_info)
LOG.info("%d checks loaded in %s." % (count, watch.elapsed()))
app = flask.Flask(__name__)
@app.route("/check/<hostname>")
@input_output_fixup
def check(hostname):
return checking.do_check(hostname, None, None)
@app.route("/detail/<hostname>")
@input_output_fixup
def detail(hostname):
try:
cmk.debug.enable()
cmk.log.set_verbosity(verbosity=2)
return checking.do_check(hostname, None, None)
finally:
cmk.log.set_verbosity(verbosity=0)
cmk.debug.disable()
@app.route("/inventory/<hostname>")
@input_output_fixup
def inventory(hostname):
return discovery.check_discovery(hostname, None)
return app
def main():
args = [
"--master",
"--http", "127.0.0.1:5001",
"--need-app",
"--enable-threads",
"--thunder-lock",
"--add-header", "Connection: Close",
"--procname-prefix-spaced", "fastchecker",
"--max-requests", "500",
"--die-on-term",
"--ignore-sigpipe",
"--listen", "2048",
"--processes", str(multiprocessing.cpu_count() * 6 + 1),
"--pidfile2", PIDFILE,
"--wsgi-file", __file__,
"--harakiri", "58",
"--disable-logging",
"--daemonize2", LOGPATH,
]
uwsgi = SITEPATH + "/local/lib/python/bin/uwsgi"
os.execl(uwsgi, uwsgi, *args)
if __name__ == "__main__":
main()
else:
application = wsgi()