#!/usr/bin/env python3 def config(name, port_max, ports_label): print("graph_category network") print("graph_info Mikrotik network flow") print(f"graph_title Network flow for {name}") print("graph_order down up") print("graph_args --base 1000") print("graph_vlabel bits in (-) / out (+) per ${graph_period}") for port in range(1, port_max + 1): print(f"down{port}.label {ports_label[port - 1]}") print(f"down{port}.type DERIVE") print(f"down{port}.graph no") print(f"down{port}.cdef down,8,*") for port in range(1, port_max + 1): print(f"up{port}.label {ports_label[port - 1]}") print(f"up{port}.type DERIVE") print(f"up{port}.negative down") print(f"up{port}.cdef up,8,*") if __name__ == '__main__': from sys import argv import subprocess ports_label = [] InOctets = [] OutOctets = [] port_max = 0 host = argv[0].split("_")[1] command = f"snmpwalk -v 2c -c public {host}" result = subprocess.getoutput(command) for line in result.splitlines(): if "sysName" in line: name = line.split()[-1] if "ifDescr" in line: ports_label.append(line.split()[-1]) if "ifIndex" in line: port_max += 1 if "ifInOctets" in line: InOctets.append(int(line.split()[-1])) if "ifOutOctets" in line: OutOctets.append(int(line.split()[-1])) if 1 < len(argv) <= 2 and argv[1] == "config": config(name, port_max, ports_label) else: for port in range(1, port_max + 1): print(f"down{port}.value {OutOctets[port - 1]}") print(f"up{port}.value {InOctets[port - 1]}")