diff options
author | Joachim Tingvold <joachim@tingvold.com> | 2016-03-25 15:23:08 +0100 |
---|---|---|
committer | Joachim Tingvold <joachim@tingvold.com> | 2016-03-25 15:23:08 +0100 |
commit | 519bae0f07f38bd82257c61a924085f64ad360cc (patch) | |
tree | d249ad5957cc3698db40df972c5a60d37679abea /examples | |
parent | 14dbbc1451b4d5ea99e1937020e4fc79fe7ba1a7 (diff) |
Moved old, unused files.
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/historical/clients/build-linknets.pl | 128 | ||||
-rwxr-xr-x | examples/historical/clients/flatify.pl | 21 | ||||
-rw-r--r-- | examples/historical/clients/snmp.sql | 25 | ||||
-rwxr-xr-x | examples/historical/clients/ssendfile.pl | 50 | ||||
-rwxr-xr-x | examples/historical/tools/make-accesspoints.pl | 24 | ||||
-rwxr-xr-x | examples/historical/tools/make-switches.pl | 36 |
6 files changed, 284 insertions, 0 deletions
diff --git a/examples/historical/clients/build-linknets.pl b/examples/historical/clients/build-linknets.pl new file mode 100755 index 0000000..015e006 --- /dev/null +++ b/examples/historical/clients/build-linknets.pl @@ -0,0 +1,128 @@ +#! /usr/bin/perl + +# sesse testing + +use strict; +use warnings; +use lib '../include'; +use nms; +use Net::CIDR; + +my $dbh = nms::db_connect(); + +my $coregws = $dbh->prepare("SELECT switch, ip, community, sysname FROM switches WHERE sysname NOT LIKE 'e%' and sysname NOT LIKE 'sw%creativia%' AND sysname NOT LIKE 'sw%crew'") + or die "Can't prepare query: $!"; +$coregws->execute; + +my %switch_id = (); # sysname -> switch database ID +my %loopbacks = (); # sysname -> primary address +my %loop_ipv6 = (); # sysname -> primary address +my %map = (); # CIDR -> (sysname,ip)* +my %lldpneigh = (); # sysname -> sysname -> 1 + +while (my $ref = $coregws->fetchrow_hashref) { + my $sysname = $ref->{'sysname'}; + $switch_id{$sysname} = $ref->{'switch'}; + + print "$sysname...\n"; + my $snmp; + eval { + $snmp = nms::snmp_open_session($ref->{'ip'}, $ref->{'community'}); + }; + warn $@ if $@; + next if not $snmp; + + my $routes = $snmp->gettable('ipCidrRouteTable'); + my $ifs = $snmp->gettable('ifTable'); + my $addrs = $snmp->gettable('ipAddrTable'); + my $lldp = $snmp->gettable('lldpRemTable'); + my $ipaddresstable = $snmp->gettable('ipAddressTable'); + + # Find all direct routes we have, and that we also have an address in. + # These are our linknet candidates. + for my $route (values %$routes) { + next if ($route->{'ipCidrRouteMask'} eq '255.255.255.255'); + next if ($route->{'ipCidrRouteNextHop'} ne '0.0.0.0'); + my $cidr = Net::CIDR::addrandmask2cidr($route->{'ipCidrRouteDest'}, $route->{'ipCidrRouteMask'}); + + for my $addr (values %$addrs) { + my $ip = $addr->{'ipAdEntAddr'}; + if (Net::CIDR::cidrlookup($ip, $cidr)) { + push @{$map{$cidr}}, [ $sysname, $ip ]; + } + } + } + + # Find the first loopback address. + my %loopbacks_this_switch = (); + for my $addr (values %$addrs) { + my $ifdescr = $ifs->{$addr->{'ipAdEntIfIndex'}}->{'ifDescr'}; + next unless $ifdescr =~ /^Loop/; + $loopbacks_this_switch{$ifdescr} = $addr->{'ipAdEntAddr'}; + } + for my $if (sort keys %loopbacks_this_switch) { + $loopbacks{$sysname} = $loopbacks_this_switch{$if}; + last; + } + + my %loopbacks_ipv6_this_switch = (); + for my $addr (values %$ipaddresstable) { + next if not $addr->{'ipAddressAddrType'} == 2; # Only IPv6 addresses please. + my $ifdescr = $ifs->{$addr->{'ipAddressIfIndex'}}->{'ifDescr'}; + next unless $ifdescr =~ /^Loop/; + $loopbacks_ipv6_this_switch{$ifdescr} = nms::convert_ipv6( $addr->{'ipAddressAddr'} ); + } + for my $if (sort keys %loopbacks_ipv6_this_switch) { + $loop_ipv6{$sysname} = $loopbacks_ipv6_this_switch{$if}; + last; + } + + # Find all LLDP neighbors. + for my $neigh (values %$lldp) { + $lldpneigh{lc($sysname)}{lc($neigh->{'lldpRemSysName'})} = 1; + } +} + +# print Dumper(\%switch_id); +# print Dumper(\%map); +# print Dumper(\%loopbacks); +# print Dumper(\%lldpneigh); + +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +# Update the switches we have loopback addresses fora +while (my ($sysname, $ip) = each %loopbacks) { + $dbh->do('UPDATE switches SET ip=? WHERE sysname=?', + undef, $ip, $sysname); +} +while (my ($sysname, $ipv6) = each %loop_ipv6) { + $dbh->do('UPDATE switches SET secondary_ip=? WHERE sysname=?', + undef, $ipv6, $sysname); +} + +# 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'); +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{lc($device_a->[0])}{lc($device_b->[0])}); + + my $switch_a = $switch_id{$device_a->[0]}; + my $switch_b = $switch_id{$device_b->[0]}; + my $ref = $dbh->selectrow_hashref($qexist, undef, $switch_a, $switch_b); + next if ($ref->{'cnt'} != 0); + + $dbh->do('INSERT INTO linknets (switch1, addr1, switch2, addr2) VALUES (?,?,?,?)', + undef, + $switch_a, $device_a->[1], + $switch_b, $device_b->[1]); + } + } +} +$dbh->commit; diff --git a/examples/historical/clients/flatify.pl b/examples/historical/clients/flatify.pl new file mode 100755 index 0000000..f2aa18a --- /dev/null +++ b/examples/historical/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/examples/historical/clients/snmp.sql b/examples/historical/clients/snmp.sql new file mode 100644 index 0000000..7e09313 --- /dev/null +++ b/examples/historical/clients/snmp.sql @@ -0,0 +1,25 @@ +create table switchtypes ( + switchtype varchar not null primary key, + ports varchar not null +); + +create table switches ( + switch serial not null primary key, + ip inet not null, + secondary_ip inet, + sysname varchar not null, + switchtype varchar not null references switchtypes, + last_updated timestamp, + locked boolean not null default 'f' +); + +create table poll ( + time timestamp not null, + switch integer not null references switches, + port integer not null, + bytes_in bigint not null, + bytes_out bigint not null, + + primary key ( time, switch, port ) +); +create index poll_switch_port on poll ( switch, port ); diff --git a/examples/historical/clients/ssendfile.pl b/examples/historical/clients/ssendfile.pl new file mode 100755 index 0000000..224f4e2 --- /dev/null +++ b/examples/historical/clients/ssendfile.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use warnings; +use strict; +use lib '../include'; +use POSIX; + +my $delaytime = 30; +my $poll_frequency = 60; + +sub mylog { + my $msg = shift; + my $time = POSIX::ctime(time); + $time =~ s/\n.*$//; + printf STDERR "[%s] %s\n", $time, $msg; +} + +if ($#ARGV != 1) { + die("Error in arguments passed\n". + "./ssendfile.pl addr configfile\n"); +} + +my $ssh = nms::switch_connect_ssh($ARGV[0]); +my $conn = $ssh->{telnet}; +if (!defined($conn)) { + die("Could not connect to switch.\n"); +} + +open(CONFIG, $ARGV[1]); +while (<CONFIG>) { + my $cmd = $_; + $cmd =~ s/[\r\n]+//g; + print "Executing: `$cmd`\n"; +# if ($cmd =~ /ip ifconfig swif0 (\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3})/) { +# print "New ip: $1\n"; +# $conn->cmd( String => $cmd, +# Timeout => 3); +# $ssh = nms::switch_connect_ssh($1); +# $conn = $ssh->{telnet}; +# if (!defined($conn)) { +# die "Could not connect to new ip: $1\n"; +# } +# } +# else { + my @data = nms::switch_exec($cmd, $conn); + foreach my $line (@data) { + $line =~ s/[\r\n]+//g; + print "$line\n"; + } +# } +} diff --git a/examples/historical/tools/make-accesspoints.pl b/examples/historical/tools/make-accesspoints.pl new file mode 100755 index 0000000..b84321a --- /dev/null +++ b/examples/historical/tools/make-accesspoints.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl +use strict; +use warnings; + +while (<>) { + my @arr = split " "; + my $ap = 'ap-'.$arr[0]; + my $core = $arr[1]; + # Trekk fra 1 + $core =~ s/^(distro)(\d+)$/$1.($2-1)/e; + + # Fjerde kabel er aksesspunkt + my $blade; + my $port; + if ($arr[5] =~ /^Gi(\d+)\/(\d+)$/) { + $blade = $1; + $port = $2; + } else { + die "Unknown port: ".$arr[5]; + } + printf "INSERT INTO switches(ip, sysname, switchtype) values(inet '127.0.0.1', '%s', 'ciscoap');\n", $ap; + printf "INSERT INTO uplinks SELECT (SELECT switch FROM switches WHERE sysname = '%s') AS switch, (SELECT switch FROM switches WHERE sysname = '%s') AS coreswitch, %d AS blade, %d AS port;\n", $ap, $core, $blade, $port; + printf "INSERT INTO ap_poll(switch) SELECT switch FROM switches WHERE sysname = '%s';\n", $ap; +} diff --git a/examples/historical/tools/make-switches.pl b/examples/historical/tools/make-switches.pl new file mode 100755 index 0000000..c0c842a --- /dev/null +++ b/examples/historical/tools/make-switches.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +my $switchtype = "ex2200"; + +print "begin;\n"; +print "delete from temppoll;\n"; +print "delete from dhcp;\n"; +print "delete from switches where switchtype = '$switchtype';\n"; +#print "SELECT pg_catalog.setval('switches_switch_seq', 1, false);\n"; +print "SELECT pg_catalog.setval('polls_poll_seq', 1, false);\n"; + +my %ip; +my $i = 1; +while (<STDIN>) { + chomp; + my @info = split(/ /); + + if (scalar @info < 5) { + die "Unknown line: $_"; + } + + my $name = $info[0]; + my $range = $info[1]; + my $ip = $info[3]; + $ip =~ s/\/.*$//; + + + print "insert into switches (ip, sysname, switchtype) values ('$ip', '$name', '$switchtype');\n"; + print "insert into dhcp select switch, '$range' from switches where sysname = '$name';\n"; +} +close HOSTS; + +print "end;\n"; |