diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-03-17 19:51:46 +0000 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-03-17 19:51:46 +0000 |
commit | 5c9d6d10dc7e53a8d06fbd056ded591a6b583167 (patch) | |
tree | 6f6290fb1b8bcc859278a2c68573a35547173931 /web/nms.gathering.org/api/private | |
parent | 76a3aad5090d304ec0f76f28e50afe1f53b7b96c (diff) | |
parent | 025f01e7e8c5bfea21b559fbac650f90c2774a29 (diff) |
Merge branch 'master' of https://github.com/tech-server/tgmanage
Diffstat (limited to 'web/nms.gathering.org/api/private')
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-add | 1 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-change | 10 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/port-state | 33 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/snmp | 29 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-add | 72 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switches-management | 2 |
6 files changed, 91 insertions, 56 deletions
diff --git a/web/nms.gathering.org/api/private/comment-add b/web/nms.gathering.org/api/private/comment-add index 2f8b0b7..26ff734 100755 --- a/web/nms.gathering.org/api/private/comment-add +++ b/web/nms.gathering.org/api/private/comment-add @@ -20,4 +20,5 @@ $nms::web::cc{'max-age'} = '0'; $nms::web::cc{'stale-while-revalidate'} = '0'; $nms::web::json{'state'} = 'ok'; +print "X-ban: /api/private/comments\n"; finalize_output(); diff --git a/web/nms.gathering.org/api/private/comment-change b/web/nms.gathering.org/api/private/comment-change index ccf336d..fb7da54 100755 --- a/web/nms.gathering.org/api/private/comment-change +++ b/web/nms.gathering.org/api/private/comment-change @@ -3,12 +3,16 @@ use lib '../../../../include'; use utf8; use nms; -use nms::web; +use nms::web qw($dbh db_safe_quote get_input finalize_output); + use strict; use warnings; -my $id = db_safe_quote('comment'); -my $state = db_safe_quote('state'); +my $in = get_input(); +my %tmp = %{JSON::XS::decode_json($in)}; + +my $id = $dbh->quote($tmp{'comment'}); +my $state = $dbh->quote($tmp{'state'}); my $q = $nms::web::dbh->prepare("UPDATE switch_comments SET state = " . $state . " WHERE id = " . $id . ";"); $q->execute(); diff --git a/web/nms.gathering.org/api/private/port-state b/web/nms.gathering.org/api/private/port-state deleted file mode 100755 index 8c6b64c..0000000 --- a/web/nms.gathering.org/api/private/port-state +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 - -use lib '../../../../include'; -use nms::web; -use strict; -use warnings; - -my $query = 'select sysname,extract(epoch from date_trunc(\'second\',time)) as time, ifname,ifhighspeed,ifhcinoctets,ifhcoutoctets from polls natural join switches where time in (select max(time) from polls where ' . $nms::web::when . ' group by switch,ifname);'; -my $q = $nms::web::dbh->prepare($query); -$q->execute(); - -while (my $ref = $q->fetchrow_hashref()) { - my @fields = ('ifhighspeed','ifhcoutoctets','ifhcinoctets'); - foreach my $val (@fields) { - $nms::web::json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{$val} = $ref->{$val}; - } - $nms::web::json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{'time'} = $ref->{'time'}; -} - -my $q3 = $nms::web::dbh->prepare('select distinct on (switch) switch,temp,time,sysname from switch_temp natural join switches where ' . $nms::web::when . ' order by switch,time desc'); - - -$q3->execute(); -while (my $ref = $q3->fetchrow_hashref()) { - my $sysname = $ref->{'sysname'}; - $nms::web::json{'switches'}{$ref->{'sysname'}}{'temp'} = $ref->{'temp'}; - $nms::web::json{'switches'}{$ref->{'sysname'}}{'temp_time'} = $ref->{'time'}; -} -$nms::web::cc{'max-age'} = '2'; -$nms::web::cc{'stale-while-revalidate'} = '120'; - -finalize_output(); diff --git a/web/nms.gathering.org/api/private/snmp b/web/nms.gathering.org/api/private/snmp new file mode 100755 index 0000000..4779659 --- /dev/null +++ b/web/nms.gathering.org/api/private/snmp @@ -0,0 +1,29 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use CGI qw(fatalsToBrowser); +use DBI; +use lib '../../../../include'; +use nms; +use nms::web; +use strict; +use warnings; +use JSON; +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);'); + +$q->execute(); +while (my $ref = $q->fetchrow_hashref()) { + my $sysname = $ref->{'sysname'}; + + # This is, strictly speaking, redundant. But by doing this, we can + # re-use the standard methods of finalize_output() and whatnot. + my $data = JSON::XS::decode_json($ref->{'data'}); + + $nms::web::json{'snmp'}{$ref->{'sysname'}} = $data; +} + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 826b65b..979a1d8 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -22,7 +22,23 @@ 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', 'placement'); +my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement'); + +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}; @@ -39,38 +55,55 @@ foreach my $tmp2 (@tmp) { } 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'}; + } + 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{$_}); + $template{$_} = $dbh->quote($switch{$_}); } } keys %switch; - if (not defined($switch{'placement'})) { - %{$switch{'placement'}} = guess_placement($switch{'sysname'}); - } - my ($x1,$x2,$y1,$y2); - $x1 = $switch{'placement'}{'x1'}; - $y1 = $switch{'placement'}{'y1'}; - $x2 = $switch{'placement'}{'xx'}; - $y2 = $switch{'placement'}{'yy'}; - my $place = "(($x1,$y1),($x2,$y2))"; - $template{'placement'} = $dbh->quote($place); $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 = $switch{'placement'}{'x1'}; - $y1 = $switch{'placement'}{'y1'}; - $x2 = $switch{'placement'}{'xx'}; - $y2 = $switch{'placement'}{'yy'}; - my $place = "(($x1,$y1),($x2,$y2))"; - $switch{'placement'} = $place; + $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'}; } my @set; map { - push @set, "$_=" . $dbh->quote($switch{$_}); + 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'}; @@ -79,4 +112,5 @@ foreach my $tmp2 (@tmp) { $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/switches-management b/web/nms.gathering.org/api/private/switches-management index 474f674..543d08d 100755 --- a/web/nms.gathering.org/api/private/switches-management +++ b/web/nms.gathering.org/api/private/switches-management @@ -12,7 +12,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "60"; -my $q2 = $nms::web::dbh->prepare('select sysname,ip,switchtype,poll_frequency,community,last_updated from switches '); +my $q2 = $nms::web::dbh->prepare('select sysname,ip,poll_frequency,community,subnet4,subnet6,last_updated from switches '); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { |