diff options
author | Kristian Lyngstol <kristian@bohemians.org> | 2015-04-05 10:11:35 +0200 |
---|---|---|
committer | Kristian Lyngstol <kristian@bohemians.org> | 2015-04-05 10:11:35 +0200 |
commit | 325d236acec7747fa202d94440462c887230a9c8 (patch) | |
tree | 92027c6ee5bf8cd541d6dce0376f2bd4489a39ff | |
parent | ee6b4ccbd44c3a85e2a1d6c8a1873aafa89c3d33 (diff) |
tg15: Clean up other unused things
-rwxr-xr-x | clients/accesspoints.pl | 59 | ||||
-rwxr-xr-x | clients/ipv6-dns.pl | 138 | ||||
-rw-r--r-- | clients/ipv6-stats.pl | 98 | ||||
-rwxr-xr-x | clients/snmpfetch.pl | 256 | ||||
-rwxr-xr-x | clients/update-public-nms.sh | 16 | ||||
-rwxr-xr-x | clients/update-public-speedometer.pl | 37 |
6 files changed, 0 insertions, 604 deletions
diff --git a/clients/accesspoints.pl b/clients/accesspoints.pl deleted file mode 100755 index 85a32c2..0000000 --- a/clients/accesspoints.pl +++ /dev/null @@ -1,59 +0,0 @@ -#! /usr/bin/perl -use strict; -use warnings; -use BER; -use DBI; -use POSIX; -use Time::HiRes; -use Net::Ping; - -use lib '../include'; -use nms; -use threads; - -poll_loop(); - -sub poll_loop { - my $dbh = nms::db_connect(); - my $qcores = $dbh->prepare('SELECT DISTINCT coreswitches.sysname, coreswitches.switch, coreswitches.ip, coreswitches.community FROM uplinks JOIN switches AS coreswitches ON (uplinks.coreswitch = coreswitches.switch)'); - my $qaps = $dbh->prepare("SELECT switches.sysname, switches.switch, uplinks.blade, uplinks.port FROM uplinks NATURAL JOIN switches WHERE uplinks.coreswitch = ?"); - my $qpoll = $dbh->prepare("UPDATE ap_poll SET model=?, last_poll=now() WHERE switch = ?"); - - while (1) { - $qcores->execute(); - my $cores = $qcores->fetchall_hashref("sysname"); - - foreach my $core (keys %$cores) { - my $ip = $cores->{$core}{'ip'}; - my $community = $cores->{$core}{'community'}; - printf "Polling %s (%s)\n", $core, $ip; - eval { - my $session = nms::snmp_open_session($ip, $community); - $qaps->execute($cores->{$core}{'switch'}); - while (my $aps = $qaps->fetchrow_hashref()) { - my $sysname = $aps->{'sysname'}; - my $blade = $aps->{'blade'}; - my $port = $aps->{'port'}; - my $oid = "1.3.6.1.2.1.105.1.1.1.9.$blade.$port"; # POWER-ETHERNET-MIB...pethPsePortType - my $mode = $session->get($oid); - $qpoll->execute($mode, $aps->{'switch'}); - printf "%s (%s:%s/%s): %s\n", $sysname, $core, $blade, $port, $mode; - } - }; - if ($@) { - mylog("ERROR: $@ (during poll of $ip)"); - $dbh->rollback; - } - } - sleep 2; - } -} - -sub mylog { - my $msg = shift; - my $time = POSIX::ctime(time); - $time =~ s/\n.*$//; - printf STDERR "[%s] %s\n", $time, $msg; -} - - diff --git a/clients/ipv6-dns.pl b/clients/ipv6-dns.pl deleted file mode 100755 index dcb059d..0000000 --- a/clients/ipv6-dns.pl +++ /dev/null @@ -1,138 +0,0 @@ -#! /usr/bin/perl -use DBI; -use Net::DNS; -use Net::IP; -use lib '../include'; -use nms; -use strict; -use warnings; - -BEGIN { -require "../include/config.pm"; - eval { - require "../include/config.local.pm"; - }; -} - -my $dbh = nms::db_connect() or die "Can't connect to database"; -my $res = Net::DNS::Resolver->new; - -$res->nameservers("$nms::config::pri_hostname.$nms::config::tgname.gathering.org"); - -my $kname = 'DHCP_UPDATER'; - -sub get_reverse { - my ($ip) = shift; - $ip = new Net::IP($ip) or return 0; - my $a = $res->query($ip->reverse_ip, 'PTR') or return 0; - foreach my $n ($a->answer) { - return $n->{'ptrdname'}; # Return first element, ignore the rest (= - } - return 0; -} - -sub any_quad_a { - my ($host) = shift; - my $a = $res->query($host, 'AAAA') or return 0; - foreach my $n ($a->answer) { - return 1 if ($n->{'type'} eq 'AAAA'); - } - return 0; -} - -print "Running automagic IPv6 DNS updates\n"; -while (1) { - - # Fetch visible IPv6 addresses from the last three minutes - #my $q = $dbh->prepare("SELECT DISTINCT ON (ipv6.mac) ipv6.address AS v6, ipv6.mac, ipv4.address AS v4, ipv6.time - ipv6.age*'1 second'::interval FROM ipv6 LEFT JOIN ipv4 ON ipv4.mac = ipv6.mac WHERE ipv6.time > NOW()-'3 min'::interval ORDER BY ipv6.mac, ipv6.time DESC, mac") - my $q = $dbh->prepare( -"SELECT DISTINCT ON (v6) v6, ipv6.mac, ipv6.seen, v4 -FROM (SELECT DISTINCT ON (address) address AS v6, mac, seen FROM seen_mac WHERE family(address) = 6 AND seen > CURRENT_TIMESTAMP - '3 min'::interval) ipv6 -LEFT JOIN (SELECT DISTINCT ON (address) address AS v4, mac, seen FROM seen_mac WHERE family(address) = 4 AND seen > CURRENT_TIMESTAMP - '3 min'::interval) ipv4 ON ipv4.mac = ipv6.mac -ORDER BY v6, ipv6.seen DESC, mac") - or die "Can't prepare query"; - $q->execute() or die "Can't execute query"; - - my $aaaas = 0; - my $aaaa_errors = 0; - my $ptrs = 0; - my $ptr_errors = 0; - my $update; - my $v6; - while (my $ref = $q->fetchrow_hashref()) { - my $hostname = get_reverse($ref->{'v4'}); - if ($hostname) { - $v6 = $ref->{'v6'}; - my @parts = split('\.', $hostname); - my $hostname = shift @parts; - my $domain = join('.', @parts); - my $v6arpa = (new Net::IP($v6))->reverse_ip; - - # Don't add records for nets we don't control - next if not $v6arpa =~ m/$nms::config::ipv6zone/; - - # Add IPv6 reverse - if (!get_reverse($ref->{'v6'})) { - $update = Net::DNS::Update->new($nms::config::ipv6zone); - $update->push(pre => nxrrset("$v6arpa IN PTR")); # Only update if the RR is nonexistent - $update->push(update => rr_add("$v6arpa IN PTR $hostname.$domain.")); - $update->sign_tsig($kname, $nms::config::ddns_key); - my $reply = $res->send($update); - if ($reply->header->rcode eq 'NOERROR') { - $ptrs++; - } else { - $ptr_errors++; - } - } - - # Add IPv6 forward - if (!any_quad_a("$hostname.$domain")) { - $update = Net::DNS::Update->new($domain); - $update->push(pre => nxrrset("$hostname.$domain. IN AAAA $v6")); # Only update if the RR is nonexistent - $update->push(update => rr_add("$hostname.$domain. IN AAAA $v6")); - $update->sign_tsig($kname, $nms::config::ddns_key); - my $reply = $res->send($update); - if ($reply->header->rcode eq 'NOERROR') { - $aaaas++; - } else { - $aaaa_errors++; - } - } - } - } - print "Added $ptrs PTR records. $ptr_errors errors occured.\n"; - print "Added $aaaas AAAA records. $aaaa_errors errors occured.\n"; - - - # Remove old PTR records, that is, for hosts we haven't seen the last four - # hours, but not older than four and a half hours, as it would take forever to - # try to delete everything. FIXME: Query the zone file and diff against the - # database, to avoid running as many NS-updates as tuples in the result set. - - $q = $dbh->prepare("SELECT DISTINCT address AS v6 FROM seen_mac WHERE seen BETWEEN CURRENT_TIMESTAMP - '4 hours'::interval AND CURRENT_TIMESTAMP - '4 hours 30 minutes'::interval") - or die "Can't prepare query"; - $q->execute() or die "Can't execute query"; - - my $i = 0; - my $errors = 0; - while (my $ref = $q->fetchrow_hashref()) { - $v6 = $ref->{'v6'}; - if (get_reverse($v6)) { - my $v6arpa = (new Net::IP($v6))->reverse_ip; - my $update = Net::DNS::Update->new($nms::config::ipv6zone); - $update->push(pre => yxrrset("$v6arpa PTR")); # Only update if the RR exists - $update->push(update => rr_del("$v6arpa IN PTR")); - $update->sign_tsig($kname, $nms::config::ddns_key); - my $reply = $res->send($update); - if ($reply->header->rcode eq 'NOERROR') { - $i++; - } else { - $errors++; - } - } - } - - print "Deleted $i old PTR records. $errors errors occured.\n"; - print "Sleeping for two minutes.\n"; - sleep(120); -} diff --git a/clients/ipv6-stats.pl b/clients/ipv6-stats.pl deleted file mode 100644 index ab76cc9..0000000 --- a/clients/ipv6-stats.pl +++ /dev/null @@ -1,98 +0,0 @@ -#! /usr/bin/perl -use strict; -use warnings; -use lib '../include'; -use nms; -use Data::Validate::IP qw(is_public_ipv6 is_public_ipv4 is_private_ipv4); -use Net::MAC qw(mac_is_unicast); - -my $dbh = nms::db_connect(); -$dbh->{AutoCommit} = 0; - -while (1) { - my $coregws = $dbh->prepare("SELECT ip, community, sysname FROM switches WHERE switchtype <> 'dlink3100'") - or die "Can't prepare query: $!"; - $coregws->execute; - - my %seen = (); - my $num_v4 = 0; - my $num_v6 = 0; - while (my $ref = $coregws->fetchrow_hashref) { - print STDERR "Querying ".$ref->{'sysname'}." ...\n"; - my $snmp; - eval { - $snmp = nms::snmp_open_session($ref->{'ip'}, $ref->{'community'}); - }; - warn $@ if $@; - next if not $snmp; - - # Pull in old media table that does not support ipv6. - my $ip_phys_table = fetch($snmp, ('ipNetToMediaNetAddress', 'ipNetToMediaPhysAddress')); - for my $entry (values %$ip_phys_table) { - my $ip_addr = $entry->{'ipNetToMediaNetAddress'}; - my $mac = Net::MAC->new( - mac => nms::convert_mac($entry->{'ipNetToMediaPhysAddress'}), - die => 0, - ); - - next if $mac->get_base() != 16 || $mac->get_mac() eq ''; # We only support base 16 addresses - next if (!is_public_ipv4($ip_addr) && !is_private_ipv4($ip_addr)); # We consider RFC1918 public - - $seen{$ip_addr} = $mac->get_mac(); - $num_v4++; - } - - # Pull in new media table with IPv6 support - $ip_phys_table = $snmp->gettable('ipNetToPhysicalTable'); - for my $entry (values %$ip_phys_table) { - my $type = $entry->{'ipNetToPhysicalNetAddressType'}; - my $ip_addr = undef; - my $mac = Net::MAC->new( - mac => nms::convert_mac($entry->{'ipNetToPhysicalPhysAddress'}), - die => 0, - ); - - if ($type != 2) { - warn "$ip_addr is of unexpected type $type (should be 2)! Tell Berge.\n"; - next; - } - - $ip_addr = nms::convert_ipv6($entry->{'ipNetToPhysicalNetAddress'}); - - next if $mac->get_base() != 16 || $mac->get_mac() eq ''; # We only support base 16 addresses - next if not is_public_ipv6($ip_addr); - - $seen{$ip_addr} = $mac->get_mac(); - $num_v6++; - } - - } - - # Populate database - my $i = 0; - foreach my $ip_addr (keys %seen) { - $i++; - my $q = $dbh->do('INSERT INTO seen_mac (address, mac) VALUES (?, ?)', undef, $ip_addr, $seen{$ip_addr}) - or die "Can't execute query: $!"; - } - - $dbh->commit; - print "Collected $num_v6 IPv6 addresses and $num_v4 IPv4 addresses. $i unique addresses.\n"; - print "Sleeping for 60 seconds ...\n"; - sleep(60); -} - - -# Fetch provided fields from a single table returning {iid => {tag => val}} -sub fetch { - my $session = shift; - my @vars = map { new SNMP::Varbind([$_]) } @_; - my $data = {}; - foreach my $result (@{$session->bulkwalk(0, 8, new SNMP::VarList(@vars))}) { - foreach my $entry (@$result) { - $data->{$entry->iid}->{$entry->tag} = $entry->val; - } - } - return $data; -} - diff --git a/clients/snmpfetch.pl b/clients/snmpfetch.pl deleted file mode 100755 index a13ef3f..0000000 --- a/clients/snmpfetch.pl +++ /dev/null @@ -1,256 +0,0 @@ -#! /usr/bin/perl -use DBI; -use POSIX; -use Time::HiRes; -use Net::Telnet; -use strict; -use warnings; - -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 -my $instant = defined($ARGV[0]); - -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 -} - -our $qswitch = $dbh->prepare(<<"EOF") -SELECT - *, - DATE_TRUNC('second', now() - last_updated - poll_frequency) AS overdue -FROM - switches - NATURAL LEFT JOIN switchtypes -WHERE $qualification -ORDER BY - priority DESC, - overdue DESC -LIMIT 1 -FOR UPDATE OF switches -EOF - 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,operstatus,ifdescr) 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; - return if (!defined($sysname)); - $qswitch->execute('%'.$sysname.'%') - or die "Couldn't get switch"; - } else { - # Find a switch to grab - $qswitch->execute() - or die "Couldn't get switch"; - } - my $switch = $qswitch->fetchrow_hashref(); - - if (!defined($switch)) { - $dbh->commit; - - if ($instant) { - mylog("No such switch $sysname available, quitting."); - return; - } else { - mylog("No available switches in pool, sleeping."); - SNMP::MainLoop(1.0); - next; - } - } - - $qlock->execute($switch->{'switch'}) - or die "Couldn't lock switch"; - $dbh->commit; - - if ($switch->{'locked'}) { - mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock"); - } - - my $msg; - if (defined($switch->{'overdue'})) { - $msg = sprintf "Polling ports %s on %s (%s), %s overdue.", - $switch->{'ports'}, $switch->{'ip'}, $switch->{'sysname'}, $switch->{'overdue'}; - } else { - $msg = sprintf "Polling ports %s on %s (%s), never polled before.", - $switch->{'ports'}, $switch->{'ip'}, $switch->{'sysname'}; - } - mylog($msg); - - my $ip = $switch->{'ip'}; - if ($ip eq '127.0.0.1') { - mylog("Polling disabled for this switch, skipping."); - $qunlock->execute($switch->{'switch'}) - or die "Couldn't unlock switch"; - $dbh->commit; - next; - } - - my $community = $switch->{'community'}; - my $start = [Time::HiRes::gettimeofday]; - my $session; - eval { - $session = nms::snmp_open_session($ip, $community, 1); - }; - if ($@) { - 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, ["ifInOctets", $port]; - push @vars, ["ifOutOctets", $port]; - push @vars, ["ifInErrors", $port]; - push @vars, ["ifDescr", $port]; - push @vars, ["ifOutErrors", $port]; - push @vars, ["ifOperStatus", $port]; - my $varlist = SNMP::VarList->new(@vars); - $session->get($varlist, [ \&callback, $switch_status, $port ]); - } - $running++; - - $dbh->rollback; - } -} - -sub expand_ports { - my $in = shift; - my @ranges = split /,/, $in; - my @ret = (); - - for my $range (@ranges) { - if ($range =~ /^\d+$/) { - push @ret, $range; - } elsif ($range =~ /^(\d+)-(\d+)$/) { - for my $i ($1..$2) { - push @ret, $i; - } - } else { - die "Couldn't understand '$range' in ports"; - } - } - - return (sort { $a <=> $b } @ret); -} - -sub mylog { - my $msg = shift; - my $time = POSIX::ctime(time); - $time =~ s/\n.*$//; - printf STDERR "[%s] %s\n", $time, $msg; -} - -sub callback { - my ($switch, $port, $vars) = @_; - - my ($in, $out, $ine, $oute) = (undef, undef, undef, undef); - my $operstatus = 2; - my $ifdescr = 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 'ifInOctets') { - $in = $var->[2]; - } elsif ($var->[0] eq 'ifOutOctets') { - $out = $var->[2]; - } elsif ($var->[0] eq 'ifInErrors') { - $ine = $var->[2]; - } elsif ($var->[0] eq 'ifOutErrors') { - $oute = $var->[2]; - } elsif ($var->[0] eq 'ifOperStatus') { - $operstatus = $var->[2]; - } elsif ($var->[0] eq 'ifDescr') { - $ifdescr = $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 ($ine)" . (defined($in) ? ": $in" : ""); - } - $ok = 0; - } - if (!defined($out) || $out !~ /^\d+$/) { - if (defined($oute)) { - warn $switch->{'sysname'}.":$port: failed reading out ($oute)" . (defined($out) ? ": $out" : ""); - } - $ok = 0; - } - if (!defined($ifdescr)) { - warn $switch->{'sysname'}.":$port: failed reading ifdescr"; - $ok = 0; - } elsif ($ifdescr =~ m/\./) { - # Skip virtual ports - $ok = 0; - } - - if ($ok) { - $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute,$operstatus,$ifdescr) || die "%s:%s: %s\n", $switch->{'switch'}, $port, $in; - $dbh->commit; - } else { - warn $switch->{'sysname'} . " failed to OK."; - } - - 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 deleted file mode 100755 index 8d56251..0000000 --- a/clients/update-public-nms.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -YEAR=15 -TGMANAGE=/root/tgmanage -DIR=/srv/www/nms-public.tg${YEAR}.gathering.org -set -x -mkdir -p $DIR - -wget -qO$DIR/nettkart-dhcp.png.new http://nms.tg${YEAR}.gathering.org/dhcpkart.pl -wget -qO$DIR/led.txt.new http://nms.tg${YEAR}.gathering.org/led.pl -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 deleted file mode 100755 index 7fd17df..0000000 --- a/clients/update-public-speedometer.pl +++ /dev/null @@ -1,37 +0,0 @@ -#! /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 |