diff options
-rwxr-xr-x | clients/ping.pl | 30 | ||||
-rw-r--r-- | sql/nms.sql | 4 | ||||
-rwxr-xr-x | web/nms.gathering.org/ping-json.pl | 11 | ||||
-rw-r--r-- | web/nms.gathering.org/ping.js | 19 |
4 files changed, 53 insertions, 11 deletions
diff --git a/clients/ping.pl b/clients/ping.pl index 6968b4d..b41febd 100755 --- a/clients/ping.pl +++ b/clients/ping.pl @@ -13,7 +13,7 @@ my $dbh = nms::db_connect(); $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; -my $q = $dbh->prepare("SELECT switch,ip FROM switches WHERE ip<>'127.0.0.1'"); +my $q = $dbh->prepare("SELECT switch,ip,secondary_ip FROM switches WHERE ip<>'127.0.0.1'"); my $lq = $dbh->prepare("SELECT linknet,addr1,addr2 FROM linknets"); while (1) { @@ -23,11 +23,19 @@ while (1) { $q->execute; my %ip_to_switch = (); + my %secondary_ip_to_switch = (); while (my $ref = $q->fetchrow_hashref) { my $switch = $ref->{'switch'}; + my $ip = $ref->{'ip'}; $ping->host_add($ip); $ip_to_switch{$ip} = $switch; + + my $secondary_ip = $ref->{'secondary_ip'}; + if (defined($secondary_ip)) { + $ping->host_add($secondary_ip); + $secondary_ip_to_switch{$secondary_ip} = $switch; + } } my $result = $ping->ping(); die $ping->get_error if (!defined($result)); @@ -35,13 +43,23 @@ while (1) { $dbh->do('COPY ping (switch, latency_ms) FROM STDIN'); # date is implicitly now. while (my ($ip, $latency) = each %$result) { my $switch = $ip_to_switch{$ip}; - if (!defined($latency)) { - $dbh->pg_putcopydata("$switch\t\\N\n"); - } else { - $dbh->pg_putcopydata("$switch\t$latency\n"); - } + next if (!defined($switch)); + + $latency //= "\\N"; + $dbh->pg_putcopydata("$switch\t$latency\n"); } $dbh->pg_putcopyend(); + + $dbh->do('COPY ping_secondary_ip (switch, latency_ms) FROM STDIN'); # date is implicitly now. + while (my ($ip, $latency) = each %$result) { + my $switch = $secondary_ip_to_switch{$ip}; + next if (!defined($switch)); + + $latency //= "\\N"; + $dbh->pg_putcopydata("$switch\t$latency\n"); + } + $dbh->pg_putcopyend(); + $dbh->commit; # ping linknets diff --git a/sql/nms.sql b/sql/nms.sql index 47aacdb..6af4e2a 100644 --- a/sql/nms.sql +++ b/sql/nms.sql @@ -594,6 +594,10 @@ create table ping ( switch integer not null, updated timestamptz not null defaul create index updated_index on ping ( updated ); alter table public.ping owner to nms; +create table ping_secondary_ip ( switch integer not null, updated timestamptz not null default current_timestamp, latency_ms float ); +create index updated_index3 on ping_secondary_ip ( updated ); +alter table public.ping_secondary_ip owner to nms; + create table linknets ( linknet serial not null, switch1 integer not null, addr1 inet not null, switch2 integer not null, addr2 inet not null ); alter table public.linknets owner to nms; diff --git a/web/nms.gathering.org/ping-json.pl b/web/nms.gathering.org/ping-json.pl index 78e795a..e661e8f 100755 --- a/web/nms.gathering.org/ping-json.pl +++ b/web/nms.gathering.org/ping-json.pl @@ -7,13 +7,18 @@ use nms; my $cgi = CGI->new; my $dbh = nms::db_connect(); +my %json = (); my $q = $dbh->prepare("SELECT DISTINCT ON (switch) switch, latency_ms FROM ping WHERE updated >= NOW() - INTERVAL '15 secs' ORDER BY switch, updated DESC;"); $q->execute(); - -my %json = (); while (my $ref = $q->fetchrow_hashref()) { - $json{'switches'}{$ref->{'switch'}} = $ref->{'latency_ms'}; + $json{'switches'}{$ref->{'switch'}}{'latency'} = $ref->{'latency_ms'}; +} + +my $qs = $dbh->prepare("SELECT DISTINCT ON (switch) switch, latency_ms FROM ping_secondary_ip WHERE updated >= NOW() - INTERVAL '15 secs' ORDER BY switch, updated DESC;"); +$qs->execute(); +while (my $ref = $qs->fetchrow_hashref()) { + $json{'switches'}{$ref->{'switch'}}{'latency_secondary'} = $ref->{'latency_ms'}; } my $lq = $dbh->prepare("SELECT DISTINCT ON (linknet) linknet, latency1_ms, latency2_ms FROM linknet_ping WHERE updated >= NOW() - INTERVAL '15 secs' ORDER BY linknet, updated DESC;"); diff --git a/web/nms.gathering.org/ping.js b/web/nms.gathering.org/ping.js index c1b2fbe..ce3d99f 100644 --- a/web/nms.gathering.org/ping.js +++ b/web/nms.gathering.org/ping.js @@ -110,6 +110,15 @@ function update_ping(json) { setTimeout(get_ping, 1000); } +function gradient_from_latency(latency_ms, latency_secondary_ms) { + if (latency_secondary_ms === undefined) { + return rgb_from_latency(latency_ms); + } + return '-webkit-gradient(linear, left top, left bottom, ' + + 'from(' + rgb_from_latency(latency_ms) + '), ' + + 'to(' + rgb_from_latency(latency_secondary_ms) + '))'; +} + function rgb_from_latency(latency_ms) { if (latency_ms === null || latency_ms === undefined) { return '#0000ff'; @@ -127,8 +136,14 @@ function rgb_from_latency(latency_ms) { function really_update_ping(json) { if (json['switches']) { for (var switchnum in switches) { - switches[switchnum].style.backgroundColor = rgb_from_latency(json['switches'][switchnum]); - } + if (json['switches'][switchnum]) { + switches[switchnum].style.background = + gradient_from_latency(json['switches'][switchnum]['latency'], + json['switches'][switchnum]['latency_secondary']); + } else { + switches[switchnum].style.background = '#0000ff'; + } + } } if (json['linknets']) { for (var linknetnum in linknets) { |