2015-11-18 14:48:14 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import bson
|
2016-02-29 18:51:58 +01:00
|
|
|
import re
|
|
|
|
import smtplib
|
|
|
|
from email.mime.text import MIMEText
|
2015-11-18 14:48:14 +01:00
|
|
|
|
|
|
|
def format_query(query_usr):
|
|
|
|
query_list = []
|
|
|
|
for key, value in query_usr.items():
|
|
|
|
if key == "hostname":
|
|
|
|
qtag = ""
|
|
|
|
else:
|
|
|
|
qtag = "%s:" % key
|
|
|
|
query_list.append("%s%s" % (qtag, value))
|
|
|
|
return " ".join(query_list)
|
|
|
|
|
2016-02-29 18:51:58 +01:00
|
|
|
allowed_filters = (
|
|
|
|
'status',
|
|
|
|
'hood',
|
2017-11-05 19:48:29 +01:00
|
|
|
'nickname',
|
|
|
|
'hardware',
|
|
|
|
'firmware',
|
|
|
|
'mac',
|
2016-02-29 18:51:58 +01:00
|
|
|
'hostname',
|
2017-11-05 19:48:29 +01:00
|
|
|
'contact',
|
2017-11-19 21:33:47 +01:00
|
|
|
'community',
|
2017-12-08 23:28:53 +01:00
|
|
|
'neighbor',
|
|
|
|
'neighbour',
|
2018-01-11 14:18:42 +01:00
|
|
|
'gw',
|
|
|
|
'selected',
|
2018-01-12 18:09:53 +01:00
|
|
|
'bat',
|
|
|
|
'batselected',
|
2018-09-21 13:47:01 +02:00
|
|
|
'network',
|
2018-11-24 14:51:00 +01:00
|
|
|
'os',
|
|
|
|
'batman',
|
|
|
|
'kernel',
|
|
|
|
'nodewatcher',
|
2016-02-29 18:51:58 +01:00
|
|
|
)
|
2017-11-05 19:48:29 +01:00
|
|
|
|
2015-11-18 14:48:14 +01:00
|
|
|
def parse_router_list_search_query(args):
|
|
|
|
query_usr = bson.SON()
|
|
|
|
if "q" in args:
|
|
|
|
for word in args["q"].strip().split(" "):
|
2017-11-05 19:48:29 +01:00
|
|
|
if not word:
|
|
|
|
# Case of "q=" without arguments
|
|
|
|
break
|
2015-11-18 14:48:14 +01:00
|
|
|
if not ':' in word:
|
|
|
|
key = "hostname"
|
|
|
|
value = word
|
|
|
|
else:
|
2015-12-09 19:57:15 +01:00
|
|
|
key, value = word.split(':', 1)
|
2015-11-18 14:48:14 +01:00
|
|
|
if key in allowed_filters:
|
|
|
|
query_usr[key] = query_usr.get(key, "") + value
|
2017-11-10 14:34:49 +01:00
|
|
|
s = ""
|
2017-11-19 13:04:49 +01:00
|
|
|
j = ""
|
2017-11-10 14:34:49 +01:00
|
|
|
t = []
|
|
|
|
i = 0
|
2015-11-18 14:48:14 +01:00
|
|
|
for key, value in query_usr.items():
|
2017-11-10 14:34:49 +01:00
|
|
|
if i==0:
|
|
|
|
prefix = " WHERE "
|
|
|
|
else:
|
|
|
|
prefix = " AND "
|
|
|
|
if value.startswith('!'):
|
|
|
|
no = "NOT "
|
|
|
|
value = value[1:]
|
|
|
|
else:
|
|
|
|
no = ""
|
|
|
|
|
|
|
|
if value == "EXISTS":
|
|
|
|
k = key + ' <> "" AND ' + key + " IS NOT NULL"
|
|
|
|
elif value == "EXISTS_NOT":
|
|
|
|
k = key + ' = "" OR ' + key + " IS NULL"
|
|
|
|
elif key == 'mac':
|
2017-11-19 13:04:49 +01:00
|
|
|
j += " INNER JOIN ( SELECT router, mac FROM router_netif GROUP BY router, mac) AS j ON router.id = j.router "
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(mac) {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2018-01-11 14:18:42 +01:00
|
|
|
elif (key == 'gw'):
|
|
|
|
j += " INNER JOIN router_gw ON router.id = router_gw.router "
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(router_gw.mac) {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2018-01-11 14:18:42 +01:00
|
|
|
elif (key == 'selected'):
|
|
|
|
j += " INNER JOIN router_gw ON router.id = router_gw.router "
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(router_gw.mac) {} REGEXP %s AND router_gw.selected = TRUE".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2018-01-12 18:09:53 +01:00
|
|
|
elif (key == 'bat'):
|
|
|
|
j += """ INNER JOIN router_gw ON router.id = router_gw.router
|
|
|
|
INNER JOIN (
|
|
|
|
gw_netif AS n1
|
2018-01-15 22:39:11 +01:00
|
|
|
INNER JOIN gw_netif AS n2 ON n1.mac = n2.vpnmac AND n1.gw = n2.gw
|
2018-01-12 18:09:53 +01:00
|
|
|
) ON router_gw.mac = n1.mac
|
|
|
|
"""
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(n2.mac) {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2018-01-12 18:09:53 +01:00
|
|
|
elif (key == 'batselected'):
|
|
|
|
j += """ INNER JOIN router_gw ON router.id = router_gw.router
|
|
|
|
INNER JOIN (
|
|
|
|
gw_netif AS n1
|
2018-01-15 22:39:11 +01:00
|
|
|
INNER JOIN gw_netif AS n2 ON n1.mac = n2.vpnmac AND n1.gw = n2.gw
|
2018-01-12 18:09:53 +01:00
|
|
|
) ON router_gw.mac = n1.mac
|
|
|
|
"""
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(n2.mac) {} REGEXP %s AND router_gw.selected = TRUE".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2017-12-08 23:28:53 +01:00
|
|
|
elif (key == 'neighbor') or (key == 'neighbour'):
|
|
|
|
j += " INNER JOIN ( SELECT router, mac FROM router_neighbor GROUP BY router, mac) AS j ON router.id = j.router "
|
2018-08-27 12:33:27 +02:00
|
|
|
k = "HEX(mac) {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace(':',''))
|
2018-08-28 15:28:25 +02:00
|
|
|
elif (key == 'hood'):
|
|
|
|
k = "hoods.name {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace("_","."))
|
|
|
|
elif (key == 'hardware') or (key == 'nickname'):
|
2017-11-10 14:34:49 +01:00
|
|
|
k = key + " {} REGEXP %s".format(no)
|
2017-11-15 13:49:50 +01:00
|
|
|
t.append(value.replace("_","."))
|
2017-11-10 14:34:49 +01:00
|
|
|
elif (key == 'hostname') or (key == 'firmware'):
|
|
|
|
k = key + " {} REGEXP %s".format(no)
|
|
|
|
t.append(value)
|
2017-11-05 19:48:29 +01:00
|
|
|
elif key == 'contact':
|
2017-11-10 14:34:49 +01:00
|
|
|
k = "contact {} REGEXP %s".format(no)
|
|
|
|
t.append(value)
|
2018-09-21 13:47:01 +02:00
|
|
|
elif key == 'network':
|
|
|
|
# local hood included for v2
|
|
|
|
if value.lower() == 'local':
|
|
|
|
k = no + " (router.v2 = TRUE AND local = TRUE)"
|
|
|
|
elif value.lower() == 'v2':
|
|
|
|
k = no + " (router.v2 = TRUE AND local = FALSE)"
|
|
|
|
elif value.lower() == 'v1':
|
|
|
|
k = no + " router.v2 = FALSE"
|
|
|
|
else:
|
|
|
|
continue
|
2018-11-24 14:51:00 +01:00
|
|
|
elif key in ('os','batman','kernel','nodewatcher',):
|
|
|
|
k = key + " {} REGEXP %s".format(no)
|
|
|
|
t.append(value.replace("_","."))
|
2015-11-18 14:48:14 +01:00
|
|
|
else:
|
2017-11-10 14:34:49 +01:00
|
|
|
k = no + key + " = %s"
|
|
|
|
t.append(value)
|
|
|
|
i += 1
|
|
|
|
s += prefix + k
|
2017-11-19 13:04:49 +01:00
|
|
|
where = j + " " + s
|
|
|
|
return (where, tuple(t), format_query(query_usr))
|
2016-02-29 18:51:58 +01:00
|
|
|
|
|
|
|
def send_email(recipient, subject, content, sender="FFF Monitoring <noreply@monitoring.freifunk-franken.de>"):
|
|
|
|
msg = MIMEText(content)
|
|
|
|
msg['Subject'] = subject
|
|
|
|
msg['From'] = sender
|
|
|
|
msg['To'] = recipient
|
|
|
|
s = smtplib.SMTP('localhost')
|
|
|
|
s.send_message(msg)
|
|
|
|
s.quit()
|
|
|
|
|
|
|
|
def is_authorized(owner, session):
|
2016-09-25 19:46:41 +02:00
|
|
|
if ("user" in session) and (owner == session.get("user")):
|
2016-02-29 18:51:58 +01:00
|
|
|
return True
|
|
|
|
elif session.get("admin"):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|