diff options
| author | Steinar H. Gunderson <sgunderson@bigfoot.com> | 2014-04-07 23:08:30 +0200 | 
|---|---|---|
| committer | Steinar H. Gunderson <sgunderson@bigfoot.com> | 2014-04-07 23:08:30 +0200 | 
| commit | 7685165ad046cbe73e5e545fc66cd5f5b24aedcf (patch) | |
| tree | a5724eb41e4fbe846ba37a7887da2cbb5d1f880c /web | |
| parent | b83b26425343dae0e587c759574e6193822ce7fa (diff) | |
Add some scripts to ping all the switches, and display the ping status on a map.
Diffstat (limited to 'web')
| -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 | 
3 files changed, 148 insertions, 0 deletions
| 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); | 
