aboutsummaryrefslogblamecommitdiff
path: root/modules/private/monitoring/status/app.py
blob: b1d419cc1cc88909a265dca4d293a1337a235b9e (plain) (tree)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
























































































































































































































































































































































































































                                                                                                                  
from flask import Flask, request, render_template_string, jsonify, make_response
from flask_login import LoginManager, UserMixin, login_required
import socket
import json
import time
import os

login_manager = LoginManager()
app = Flask(__name__)
login_manager.init_app(app)

STATUS = [
        "ok",
        "warning",
        "error",
        "unknown"
        ]

HOST_STATUS = [
        "up",
        "down",
        "unreachable",
        ]

#### Push
AUTHORIZED_KEYS = os.environ.get("TOKENS", "").split()
COMMAND_FILE = "/var/run/naemon/naemon.cmd"

ERROR_NO_REQUEST_HANDLER="NO REQUEST HANDLER"
ERROR_NO_TOKEN_SUPPLIED="NO TOKEN"
ERROR_BAD_TOKEN_SUPPLIED="BAD TOKEN"

ERROR_BAD_COMMAND_FILE="BAD COMMAND FILE"
ERROR_COMMAND_FILE_OPEN_WRITE="COMMAND FILE UNWRITEABLE"
ERROR_COMMAND_FILE_OPEN="CANNOT OPEN COMMAND FILE"
ERROR_BAD_WRITE="WRITE ERROR"

ERROR_BAD_DATA="BAD DATA"
ERROR_BAD_JSON="BAD JSON"

ERROR_NO_CORRECT_STATUS="NO STATUS WAS CORRECT"
#### /Push

def get_lq(request):
    # https://mathias-kettner.de/checkmk_livestatus.html
    socket_path="/var/run/naemon/live"
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(socket_path)
    s.send(request.encode())
    s.shutdown(socket.SHUT_WR)
    chunks = []
    while len(chunks) == 0 or len(chunks[-1]) > 0:
        chunks.append(s.recv(4096))
    s.close()
    return b"".join(chunks).decode()

class Host:
    def __init__(self, name, alias, status, webname):
        self.name = name
        self.alias = alias
        self.webname = webname or alias
        self.status = status
        self.services = []

    @classmethod
    def parse_hosts(cls, payload):
        parsed = [cls.parse(p) for p in json.loads(payload)]
        return {p.name: p for p in parsed}

    @classmethod
    def parse(cls, payload):
        return cls(payload[0], payload[1], HOST_STATUS[payload[2]], payload[3].get("WEBSTATUS_NAME"))

    def __repr__(self):
        return "Host {}: {} ({})".format(self.name, self.alias, self.webname)

    @classmethod
    def query(cls):
        answer = get_lq("""GET hosts
Filter: groups >= webstatus-hosts
Columns: name alias state custom_variables
OutputFormat: json
""")
        return cls.parse_hosts(answer)

    def fill_services(self, services):
        self.services = [service for service in services if service.host == self.name]

class ServiceGroup:
    def __init__(self, name, alias):
        self.name = name
        self.alias = alias
        self.services = []

    @classmethod
    def parse_groups(cls, payload):
        parsed = [cls.parse(p) for p in json.loads(payload)]
        return {p.name: p for p in parsed}

    @classmethod
    def parse(cls, payload):
        return cls(payload[0], payload[1])

    @classmethod
    def query(cls):
        answer = get_lq("""GET servicegroups
Filter: name ~ ^webstatus-
Columns: name alias custom_variables
OutputFormat: json
""")
        return cls.parse_groups(answer)

    def fill_services(self, services):
        self.services = [service for service in services if any([group == self.name for group in service.groups])]

    def __repr__(self):
        return "ServiceGroup {}: {}".format(self.name, self.alias)

