diff options
Diffstat (limited to 'web/js/nms-map-handlers.js')
-rw-r--r-- | web/js/nms-map-handlers.js | 336 |
1 files changed, 270 insertions, 66 deletions
diff --git a/web/js/nms-map-handlers.js b/web/js/nms-map-handlers.js index f4aba57..61a5a05 100644 --- a/web/js/nms-map-handlers.js +++ b/web/js/nms-map-handlers.js @@ -11,12 +11,6 @@ * utilize information from any aspect of NMS, and thus opens NMS up to the * world of intelligent maps base don multiple data sources. * - * Warning: This paradigm will change. Handlers will be expected to - * register their own callbacks for nmsData. Work in progress. - * - */ - -/* */ var handler_uplinks = { @@ -33,8 +27,9 @@ var handler_temp = { var handler_ping = { init:pingInit, + getInfo:pingInfo, tag:"ping", - name:"IPv4 Ping" + name:"Ping" }; var handler_traffic = { @@ -63,6 +58,7 @@ var handler_disco = { var handler_snmp = { init:snmpInit, + getInfo:snmpInfo, tag:"snmp", name:"SNMP state" }; @@ -73,7 +69,51 @@ var handler_cpu = { name:"CPU utilization" }; +var handler_health = { + init:healthInit, + getInfo:healthInfo, + tag:"health", + name:"Health" +}; + +var handler_mgmt = { + getInfo:mgmtInfo, + name:"Management info" +}; + +var handlerInfo = function(tag,desc) { + /* + * Short name, typically matching the url anchor. + */ + this.tag = tag; + this.description = desc || "Unknown"; + this.data = [ + { + value: undefined, + description: desc || "Generic info" + }]; + /* + * 0: all good. + * 1000: messed up. + * + * This can be "intelligent". E.g.: pingInfo() takes latency and + * the age of the reply into account in addition to having a + * special handling of lack of a result. + */ + this.score = 0; + /* + * Why the score is what it is. E.g.: If you have multiple + * conditions that set the score, what is the final value based on? + */ + this.why = "0 score is the default"; +}; + +/* + * Order matches what's seen in the infobox + */ var handlers = [ + handler_health, + handler_mgmt, handler_uplinks, handler_temp, handler_ping, @@ -110,13 +150,13 @@ function uplinkUpdater() if (uplinks == 0) { nmsMap.setSwitchColor(sw,"white"); } else if (uplinks == 1) { - nmsMap. setSwitchColor(sw, red); + nmsMap. setSwitchColor(sw, nmsColor.red); } else if (uplinks == 2) { - nmsMap.setSwitchColor(sw, orange); + nmsMap.setSwitchColor(sw, nmsColor.orange); } else if (uplinks == 3) { - nmsMap.setSwitchColor(sw,green); + nmsMap.setSwitchColor(sw, nmsColor.green); } else if (uplinks > 3) { - nmsMap.setSwitchColor(sw, blue); + nmsMap.setSwitchColor(sw, nmsColor.blue); } } } @@ -129,10 +169,10 @@ function uplinkInit() nmsData.addHandler("switches","mapHandler",uplinkUpdater); nmsData.addHandler("switchstate","mapHandler",uplinkUpdater); setLegend(1,"white","0 uplinks"); - setLegend(2,red,"1 uplink"); - setLegend(3,orange,"2 uplinks"); - setLegend(4,green,"3 uplinks"); - setLegend(5,blue,"4 uplinks"); + setLegend(2,nmsColor.red,"1 uplink"); + setLegend(3,nmsColor.orange,"2 uplinks"); + setLegend(4,nmsColor.green,"3 uplinks"); + setLegend(5,nmsColor.blue,"4 uplinks"); } /* @@ -143,7 +183,7 @@ function trafficInit() nmsData.addHandler("switches","mapHandler",trafficUpdater); nmsData.addHandler("switchstate","mapHandler",trafficUpdater); var m = 1024 * 1024 / 8; - drawGradient([lightgreen,green,orange,red]); + nmsColor.drawGradient([nmsColor.lightgreen, nmsColor.green, nmsColor.orange, nmsColor.red]); setLegend(1,colorFromSpeed(0),"0 (N/A)"); setLegend(5,colorFromSpeed(1100 * m) , "1100Mb/s"); setLegend(4,colorFromSpeed(600 * m),"600Mb/s"); @@ -178,7 +218,7 @@ function trafficTotInit() nmsData.addHandler("switches","mapHandler",trafficTotUpdater); nmsData.addHandler("switchstate","mapHandler",trafficTotUpdater); var m = 1024 * 1024 / 8; - drawGradient([lightgreen,green,orange,red]); + nmsColor.drawGradient([nmsColor.lightgreen, nmsColor.green, nmsColor.orange, nmsColor.red]); setLegend(1,colorFromSpeed(0),"0 (N/A)"); setLegend(5,colorFromSpeed(5000 * m,5) , "5000Mb/s"); setLegend(4,colorFromSpeed(3000 * m,5),"3000Mb/s"); @@ -212,9 +252,9 @@ function colorFromSpeed(speed,factor) if (factor == undefined) factor = 1.1; if (speed == 0) - return blue; + return nmsColor.blue; speed = speed < 0 ? 0 : speed; - return getColorStop( 1000 * (speed / (factor * (1000 * m)))); + return nmsColor.getColorStop( 1000 * (speed / (factor * (1000 * m)))); } /* @@ -226,11 +266,11 @@ function temp_color(t) { if (t == undefined) { console.log("Temp_color, but temp is undefined"); - return blue; + return nmsColor.blue; } t = parseInt(t) - 12; t = Math.floor((t / 23) * 1000); - return getColorStop(t); + return nmsColor.getColorStop(t); } function tempUpdater() @@ -257,7 +297,7 @@ function tempUpdater() function tempInit() { //Padded the gradient with extra colors for the upper unused values - drawGradient(["black",blue,lightblue,lightgreen,green,orange,red]); + nmsColor.drawGradient(["black", nmsColor.blue, nmsColor.lightblue, nmsColor.lightgreen, nmsColor.green, nmsColor.orange, nmsColor.red]); setLegend(1,temp_color(15),"15 °C"); setLegend(2,temp_color(20),"20 °C"); setLegend(3,temp_color(25),"25 °C"); @@ -272,28 +312,68 @@ function pingUpdater() return; } for (var sw in nmsData.switches.switches) { - try { - var c; - if (nmsData.ping.switches[sw].age > 0) { - c = red; - } else { - c = gradient_from_latency(nmsData.ping.switches[sw].latency); - } + var c = nmsColor.getColorStop(pingInfo(sw).score); + if (c == 1000) { + nmsMap.setSwitchColor(sw, nmsColor.blue); + } else { nmsMap.setSwitchColor(sw, c); - } catch (e) { - nmsMap.setSwitchColor(sw, blue); } } } +function pingInfo(sw) +{ + var ret = new handlerInfo("ping","Latency(ms)"); + ret.why = "Latency"; + if (testTree(nmsData,['ping','switches',sw])) { + var v4 = nmsData.ping.switches[sw].latency4; + var v6 = nmsData.ping.switches[sw].latency6; + if (v4 == undefined) + v4 = undefined; + if (v6 == undefined) + v6 = undefined; + ret.data[0].value = v4; + ret.data[0].description = "IPv4 latency(ms)"; + ret.data[1] = {}; + ret.data[1].value = v6; + ret.data[1].description = "IPv6 latency(ms)"; + if (v4 == undefined && v6 == undefined) { + ret.score = 1000; + ret.why = "No IPv4 or IPv6 ping reply"; + } else if(v6 == undefined) { + ret.score = 200; + ret.why = "No IPv6 ping reply"; + } else if (v4 == undefined) { + ret.score = 199; + ret.why = "No IPv4 ping reply"; + } + + v4 = parseFloat(v4) * 10; + v6 = parseFloat(v6) * 10; + if (v4 > ret.score || v6 > ret.score) { + ret.why = "Latency"; + ret.score = parseInt(v4 > v6 ? v4 : v6); + } + if (nmsData.ping.switches[sw].age4 > 5 || nmsData.ping.switches[sw].age6 > 5) { + ret.why = "Old ping"; + ret.score = 900; + } + } else { + ret.data[0].value = "N/A - no ping replies"; + ret.why = "No ping replies"; + ret.score = 999; + } + return ret; +} + function pingInit() { - drawGradient([green,lightgreen,orange,red]); - setLegend(1,gradient_from_latency(1),"1ms"); - setLegend(2,gradient_from_latency(30),"30ms"); - setLegend(3,gradient_from_latency(60),"60ms"); - setLegend(4,gradient_from_latency(100),"100ms"); - setLegend(5,gradient_from_latency(undefined) ,"No response"); + nmsColor.drawGradient([nmsColor.green,nmsColor.lightgreen, nmsColor.orange, nmsColor.red]); + setLegend(1,nmsColor.getColorStop(10),"1ms"); + setLegend(2,nmsColor.getColorStop(300),"30ms"); + setLegend(3,nmsColor.getColorStop(600),"60ms"); + setLegend(4,nmsColor.getColorStop(1000),"100ms"); + setLegend(5,nmsColor.blue,"No response"); nmsData.addHandler("ping","mapHandler",pingUpdater); nmsData.addHandler("switches","mapHandler",pingUpdater); nmsData.addHandler("ticker", "mapHandler", pingUpdater); @@ -307,38 +387,31 @@ function getDhcpColor(stop) stop = 1000; if (stop > 1000) stop = 1000; - return getColorStop(stop); + return nmsColor.getColorStop(stop); } function dhcpUpdater() { - if (nmsData.dhcp == undefined || nmsData.dhcp.dhcp == undefined) { + if (!testTree(nmsData,['dhcp','dhcp']) || !testTree(nmsData,['switches','switches'])) { return } - if (nmsData.switches == undefined || nmsData.switches.switches == undefined) { - return; - } var now = nmsData.dhcp.time; - try { for (var sw in nmsData.switches.switches) { - var c = blue; - if (nmsData.dhcp.dhcp[sw] == undefined) { + var c = nmsColor.blue; + var s = nmsData.dhcp.dhcp[sw]; + if (s == undefined) { nmsMap.setSwitchColor(sw,c); continue; } - var s = nmsData.dhcp.dhcp[sw]; var then = parseInt(s); c = getDhcpColor(now - then); nmsMap.setSwitchColor(sw, c); } - } catch(e) { - console.log(e); - } } function dhcpInit() { - drawGradient([green,lightgreen,orange,red]); + nmsColor.drawGradient([nmsColor.green, nmsColor.lightgreen, nmsColor.orange, nmsColor.red]); nmsData.addHandler("dhcp","mapHandler",dhcpUpdater); setLegend(1,"white","Undefined"); setLegend(2,getDhcpColor(1),"1 Second old"); @@ -360,47 +433,96 @@ function randomizeColors() return; } for (var sw in nmsData.switches.switches) { - nmsMap.setSwitchColor(sw, getRandomColor()); + nmsMap.setSwitchColor(sw, nmsColor.random()); } } function discoDo() { randomizeColors(); - setTimeout(randomizeColors,500); } function discoInit() { nmsData.addHandler("ticker", "mapHandler", discoDo); setNightMode(true); - setLegend(1,blue,"Y"); - setLegend(2,red, "M"); - setLegend(3,orange,"C"); - setLegend(4,green, "A"); + setLegend(1,nmsColor.blue,"Y"); + setLegend(2,nmsColor.red, "M"); + setLegend(3,nmsColor.orange,"C"); + setLegend(4,nmsColor.green, "A"); setLegend(5,"white","!"); } function snmpUpdater() { for (var sw in nmsData.switches.switches) { if (nmsData.snmp.snmp[sw] == undefined || nmsData.snmp.snmp[sw].misc == undefined) { - nmsMap.setSwitchColor(sw, red); + nmsMap.setSwitchColor(sw, nmsColor.red); } else if (nmsData.snmp.snmp[sw].misc.sysName[0] != sw) { - nmsMap.setSwitchColor(sw, orange); + nmsMap.setSwitchColor(sw, nmsColor.orange); } else { - nmsMap.setSwitchColor(sw, green); + nmsMap.setSwitchColor(sw, nmsColor.green); + } + } +} + +function secondsToTime(input) { + var h, m, s; + h = Math.floor(input / 60 / 60); + m = Math.floor((input%3600)/60); + s = Math.floor(input%60); + var string = ""; + if (h > 0) + string = h + " hours "; + if (h > 0 || m > 0) + string += m + " minutes "; + if (string == "") + string += s + " seconds"; + return string; +} + +function snmpInfo(sw) { + var ret = new handlerInfo("snmp","SNMP data"); + ret.why = "No data"; + if (!testTree(nmsData,['snmp','snmp',sw,'misc'])) { + ret.score = 800; + ret.why = "No data"; + ret.data[0].value = "No data"; + } else if (nmsData.snmp.snmp[sw].misc.sysName[0] != sw) { + ret.score = 200; + ret.why = "SNMP sysName doesn't match Gondul sysname"; + ret.data[0].value = ret.why; + ret.data[1] = { description: "SNMP sysName", value: nmsData.snmp.snmp[sw].misc.sysName[0] }; + } else { + ret.score = 0; + ret.data[0].value = "SNMP freshly updated"; + ret.why = "SNMP all good"; + } + if (testTree(nmsData,['snmp','snmp',sw,'misc','sysUpTimeInstance',''])) { + var uptime = parseInt(nmsData.snmp.snmp[sw]["misc"]["sysUpTimeInstance"][""]) / 100; + var upstring = secondsToTime(uptime); + ret.data.push({value: upstring, description: "System uptime"}); + if (uptime < 60*5 && ret.score < 500) { + ret.score = 500; + ret.why = "System rebooted last 5 minutes"; + } + if (uptime < 60*15 && ret.score < 250) { + ret.score = 250; + ret.why = "System rebooted last 15 minutes"; } } + return ret; } + function snmpInit() { nmsData.addHandler("snmp", "mapHandler", snmpUpdater); - setLegend(1,green,"OK"); - setLegend(2,orange, "Sysname mismatch"); - setLegend(3,red,"No SNMP data"); - setLegend(4,green, ""); - setLegend(5,green,""); + setLegend(1,nmsColor.green,"OK"); + setLegend(2,nmsColor.orange, "Sysname mismatch"); + setLegend(3,nmsColor.red,"No SNMP data"); + setLegend(4,nmsColor.green, ""); + setLegend(5,nmsColor.green,""); } + function cpuUpdater() { for (var sw in nmsData.switches.switches) { try { @@ -409,7 +531,7 @@ function cpuUpdater() { var local = nmsData.snmp.snmp[sw].misc['jnxOperatingCPU'][u]; cpu = Math.max(nmsData.snmp.snmp[sw].misc.jnxOperatingCPU[u],cpu); } - nmsMap.setSwitchColor(sw, getColorStop(cpu * 10)); + nmsMap.setSwitchColor(sw, nmsColor.getColorStop(cpu * 10)); nmsMap.setSwitchInfo(sw, cpu + " % "); } catch (e) { nmsMap.setSwitchColor(sw, "white"); @@ -418,12 +540,94 @@ function cpuUpdater() { } } +function mgmtInfo(sw) { + var ret = new handlerInfo("mgmt","Management info"); + ret.score = 0; + ret.why = "All good"; + if (testTree(nmsData,['smanagement','switches',sw])) { + var mg = nmsData.smanagement.switches[sw]; + ret.data = + [{ + value: mg.mgmt_v4_addr || "", + description: "Management IP (v4)" + }, { + value: mg.mgmt_v6_addr || "", + description: "Management IP (v6)" + }, { + value: mg.subnet4 || "", + description: "Subnet (v4)" + }, { + value: mg.subnet6 || "", + description: "Subnet (v6)" + }]; + if ((mg.mgmt_v4_addr == undefined || mg.mgmt_v4_addr == "") && (mg.mgmt_v6_addr == undefined || mg.mgmt_v6_addr == "")) { + ret.why = "No IPv4 or IPv6 mamagement IP"; + ret.score = 1000; + } else if (mg.mgmt_v4_addr == undefined || mg.mgmt_v4_addr == "") { + ret.why = "No IPv4 management IP"; + ret.score = 240; + } else if (mg.mgmt_v6_addr == undefined || mg.mgmt_v6_addr == "") { + ret.why = "No IPv6 management IP"; + ret.score = 239; + } + } else { + ret.score = 1000; + ret.why = "No management info"; + ret.data = [{}]; + ret.data[0].value = "N/A"; + ret.data[0].description = "Management info"; + }; + return ret; + +} + function cpuInit() { nmsData.addHandler("snmp", "mapHandler", cpuUpdater); - drawGradient([green,orange,red]); + nmsColor.drawGradient([nmsColor.green,nmsColor.orange,nmsColor.red]); setLegend(1,getColorStop(0),"0 %"); setLegend(2,getColorStop(250),"25 %"); setLegend(3,getColorStop(600),"60 %"); setLegend(4,getColorStop(1000),"100 %"); setLegend(5,"white","N/A"); } + +function healthInfo(sw) { + var worst = new handlerInfo("health", "Health"); + worst.score = 0; + worst.why = "All good"; + for (var h in handlers) { + if (handlers[h].tag== "health") + continue; + if (handlers[h].getInfo == undefined) + continue; + var ret = handlers[h].getInfo(sw); + if (ret.score > worst.score) { + worst = ret; + } + } + worst.data = [{ + description: "Health (lower is better): ", + value: worst.score + " (" + worst.why + ")" + }]; + return worst; +} + +function healthUpdater() { + if (nmsData.switches == undefined || nmsData.switches.switches == undefined) + return; + for (var sw in nmsData.switches.switches) { + var worst = healthInfo(sw); + nmsMap.setSwitchColor(sw, nmsColor.getColorStop(worst.score)); + nmsMap.setSwitchInfo(sw, worst.tag); + } +} + +function healthInit() { + nmsData.addHandler("ping", "mapHandler", healthUpdater); + nmsColor.drawGradient([nmsColor.green,nmsColor.orange,nmsColor.red]); + setLegend(1,nmsColor.getColorStop(0),"All good"); + setLegend(2,nmsColor.getColorStop(250),"Ok-ish"); + setLegend(3,nmsColor.getColorStop(600),"Ick-ish"); + setLegend(4,nmsColor.getColorStop(800),"Nasty"); + setLegend(5,nmsColor.getColorStop(1000),"WTF?"); +} |