gwinfo: Delete old netif data

Netif information is deleted 48 hours after the MAC addresses
have changed.

This requires changes to the MySQL database!

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-03-05 14:20:13 +01:00
parent 254236445f
commit 306681eee0
3 changed files with 13 additions and 6 deletions

View File

@ -7,6 +7,7 @@ CONFIG = {
"offline_threshold_minutes": 15, # Router switches to offline after X minutes
"orphan_threshold_days": 7, # Router switches to orphaned state after X days
"delete_threshold_days": 180, # Router is deleted after X days
"gw_netif_threshold_hours": 48, # Hours which outdated netif from gwinfo is preserved for statistics
"router_stat_days": 30, # Router stats are collected for X days
"router_stat_netif": 21, # Router stats for netifs are collected for X days
"router_stat_mindiff_secs": 10, # Time difference (uptime) in seconds required for a new entry in router stats

View File

@ -46,7 +46,8 @@ mysql.execute("""
`gw` smallint(5) UNSIGNED NOT NULL,
`mac` char(17) COLLATE utf8_unicode_ci NOT NULL,
`netif` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`vpnmac` char(17) COLLATE utf8_unicode_ci DEFAULT NULL
`vpnmac` char(17) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_contact` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
""")

View File

@ -15,7 +15,9 @@ import time
def import_gw_data(mysql, gw_data):
if "hostname" in gw_data and "netifs" in gw_data:
time = utcnow().strftime('%Y-%m-%d %H:%M:%S')
threshold = mysql.formatdt(utcnow() - datetime.timedelta(hours=CONFIG["gw_netif_threshold_hours"]))
stats_page = gw_data.get("stats_page","")
if not stats_page:
stats_page = None
newid = mysql.findone("SELECT id FROM gw WHERE name = %s LIMIT 1",(gw_data["hostname"],),"id")
@ -47,17 +49,20 @@ def import_gw_data(mysql, gw_data):
else:
n["vpnmac"] = None
ndata.append((newid,n["mac"],n["netif"],n["vpnmac"]))
ndata.append((newid,n["mac"],n["netif"],n["vpnmac"],time,))
mysql.executemany("""
INSERT INTO gw_netif (gw, mac, netif, vpnmac)
VALUES (%s, %s, %s, %s)
INSERT INTO gw_netif (gw, mac, netif, vpnmac, last_contact)
VALUES (%s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
gw=VALUES(gw),
netif=VALUES(netif),
vpnmac=VALUES(vpnmac)
vpnmac=VALUES(vpnmac),
last_contact=VALUES(last_contact)
""",ndata)
mysql.execute("DELETE FROM gw_netif WHERE last_contact < %s",(threshold,))
adata = []
aid = 0
for a in gw_data["admins"]: