diff options
-rwxr-xr-x | clients/ping.pl | 48 | ||||
-rw-r--r-- | sql/nms.sql | 4 | ||||
-rwxr-xr-x | web/nms.gathering.org/ping-json.pl | 20 | ||||
-rw-r--r-- | web/nms.gathering.org/ping.html | 100 | ||||
-rwxr-xr-x | web/nms.gathering.org/switches-json.pl | 28 |
5 files changed, 200 insertions, 0 deletions
diff --git a/clients/ping.pl b/clients/ping.pl new file mode 100755 index 0000000..b0eb57d --- /dev/null +++ b/clients/ping.pl @@ -0,0 +1,48 @@ +#! /usr/bin/perl +use DBI; +use POSIX; +use Time::HiRes; +use Net::Oping; +use Data::Dumper; +use strict; +use warnings; + +use lib '../include'; +use nms; + +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'"); + +while (1) { + my $ping = Net::Oping->new; + $ping->timeout(0.2); + + $q->execute; + my %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 $result = $ping->ping(); + die $ping->get_error if (!defined($result)); + + $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"); + } + } + $dbh->pg_putcopyend(); + $dbh->commit; + + sleep 1; +} + diff --git a/sql/nms.sql b/sql/nms.sql index acadcc5..3b2c029 100644 --- a/sql/nms.sql +++ b/sql/nms.sql @@ -606,6 +606,10 @@ CREATE TABLE uplinks ( ALTER TABLE public.uplinks OWNER TO nms; +create table ping ( switch integer not null, updated timestamptz not null default current_timestamp, latency_ms float ); +create index updated_index on ping ( updated ); +alter table public.ping owner to nms; + -- -- Data for Name: ap_poll; Type: TABLE DATA; Schema: public; Owner: nms -- diff --git a/web/nms.gathering.org/ping-json.pl b/web/nms.gathering.org/ping-json.pl new file mode 100755 index 0000000..b304de6 --- /dev/null +++ b/web/nms.gathering.org/ping-json.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl +use CGI; +use GD; +use DBI; +use JSON::XS; +use lib '../../include'; +use nms; +my $cgi = CGI->new; + +my $dbh = nms::db_connect(); + +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{$ref->{'switch'}} = $ref->{'latency_ms'}; +} +print $cgi->header(-type=>'text/json; charset=utf-8'); +print JSON::XS::encode_json(\%json); diff --git a/web/nms.gathering.org/ping.html b/web/nms.gathering.org/ping.html new file mode 100644 index 0000000..b1d9b0f --- /dev/null +++ b/web/nms.gathering.org/ping.html @@ -0,0 +1,100 @@ +<html> + <body> + <p style="border: 1px solid black;" id="playground"><img src="bg07.png" alt="" id="map" style="border: 1px solid black"; /></p> + <script type="text/javascript"> + <!-- +var switches = []; +var last_dataset = []; +get_switches(); +get_ping(); + +function json_request(url, func, repeat_ms) { + var request = new XMLHttpRequest(); + request.open('GET', url, true); + + request.onload = function() { + if (this.status >= 200 && this.status < 400) { + func(JSON.parse(this.response)); + } else if (this.status != 410) { + json_request(url, func, repeat_ms); + } + }; + request.onerror = function() { + json_request(url, func, repeat_ms); + }; + request.send(); +} + +function get_switches() { + json_request('/switches-json.pl', draw_switches, 1000); +} + +function get_ping() { + json_request('/ping-json.pl', update_ping, 1000); +} + +function draw_switches(json) { + for (var switchnum in switches) { + document.body.removeChild(switches[switchnum]); + } + switches = []; + + for (var switchnum in json) { + var s = json[switchnum]; + create_switch(switchnum, + s['sysname'], + parseInt(s['x']), + parseInt(s['y']), + parseInt(s['width']), + parseInt(s['height'])); + } + setTimeout(get_switches, 60000); + really_update_ping(last_dataset); +} + +function update_ping(json) { + last_dataset = json; + really_update_ping(json); + setTimeout(get_ping, 1000); +} + +function really_update_ping(json) { + for (var switchnum in switches) { + if (json[switchnum] === null || json[switchnum] === undefined) { + switches[switchnum].style.backgroundColor = '#0000ff'; + } else { + // 10ms is max + var l = json[switchnum] / 10.0; + if (l >= 1.0) { l = 1.0; } + l = Math.pow(l, 1.0/2.2); + l = Math.round(l * 255.0); + + switches[switchnum].style.backgroundColor = 'rgb(' + l + ', 255, 0)'; + } + } +} + +function create_switch(switchnum, sysname, x, y, width, height) { + var s = document.createElement("div"); + var map = document.getElementById('map'); + var top_offset = map.getBoundingClientRect().top; + var left_offset = map.getBoundingClientRect().left; + + s.style.position = 'absolute'; + s.style.left = (left_offset + x) + 'px'; + s.style.top = (top_offset + y) + 'px'; + s.style.width = width + 'px'; + s.style.height = height + 'px'; + s.style.backgroundColor = '#0000ff'; + s.style.border = '1px solid black'; + switches[switchnum] = s; + + var text = document.createTextNode(sysname); + s.appendChild(text); + + document.body.appendChild(s); +} + --> + </script> + </body> +</html> diff --git a/web/nms.gathering.org/switches-json.pl b/web/nms.gathering.org/switches-json.pl new file mode 100755 index 0000000..1dccfe2 --- /dev/null +++ b/web/nms.gathering.org/switches-json.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl +use CGI; +use GD; +use DBI; +use JSON::XS; +use lib '../../include'; +use nms; +my $cgi = CGI->new; + +my $dbh = nms::db_connect(); + +my $q = $dbh->prepare('select switch,sysname,placement from switches natural join placements'); +$q->execute(); + +my %json = (); +while (my $ref = $q->fetchrow_hashref()) { + $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/; + my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4); + $json{$ref->{'switch'}} = { + sysname => $ref->{'sysname'}, + x => $x2, + y => $y1, + width => $x1 - $x2, + height => $y1 - $y2 + }; +} +print $cgi->header(-type=>'text/json; charset=utf-8'); +print JSON::XS::encode_json(\%json); |