diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-03-16 19:31:48 +0000 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-03-16 19:31:48 +0000 |
commit | 314bc052854aabb44dd9272ddd82176be5c469d5 (patch) | |
tree | f24943727a66aa32cb231830cd5f1f214d12ceb5 | |
parent | 77525f31852c93f7e336b0172a9f87674941fd09 (diff) |
NMS: Implement improved ping map
e69-2 is looking good.
Now with a separate age-entity. Not really used for anything fancy right
now, but could be.
-rwxr-xr-x | clients/ping.pl | 1 | ||||
-rwxr-xr-x | include/nms/web.pm | 13 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/ping | 12 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-data.js | 12 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-map-handlers.js | 31 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms.js | 19 |
6 files changed, 63 insertions, 25 deletions
diff --git a/clients/ping.pl b/clients/ping.pl index 80c389a..93d759f 100755 --- a/clients/ping.pl +++ b/clients/ping.pl @@ -46,6 +46,7 @@ while (1) { next if (!defined($switch)); $latency //= "\\N"; + $latency*=1000; $dbh->pg_putcopydata("$switch\t$latency\n"); } $dbh->pg_putcopyend(); diff --git a/include/nms/web.pm b/include/nms/web.pm index 2a336c6..7372916 100755 --- a/include/nms/web.pm +++ b/include/nms/web.pm @@ -52,15 +52,20 @@ sub db_safe_quote { # returns a valid $when statement # Also sets cache-control headers if time is overridden +# This can be called explicitly to override the window of time we evaluate. +# Normally up to 15 minutes old data will be returned, but for some API +# endpoints it is better to return no data than old data (e.g.: ping). sub setwhen { - my $when; $now = "now()"; + my $window = '15m'; + if (@_ == 1) { + $window = $_[0]; + } if (defined($get_params{'now'})) { $now = db_safe_quote('now') . "::timestamp "; $cc{'max-age'} = "3600"; } - $when = " time > " . $now . " - '15m'::interval and time < " . $now . " "; - return $when; + $when = " time > " . $now . " - '".$window."'::interval and time < " . $now . " "; } sub finalize_output { @@ -93,6 +98,6 @@ BEGIN { $dbh = nms::db_connect(); populate_params(); - $when = setwhen(); + setwhen(); } 1; diff --git a/web/nms.gathering.org/api/public/ping b/web/nms.gathering.org/api/public/ping index f13a03b..bf92440 100755 --- a/web/nms.gathering.org/api/public/ping +++ b/web/nms.gathering.org/api/public/ping @@ -2,10 +2,18 @@ use lib '../../../../include'; use nms::web; -my $q = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) time,sysname, latency_ms FROM ping NATURAL JOIN switches WHERE time in (select max(time) from ping where " . $nms::web::when . " group by switch)"); +#nms::web::setwhen('1s'); + +my $q = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) (now() - time) as age,sysname, latency_ms FROM ping NATURAL JOIN switches WHERE time in (select max(time) from ping where " . $nms::web::when . " group by switch)"); $q->execute(); while (my $ref = $q->fetchrow_hashref()) { $nms::web::json{'switches'}{$ref->{'sysname'}}{'latency'} = $ref->{'latency_ms'}; + # This isn't pretty, feel free to fix, but I want age == seconds + # without decimals. + my ($h,$m,$ss) = split(':', $ref->{'age'}); + my ($s,undef) = split('\.', "$ss"); + + $nms::web::json{'switches'}{$ref->{'sysname'}}{'age'} = ($h*60*60) + ($m*60) + $s;# $$ref->{'age'}; } my $qs = $nms::web::dbh->prepare("SELECT DISTINCT ON (switch) switch, latency_ms FROM ping_secondary_ip WHERE " . $nms::web::when . " ORDER BY switch, time DESC;"); @@ -20,8 +28,6 @@ while (my $ref = $lq->fetchrow_hashref()) { $nms::web::json{'linknets'}{$ref->{'linknet'}} = [ $ref->{'latency1_ms'}, $ref->{'latency2_ms'} ]; } -$q->execute(); - $nms::web::cc{'max-age'} = "1"; $nms::web::cc{'stale-while-revalidate'} = "5"; finalize_output(); diff --git a/web/nms.gathering.org/js/nms-data.js b/web/nms.gathering.org/js/nms-data.js index ef41a3a..155b5a8 100644 --- a/web/nms.gathering.org/js/nms-data.js +++ b/web/nms.gathering.org/js/nms-data.js @@ -168,6 +168,18 @@ nmsData.unregisterHandler = function(name, id) { * after a comment is posted). */ nmsData.updateSource = function(name) { + /* + * See comment in nms.js nmsINIT(); + */ + if (name == "ticker" ) { + for (var i in nmsData._sources[name].cbs) { + var tmp = nmsData._sources[name].cbs[i]; + if (tmp.cb != undefined) { + tmp.cb(tmp.cbdata); + } + } + return; + } this._genericUpdater(name, true); } diff --git a/web/nms.gathering.org/js/nms-map-handlers.js b/web/nms.gathering.org/js/nms-map-handlers.js index 0126c38..e6b9103 100644 --- a/web/nms.gathering.org/js/nms-map-handlers.js +++ b/web/nms.gathering.org/js/nms-map-handlers.js @@ -162,9 +162,6 @@ function trafficUpdater() } } -/* - * Init-function for uplink map - */ function trafficTotInit() { var m = 1024 * 1024 / 8; @@ -250,23 +247,20 @@ function tempInit() function pingUpdater() { - if (!nms.ping_data || !nms.ping_data["switches"]) { + if (nmsData.switches == undefined || nmsData.switches.switches == undefined) { return; } - for (var sw in nms.switches_now["switches"]) { - var c = blue; - if (nms.ping_data['switches'] && nms.ping_data['switches'][sw]) - c = gradient_from_latency(nms.ping_data["switches"][sw]["latency"]); - setSwitchColor(sw, c); - } - for (var ln in nms.switches_now["linknets"]) { - var c1 = blue; - var c2 = c1; - if (nms.ping_data['linknets'] && nms.ping_data['linknets'][ln]) { - c1 = gradient_from_latency(nms.ping_data["linknets"][ln][0]); - c2 = gradient_from_latency(nms.ping_data["linknets"][ln][1]); + for (var sw in nmsData.switches.switches) { + try { + if (nmsData.ping.switches[sw].age > 0) { + c = red; + } else { + c = gradient_from_latency(nmsData.ping.switches[sw].latency); + } + nmsMap.setSwitchColor(sw, c); + } catch (e) { + nmsMap.setSwitchColor(sw, blue); } - setLinknetColors(ln, c1, c2); } } @@ -278,6 +272,9 @@ function pingInit() setLegend(3,gradient_from_latency(60),"60ms"); setLegend(4,gradient_from_latency(100),"100ms"); setLegend(5,gradient_from_latency(undefined) ,"No response"); + nmsData.addHandler("ping","mapHandler",pingUpdater); + nmsData.addHandler("switches","mapHandler",pingUpdater); + nmsData.addHandler("ticker", "mapHandler", pingUpdater); } function commentUpdater() diff --git a/web/nms.gathering.org/js/nms.js b/web/nms.gathering.org/js/nms.js index 0484ffa..025fab9 100644 --- a/web/nms.gathering.org/js/nms.js +++ b/web/nms.gathering.org/js/nms.js @@ -320,7 +320,17 @@ function setUpdater(fo) { nmsMap.reset(); nmsData.unregisterHandlerWildcard("mapHandler"); - fo.init(); + try { + fo.init(); + } catch (e) { + /* + * This can happen typically on initial load where the data + * hasn't been retrieved yet. Instead of breaking the + * entire init-process, just bail out here. + */ + console.log("Possibly broken handler: " + fo.name); + console.log(e); + } var foo = document.getElementById("updater_name"); foo.innerHTML = fo.name + " "; document.location.hash = fo.tag; @@ -455,6 +465,13 @@ function initNMS() { nmsData.registerSource("ping", "/api/public/ping"); nmsData.registerSource("switches","/api/public/switches"); nmsData.registerSource("switchstate","/api/public/switch-state"); + + // This is a magic dummy-source, it's purpose is to give a unified + // way to get ticks every second. It is mainly meant to allow map + // handlers to register for ticks so they will execute without data + // (and thus notice stale data instead of showing a green ping-map + // despite no pings) + nmsData.registerSource("ticker","bananabananbanana"); // Private nmsData.registerSource("snmp","/api/private/snmp"); |