class Service:
    def __init__(self, name, host, groups, status, webname, url, description, infos):
        self.name = name
        self.host = host
        self.groups = groups
        self.status = status
        self.webname = webname
        self.url = url
        self.description = description
        self.infos = infos

    @classmethod
    def parse_services(cls, payload):
        parsed = json.loads(payload)
        return [cls.parse(p) for p in parsed if cls.valid(p[2])]

    @staticmethod
    def valid(groups):
        return any([b.startswith("webstatus-") for b in groups])

    @classmethod
    def parse(cls, payload):
        return cls(payload[0],
                payload[1],
                payload[2],
                STATUS[payload[3]],
                payload[4].get("WEBSTATUS_NAME"),
                payload[4].get("WEBSTATUS_URL"),
                payload[5],
                payload[6])

    @classmethod
    def query(cls):
        answer = get_lq("""GET services
Columns: display_name host_name groups state custom_variables description plugin_output
OutputFormat: json
""")
        return cls.parse_services(answer)

    def __repr__(self):
        return "Service {}: {}".format(self.name, self.webname)

def get_infos():
    hosts = Host.query()
    servicegroups = ServiceGroup.query()
    services = Service.query()

    for host in hosts:
        hosts[host].fill_services(services)
    for group in servicegroups:
        servicegroups[group].fill_services(services)
    return (hosts, servicegroups, services)

TEMPLATE='''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Status</title>
        <meta name="referrer" content="no-referrer" />
        <style type="text/css">
        ul {
            list-style: none;
            margin: 0px;
        }
        ul li:nth-child(2n) {
            background-color: rgb(240, 240, 240);
        }
        li.resource, li.service {
            margin: 1px 0px;
        }
        span.status {
            display: inline-block;
            width: 150px;
            text-align: center;
            margin-right: 5px;
            font-variant: small-caps;
            font-size: 1.2em;
        }
        .status_ok,.status_up {
            background-color: rgba(0, 255, 0, 0.5);;
        }
        .status_warning {
            background-color: rgba(255, 255, 0, 0.5);;
        }
        .status_error,.status_down {
            background-color: rgba(255, 0, 0, 0.5);;
        }
        .status_unknown,.status_unreachable {
            background-color: rgba(0, 0, 255, 0.5);;
        }
        .infos {
            margin-left: 40px;
            color: rgb(100, 100, 100);
        }
        div#services {
            column-count: auto;
            column-width: 36em;
        }
        div.servicegroup {
            -webkit-column-break-inside: avoid;
            break-inside: avoid;
        }
        h3.servicegroup_title, h3.host_title {
            margin: 1px 0px;
        }
        span.service_host, span.infos {
            float: right;
            display: inline-block;
            color: rgb(100, 100, 100);
        }
        </style>
    </head>
    <body>
        <h2>Hosts</h2>
        {%- for host in hosts.values() %}
            <h3 class="host_title">
                <span class="status status_{{ host.status }}">{{ host.status }}</span>
                <span class="host">{{ host.webname }}</span>
            </h3>
            {%- for service in servicegroups["webstatus-resources"].services if service.host == host.name -%}
                {%- if loop.first %}
                <ul class="resources">
                {% endif %}

                <li class="resource">
                    <span class="status status_{{ service.status }}">{{ service.status }}</span>
                    <span class="description">{{ service.description }}</span>
                    <span class="infos">{{ service.infos }}</span>
                </li>

                {%- if loop.last %}
                </ul>
                {% endif %}
            {% endfor %}
        {%- endfor %}

        <h2>Services</h2>
        <div id="services">
        {%- for group in servicegroups.values() if group.services and group.name != "webstatus-resources" %}
            <div class="servicegroup">
            <h3 class="servicegroup_title">{{ group.alias }}</h3>
            {%- for service in group.services -%}
                {%- if loop.first %}
                <ul class="services">
                {% endif %}

                <li class="service" title="{{ service.infos }}">
                    <span class="status status_{{ service.status }}">{{ service.status }}</span>
                    <span class="description">
                        {% if service.url and service.url.startswith("https://") %}
                        <a href="{{ service.url }}">{{ service.webname or service.description }}</a>
                        {% else %}
                        {{ service.webname or service.description }}
                        {% endif %}
                    </span>
                    <span class="service_host">{{ hosts[service.host].webname }}</span>
                </li>

                {%- if loop.last %}
                </ul>
                {% endif %}
            {%- endfor -%}
            </div>
        {%- endfor %}
        </div>
    </body>
</html>
'''

