Browse Source

next 1

master
gsd 5 days ago
parent
commit
e08db42d84
  1. 30
      fetchers.py
  2. 8
      service.py

30
fetchers.py

@ -5,6 +5,7 @@ import datetime as datetimesrc
import psutil import psutil
import subprocess import subprocess
from colors import Colors, getColorsDict from colors import Colors, getColorsDict
import ipaddress
def pretty_mac(_mac): def pretty_mac(_mac):
try: try:
@ -113,7 +114,20 @@ def getMemPercent(colors = True):
def getRowSeparator(): def getRowSeparator():
return Colors.NEGATIVE + "".join([" " for i in range(0, 60)]) + Colors.END return Colors.NEGATIVE + "".join([" " for i in range(0, 60)]) + Colors.END
def getArp(): def checkIpInPool(ip, pool):
if not ip or not pool:
return True
return ipaddress.ip_address(ip) in ipaddress.ip_network(pool)
def pingHostViaNMap(ip_pool = None):
if ip_pool is None:
return ""
subprocess.check_output(f"nmap -sn {ip_pool}", shell=True, text=True)
return getDateTime()
def getArp(ip_pool = None):
#pingHostViaNMap(ip_pool)
global maclookup global maclookup
if maclookup is None: if maclookup is None:
maclookup = MacLookup() maclookup = MacLookup()
@ -128,6 +142,18 @@ def getArp():
for line in arp_res.split("\n"): for line in arp_res.split("\n"):
if line: if line:
host, ip, at, mac, *other = line.split(" ") host, ip, at, mac, *other = line.split(" ")
ip = ip[1:-1]
if checkIpInPool(ip, ip_pool):
pass
else:
continue
if mac == "(incomplete)":
continue
if mac == "FF:FF:FF:FF:FF:FF".lower():
continue
mac = pretty_mac(mac) mac = pretty_mac(mac)
if mac not in firstMacLookup: if mac not in firstMacLookup:
@ -137,7 +163,7 @@ def getArp():
delta = int(time.time()) - firstMacLookup[mac] delta = int(time.time()) - firstMacLookup[mac]
result.append({ result.append({
"host": host, "host": host,
"ip": ip[1:-1], "ip": ip,
"mac": mac, "mac": mac,
"mac_name": maclookup.lookup(mac), "mac_name": maclookup.lookup(mac),
"delta": delta, "delta": delta,

8
service.py

@ -21,13 +21,13 @@ def printColors():
res += f"{v}{k}" res += f"{v}{k}"
print(res) print(res)
def createDaemon(func, key, sleep = 60): def createDaemon(func, key, sleep = 60, *args):
from threading import Thread from threading import Thread
results[key] = "" results[key] = ""
def resTo(): def resTo():
while True: while True:
try: try:
results[key] = func() results[key] = func(*args)
if logging: if logging:
print(f'[{time.time()}] Save result to {key}') print(f'[{time.time()}] Save result to {key}')
except: except:
@ -50,7 +50,8 @@ class Service:
createDaemon(getDateTime, "date_time", 1) createDaemon(getDateTime, "date_time", 1)
createDaemon(getCpuPercent, "cpu_percent", 1) createDaemon(getCpuPercent, "cpu_percent", 1)
createDaemon(getMemPercent, "mem_percent", 1) createDaemon(getMemPercent, "mem_percent", 1)
createDaemon(getArp, "arp_result", 60) createDaemon(getArp, "arp_result", 60, self.args.ip_pool)
createDaemon(pingHostViaNMap, "nmap_pinger", 60, self.args.ip_pool)
try: try:
from haSensors import getHaSensors from haSensors import getHaSensors
createDaemon(getHaSensors, "ha_sensors", 60) createDaemon(getHaSensors, "ha_sensors", 60)
@ -86,6 +87,7 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--cols", default=60) parser.add_argument("--cols", default=60)
parser.add_argument("--rows", default=40) parser.add_argument("--rows", default=40)
parser.add_argument("--ip-pool", default="192.168.3.0/24")
args = parser.parse_args() args = parser.parse_args()
Service(args) Service(args)
#printColors() #printColors()
Loading…
Cancel
Save