From cc2534ef349680951afb0d0dfe7797843d10cccf Mon Sep 17 00:00:00 2001 From: Nicolai Tellefsen Date: Sat, 19 Mar 2016 19:54:14 +0100 Subject: NMS: Add basic add-switch interface --- web/nms.gathering.org/api/private/switch-add | 32 +------ web/nms.gathering.org/api/private/switch-update | 122 ++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 31 deletions(-) create mode 100755 web/nms.gathering.org/api/private/switch-update (limited to 'web/nms.gathering.org/api/private') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 3d7b119..70d6212 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -83,40 +83,10 @@ foreach my $tmp2 (@tmp) { $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'});"); push @added, $switch{'sysname'}; - } else { - if (defined($switch{'placement'})) { - my %placement; - if ($switch{'placement'} eq "reset") { - %placement = guess_placement($switch{'sysname'}); - } else { - %placement = %{convertplace($switch{'placement'})}; - } - my ($x1,$x2,$y1,$y2); - $x1 = $placement{'x1'}; - $y1 = $placement{'y1'}; - $x2 = $placement{'xx'}; - $y2 = $placement{'yy'}; - $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; - push @dups, "not really, but: " . $switch{'placement'}; - } - if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { - $switch{'ip'} = $switch{'mgtmt4'}; - } - if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { - $switch{'secondary_ip'} = $switch{'mgtmt6'}; - } - my @set; - map { - if (defined($template{$_})) { - push @set, "$_=" . $dbh->quote($switch{$_}); - } - } keys %switch; - $nms::web::dbh->do("UPDATE SWITCHES SET " . join(", ", @set) . "WHERE sysname=" . $dbh->quote($switch{'sysname'}) . ";"); - push @dups, $switch{'sysname'}; } } + $json{'switches_addded'} = \@added; -$json{'switches_updated'} = \@dups; print "X-ban: /api/.*switches.*\n"; finalize_output(); diff --git a/web/nms.gathering.org/api/private/switch-update b/web/nms.gathering.org/api/private/switch-update new file mode 100755 index 0000000..3d7b119 --- /dev/null +++ b/web/nms.gathering.org/api/private/switch-update @@ -0,0 +1,122 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +#use CGI qw(fatalsToBrowser); +use DBI; +use lib '../../../../include'; +use nms; +use nms::web qw(%get_params %json finalize_output get_input $dbh); +use nms::util qw(guess_placement); +use strict; +use warnings; +use JSON; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "0"; + +my $in = get_input(); +my @tmp = @{JSON::XS::decode_json($in)}; + +my @added; +my @dups; + +my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); + +my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement', 'distro', 'secondary_ip'); + +sub convertplace +{ + my %in = %{$_[0]}; + my %out = (); + + if (not defined $in{'x1'} and defined($in{'x'})) { + $out{'x1'} = int($in{'x'}); + $out{'y1'} = int($in{'y'}); + $out{'xx'} = int($in{'x'} + $in{'width'}); + $out{'yy'} = int($in{'y'} + $in{'height'}); + } else { + return \%in; + } + return \%out; +} + +foreach my $tmp2 (@tmp) { + my %switch = %{$tmp2}; + my $affected = 0; + my %template = (); + map { $template{$_} = 'DEFAULT' } @fields; + if (not defined($switch{'sysname'})) { + next; + } + + $sth->execute( $switch{'sysname'}); + while ( my @row = $sth->fetchrow_array ) { + $affected += 1; + } + + if ($affected == 0) { + my %placement; + if (not defined ($switch{'placement'})) { + %placement = guess_placement($switch{'sysname'}); + } else { + %placement = %{convertplace($switch{'placement'})}; + } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; + } + if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { + $switch{'secondary_ip'} = $switch{'mgtmt6'}; + } + my ($x1,$x2,$y1,$y2); + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; + + map { + if (defined ($template{$_})) { + $template{$_} = $dbh->quote($switch{$_}); + } + } keys %switch; + + + $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'});"); + push @added, $switch{'sysname'}; + } else { + if (defined($switch{'placement'})) { + my %placement; + if ($switch{'placement'} eq "reset") { + %placement = guess_placement($switch{'sysname'}); + } else { + %placement = %{convertplace($switch{'placement'})}; + } + my ($x1,$x2,$y1,$y2); + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; + push @dups, "not really, but: " . $switch{'placement'}; + } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; + } + if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { + $switch{'secondary_ip'} = $switch{'mgtmt6'}; + } + my @set; + map { + if (defined($template{$_})) { + push @set, "$_=" . $dbh->quote($switch{$_}); + } + } keys %switch; + $nms::web::dbh->do("UPDATE SWITCHES SET " . join(", ", @set) . "WHERE sysname=" . $dbh->quote($switch{'sysname'}) . ";"); + push @dups, $switch{'sysname'}; + } +} +$json{'switches_addded'} = \@added; +$json{'switches_updated'} = \@dups; + +print "X-ban: /api/.*switches.*\n"; +finalize_output(); -- cgit v1.2.3 From b5b57c04f96b3ddedf8b94dc2188e9d421b53cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Kir=C3=B8?= Date: Sun, 20 Mar 2016 20:33:02 +0100 Subject: line breakup in snmp --- web/nms.gathering.org/api/private/snmp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'web/nms.gathering.org/api/private') diff --git a/web/nms.gathering.org/api/private/snmp b/web/nms.gathering.org/api/private/snmp index 4779659..f69ca62 100755 --- a/web/nms.gathering.org/api/private/snmp +++ b/web/nms.gathering.org/api/private/snmp @@ -13,7 +13,8 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "10"; -my $q = $nms::web::dbh->prepare('select sysname,data from snmp natural join switches where id in (select max(id) from snmp where ' . $nms::web::when . 'group by switch);'); +my $q = $nms::web::dbh->prepare('select sysname,data from snmp natural join switches where id in (select max(id) from snmp where ' + . $nms::web::when . 'group by switch);'); $q->execute(); while (my $ref = $q->fetchrow_hashref()) { -- cgit v1.2.3 From 11fc373807b9a14ee14499165b00f5e775d11a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Kir=C3=B8?= Date: Sun, 20 Mar 2016 21:49:15 +0100 Subject: which switch an I on script file. --- web/nms.gathering.org/api/private/which-switch-am-i-on | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 web/nms.gathering.org/api/private/which-switch-am-i-on (limited to 'web/nms.gathering.org/api/private') diff --git a/web/nms.gathering.org/api/private/which-switch-am-i-on b/web/nms.gathering.org/api/private/which-switch-am-i-on new file mode 100755 index 0000000..4adfb2b --- /dev/null +++ b/web/nms.gathering.org/api/private/which-switch-am-i-on @@ -0,0 +1,15 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use lib '../../../../include'; +use nms::web; +use strict; +use warnings; + +my $query = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,state,username,id,comment from switch_comments natural join switches where state != \'delete\' order by time desc'); +$query->execute(); +while (my $ref = $query->fetchrow_hashref()) { + push @{$nms::web::json{'comments'}{$ref->{'sysname'}}{'comments'}},$ref; +} + +nms::web::finalize_output(); -- cgit v1.2.3 From 6fa911c8565a54c9b56cfae479a92da094990876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Kir=C3=B8?= Date: Sun, 20 Mar 2016 23:53:46 +0100 Subject: which switch: getting IP and printing it. Need to style with html. --- .../api/private/which-switch-am-i-on | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'web/nms.gathering.org/api/private') diff --git a/web/nms.gathering.org/api/private/which-switch-am-i-on b/web/nms.gathering.org/api/private/which-switch-am-i-on index 4adfb2b..82436ea 100755 --- a/web/nms.gathering.org/api/private/which-switch-am-i-on +++ b/web/nms.gathering.org/api/private/which-switch-am-i-on @@ -5,11 +5,26 @@ use lib '../../../../include'; use nms::web; use strict; use warnings; +use Data::Dumper; -my $query = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,state,username,id,comment from switch_comments natural join switches where state != \'delete\' order by time desc'); +#my $query = $nms::web::dbh->prepare("select * from switches where '185.110.150.7' << subnet4"); +my $query = $nms::web::dbh->prepare("select * from switches where '88.92.5.67' << subnet4"); $query->execute(); -while (my $ref = $query->fetchrow_hashref()) { - push @{$nms::web::json{'comments'}{$ref->{'sysname'}}{'comments'}},$ref; + +print "Cache-Control: max-age=0"; +print "Content-Type: text/html"; +print "\n\n"; + +# get user ip from somewhere. +# HTTP_X_FORWARDED_FOR is set by varnish. When using varnish, the REMOTE_ADDR will always be localhost. +my $addr = $ENV{HTTP_X_FORWARDED_FOR} // $ENV{REMOTE_ADDR}; +print "-:" . $addr ."\n"; + + +# add start html: header + body etc. + +while ( my $ref = $query->fetchrow_hashref() ) { + print $ref->{sysname}. " @ " . $ref->{distro}; } -nms::web::finalize_output(); +# add end html -- cgit v1.2.3 From 4dffc5e5431c83595f2bd102f120ad6def011d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Kir=C3=B8?= Date: Mon, 21 Mar 2016 10:33:25 +0100 Subject: which switch html finished. --- web/nms.gathering.org/api/private/which-switch-am-i-on | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'web/nms.gathering.org/api/private') diff --git a/web/nms.gathering.org/api/private/which-switch-am-i-on b/web/nms.gathering.org/api/private/which-switch-am-i-on index 82436ea..0d3f2b4 100755 --- a/web/nms.gathering.org/api/private/which-switch-am-i-on +++ b/web/nms.gathering.org/api/private/which-switch-am-i-on @@ -18,13 +18,23 @@ print "\n\n"; # get user ip from somewhere. # HTTP_X_FORWARDED_FOR is set by varnish. When using varnish, the REMOTE_ADDR will always be localhost. my $addr = $ENV{HTTP_X_FORWARDED_FOR} // $ENV{REMOTE_ADDR}; -print "-:" . $addr ."\n"; - # add start html: header + body etc. +print " + + +"; + +# print address +print "" . $addr ."
"; +# print switch name and distroname. while ( my $ref = $query->fetchrow_hashref() ) { print $ref->{sysname}. " @ " . $ref->{distro}; } # add end html +print " + + +"; -- cgit v1.2.3