munin_mikrotik/mikrotik_

55 lines
1.9 KiB
Plaintext
Raw Normal View History

2021-10-29 15:22:15 +02:00
#!/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")
2021-10-29 17:02:16 +02:00
print("graph_vlabel in (-) / out (+)")
2021-10-29 15:22:15 +02:00
for port in range(1, port_max + 1):
print(f"down{port}.label {ports_label[port - 1]}")
print(f"down{port}.type DERIVE")
2021-12-04 23:00:37 +01:00
print(f"down{port}.min 0")
2021-10-29 15:22:15 +02:00
print(f"down{port}.graph no")
2021-10-29 17:02:16 +02:00
print(f"down{port}.cdef down{port},8,*")
2021-10-29 15:22:15 +02:00
for port in range(1, port_max + 1):
print(f"up{port}.label {ports_label[port - 1]}")
print(f"up{port}.type DERIVE")
2021-12-04 23:00:37 +01:00
print(f"up{port}.min 0")
2021-10-29 17:02:16 +02:00
print(f"up{port}.negative down{port}")
print(f"up{port}.cdef up{port},8,*")
2021-10-29 15:22:15 +02:00
if __name__ == '__main__':
from sys import argv
import subprocess
2021-10-29 17:02:16 +02:00
import re
2021-10-29 15:22:15 +02:00
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]
2021-10-29 17:02:16 +02:00
name = re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", name)
2021-10-29 15:22:15 +02:00
if "ifDescr" in line:
2021-10-29 17:02:16 +02:00
label = (line.split()[-1])
label = re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", label)
ports_label.append(label)
2021-10-29 15:22:15 +02:00
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]}")