moniteur/modules/dnsmasq_bounds.py

74 lines
2.1 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from curses import color_pair as get_pair
border = '|'
critical_bounds = 60
bound_file = '/var/lib/misc/dnsmasq.leases'
# C'est pour importer des modules du repertoire parent
from os.path import dirname
from sys import path as pythonpath
pythonpath.insert (0, dirname (dirname (__file__)))
from utils import seconds_to_time
def dnsmasq_dhcpbounds ():
'''
Return a tuple of the active leases and the remaining time in second
tuple format for each bound :
(remaining_time, mac, ip, hostname)
'''
from csv import reader
from socket import inet_aton
from time import time
bounds1 = []
for i in reader (open (bound_file), delimiter = ' '):
if i [0] != 'duid':
bounds1.append (i)
bounds2 = []
for i in sorted (bounds1, key=lambda item: inet_aton(item[2])):
remain = int (i [0]) - int (time ())
bounds2.append ((remain, i[1], i[2], i[3]))
return bounds2
def main (line, screen):
screen.addstr (line, 0, '-->', get_pair (5))
screen.addstr (line, 3, 'Baux DHCP', get_pair (1))
line += 1
bounds = dnsmasq_dhcpbounds ()
max_lengths = [0, 0, 0, 0]
border_length = len (border)
for i in bounds:
length = len (seconds_to_time (i [0]))
if length > max_lengths [0]:
max_lengths [0] = length
length = len (i [1])
if length > max_lengths [1]:
max_lengths [1] = length
length = len (i [2])
if length > max_lengths [2]:
max_lengths [2] = length
length = len (i [3])
if length > max_lengths [3]:
max_lengths [3] = length
for i in bounds:
x=0
if i [0] < critical_bounds:
screen.addstr (line, x, seconds_to_time (i [0]), get_pair (4))
else:
screen.addstr (line, x, seconds_to_time (i [0]), get_pair (3))
x += max_lengths [0] + 1
screen.addstr (line, x, '|', get_pair (1))
x += border_length + 1
screen.addstr (line, x, i [1], get_pair (2))
x += max_lengths [1] + 1
screen.addstr (line, x, '|', get_pair (1))
x += border_length + 1
screen.addstr (line, x, i [2], get_pair (2))
x += max_lengths [2] + 1
screen.addstr (line, x, '|', get_pair (1))
x += border_length + 1
screen.addstr (line, x, i [3], get_pair (2))
x += max_lengths [3] + 1
line += 1
return (line)