v0.9
Complete README, Blocked queries graph, Lightsoff mode
This commit is contained in:
130
dnsmonitor.py
130
dnsmonitor.py
@@ -71,12 +71,13 @@ def get_device(actual_args=None):
|
||||
def sigterm_handler(signum, frame):
|
||||
sys.exit(0)
|
||||
|
||||
def scalepoints(points):
|
||||
def scalepoints(points, blpoints):
|
||||
if device.height == 32:
|
||||
h = 31
|
||||
else:
|
||||
h = 46
|
||||
sclpoint = [0] * len(points)
|
||||
blsclpoint = [0] * len(blpoints)
|
||||
scl = 1
|
||||
maxpnt = 0
|
||||
i = 0
|
||||
@@ -90,10 +91,14 @@ def scalepoints(points):
|
||||
while i <= len(points) -1:
|
||||
sclpoint[i] = int(points[i] / scl)
|
||||
i += 1
|
||||
i = 0
|
||||
while i <= len(blpoints) -1:
|
||||
blsclpoint[i] = int(blpoints[i] / scl)
|
||||
i += 1
|
||||
maxcanv = h * scl
|
||||
return sclpoint, maxcanv
|
||||
return sclpoint, blsclpoint, maxcanv
|
||||
|
||||
def movepoints(points, insertq, restarts, insertr):
|
||||
def movepoints(points, insertq, blpoints, insertbl, restarts, insertr):
|
||||
i = 1
|
||||
tmp = points[0]
|
||||
tmp2 = 0
|
||||
@@ -118,7 +123,21 @@ def movepoints(points, insertq, restarts, insertr):
|
||||
restarts[i] = tmp3
|
||||
tmp3 = tmp4
|
||||
i += 1
|
||||
return points, restarts
|
||||
|
||||
i = 1
|
||||
tmp = blpoints[0]
|
||||
tmp2 = 0
|
||||
blpoints[0] = insertbl
|
||||
while i < len(blpoints):
|
||||
if i == len(blpoints) - 1:
|
||||
blpoints[i] = tmp
|
||||
else:
|
||||
tmp2 = blpoints[i]
|
||||
blpoints[i] = tmp
|
||||
tmp = tmp2
|
||||
i += 1
|
||||
i = 1
|
||||
return points, blpoints, restarts
|
||||
|
||||
def extractor(prev, flaunch, url):
|
||||
try:
|
||||
@@ -141,7 +160,7 @@ def extractor(prev, flaunch, url):
|
||||
else:
|
||||
return 0, -1
|
||||
except:
|
||||
cliout("Extraction failed.", 2)
|
||||
cliout("Extraction (Q) failed.", 2)
|
||||
if flaunch == True:
|
||||
return -1
|
||||
else:
|
||||
@@ -151,7 +170,38 @@ def extractor(prev, flaunch, url):
|
||||
else:
|
||||
return count, count - prev
|
||||
|
||||
def drawout(sclpo, maxcanv, failture, restart, distance, dots):
|
||||
def blextractor(prev, flaunch, url):
|
||||
try:
|
||||
data = str(urllib.request.urlopen(url).read()).split("\\n")
|
||||
count = 0
|
||||
i = 0
|
||||
while data[i] != "# TYPE blocky_response_total counter":
|
||||
i += 1
|
||||
i += 1
|
||||
while True:
|
||||
if "blocky_response_total{reason=\"CACHED\",response_code=\"NOERROR\",response_type=\"CACHED\"}" in data[i]:
|
||||
break
|
||||
else:
|
||||
count += int(re.sub('blocky_response_total{.*?} ', '', data[i]))
|
||||
i += 1
|
||||
except urllib.error.URLError as e:
|
||||
cliout(f"Connection to server failed: {e.reason}", 2)
|
||||
if flaunch == True:
|
||||
return -1
|
||||
else:
|
||||
return 0, -1
|
||||
except:
|
||||
cliout("Extraction (BL) failed.", 2)
|
||||
if flaunch == True:
|
||||
return -1
|
||||
else:
|
||||
return 0, -1
|
||||
if flaunch == True:
|
||||
return count
|
||||
else:
|
||||
return count, count - prev
|
||||
|
||||
def drawout(sclpo, sclbl, maxcanv, failture, restart, distance, dots):
|
||||
match device.height:
|
||||
case 32:
|
||||
# todo: backport
|
||||
@@ -173,7 +223,23 @@ def drawout(sclpo, maxcanv, failture, restart, distance, dots):
|
||||
draw.text((device.width - 30, 0), "\uf021", font=icons, fill="white")
|
||||
case 64:
|
||||
with canvas(device) as draw:
|
||||
# Graph
|
||||
# Blocked (solid) Graph
|
||||
|
||||
currpoint = 1
|
||||
targetpx = 128 - distance
|
||||
while currpoint < len(sclbl):
|
||||
if (sclbl[currpoint - 1] < sclbl[currpoint]):
|
||||
draw.polygon([(targetpx + distance, 47 - sclbl[currpoint - 1]), (targetpx, 47 - sclbl[currpoint]), (targetpx, 47 - sclbl[currpoint - 1])], fill="white")
|
||||
#cliout(f"RectDBG (less): X1: {targetpx}, Y1: {47 - sclbl[currpoint - 1]}, X2:{targetpx + distance}, Y2: 47",4)
|
||||
draw.rectangle((targetpx, 47 - sclbl[currpoint - 1], targetpx + distance, 47), fill="white")
|
||||
else:
|
||||
draw.polygon([(targetpx + distance, 47 - sclbl[currpoint - 1]), (targetpx + distance, 47 - sclbl[currpoint]), (targetpx, 47 - sclbl[currpoint])], fill="white")
|
||||
#cliout(f"RectDBG (more): X1: {targetpx}, Y1: {47 - sclbl[currpoint]}, X1: {targetpx + distance}, Y2: 47", 4)
|
||||
draw.rectangle((targetpx, 47 - sclbl[currpoint], targetpx + distance, 47), fill="white")
|
||||
currpoint += 1
|
||||
targetpx -= distance
|
||||
|
||||
# Main Graph
|
||||
currpoint = 1
|
||||
targetpx = 128 - distance
|
||||
while currpoint < len(sclpo):
|
||||
@@ -201,16 +267,17 @@ def drawout(sclpo, maxcanv, failture, restart, distance, dots):
|
||||
with canvas(device) as draw:
|
||||
draw.text((0,0), "Display not supported", font=smafont, fill="white")
|
||||
|
||||
def getdata(oldtotal, url):
|
||||
def getdata(oldtotal, oldbltotal, url):
|
||||
total, query = extractor(oldtotal, False, url)
|
||||
bltotal, blquery = blextractor(oldbltotal, False, url)
|
||||
# Server lifetime counter | Difference | Err? | Restarted?
|
||||
if query == -1:
|
||||
cliout("Extractor finished with error.", 1)
|
||||
return oldtotal, 0, True, False
|
||||
return oldtotal, 0, bltotal, 0, True, False
|
||||
if total < oldtotal:
|
||||
cliout("Server restart was detected.", 1)
|
||||
return total, 0, False, True
|
||||
return total, query, False, False
|
||||
return total, 0, bltotal, 0, False, True
|
||||
return total, query, bltotal, blquery, False, False
|
||||
|
||||
def cliout(data, code):
|
||||
time = datetime.datetime.now().strftime("%H:%M:%S")
|
||||
@@ -223,11 +290,13 @@ def cliout(data, code):
|
||||
codes = "E"
|
||||
case 3:
|
||||
codes = "F"
|
||||
case 4:
|
||||
codes = "V"
|
||||
print(f"[{codes}] [{time}] {data}")
|
||||
|
||||
def main():
|
||||
cliout("Blocky Graph Monitor for OLED v0.8b", 0)
|
||||
cliout("(C) Nikopol 2024", 0)
|
||||
cliout("Blocky Graph Monitor for OLED v0.9b", 0)
|
||||
cliout("Nikopol 2024", 0)
|
||||
cliout("Init...", 0)
|
||||
|
||||
# vars spagetti
|
||||
@@ -241,7 +310,10 @@ def main():
|
||||
restart = False
|
||||
rstate = False
|
||||
dots = False
|
||||
device.contrast(0)
|
||||
lightsoff = False
|
||||
start = 22
|
||||
stop = 6
|
||||
device.contrast(1)
|
||||
|
||||
|
||||
# Load config
|
||||
@@ -249,12 +321,12 @@ def main():
|
||||
filedata = open("config.ini").readlines()
|
||||
except FileNotFoundError:
|
||||
f = open("config.ini", "x")
|
||||
f.write("[source]\nurl = http://127.0.0.1:4500/metrics\n[appearance]\npoints = 14\npointsDistance = 10")
|
||||
f.write("[source]\nurl = http://127.0.0.1:4500/metrics\n[appearance]\npoints = 14\npointsDistance = 10\ndots = True\n[lightsoff]\nenabled = False\nstart = 23\nstop = 06")
|
||||
f.close()
|
||||
cliout("config.ini does not exist. Please edit newly created file and start the program.", 3)
|
||||
exit()
|
||||
except:
|
||||
cliout("Failed to read config file. Are permissions correct?", 3)
|
||||
cliout("Failed to read/write config file. Are permissions correct?", 3)
|
||||
exit()
|
||||
while i < len(filedata):
|
||||
if "url = " in filedata[i]:
|
||||
@@ -265,13 +337,21 @@ def main():
|
||||
distance = int(filedata[i][17:])
|
||||
if "dots = True" in filedata[i]:
|
||||
dots = True
|
||||
if "enabled = True" in filedata[i]:
|
||||
lightsoff = True
|
||||
if "start = " in filedata[i]:
|
||||
start = int(filedata[i][8:])
|
||||
if "stop = " in filedata[i]:
|
||||
stop = int(filedata[i][7:])
|
||||
i += 1
|
||||
i = 0
|
||||
cliout("Config load ok", 0)
|
||||
|
||||
pointsmap = [0] * pointscount
|
||||
blockedmap = [0] * len(pointsmap)
|
||||
restartmap = [True] * len(pointsmap)
|
||||
total = extractor(0, True, url)
|
||||
bltotal = blextractor(0, True, url)
|
||||
|
||||
# Show fail on boot time if occurs.
|
||||
if total == -1:
|
||||
@@ -281,21 +361,24 @@ def main():
|
||||
now = datetime.datetime.now()
|
||||
today_time = now.strftime("%H:%M")
|
||||
hrs = now.strftime("%H")
|
||||
#cliout(f"start is {start}", 1)
|
||||
if today_time != today_last_time:
|
||||
#if 1:
|
||||
today_last_time = today_time
|
||||
if hrs != last_hrs:
|
||||
#if 1:
|
||||
if (int(hrs) < stop and int(hrs) >= start and lightsoff == True):
|
||||
cliout(f"Lightsoff is enabled and within time range. Screen will be black this hour.", 0)
|
||||
last_hrs = hrs
|
||||
# Grab fresh data
|
||||
cliout("Extracting data...", 0)
|
||||
total, query, failture, rstate = getdata(total, url)
|
||||
total, query, bltotal, blquery, failture, rstate = getdata(total, bltotal, url)
|
||||
#print(total)
|
||||
#print(query)
|
||||
# Now write it to (raw) array and move older data
|
||||
pointsmap, restartmap = movepoints(pointsmap, query, restartmap, rstate)
|
||||
pointsmap, blockedmap, restartmap = movepoints(pointsmap, query, blockedmap, blquery, restartmap, rstate)
|
||||
# ...and scale it to screen size
|
||||
sclpo, maxcanv = scalepoints(pointsmap)
|
||||
sclpo, sclbl, maxcanv = scalepoints(pointsmap, blockedmap)
|
||||
# also write restart map
|
||||
restart = False
|
||||
while i < len(restartmap):
|
||||
@@ -303,8 +386,13 @@ def main():
|
||||
restart = True
|
||||
i += 1
|
||||
i = 0
|
||||
cliout(f"Done. Current: {query}", 0)
|
||||
drawout(sclpo, maxcanv, failture, restart, distance, dots)
|
||||
cliout(f"Done. Current: {query}, Blocked: {blquery}", 0)
|
||||
if (int(hrs) < stop and int(hrs) >= start and lightsoff == True):
|
||||
with canvas(device) as draw:
|
||||
# :troll:
|
||||
hrs = hrs
|
||||
else:
|
||||
drawout(sclpo, sclbl, maxcanv, failture, restart, distance, dots)
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user