From 16c13756f8bfb5bb057d32d06c90b3267e29159d Mon Sep 17 00:00:00 2001 From: AntoineVe Date: Sat, 4 Dec 2021 23:01:24 +0100 Subject: [PATCH 1/4] Initial commit --- LICENSE | 9 +++++++++ README.md | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ed1c51 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# munin_mikrotik + From 00d8503847110b673e70842eaa98bf087e7eae88 Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Fri, 29 Oct 2021 15:22:15 +0200 Subject: [PATCH 2/4] First version --- mikrotik_ | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 mikrotik_ diff --git a/mikrotik_ b/mikrotik_ new file mode 100755 index 0000000..3563b50 --- /dev/null +++ b/mikrotik_ @@ -0,0 +1,48 @@ +#!/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]}") From 4d405fcb7f5f465fd0ba60eafd274ecc2ce65329 Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Fri, 29 Oct 2021 17:02:16 +0200 Subject: [PATCH 3/4] bug fixes and fieldnames protection --- mikrotik_ | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mikrotik_ b/mikrotik_ index 3563b50..0a00219 100755 --- a/mikrotik_ +++ b/mikrotik_ @@ -6,22 +6,23 @@ def config(name, port_max, ports_label): 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}") + print("graph_vlabel in (-) / out (+)") 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,*") + print(f"down{port}.cdef down{port},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,*") + print(f"up{port}.negative down{port}") + print(f"up{port}.cdef up{port},8,*") if __name__ == '__main__': from sys import argv import subprocess + import re ports_label = [] InOctets = [] OutOctets = [] @@ -32,8 +33,11 @@ if __name__ == '__main__': for line in result.splitlines(): if "sysName" in line: name = line.split()[-1] + name = re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", name) if "ifDescr" in line: - ports_label.append(line.split()[-1]) + label = (line.split()[-1]) + label = re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", label) + ports_label.append(label) if "ifIndex" in line: port_max += 1 if "ifInOctets" in line: From a2d93dabf7996ada8916541db8c315cbe92d6bdf Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Sat, 4 Dec 2021 23:00:37 +0100 Subject: [PATCH 4/4] Ignore negative values --- mikrotik_ | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mikrotik_ b/mikrotik_ index 0a00219..6223e37 100755 --- a/mikrotik_ +++ b/mikrotik_ @@ -10,11 +10,13 @@ def config(name, port_max, ports_label): 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}.min 0") print(f"down{port}.graph no") print(f"down{port}.cdef down{port},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}.min 0") print(f"up{port}.negative down{port}") print(f"up{port}.cdef up{port},8,*")