-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_mensa.py
executable file
·73 lines (58 loc) · 1.91 KB
/
check_mensa.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
#! /usr/bin/python3
#
# Icinga plugin for monitoring the Uni Mensa
#
# Copy this file into /usr/local/lib/nagios/plugins/
#
# Usage: check_mensa
#
import datetime
import re
import sys
import traceback
from lxml import html
import requests
SERVICE_NAME = "MENSA"
SECTIONS = ["EG Nord", "EG Süd", "MG Nord", "MG Süd"]
URL = "https://www.kstw.de/gastronomie/speiseplan?l=1"
USER_AGENT = "check_mensa.py/0.0.1 Icinga Plugin"
try:
# Scrape the mensa site.
r = requests.get(URL, headers={"user-agent": USER_AGENT})
r.raise_for_status()
# The server just sends an HTML fragment without encoding information. It
# works for them because the fragment gets inserted into an utf-8 encoded
# page, thus inheriting the encoding. But we have to declare the encoding
# explicitly.
# r.encoding = 'utf-8'
# Look for <div class="tx-epwerkmenu-menu-locationpart-title"><strong>MG Nord</strong></div>
up = set()
root = html.fromstring(r.text)
for e in root.xpath(
'//div[contains (concat (" ", @class, " "), " tx-epwerkmenu-menu-locationpart-title ")]'
):
# print (e.text_content ())
m = re.search(r"(EG|MG)\s+(Nord|Süd)", e.text_content())
if m:
up.add("%s %s" % (m.group(1), m.group(2)))
down = set(SECTIONS) - up
# exit
data = {
"name": SERVICE_NAME,
"up": len(up),
"down": len(down),
"ups": ", ".join(sorted(up)),
"downs": ", ".join(sorted(down)),
}
if data["down"] == 0:
print("{name} OK - {up} UP ({ups})".format(**data))
sys.exit(0)
if data["up"] >= 1:
print("{name} WARNING - {up} UP ({ups}) - {down} DOWN ({downs})".format(**data))
sys.exit(1)
print("{name} CRITICAL - {down} DOWN ({downs})".format(**data))
sys.exit(2)
except Exception as exc:
print("%s UNKNOWN - %s" % (SERVICE_NAME, str(exc)))
traceback.print_exc()
sys.exit(3)