diff options
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-add | 32 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-update | 122 | ||||
-rw-r--r-- | web/nms.gathering.org/index.html | 3 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-info-box.js | 45 |
4 files changed, 169 insertions, 33 deletions
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(); diff --git a/web/nms.gathering.org/index.html b/web/nms.gathering.org/index.html index e70c9e3..a20ae67 100644 --- a/web/nms.gathering.org/index.html +++ b/web/nms.gathering.org/index.html @@ -74,6 +74,7 @@ <li class="dropdown-header">Move switches</li> <li><a href="#" onclick="nmsMap.moveSet(true);">Enable switch moving</a></li> <li><a href="#" onclick="nmsMap.moveSet(false);">Disable switch moving</a></li> + <li><a href="#" onclick="nmsInfoBox._createShow();">Add switch</a></li> <li class="divider"> </li> <li class="dropdown-header">Help</li> <li><a href="#" onclick="toggleLayer('aboutKeybindings');" >Keyboard Shortcuts</a></li> @@ -104,7 +105,7 @@ </div> </nav> - <div class="container-fluid"> + <div class="container-fluid" id="wrap"> <div class="row-fluid"> <div class="span12"> diff --git a/web/nms.gathering.org/js/nms-info-box.js b/web/nms.gathering.org/js/nms-info-box.js index 0175b6f..7e27ce7 100644 --- a/web/nms.gathering.org/js/nms-info-box.js +++ b/web/nms.gathering.org/js/nms-info-box.js @@ -65,6 +65,7 @@ nmsInfoBox._hide = function() nmsInfoBox._showing = ""; nmsInfoBox._editHide(); nmsInfoBox._snmpHide(); + nmsInfoBox._createHide(); } /* @@ -388,7 +389,7 @@ nmsInfoBox._editSave = function(sw, e) { var myData = nmsInfoBox._editStringify(sw); $.ajax({ type: "POST", - url: "/api/private/switch-add", + url: "/api/private/switch-update", dataType: "text", data:myData, success: function (data, textStatus, jqXHR) { @@ -398,3 +399,45 @@ nmsInfoBox._editSave = function(sw, e) { }); nmsInfoBox._editHide(); } + + +/* + * Display infobox for new switch + * + * TODO: Integrate and rebuild info-box display logic + */ +nmsInfoBox._createShow = function() +{ + var container = document.createElement("div"); + container.className = "col-md-5"; + container.id = "nmsInfoBox-create"; + container.style.zIndex = "999"; + + var swtop = document.getElementById("wrap"); + nmsInfoBox._hide(); + + container.innerHTML = '<div class="panel panel-default"> <div class="panel-heading">Add new switch <button type="button" class="close" aria-label="Close" onclick="nmsInfoBox._createHide();" style="float: right;">X</button></div> <div class="panel-body"><input type="text" class="form-control" id="create-sysname" placeholder="Sysname id"><button class="btn btn-default" onclick="nmsInfoBox._createSave(document.getElementById(\'create-sysname\').value);">Add switch</button></div><div id="create-switch-feedback"></div> </div>'; + + swtop.appendChild(container); +} +nmsInfoBox._createSave = function(sw) { + var feedback = document.getElementById("create-switch-feedback"); + var myData = JSON.stringify([{'sysname':sw}]); + $.ajax({ + type: "POST", + url: "/api/private/switch-add", + dataType: "text", + data:myData, + success: function (data, textStatus, jqXHR) { + var result = JSON.parse(data); + if(result.switches_addded.length > 0) { + nmsInfoBox._createHide(); + } + } + }); +} +nmsInfoBox._createHide = function() { + var container = document.getElementById("nmsInfoBox-create"); + if (container != undefined) + container.parentNode.removeChild(container); +} |