root / plugins / tplink / tl_sg @ 83a8cfc7
Historique | Voir | Annoter | Télécharger (6,41 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
# -*- perl -*- |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU General Public License |
| 6 |
# as published by the Free Software Foundation; version 2 dated June, |
| 7 |
# 1991. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
# |
| 18 |
# Magic markers (used by munin-node-configure and some installation scripts): |
| 19 |
#%# family=auto |
| 20 |
#%# capabilities=autoconf |
| 21 |
|
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
use WWW::Mechanize; |
| 25 |
|
| 26 |
=pod |
| 27 |
|
| 28 |
=encoding UTF-8 |
| 29 |
|
| 30 |
=head1 NAME |
| 31 |
|
| 32 |
tl_sg - Plugin to monitor packets per second and link speed for TP-Link SG108E/SG1016E switches |
| 33 |
|
| 34 |
=head1 APPLICABLE SYSTEMS |
| 35 |
|
| 36 |
TP-Link SG108E/SG1016E switches with web management (http). Tested with software version 1.0.2 Build 20160526 Rel.34615 on TL SG108E |
| 37 |
|
| 38 |
=head1 CONFIGURATION |
| 39 |
|
| 40 |
Add this to the relevant munin-node config file. You can specify switch address, username, password and description for each port |
| 41 |
(the switch management doesn't allow port descriptions) |
| 42 |
|
| 43 |
[tl_sg] |
| 44 |
env.host 192.168.1.12 |
| 45 |
env.port 80 |
| 46 |
env.numberOfPorts 8 |
| 47 |
env.username admin |
| 48 |
env.password mySecretPassword |
| 49 |
env.p1 'Link to PC1' |
| 50 |
env.p2 'Link to server1' |
| 51 |
env.p3 'Not used' |
| 52 |
env.p4 'Link to AP' |
| 53 |
env.p5 'Link to PC2' |
| 54 |
env.p6 'Link to PC3' |
| 55 |
env.p7 'Not used' |
| 56 |
env.p8 'Uplink' |
| 57 |
|
| 58 |
If you're monitoring multiple switches, create different symlinks in /etc/munin/plugins pointing to this plugin and use the symlink |
| 59 |
name as a configuration section as described above. |
| 60 |
|
| 61 |
Requires WWW:Mechanize module: |
| 62 |
sudo apt-get install libwww-mechanize-perl |
| 63 |
|
| 64 |
=head1 BUGS/GOTCHAS |
| 65 |
|
| 66 |
The link speed is represented as a number: |
| 67 |
0 - down |
| 68 |
5 - 100Mbps full |
| 69 |
6 - 1Gbps |
| 70 |
|
| 71 |
=head1 AUTHOR |
| 72 |
|
| 73 |
Adrian Popa (https://github.com/mad-ady) |
| 74 |
|
| 75 |
=head1 COPYRIGHT |
| 76 |
|
| 77 |
Copyright (c) 2018, Adrian Popa |
| 78 |
|
| 79 |
All rights reserved. This program is free software; you can |
| 80 |
redistribute it and/or modify it under the terms of the GNU General |
| 81 |
Public License as published by the Free Software Foundation; version 2 |
| 82 |
dated June, 1991. |
| 83 |
|
| 84 |
=head1 VERSION |
| 85 |
|
| 86 |
1.1 |
| 87 |
|
| 88 |
=cut |
| 89 |
|
| 90 |
# read parameters from munin |
| 91 |
my $host = ( $ENV{host} || '192.168.1.1' );
|
| 92 |
my $tcpport = ( $ENV{port} || '80' );
|
| 93 |
my $username = ( $ENV{username} || 'admin' );
|
| 94 |
my $password = ( $ENV{password} || 'admin' );
|
| 95 |
my $numberOfPorts = ( $ENV{numberOfPorts} || '8' );
|
| 96 |
|
| 97 |
#populate the ports and descriptions based on ENV |
| 98 |
my %ports = (); |
| 99 |
for ( 1 .. $numberOfPorts ) {
|
| 100 |
my $i = $_; |
| 101 |
if ( defined $ENV{"p$i"} ) {
|
| 102 |
$ports{$i} = $ENV{"p$i"};
|
| 103 |
} |
| 104 |
else {
|
| 105 |
#no description |
| 106 |
$ports{$i} = "Port $i";
|
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( $ARGV[0] and $ARGV[0] eq 'autoconf' ) {
|
| 111 |
print "no (manual configuration needed)\n"; |
| 112 |
exit 0; |
| 113 |
} |
| 114 |
|
| 115 |
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
|
| 116 |
foreach my $graphType (qw/Packets Speed/) {
|
| 117 |
foreach my $port ( sort keys %ports ) {
|
| 118 |
print "multigraph ${graphType}_if_$port\n",
|
| 119 |
"graph_title $graphType for $host port $port $ports{$port}\n",
|
| 120 |
"graph_info $graphType graph for port $port $ports{$port}\n",
|
| 121 |
"graph_args --base 1000 -l 0\n", |
| 122 |
"graph_scale yes\n", |
| 123 |
"graph_category network\n"; |
| 124 |
if ( $graphType eq 'Speed' ) {
|
| 125 |
print "graph_vlabel speed\n"; |
| 126 |
print "p${port}.label Port $port Link speed\n";
|
| 127 |
print "p${port}.type GAUGE\n";
|
| 128 |
} |
| 129 |
else {
|
| 130 |
print "graph_vlabel packets\n"; |
| 131 |
foreach my $gb (qw/good bad/) {
|
| 132 |
foreach my $direction (qw/tx rx/) {
|
| 133 |
print "p${port}${direction}${gb}.label Port $port $direction ($gb)\n";
|
| 134 |
print "p${port}${direction}${gb}.type COUNTER\n";
|
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
exit 0; |
| 141 |
} |
| 142 |
|
| 143 |
# extract data from the switch - uses web scraping (tested with 1.0.2 Build 20160526 Rel.34615) |
| 144 |
|
| 145 |
# print STDERR "Connecting to $host with user $username"; |
| 146 |
my $mech = WWW::Mechanize->new( |
| 147 |
autocheck => 0, |
| 148 |
requests_redirectable => [ 'GET', 'HEAD', 'POST' ] |
| 149 |
); |
| 150 |
my $result = $mech->post( |
| 151 |
"http://$host:$tcpport/logon.cgi", |
| 152 |
[ username => $username, password => $password, logon => 'Login' ], |
| 153 |
"Referer" => "http://$host:$tcpport/" |
| 154 |
); |
| 155 |
my $response = $mech->response(); |
| 156 |
|
| 157 |
# navigate to the page with the network stats |
| 158 |
$result = $mech->get("http://$host:$tcpport/PortStatisticsRpm.htm");
|
| 159 |
$response = $mech->response(); |
| 160 |
|
| 161 |
# print STDERR $response->code()."\n"; |
| 162 |
|
| 163 |
# get the data |
| 164 |
my $data = $mech->content( raw => 1 ); |
| 165 |
|
| 166 |
# print STDERR "$data\n"; |
| 167 |
|
| 168 |
# The page stores the data in a table, but internally it is stored in 3 javascript arrays: |
| 169 |
# state:[1,1,1,1,1,1,1,1,0,0], |
| 170 |
# link_status:[0,5,0,0,5,5,5,6,0,0], |
| 171 |
# pkts:[0,0,0,0,14141090,0,10461386,0,14226,0,12252,0,0,0,0,0,2872063,0,1402200,0,59764503,0,34619246,0,4913873,0,4393574,0,44170456,0,68499653,0,0,0] |
| 172 |
|
| 173 |
# state: 1 - Enabled, 0 - Disabled (administratively) |
| 174 |
# link_status: 0 - down, 5 - 100Mbps full, 6 - 1Gbps |
| 175 |
# pkts: every group of 4 values represent txGoodPkt, txBadPkt, rxGoodPkt, rxBadPkt |
| 176 |
|
| 177 |
# parse good/bad packets |
| 178 |
if ( $data =~ /pkts:\[([0-9,]+)\]/ ) {
|
| 179 |
|
| 180 |
my $packetString = $1; |
| 181 |
my @packets = split( /,/, $packetString ); |
| 182 |
|
| 183 |
for ( 1 .. $numberOfPorts ) {
|
| 184 |
my $currentPort = $_; |
| 185 |
my $txGoodPkt = $packets[ ( $currentPort - 1 ) * 4 ]; |
| 186 |
my $txBadPkt = $packets[ ( $currentPort - 1 ) * 4 + 1 ]; |
| 187 |
my $rxGoodPkt = $packets[ ( $currentPort - 1 ) * 4 + 2 ]; |
| 188 |
my $rxBadPkt = $packets[ ( $currentPort - 1 ) * 4 + 3 ]; |
| 189 |
print "multigraph Packets_if_$currentPort\n"; |
| 190 |
|
| 191 |
print "p${currentPort}txgood.value $txGoodPkt\n";
|
| 192 |
print "p${currentPort}rxgood.value $rxGoodPkt\n";
|
| 193 |
print "p${currentPort}txbad.value $txBadPkt\n";
|
| 194 |
print "p${currentPort}rxbad.value $rxBadPkt\n";
|
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
# parse link speed |
| 199 |
if ( $data =~ /link_status:\[([0-9,]+)\]/ ) {
|
| 200 |
|
| 201 |
my $linkString = $1; |
| 202 |
my @links = split( /,/, $linkString ); |
| 203 |
for ( 1 .. $numberOfPorts ) {
|
| 204 |
my $currentPort = $_; |
| 205 |
my $link = $links[ $currentPort - 1 ]; |
| 206 |
print "multigraph Speed_if_$currentPort\n"; |
| 207 |
|
| 208 |
print "p${currentPort}.value $link\n";
|
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
# vim: ft=perl : ts=4 : expandtab |
