mirror of
https://github.com/FreifunkFranken/fff-monitoring.git
synced 2024-06-16 12:24:20 +02:00
Change router_stats_netif to use ids for netifs
This introduces a serious of changes to code and database. This patch requires changes to the MySQL database. Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
parent
ec66c05361
commit
58ce32e322
|
@ -12,12 +12,30 @@ mysql.execute("""
|
||||||
CREATE TABLE banned (
|
CREATE TABLE banned (
|
||||||
`mac` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
|
`mac` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
|
||||||
`added` datetime NOT NULL
|
`added` datetime NOT NULL
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
|
||||||
""")
|
""")
|
||||||
|
|
||||||
mysql.execute("""
|
mysql.execute("""
|
||||||
ALTER TABLE `banned`
|
ALTER TABLE `banned`
|
||||||
ADD PRIMARY KEY (`mac`);
|
ADD PRIMARY KEY (`mac`)
|
||||||
|
""")
|
||||||
|
|
||||||
|
mysql.execute("""
|
||||||
|
CREATE TABLE netifs (
|
||||||
|
`id` smallint(6) UNSIGNED NOT NULL,
|
||||||
|
`name` varchar(15) COLLATE utf8_unicode_ci NOT NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
|
||||||
|
""")
|
||||||
|
|
||||||
|
mysql.execute("""
|
||||||
|
ALTER TABLE netifs
|
||||||
|
ADD PRIMARY KEY (`id`),
|
||||||
|
ADD UNIQUE KEY `name` (`name`)
|
||||||
|
""")
|
||||||
|
|
||||||
|
mysql.execute("""
|
||||||
|
ALTER TABLE netifs
|
||||||
|
MODIFY `id` smallint(6) UNSIGNED NOT NULL AUTO_INCREMENT
|
||||||
""")
|
""")
|
||||||
|
|
||||||
mysql.execute("""
|
mysql.execute("""
|
||||||
|
@ -178,7 +196,7 @@ mysql.execute("""
|
||||||
mysql.execute("""
|
mysql.execute("""
|
||||||
CREATE TABLE router_stats_netif (
|
CREATE TABLE router_stats_netif (
|
||||||
`router` int(11) NOT NULL,
|
`router` int(11) NOT NULL,
|
||||||
`netif` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
|
`netif` smallint(6) UNSIGNED NOT NULL,
|
||||||
`rx` bigint(20) NOT NULL,
|
`rx` bigint(20) NOT NULL,
|
||||||
`tx` bigint(20) NOT NULL,
|
`tx` bigint(20) NOT NULL,
|
||||||
`time` int(11) NOT NULL,
|
`time` int(11) NOT NULL,
|
||||||
|
|
|
@ -40,7 +40,7 @@ def ban_router(mysql,dbid):
|
||||||
mysql.execute("INSERT INTO banned (mac, added) VALUES (%s, %s)",(mac,added,))
|
mysql.execute("INSERT INTO banned (mac, added) VALUES (%s, %s)",(mac,added,))
|
||||||
mysql.commit()
|
mysql.commit()
|
||||||
|
|
||||||
def import_nodewatcher_xml(mysql, mac, xml, banned):
|
def import_nodewatcher_xml(mysql, mac, xml, banned, netifdict):
|
||||||
global router_rate_limit_list
|
global router_rate_limit_list
|
||||||
|
|
||||||
t = utcnow()
|
t = utcnow()
|
||||||
|
@ -181,7 +181,7 @@ def import_nodewatcher_xml(mysql, mac, xml, banned):
|
||||||
mysql.executemany("INSERT INTO router_neighbor (router, mac, quality, net_if, type) VALUES (%s, %s, %s, %s, %s)",nbdata)
|
mysql.executemany("INSERT INTO router_neighbor (router, mac, quality, net_if, type) VALUES (%s, %s, %s, %s, %s)",nbdata)
|
||||||
|
|
||||||
if router_id:
|
if router_id:
|
||||||
new_router_stats(mysql, router_id, uptime, router_update)
|
new_router_stats(mysql, router_id, uptime, router_update, netifdict)
|
||||||
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -383,7 +383,7 @@ def set_status(mysql,router_id,status):
|
||||||
mysql.utcnow(),
|
mysql.utcnow(),
|
||||||
router_id,))
|
router_id,))
|
||||||
|
|
||||||
def new_router_stats(mysql, router_id, uptime, router_update):
|
def new_router_stats(mysql, router_id, uptime, router_update, netifdict):
|
||||||
if (uptime + CONFIG["router_stat_mindiff_secs"]) < router_update["sys_uptime"]:
|
if (uptime + CONFIG["router_stat_mindiff_secs"]) < router_update["sys_uptime"]:
|
||||||
time = mysql.utctimestamp()
|
time = mysql.utctimestamp()
|
||||||
|
|
||||||
|
@ -402,11 +402,32 @@ def new_router_stats(mysql, router_id, uptime, router_update):
|
||||||
router_update["clients"],))
|
router_update["clients"],))
|
||||||
|
|
||||||
ndata = []
|
ndata = []
|
||||||
|
nkeys = []
|
||||||
for netif in router_update["netifs"]:
|
for netif in router_update["netifs"]:
|
||||||
# sanitize name
|
# sanitize name
|
||||||
name = netif["name"].replace(".", "").replace("$", "")
|
name = netif["name"].replace(".", "").replace("$", "")
|
||||||
with suppress(KeyError):
|
with suppress(KeyError):
|
||||||
ndata.append((router_id,name,time,netif["traffic"]["rx"],netif["traffic"]["tx"],))
|
if name in netifdict.keys():
|
||||||
|
ndata.append((router_id,netifdict[name],time,netif["traffic"]["rx"],netif["traffic"]["tx"],))
|
||||||
|
else:
|
||||||
|
nkeys.append((name,))
|
||||||
|
|
||||||
|
# 99.9 % of the routers will NOT enter this, so the doubled code is not a problem
|
||||||
|
if nkeys:
|
||||||
|
mysql.executemany("""
|
||||||
|
INSERT INTO netifs (name)
|
||||||
|
VALUES (%s)
|
||||||
|
ON DUPLICATE KEY UPDATE name=name
|
||||||
|
""",nkeys)
|
||||||
|
netifdict = mysql.fetchdict("SELECT id, name FROM netifs",(),"name","id")
|
||||||
|
|
||||||
|
ndata = []
|
||||||
|
for netif in router_update["netifs"]:
|
||||||
|
# sanitize name
|
||||||
|
name = netif["name"].replace(".", "").replace("$", "")
|
||||||
|
with suppress(KeyError):
|
||||||
|
ndata.append((router_id,netifdict[name],time,netif["traffic"]["rx"],netif["traffic"]["tx"],))
|
||||||
|
|
||||||
mysql.executemany("""
|
mysql.executemany("""
|
||||||
INSERT INTO router_stats_netif (router, netif, time, rx, tx)
|
INSERT INTO router_stats_netif (router, netif, time, rx, tx)
|
||||||
VALUES (%s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s)
|
||||||
|
|
|
@ -98,6 +98,7 @@ def alfred():
|
||||||
banned = mysql.fetchall("""
|
banned = mysql.fetchall("""
|
||||||
SELECT mac FROM banned
|
SELECT mac FROM banned
|
||||||
""",(),"mac")
|
""",(),"mac")
|
||||||
|
netifdict = mysql.fetchdict("SELECT id, name FROM netifs",(),"name","id")
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
alfred_data = request.get_json()
|
alfred_data = request.get_json()
|
||||||
|
|
||||||
|
@ -105,7 +106,7 @@ def alfred():
|
||||||
# load router status xml data
|
# load router status xml data
|
||||||
i = 1
|
i = 1
|
||||||
for mac, xml in alfred_data.get("64", {}).items():
|
for mac, xml in alfred_data.get("64", {}).items():
|
||||||
import_nodewatcher_xml(mysql, mac, xml, banned)
|
import_nodewatcher_xml(mysql, mac, xml, banned, netifdict)
|
||||||
if (i%500 == 0):
|
if (i%500 == 0):
|
||||||
mysql.commit()
|
mysql.commit()
|
||||||
i += 1
|
i += 1
|
||||||
|
|
|
@ -126,7 +126,10 @@ def router_info(dbid):
|
||||||
s["time"] = mysql.utcawareint(s["time"])
|
s["time"] = mysql.utcawareint(s["time"])
|
||||||
|
|
||||||
netiffetch = mysql.fetchall("""
|
netiffetch = mysql.fetchall("""
|
||||||
SELECT netif, rx, tx, time FROM router_stats_netif WHERE router = %s
|
SELECT netifs.name AS netif, rx, tx, time
|
||||||
|
FROM router_stats_netif
|
||||||
|
INNER JOIN netifs ON router_stats_netif.netif = netifs.id
|
||||||
|
WHERE router = %s
|
||||||
""",(dbid,))
|
""",(dbid,))
|
||||||
|
|
||||||
for ns in netiffetch:
|
for ns in netiffetch:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user