diff options
Diffstat (limited to 'clients')
-rwxr-xr-x | clients/admintool.sh | 19 | ||||
-rwxr-xr-x | clients/build-linknets.pl | 6 | ||||
-rwxr-xr-x | clients/flatify.pl | 21 | ||||
-rwxr-xr-x | clients/lldpdiscover.pl | 4 | ||||
-rwxr-xr-x | clients/smanagrun.pl | 40 | ||||
-rwxr-xr-x | clients/snmpfetch.pl | 235 | ||||
-rwxr-xr-x | clients/update-public-nms.sh | 7 | ||||
-rwxr-xr-x | clients/update-public-speedometer.pl | 37 | ||||
-rw-r--r-- | clients/update-switch-placements.pl | 53 |
9 files changed, 188 insertions, 234 deletions
diff --git a/clients/admintool.sh b/clients/admintool.sh deleted file mode 100755 index 889dd19..0000000 --- a/clients/admintool.sh +++ /dev/null @@ -1,19 +0,0 @@ -while :; do - ( - for i in $( cut -d" " -f1 pingswitches.txt ); do - ADMINADDR=$( echo $i | perl -pi -le '@x = split /\./; $x[3] += 2; $_ = join(".", @x);' ) - ( ( - if ping -c2 -W3 -q $ADMINADDR >/dev/null; then - grep $i pingswitches.txt | sed 's/^/PONGER: /' - else - grep $i pingswitches.txt | sed 's/^/PONGER IKKE: /' - fi - ) & ) - done - ) > pong.new - while pidof ping > /dev/null; do sleep 1; done - mv pong.new pong - echo "sleeping" - sleep 10 -done - diff --git a/clients/build-linknets.pl b/clients/build-linknets.pl index b1cd74c..3dc5913 100755 --- a/clients/build-linknets.pl +++ b/clients/build-linknets.pl @@ -79,7 +79,7 @@ while (my $ref = $coregws->fetchrow_hashref) { # Find all LLDP neighbors. for my $neigh (values %$lldp) { - $lldpneigh{$sysname}{$neigh->{'lldpRemSysName'}} = 1; + $lldpneigh{lc($sysname)}{lc($neigh->{'lldpRemSysName'})} = 1; } } @@ -104,14 +104,14 @@ while (my ($sysname, $ipv6) = each %loop_ipv6) { # Now go through each linknet candidate, and see if we can find any # direct LLDP neighbors. my $qexist = $dbh->prepare('SELECT COUNT(*) AS cnt FROM linknets WHERE switch1=? AND switch2=?'); -$dbh->do('DELETE FROM linknets'); +#$dbh->do('DELETE FROM linknets'); while (my ($cidr, $devices) = each %map) { for (my $i = 0; $i < scalar @$devices; ++$i) { my $device_a = $devices->[$i]; for (my $j = $i + 1; $j < scalar @$devices; ++$j) { my $device_b = $devices->[$j]; next if $device_a->[0] eq $device_b->[0]; - next unless exists($lldpneigh{$device_a->[0]}{$device_b->[0]}); + next unless exists($lldpneigh{lc($device_a->[0])}{lc($device_b->[0])}); my $switch_a = $switch_id{$device_a->[0]}; my $switch_b = $switch_id{$device_b->[0]}; diff --git a/clients/flatify.pl b/clients/flatify.pl new file mode 100755 index 0000000..f2aa18a --- /dev/null +++ b/clients/flatify.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +# Make the given switch into a D-Link placement-wise. + +use strict; +use warnings; +use lib '../include'; +use nms; + +my $dbh = nms::db_connect(); +my $q = $dbh->prepare('SELECT switch,placement FROM switches NATURAL JOIN placements WHERE sysname LIKE ?'); +$q->execute('%'.$ARGV[0].'%'); + +while (my $ref = $q->fetchrow_hashref) { + $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/ or die; + my ($x1,$y1,$x2,$y2) = ($1, $2, $3, $4); + my $placement = sprintf "(%d,%d),(%d,%d)", $x2 - 100, $y2 - 16, $x2, $y2; + $dbh->do("UPDATE placements SET placement=? WHERE switch=?", + undef, $placement, $ref->{'switch'}); + last; # Take only one. +} diff --git a/clients/lldpdiscover.pl b/clients/lldpdiscover.pl index 581f2d8..f72314e 100755 --- a/clients/lldpdiscover.pl +++ b/clients/lldpdiscover.pl @@ -37,7 +37,7 @@ while (my $ref = $snmpq->fetchrow_hashref) { } # First, find all machines that lack an LLDP chassis ID. -my $q = $dbh->prepare("SELECT switch, ip, community FROM switches WHERE lldp_chassis_id IS NULL AND ip <> '127.0.0.1' AND switchtype <> 'dlink3100'"); +my $q = $dbh->prepare("SELECT switch, ip, community FROM switches WHERE lldp_chassis_id IS NULL AND ip <> '127.0.0.1'"); $q->execute; while (my $ref = $q->fetchrow_hashref) { @@ -55,6 +55,7 @@ while (my $ref = $q->fetchrow_hashref) { $dbh->rollback; } } +$dbh->commit; # Now ask all switches for their LLDP neighbor table. $q = $dbh->prepare("SELECT ip, sysname, community FROM switches WHERE lldp_chassis_id IS NOT NULL AND ip <> '127.0.0.1' AND switchtype <> 'dlink3100'"); @@ -69,6 +70,7 @@ while (my $ref = $q->fetchrow_hashref) { mylog("ERROR: $@ (during poll of $ip)"); $dbh->rollback; } + $dbh->commit; } $dbh->disconnect; diff --git a/clients/smanagrun.pl b/clients/smanagrun.pl index 4d03696..0546901 100755 --- a/clients/smanagrun.pl +++ b/clients/smanagrun.pl @@ -19,9 +19,7 @@ BEGIN { } # Tweak and check my $password = ''; -my $timeout = 15; my $delaytime = 30; -my $poll_frequency = 60; my $dbh = db_connect(); $dbh->{AutoCommit} = 0; @@ -68,44 +66,6 @@ my $sresult = $dbh->prepare("UPDATE squeue SET updated = now(), result = ?, my $sdelay = $dbh->prepare("UPDATE squeue SET delay = now() + delaytime, updated=now(), result = ? WHERE sysname = ?") or die "Unable to prepae sdelay"; -## Send a command to switch and return the data recvied from the switch -#sub switch_exec($$) { -# my ($cmd, $conn) = @_; -# -# # Send the command and get data from switch -# my @data = $conn->cmd($cmd); -# my @lines = (); -# foreach my $line (@data) { -# # Remove escape-7 sequence -# $line =~ s/\x1b\x37//g; -# push (@lines, $line); -# } -# -# return @data; -#} -# -#sub switch_connect($) { -# my ($ip) = @_; -# -# my $conn = new Net::Telnet( Timeout => $timeout, -# Dump_Log => '/tmp/dumplog-queue', -# Errmode => 'return', -# Prompt => '/DGS-3100\#/i'); -# my $ret = $conn->open( Host => $ip); -# if (!$ret || $ret != 1) { -# return (undef); -# } -# # XXX: Just send the password as text, I did not figure out how to -# # handle authentication with only password through $conn->login(). -# #$conn->login(»·Prompt => '/password[: ]*$/i', -# # Name => $password, -# # Password => $password); -# $conn->cmd($password); -# # Get rid of banner -# $conn->get; -# return ($conn); -#} -# sub mylog { my $msg = shift; my $time = POSIX::ctime(time); diff --git a/clients/snmpfetch.pl b/clients/snmpfetch.pl index 9a8ff50..968ace8 100755 --- a/clients/snmpfetch.pl +++ b/clients/snmpfetch.pl @@ -10,43 +10,28 @@ use lib '../include'; use nms; use threads; +our $running = 0; + +our $dbh = nms::db_connect(); +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + # normal mode: fetch switches from the database # instant mode: poll the switches specified on the command line -if (defined($ARGV[0])) { - poll_loop(@ARGV); -} else { - my $threads = 50; - for (1..$threads) { - if (fork() == 0) { - # child - poll_loop(); - exit; - } - } - poll_loop(); -} - -sub poll_loop { - my @switches = @_; - my $instant = (scalar @switches > 0); - my $timeout = 15; +my $instant = defined($ARGV[0]); - my $dbh = nms::db_connect(); - $dbh->{AutoCommit} = 0; - $dbh->{RaiseError} = 1; - - my $qualification; - if ($instant) { - $qualification = "sysname=?"; - } else { - $qualification = <<"EOF"; - (last_updated IS NULL OR now() - last_updated > poll_frequency) - AND (locked='f' OR now() - last_updated > '5 minutes'::interval) - AND ip is not null +my $qualification; +if ($instant) { + $qualification = "sysname LIKE ?"; +} else { + $qualification = <<"EOF"; +(last_updated IS NULL OR now() - last_updated > poll_frequency) +AND (locked='f' OR now() - last_updated > '5 minutes'::interval) +AND ip is not null EOF - } +} - my $qswitch = $dbh->prepare(<<"EOF") +our $qswitch = $dbh->prepare(<<"EOF") SELECT *, DATE_TRUNC('second', now() - last_updated - poll_frequency) AS overdue @@ -60,24 +45,30 @@ ORDER BY LIMIT 1 FOR UPDATE OF switches EOF - or die "Couldn't prepare qswitch"; - my $qlock = $dbh->prepare("UPDATE switches SET locked='t', last_updated=now() WHERE switch=?") - or die "Couldn't prepare qlock"; - my $qunlock = $dbh->prepare("UPDATE switches SET locked='f', last_updated=now() WHERE switch=?") - or die "Couldn't prepare qunlock"; - my $qpoll = $dbh->prepare("INSERT INTO polls (time, switch, port, bytes_in, bytes_out, errors_in, errors_out, official_port) VALUES (timeofday()::timestamp,?,?,?,?,?,?,true)") - or die "Couldn't prepare qpoll"; - my $qtemppoll = $dbh->prepare("INSERT INTO temppoll (time, switch, temp) VALUES (timeofday()::timestamp,?::text::int,?::text::float)") - or die "Couldn't prepare qtemppoll"; - my $qcpupoll = $dbh->prepare("INSERT INTO cpuloadpoll (time, switch, entity, value) VALUES (timeofday()::timestamp,?::text::int,?,?)") - or die "Couldn't prepare qtemppoll"; + or die "Couldn't prepare qswitch"; +our $qlock = $dbh->prepare("UPDATE switches SET locked='t', last_updated=now() WHERE switch=?") + or die "Couldn't prepare qlock"; +our $qunlock = $dbh->prepare("UPDATE switches SET locked='f', last_updated=now() WHERE switch=?") + or die "Couldn't prepare qunlock"; +our $qpoll = $dbh->prepare("INSERT INTO polls (time, switch, port, bytes_in, bytes_out, errors_in, errors_out, official_port) VALUES (timeofday()::timestamp,?,?,?,?,?,?,true)") + or die "Couldn't prepare qpoll"; + +poll_loop(@ARGV); +while ($running > 0) { + SNMP::MainLoop(0.1); +} + +sub poll_loop { + my @switches = @_; + my $instant = (scalar @switches > 0); + my $timeout = 15; while (1) { my $sysname; if ($instant) { $sysname = shift @ARGV; - exit if (!defined($sysname)); - $qswitch->execute($sysname) + return if (!defined($sysname)); + $qswitch->execute('%'.$sysname.'%') or die "Couldn't get switch"; } else { # Find a switch to grab @@ -91,10 +82,10 @@ EOF if ($instant) { mylog("No such switch $sysname available, quitting."); - exit; + return; } else { mylog("No available switches in pool, sleeping."); - sleep 15; + SNMP::MainLoop(1.0); next; } } @@ -118,7 +109,6 @@ EOF mylog($msg); my $ip = $switch->{'ip'}; - if ($ip eq '127.0.0.1') { mylog("Polling disabled for this switch, skipping."); $qunlock->execute($switch->{'switch'}) @@ -129,41 +119,41 @@ EOF my $community = $switch->{'community'}; my $start = [Time::HiRes::gettimeofday]; + my $session; eval { - my $session = nms::snmp_open_session($ip, $community); - my @ports = expand_ports($switch->{'ports'}); - - for my $port (@ports) { - my $in = $session->get("ifHCInOctets.$port"); - if (!defined($in) || $in !~ /^\d+$/) { - warn $switch->{'sysname'}.":$port: failed reading in"; - next; - } - my $out = $session->get("ifHCOutOctets.$port"); - if (!defined($out) || $out !~ /^\d+$/) { - warn $switch->{'sysname'}.":$port: failed reading in"; - next; - } - my $ine = $session->get("ifInErrors.$port"); - $ine = -1 if (!defined($ine) || $ine !~ /^\d+$/); - my $oute = $session->get("ifOutErrors.$port"); - $oute = -1 if (!defined($oute) || $oute !~ /^\d+$/); - - $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute) || die "%s:%s: %s\n", $switch->{'switch'}, $port, $in; - } + $session = nms::snmp_open_session($ip, $community, 1); }; if ($@) { - mylog("ERROR: $@ (during poll of $ip)"); - $dbh->rollback; + warn "Couldn't open session (even an async one!) to $ip: $!"; + $qunlock->execute($switch->{'switch'}) + or die "Couldn't unlock switch"; + $dbh->commit; + next; + }; + my @ports = expand_ports($switch->{'ports'}); + + my $switch_status = { + session => $session, + ip => $switch->{'ip'}, + sysname => $switch->{'sysname'}, + switch => $switch->{'switch'}, + num_ports => scalar @ports, + num_done => 0, + start => $start, + }; + + for my $port (@ports) { + my @vars = (); + push @vars, ["ifHCInOctets", $port]; + push @vars, ["ifHCOutOctets", $port]; + push @vars, ["ifInErrors", $port]; + push @vars, ["ifOutErrors", $port]; + my $varlist = SNMP::VarList->new(@vars); + $session->get($varlist, [ \&callback, $switch_status, $port ]); } - - my $elapsed = Time::HiRes::tv_interval($start); - $msg = sprintf "Polled $switch->{'ip'} in %5.3f seconds.", $elapsed; - mylog($msg); + $running++; - $qunlock->execute($switch->{'switch'}) - or warn "Couldn't unlock switch"; - $dbh->commit; + $dbh->rollback; } } @@ -194,41 +184,56 @@ sub mylog { printf STDERR "[%s] %s\n", $time, $msg; } -#sub switch_exec { -# my ($cmd, $conn) = @_; -# -# # Send the command and get data from switch -## $conn->dump_log(*STDOUT); -# my @data = $conn->cmd($cmd); -# my @lines = (); -# foreach my $line (@data) { -# # Remove escape-7 sequence -# $line =~ s/\x1b\x37//g; -# push @lines, $line; -# } -# -# return @lines; -#} - -#sub switch_connect { -# my ($ip) = @_; -# -# my $conn = new Net::Telnet( Timeout => $timeout, -# Dump_Log => '/tmp/dumplog-tempfetch', -# Errmode => 'return', -# Prompt => '/es-3024|e(\-)?\d+\-\dsw>/i'); -# my $ret = $conn->open( Host => $ip); -# if (!$ret || $ret != 1) { -# return (0); -# } -# # XXX: Just send the password as text, I did not figure out how to -# # handle authentication with only password through $conn->login(). -# #$conn->login( Prompt => '/password[: ]*$/i', -# # Name => $password, -# # Password => $password); -# my @data = $conn->cmd($password); -# # Get rid of banner -# $conn->get; -# return $conn; -#} +sub callback { + my ($switch, $port, $vars) = @_; + + my ($in, $out, $ine, $oute) = (undef, undef, undef, undef); + + for my $var (@$vars) { + if ($port != $var->[1]) { + die "Response for unknown OID $var->[0].$var->[1] (expected port $port)"; + } + if ($var->[0] eq 'ifHCInOctets') { + $in = $var->[2]; + } elsif ($var->[0] eq 'ifHCOutOctets') { + $out = $var->[2]; + } elsif ($var->[0] eq 'ifInErrors') { + $ine = $var->[2]; + } elsif ($var->[0] eq 'ifOutErrors') { + $oute = $var->[2]; + } else { + die "Response for unknown OID $var->[0].$var->[1]"; + } + } + my $ok = 1; + if (!defined($in) || $in !~ /^\d+$/) { + if (defined($ine)) { + warn $switch->{'sysname'}.":$port: failed reading in"; + } + $ok = 0; + } + if (!defined($out) || $out !~ /^\d+$/) { + if (defined($oute)) { + warn $switch->{'sysname'}.":$port: failed reading in"; + } + $ok = 0; + } + + if ($ok) { + $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute) || die "%s:%s: %s\n", $switch->{'switch'}, $port, $in; + $dbh->commit; + } + + if (++$switch->{'num_done'} == $switch->{'num_ports'}) { + --$running; + + my $elapsed = Time::HiRes::tv_interval($switch->{'start'}); + my $msg = sprintf "Polled $switch->{'ip'} in %5.3f seconds.", $elapsed; + mylog($msg); + + $qunlock->execute($switch->{'switch'}) + or warn "Couldn't unlock switch"; + $dbh->commit; + } +} diff --git a/clients/update-public-nms.sh b/clients/update-public-nms.sh index 535d02d..fe384cb 100755 --- a/clients/update-public-nms.sh +++ b/clients/update-public-nms.sh @@ -1,12 +1,13 @@ #!/bin/sh -DIR=/root/tgmanage/web/nms-public.gathering.org +TGMANAGE=/root/tgmanage +DIR=$TGMANAGE/web/nms-public.gathering.org -wget -qO$DIR/nettkart-trafikk.png.new http://nms.tg14.gathering.org/nettkart.pl wget -qO$DIR/nettkart-dhcp.png.new http://nms.tg14.gathering.org/dhcpkart.pl wget -qO$DIR/led.txt.new http://nms.tg14.gathering.org/led.pl -mv $DIR/nettkart-trafikk.png.new $DIR/nettkart-trafikk.png mv $DIR/nettkart-dhcp.png.new $DIR/nettkart-dhcp.png mv $DIR/led.txt.new $DIR/led.txt +/usr/bin/perl $TGMANAGE/clients/update-public-speedometer.pl > $DIR/speedometer.json.tmp +mv $DIR/speedometer.json.tmp $DIR/speedometer.json /usr/bin/perl -i -pe 'use POSIX qw(strftime); my $timestamp = strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())); s/Sist oppdatert:.*/Sist oppdatert: $timestamp/g;' $DIR/dhcp.html /usr/bin/perl -i -pe 'use POSIX qw(strftime); my $timestamp = strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())); s/Sist oppdatert:.*/Sist oppdatert: $timestamp/g;' $DIR/trafikk.html diff --git a/clients/update-public-speedometer.pl b/clients/update-public-speedometer.pl new file mode 100755 index 0000000..7fd17df --- /dev/null +++ b/clients/update-public-speedometer.pl @@ -0,0 +1,37 @@ +#! /usr/bin/perl -I/root/tgmanage/include +use strict; +use warnings; +use lib 'include'; +use nms; +use Data::Dumper::Simple; + +my $dbh = nms::db_connect(); +$dbh->{AutoCommit} = 0; + +# D-Links +my $sth = $dbh->prepare("select sum(bytes_in) * 8 / 1048576.0 / 1024.0 as traffic_in, sum(bytes_out) * 8 / 1048576.0 / 1024.0 as traffic_out from get_current_datarate() natural join switches where switchtype like 'dlink3100%' and port < 45") + or die "Can't prepare query: $!"; +$sth->execute(); +my $total_traffic_dlink = $sth->fetchrow_hashref(); +$sth->finish(); + +# TeleGW +$sth = $dbh->prepare("select sum(bytes_in) * 8 / 1048576.0 / 1024.0 as traffic_in, sum(bytes_out) * 8 / 1048576.0 / 1024.0 as traffic_out from get_current_datarate() natural join switches where sysname like '%TeleGW%' and (port=64 or port=65 or port=69 or port=70)") + or die "Can't prepare query: $!"; +$sth->execute(); +my $total_traffic_telegw = $sth->fetchrow_hashref(); +$sth->finish(); + +$dbh->disconnect(); + +my $total = $total_traffic_dlink->{'traffic_in'} + $total_traffic_dlink->{'traffic_out'}; +$total += $total_traffic_telegw->{'traffic_in'} + $total_traffic_telegw->{'traffic_out'}; + +# Now we have summarized in+out for clients and in+out for internet-traffic +# We divide by two to get an average +$total = $total / 2; +my $nicetotal = sprintf ("%.2f", $total); + +# {"speed":19.12} +print qq({"speed":$nicetotal}); +exit 0 diff --git a/clients/update-switch-placements.pl b/clients/update-switch-placements.pl deleted file mode 100644 index d635f87..0000000 --- a/clients/update-switch-placements.pl +++ /dev/null @@ -1,53 +0,0 @@ -#! /usr/bin/perl - -print "begin;\n"; -print "delete from placements;\n"; - -open PATCHLIST, "../patchlist.txt" - or die "../patchlist.txt: $!"; - -my $RANGE = "87.76."; - -my $i = 1; -while (<PATCHLIST>) { - chomp; - my ($name, $distro, $port) = split / /; - - $name =~ /e(\d+)-(\d+)/; - my ($e, $s) = ($1, $2); - - my $x = int(168 + $e * 11); - my $y; - - $x += 1 if ($e >= 11); - $x += 2 if ($e >= 15); - $x += 2 if ($e >= 15 && $e < 45 && $s > 2); - $x += 2 if ($e >= 21); - $x += 2 if ($e >= 27 && $e < 45 && $s > 2); - $x += 9 if ($e >= 29); - $x += 1 if ($e >= 31); - $x += 2 if ($e >= 35); - $x += 15 if ($e >= 45); - $x += 2 if ($e >= 51); - $x += 11 if ($e >= 61); - $x += 1 if ($e >= 67); - $x += 1 if ($e >= 71); - $x += 1 if ($e >= 75); - $x += 1 if ($e >= 81); - - if ($s > 2) { - $y = 152 + 88 - 88 * ($s-3); - } else { - $y = 357 + 88 - 88 * ($s-1); - } - - my $xx = $x + 16; - my $yy = $y + 88; - - # Justeringer - - print "insert into placements (switch, placement) values ($i, box '(($x,$y),($xx,$yy))');\n"; - $i++; -} - -print "end;\n"; |