aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Lyngstol <kly@kly.no>2016-03-10 19:15:17 +0000
committerKristian Lyngstol <kly@kly.no>2016-03-10 19:15:17 +0000
commit820287be1769b442f101a712a0f008b739f50699 (patch)
treeb8e8311257051ae4ddedbce3c755bc59bfb77c33
parentb8314f8319f00c9bb507d67f11ab8e75d1ca659f (diff)
NMS: Tweak caching and updates
-rwxr-xr-xweb/nms.gathering.org/api/private/port-state2
-rwxr-xr-xweb/nms.gathering.org/api/private/switch-add33
-rwxr-xr-xweb/nms.gathering.org/api/public/ping2
-rwxr-xr-xweb/nms.gathering.org/api/public/switch-state2
-rwxr-xr-xweb/nms.gathering.org/api/public/switches2
5 files changed, 34 insertions, 7 deletions
diff --git a/web/nms.gathering.org/api/private/port-state b/web/nms.gathering.org/api/private/port-state
index 1f30181..8c6b64c 100755
--- a/web/nms.gathering.org/api/private/port-state
+++ b/web/nms.gathering.org/api/private/port-state
@@ -27,5 +27,7 @@ while (my $ref = $q3->fetchrow_hashref()) {
$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/switch-add b/web/nms.gathering.org/api/private/switch-add
index af8db38..826b65b 100755
--- a/web/nms.gathering.org/api/private/switch-add
+++ b/web/nms.gathering.org/api/private/switch-add
@@ -6,6 +6,7 @@ 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;
@@ -21,13 +22,16 @@ 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');
+my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'placement');
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 ) {
@@ -40,6 +44,21 @@ foreach my $tmp2 (@tmp) {
$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 ($x1,$x2,$y1,$y2);
$x1 = $switch{'placement'}{'x1'};
@@ -47,15 +66,17 @@ foreach my $tmp2 (@tmp) {
$x2 = $switch{'placement'}{'xx'};
$y2 = $switch{'placement'}{'yy'};
my $place = "(($x1,$y1),($x2,$y2))";
- $template{'placement'} = $place;
+ $switch{'placement'} = $place;
}
- $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'});");
- push @added, $switch{'sysname'};
- } else {
+ my @set;
+ map {
+ 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_duplicate'} = \@dups;
+$json{'switches_updated'} = \@dups;
finalize_output();
diff --git a/web/nms.gathering.org/api/public/ping b/web/nms.gathering.org/api/public/ping
index f713df1..f13a03b 100755
--- a/web/nms.gathering.org/api/public/ping
+++ b/web/nms.gathering.org/api/public/ping
@@ -22,4 +22,6 @@ while (my $ref = $lq->fetchrow_hashref()) {
$q->execute();
+$nms::web::cc{'max-age'} = "1";
+$nms::web::cc{'stale-while-revalidate'} = "5";
finalize_output();
diff --git a/web/nms.gathering.org/api/public/switch-state b/web/nms.gathering.org/api/public/switch-state
index e494e6d..999a1d8 100755
--- a/web/nms.gathering.org/api/public/switch-state
+++ b/web/nms.gathering.org/api/public/switch-state
@@ -41,4 +41,6 @@ while (my $ref = $qs->fetchrow_hashref()) {
$nms::web::json{'switches'}{$ref->{'switch'}}{'latency_secondary'} = $ref->{'latency_ms'};
}
+$nms::web::cc{'max-age'} = "5";
+$nms::web::cc{'stale-while-revalidate'} = "30";
finalize_output();
diff --git a/web/nms.gathering.org/api/public/switches b/web/nms.gathering.org/api/public/switches
index d62169c..4e3bbc8 100755
--- a/web/nms.gathering.org/api/public/switches
+++ b/web/nms.gathering.org/api/public/switches
@@ -12,7 +12,7 @@ use Data::Dumper;
$nms::web::cc{'max-age'} = "60";
-my $q2 = $nms::web::dbh->prepare('select switch,sysname,placement,ip,switchtype,poll_frequency,community,last_updated from switches');
+my $q2 = $nms::web::dbh->prepare('select switch,sysname,placement,ip,switchtype,poll_frequency,community,last_updated from switches where placement like null');
$q2->execute();
while (my $ref = $q2->fetchrow_hashref()) {