2016-04-22 10:09:20 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2017-11-22 23:36:04 +01:00
|
|
|
import time
|
2016-04-22 10:09:20 +02:00
|
|
|
import datetime
|
|
|
|
|
2017-12-10 19:04:31 +01:00
|
|
|
from ffmap.config import CONFIG
|
2018-08-27 16:58:00 +02:00
|
|
|
#from socket import inet_pton, inet_ntop, AF_INET6
|
|
|
|
from ipaddress import IPv4Address, IPv6Address
|
2017-12-10 19:04:31 +01:00
|
|
|
|
2016-04-22 10:09:20 +02:00
|
|
|
def utcnow():
|
|
|
|
return datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
|
2017-11-22 23:36:04 +01:00
|
|
|
|
2018-08-27 12:33:27 +02:00
|
|
|
def int2mac(data,keys=None):
|
|
|
|
if keys:
|
|
|
|
for k in keys:
|
|
|
|
data[k] = int2mac(data[k])
|
|
|
|
return data
|
|
|
|
if data:
|
|
|
|
return ':'.join(format(s, '02x') for s in data.to_bytes(6,byteorder='big'))
|
|
|
|
#return ':'.join(format(s, '02x') for s in bytes.fromhex('{0:x}'.format(data)))
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def int2shortmac(data,keys=None):
|
|
|
|
if keys:
|
|
|
|
for k in keys:
|
|
|
|
data[k] = int2shortmac(data[k])
|
|
|
|
return data
|
|
|
|
if data:
|
|
|
|
return '{:012x}'.format(data)
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def shortmac2mac(data):
|
|
|
|
if data:
|
|
|
|
return ':'.join(format(s, '02x') for s in bytes.fromhex(data.replace(':','')))
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def mac2int(data):
|
|
|
|
if data:
|
|
|
|
return int(data.replace(":",""),16)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def int2mactuple(data,index=None):
|
|
|
|
if index:
|
|
|
|
for r in data:
|
|
|
|
r[index] = int2mac(r[index])
|
|
|
|
else:
|
|
|
|
for r in data:
|
|
|
|
r = int2mac(r)
|
|
|
|
return data
|
|
|
|
|
2018-08-27 16:58:00 +02:00
|
|
|
def ipv6tobin(data):
|
|
|
|
if data:
|
|
|
|
return IPv6Address(data).packed
|
|
|
|
#return inet_pton(AF_INET6,data)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
def bintoipv6(data):
|
|
|
|
if data:
|
|
|
|
return IPv6Address(data).compressed
|
|
|
|
#return inet_ntop(AF_INET6,data)
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def ipv4toint(data):
|
|
|
|
if data:
|
|
|
|
return int(IPv4Address(data))
|
|
|
|
#return inet_pton(AF_INET,data)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
def inttoipv4(data):
|
|
|
|
if data:
|
|
|
|
return str(IPv4Address(data))
|
|
|
|
#return inet_ntop(AF_INET,data)
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
2017-11-22 23:36:04 +01:00
|
|
|
def writelog(path, content):
|
|
|
|
with open(path, "a") as csv:
|
|
|
|
csv.write(time.strftime('{%Y-%m-%d %H:%M:%S}') + " - " + content + "\n")
|
2017-12-10 19:04:31 +01:00
|
|
|
|
|
|
|
def writefulllog(content):
|
|
|
|
with open(CONFIG["debug_dir"] + "/fulllog.log", "a") as csv:
|
|
|
|
csv.write(time.strftime('{%Y-%m-%d %H:%M:%S}') + " - " + content + "\n")
|
2018-02-10 17:51:44 +01:00
|
|
|
|
2018-04-29 15:33:31 +02:00
|
|
|
def neighbor_color(quality,netif,rt_protocol):
|
|
|
|
color = "#04ff0a"
|
2018-02-10 17:51:44 +01:00
|
|
|
if rt_protocol=="BATMAN_V":
|
2018-04-29 15:33:31 +02:00
|
|
|
if quality < 10:
|
2018-02-10 17:51:44 +01:00
|
|
|
color = "#ff1e1e"
|
|
|
|
elif quality < 20:
|
|
|
|
color = "#ff4949"
|
|
|
|
elif quality < 40:
|
|
|
|
color = "#ff6a6a"
|
|
|
|
elif quality < 80:
|
|
|
|
color = "#ffac53"
|
|
|
|
elif quality < 1000:
|
|
|
|
color = "#ffeb79"
|
|
|
|
else:
|
2018-04-29 15:33:31 +02:00
|
|
|
if quality < 105:
|
2018-02-10 17:51:44 +01:00
|
|
|
color = "#ff1e1e"
|
|
|
|
elif quality < 130:
|
|
|
|
color = "#ff4949"
|
|
|
|
elif quality < 155:
|
|
|
|
color = "#ff6a6a"
|
|
|
|
elif quality < 180:
|
|
|
|
color = "#ffac53"
|
|
|
|
elif quality < 205:
|
|
|
|
color = "#ffeb79"
|
|
|
|
elif quality < 230:
|
|
|
|
color = "#79ff7c"
|
2018-04-29 15:33:31 +02:00
|
|
|
if netif.startswith("eth"):
|
|
|
|
#color = "#999999"
|
|
|
|
color = "#008c00"
|
|
|
|
if quality < 0:
|
|
|
|
color = "#06a4f4"
|
2018-02-10 17:51:44 +01:00
|
|
|
return color
|
2018-04-12 22:13:06 +02:00
|
|
|
|
|
|
|
def defrag_table(mysql,table,sleep):
|
|
|
|
minustime=0
|
|
|
|
allrows=0
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
qry = "ALTER TABLE `%s` ENGINE = InnoDB" % (table)
|
|
|
|
mysql.execute(qry)
|
|
|
|
mysql.commit()
|
|
|
|
|
|
|
|
end_time = time.time()
|
|
|
|
if sleep > 0:
|
|
|
|
time.sleep(sleep)
|
|
|
|
|
|
|
|
writelog(CONFIG["debug_dir"] + "/deletetime.txt", "Defragmented table %s: %.3f seconds" % (table,end_time - start_time))
|
|
|
|
print("--- Defragmented table %s: %.3f seconds ---" % (table,end_time - start_time))
|
|
|
|
|
|
|
|
def defrag_all(mysql,doall=False):
|
2018-08-28 15:28:25 +02:00
|
|
|
alltables = ('gw','gw_admin','gw_netif','hoods','hoodsv1','hoodsv2','netifs','router','router_events','router_gw','router_ipv6','router_neighbor','router_netif','users')
|
2018-04-12 22:13:06 +02:00
|
|
|
stattables = ('router_stats','router_stats_gw','router_stats_neighbor','router_stats_netif','stats_global','stats_gw','stats_hood')
|
|
|
|
|
|
|
|
for t in alltables:
|
|
|
|
defrag_table(mysql,t,1)
|
|
|
|
|
|
|
|
if doall:
|
|
|
|
for t in stattables:
|
|
|
|
defrag_table(mysql,t,60)
|
|
|
|
|
|
|
|
writelog(CONFIG["debug_dir"] + "/deletetime.txt", "-------")
|