@login_manager.request_loader
def load_user_from_request(request):
    api_key = request.headers.get('Token')
    if api_key in AUTHORIZED_KEYS:
        return UserMixin()
    content = request.get_json(force=True, silent=True)
    if content is not None and content.get("token") in AUTHORIZED_KEYS:
        return UserMixin()

@app.route("/live", methods=["POST"])
@login_required
def live():
    query = request.get_data()
    result = get_lq(query.decode() + "\n")
    resp = make_response(result)
    resp.content_type = "text/plain"
    return resp

@app.route("/", methods=["GET"])
def get():
    (hosts, servicegroups, services) = get_infos()
    resp = make_response(render_template_string(TEMPLATE, hosts=hosts, servicegroups=servicegroups))
    resp.content_type = "text/html"
    return resp

@app.route("/", methods=["POST"])
@login_required
def push():
    content = request.get_json(force=True, silent=True)
    if content is None:
        return ERROR_BAD_JSON
    if content.get("cmd") != "submitcheck":
        return render_error(ERROR_NO_REQUEST_HANDLER)
    if "checkresult" not in content or not isinstance(content["checkresult"], list):
        return render_error(ERROR_BAD_DATA)

    checks = 0
    errors = 0
    for check in map(lambda x: CheckResult.from_json(x), content["checkresult"]):
        if check is None:
            errors += 1
            continue
        try:
            write_check_output(check)
        except Exception as e:
            return render_error(str(e))
        checks += 1
    return render_response(checks, errors)

def write_check_output(check):
    if check.type== "service":
        command = "[{time}] PROCESS_SERVICE_CHECK_RESULT;{hostname};{servicename};{state};{output}";
    else:
        command = "[{time}] PROCESS_HOST_CHECK_RESULT;{hostname};{state};{output}";
    formatted = command.format(
            time=int(time.time()),
            hostname=check.hostname,
            state=check.state,
            output=check.output,
            servicename=check.servicename,
        )

    if not os.path.exists(COMMAND_FILE):
        raise Exception(ERROR_BAD_COMMAND_FILE)
    if not os.access(COMMAND_FILE, os.W_OK):
        raise Exception(ERROR_COMMAND_FILE_OPEN_WRITE)
    if not os.access(COMMAND_FILE, os.W_OK):
        raise Exception(ERROR_COMMAND_FILE_OPEN_WRITE)
    try:
        with open(COMMAND_FILE, "w") as c:
            c.write(formatted + "\n")
    except Exception as e:
        raise Exception(ERROR_BAD_WRITE)

def render_error(error):
    return jsonify({
            "status": "error",
            "message": error,
            })

def render_response(checks, errors):
    if checks > 0:
        return jsonify({
            "status": "ok",
            "result": {
                "checks": checks,
                "errors": errors,
                }
            })
    else:
        return jsonify({
            "status": "error",
            "message": ERROR_NO_CORRECT_STATUS,
            })

class CheckResult:
    def __init__(self, hostname, state, output, servicename, checktype):
        self.hostname = hostname
        self.state = state
        self.output = output
        self.servicename = servicename
        self.type = checktype

    @classmethod
    def from_json(klass, j):
        if not isinstance(j, dict):
            return None
        for key in ["hostname", "state", "output"]:
            if key not in j or not isinstance(j[key], str):
                return None
        for key in ["servicename", "type"]:
            if key in j and not isinstance(j[key], str):
                return None
        return klass(
                j["hostname"],
                j["state"],
                j["output"],
                j.get("servicename", ""),
                j.get("type", "host"))