From 81494f55191939106bed527a30a68163f9040482 Mon Sep 17 00:00:00 2001 From: VC Date: Fri, 5 Jul 2024 11:53:06 +0200 Subject: [PATCH] Ajout de la supervision Mikrotik --- roles/munin-client/files/mikrotikcpu_ | 58 ++++++++++++++ roles/munin-client/files/mikrotikdiskspace_ | 59 ++++++++++++++ roles/munin-client/files/mikrotikifrate_ | 87 +++++++++++++++++++++ roles/munin-client/files/mikrotikmemory_ | 59 ++++++++++++++ roles/munin-client/files/mikrotikvoltage_ | 56 +++++++++++++ roles/munin-client/tasks/main.yml | 23 +++++- roles/munin-client/vars/main.yml | 40 ++++++++++ 7 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 roles/munin-client/files/mikrotikcpu_ create mode 100644 roles/munin-client/files/mikrotikdiskspace_ create mode 100644 roles/munin-client/files/mikrotikifrate_ create mode 100644 roles/munin-client/files/mikrotikmemory_ create mode 100644 roles/munin-client/files/mikrotikvoltage_ create mode 100644 roles/munin-client/vars/main.yml diff --git a/roles/munin-client/files/mikrotikcpu_ b/roles/munin-client/files/mikrotikcpu_ new file mode 100644 index 0000000..0a78bf2 --- /dev/null +++ b/roles/munin-client/files/mikrotikcpu_ @@ -0,0 +1,58 @@ +#!/usr/bin/perl +############################################################################### +use diagnostics; +use Net::SNMP; +use strict; +use warnings; +############################################################################### +my $CPUOID = ".1.3.6.1.2.1.25.3.3.1.2.1"; +my $SNMPCommunity = "public"; +my $SNMPPort = "161"; + +############################################################################### +## Determine Hostname +my $Host = undef; +$0 =~ /mikrotikcpu_(.+)*$/; +unless ($Host = $1) { + exit 2; +} + +############################################################################### +## Initiate SNMP Session +my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, + -community => $SNMPCommunity, + -port => $SNMPPort, + -timeout => 60, + -retries => 5, + -version => 1); +if (!defined($Session)) { + die "Croaking: $Error"; +} + +############################################################################### +## Configuration +if ($ARGV[0] && $ARGV[0] eq "config") { + print "host_name " . $Host . "\n"; + print "graph_args -l 0 -r --vertical-label percent --lower-limit 0 --upper-limit 100\n"; + print "graph_title CPU usage\n"; + print "graph_category system\n"; + print "graph_info This graph shows the router's CPU usage.\n"; + print "graph_order Total\n"; + print "graph_vlabel %\n"; + print "graph_scale no\n"; + print "Total.label CPU Usage\n"; + print "Total.draw AREA\n"; + print "Total.warning 60\n"; + print "Total.critical 90\n"; + $Session->close; + exit; +} + +############################################################################### +## Execution +if (my $Result = $Session->get_request(-varbindlist => [$CPUOID])) { + print "Total.value " . $Result->{$CPUOID} . "\n"; + $Session->close; + exit; +} + diff --git a/roles/munin-client/files/mikrotikdiskspace_ b/roles/munin-client/files/mikrotikdiskspace_ new file mode 100644 index 0000000..ad609f5 --- /dev/null +++ b/roles/munin-client/files/mikrotikdiskspace_ @@ -0,0 +1,59 @@ +#!/usr/bin/perl +############################################################################### +use diagnostics; +use Net::SNMP; +use strict; +use warnings; +############################################################################### +my $DiskTotalOID = ".1.3.6.1.2.1.25.2.3.1.5.1"; +my $DiskUsedOID = ".1.3.6.1.2.1.25.2.3.1.6.1"; +my $SNMPCommunity = "public"; +my $SNMPPort = "161"; + +############################################################################### +## Determine Hostname +my $Host = undef; +$0 =~ /mikrotikdiskspace_(.+)*$/; +unless ($Host = $1) { + exit 2; +} + +############################################################################### +## Initiate SNMP Session +my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, + -community => $SNMPCommunity, + -port => $SNMPPort, + -timeout => 60, + -retries => 5, + -version => 1); +if (!defined($Session)) { + die "Croaking: $Error"; +} + +############################################################################### +## Configuration +if ($ARGV[0] && $ARGV[0] eq "config") { + my $Result = $Session->get_request(-varbindlist => [$DiskTotalOID]); + print "host_name " . $Host . "\n"; + print "graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit " . ($Result->{$DiskTotalOID} * 1024) . "\n"; + print "graph_title Disk Space usage\n"; + print "graph_category system\n"; + print "graph_info This graph shows the router's Disk Space usage.\n"; + print "graph_order Total Used\n"; + print "graph_vlabel bytes\n"; + print "Total.label Total Disk Space\n"; + print "Total.draw AREA\n"; + print "Used.label Used Disk Space\n"; + print "Used.draw AREA\n"; + $Session->close; + exit; +} + +############################################################################### +## Execution +if (my $Result = $Session->get_request(-varbindlist => [$DiskTotalOID, $DiskUsedOID])) { + print "Total.value " . ($Result->{$DiskTotalOID} * 1024) . "\n"; + print "Used.value " . ($Result->{$DiskUsedOID} * 1024) . "\n"; + $Session->close; + exit; +} diff --git a/roles/munin-client/files/mikrotikifrate_ b/roles/munin-client/files/mikrotikifrate_ new file mode 100644 index 0000000..8968158 --- /dev/null +++ b/roles/munin-client/files/mikrotikifrate_ @@ -0,0 +1,87 @@ +#!/usr/bin/perl +############################################################################### +#use diagnostics; +use Net::SNMP; +#use strict; +#use warnings; +############################################################################### +my $SNMPCommunity = "public"; +my $SNMPPort = "161"; + +############################################################################### +## Determine Hostname +my $Host = undef; +my $ifname = undef; +my $OID = ".1.3.6.1.2.1.2.2.1.2"; +$0 =~ /mikrotikifrate_(.+)*$/; + +unless ($Host = $1) { + exit 2; +} + +my @str = split(/_/, $Host); +$Host = $str[0]; +$ifname = $str[1]; + +############################################################################### +## Initiate SNMP Session +my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, + -community => $SNMPCommunity, + -port => $SNMPPort, + -timeout => 10, + -retries => 3, + -version => 2); +if (!defined($Session)) { + die "Croaking: $Error"; +} + +############################################################################### +## Find interface OID +my $walkresult = $Session->get_table($OID); +my @match = undef; +while ( my ($id, $value) = each(%$walkresult) ) { + if ( $value eq $ifname ) { + @match = ("$id","$value"); + } +} + +@str = split(/\./, $match[0]); +my $IFACEID = $str[-1]; +my $IFACEOID = ".1.3.6.1.2.1.2.2.1.2.".$IFACEID; +my $IFACEBYTESIN = ".1.3.6.1.2.1.31.1.1.1.6.".$IFACEID; +my $IFACEBYTESOUT = ".1.3.6.1.2.1.31.1.1.1.10.".$IFACEID; + +############################################################################### +## Configuration +if ($ARGV[0] && $ARGV[0] eq "config") { + print "host_name " . $Host . "\n"; + print "graph_args --base 1000\n"; + print "graph_title " . $ifname . " traffic\n"; + print "graph_vlabel bits per second\n"; + print "graph_category network\n"; + print "graph_info This graph shows the incoming and outgoing traffic rate of an interface\n"; + print "in.label received\n"; + print "in.type DERIVE\n"; + print "in.draw AREA\n"; + print "in.min 0\n"; + print "in.cdef in,8,*\n"; + print "out.label sent\n"; + print "out.type DERIVE\n"; + print "out.draw LINE1\n"; + print "out.min 0\n"; + print "out.cdef out,8,*\n"; + $Session->close; + exit; +} + +############################################################################### +## Execution +if (my $Result = $Session->get_request(-varbindlist => [$IFACEBYTESIN, $IFACEBYTESOUT])) { + print "in.value " . $Result->{$IFACEBYTESIN} . "\n"; + print "out.value " . $Result->{$IFACEBYTESOUT} . "\n"; + $Session->close; + exit; +} + +$Session->close; +exit; diff --git a/roles/munin-client/files/mikrotikmemory_ b/roles/munin-client/files/mikrotikmemory_ new file mode 100644 index 0000000..fd58d7b --- /dev/null +++ b/roles/munin-client/files/mikrotikmemory_ @@ -0,0 +1,59 @@ +#!/usr/bin/perl +############################################################################### +use diagnostics; +use Net::SNMP; +use strict; +use warnings; +############################################################################### +my $MemTotalOID = ".1.3.6.1.2.1.25.2.3.1.5.2"; +my $MemUsedOID = ".1.3.6.1.2.1.25.2.3.1.6.2"; +my $SNMPCommunity = "public"; +my $SNMPPort = "161"; + +############################################################################### +## Determine Hostname +my $Host = undef; +$0 =~ /mikrotikmemory_(.+)*$/; +unless ($Host = $1) { + exit 2; +} + +############################################################################### +## Initiate SNMP Session +my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, + -community => $SNMPCommunity, + -port => $SNMPPort, + -timeout => 60, + -retries => 5, + -version => 1); +if (!defined($Session)) { + die "Croaking: $Error"; +} + +############################################################################### +## Configuration +if ($ARGV[0] && $ARGV[0] eq "config") { + my $Result = $Session->get_request(-varbindlist => [$MemTotalOID]); + print "host_name " . $Host . "\n"; + print "graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit " . ($Result->{$MemTotalOID} * 1024) . "\n"; + print "graph_title Memory usage\n"; + print "graph_category system\n"; + print "graph_info This graph shows the router's memory usage.\n"; + print "graph_order Total Used\n"; + print "graph_vlabel bytes\n"; + print "Total.label Total Memory\n"; + print "Total.draw AREA\n"; + print "Used.label Used Memory\n"; + print "Used.draw AREA\n"; + $Session->close; + exit; +} + +############################################################################### +## Execution +if (my $Result = $Session->get_request(-varbindlist => [$MemTotalOID, $MemUsedOID])) { + print "Total.value " . ($Result->{$MemTotalOID} * 1024) . "\n"; + print "Used.value " . ($Result->{$MemUsedOID} * 1024) . "\n"; + $Session->close; + exit; +} diff --git a/roles/munin-client/files/mikrotikvoltage_ b/roles/munin-client/files/mikrotikvoltage_ new file mode 100644 index 0000000..c180085 --- /dev/null +++ b/roles/munin-client/files/mikrotikvoltage_ @@ -0,0 +1,56 @@ +#!/usr/bin/perl +############################################################################### +#use diagnostics; +use Net::SNMP; +#use strict; +#use warnings; +############################################################################### +my $Voltage = ".1.3.6.1.4.1.14988.1.1.3.8.0"; +my $SNMPCommunity = "public"; +my $SNMPPort = "161"; + +############################################################################### +## Determine Hostname +my $Host = undef; +$0 =~ /mikrotikvoltage_(.+)*$/; +unless ($Host = $1) { + exit 2; +} + +############################################################################### +## Initiate SNMP Session +my ($Session, $Error) = Net::SNMP->session (-hostname => $Host, + -community => $SNMPCommunity, + -port => $SNMPPort, + -timeout => 10, + -retries => 3, + -version => 1); +if (!defined($Session)) { + die "Croaking: $Error"; +} + +############################################################################### +## Configuration +if ($ARGV[0] && $ARGV[0] eq "config") { + my $Result = $Session->get_request(-varbindlist => [$Voltage]); + print "host_name " . $Host . "\n"; + print "graph_args -l 0 -r --vertical-label volts --lower-limit 10 --upper-limit 30\n"; + print "graph_title Voltage usage\n"; + print "graph_category system\n"; + print "graph_info This graph shows the router's voltage usage.\n"; + print "graph_order Total\n"; + print "graph_vlabel volts\n"; + print "graph_scale no\n"; + print "Total.label Total Voltage\n"; + print "Total.draw AREA\n"; + $Session->close; + exit; +} + +############################################################################### +## Execution +if (my $Result = $Session->get_request(-varbindlist => [$Voltage])) { + print "Total.value " . ($Result->{$Voltage} / 10) . "\n"; + $Session->close; + exit; +} diff --git a/roles/munin-client/tasks/main.yml b/roles/munin-client/tasks/main.yml index 53dc918..c962cfe 100644 --- a/roles/munin-client/tasks/main.yml +++ b/roles/munin-client/tasks/main.yml @@ -2,7 +2,7 @@ muninpkgs: - muninlite munin_need_reconfigure: False - when: ansible_facts['distribution'] == "LEDE" or ansible_facts['distribution'] == "OpenWRT" + when: ansible_facts['distribution'] == "LEDE" or ansible_facts['distribution'] == "OpenWRT" or ansible_facts['distribution'] == "OpenWrt" - set_fact: muninpkgs: @@ -64,6 +64,27 @@ - restart munin-node when: "'pgsqlservers' in group_names" +# For Munin servers +- name: deploy mikrotik unitary scripts + copy: + src: "./files/{{ item.0 }}" + dest: "/etc/munin/plugins/{{ item.0 }}{{ item.1.name }}" + mode: 0755 + loop: "{{ mikrotik_unitary_scripts|product(mikrotik_hosts)|list }}" + notify: + - restart munin-node + when: "'muninservers' in group_names" + +- name: deploy mikrotik per iface scripts + copy: + src: "./files/{{ item.1 }}" + dest: "/etc/munin/plugins/{{ item.1 }}{{ item.0.0.name }}_{{ item.0.1 }}" + mode: 0755 + loop: "{{ mikrotik_hosts|subelements('ifaces')|product(mikrotik_periface_scripts) }}" + notify: + - restart munin-node + when: "'muninservers' in group_names" + # for NUT (UPS) servers - name: "add specific nut value for UPS" file: diff --git a/roles/munin-client/vars/main.yml b/roles/munin-client/vars/main.yml new file mode 100644 index 0000000..75c41ac --- /dev/null +++ b/roles/munin-client/vars/main.yml @@ -0,0 +1,40 @@ +mikrotik_unitary_scripts: + - mikrotikcpu_ + - mikrotikdiskspace_ + - mikrotikmemory_ + - mikrotikvoltage_ + +mikrotik_periface_scripts: + - mikrotikifrate_ + +mikrotik_hosts: + - name: garreg-mach.mateu.be + ifaces: + - ether1 + - ether2 + - ether3 + - ether4 + - ether5 + - ether6 + - ether7 + - ether8 + - ether9 + - ether10 + - ether11 + - ether12 + - ether13 + - ether14 + - ether15 + - ether16 + - ether17 + - ether18 + - ether19 + - ether20 + - ether21 + - ether22 + - ether23 + - ether24 + - sftp-sftpplus1 + - sftp-sftpplus2 + - sftp-sftpplus3 + - sftp-sftpplus4