notification on node down status
-
Hi,
I'm using MyController from about 1 year ago, and I'm adding more and more nodes to my network.
I'd like to receive a notification when one of them is down.
Till now I managed this using the standard rules:
but it's very annoing to be managed for each new node.
Is there a way to loop thru nodes in a script and the send a pushbullet notification based on the status?
Thank
Daniele -
I'm following this example, changed the notification to pushbullet and added this row to filter only broken nodes:
options.put("state", "DOWN");
but I receive the error:
"errorMessage": "java.lang.String cannot be cast to java.lang.Enum"
I'm not a Java programmer, so I'm trying to understand how to use the HashMap looking at the examples I found on your forum.
It's surely a stupid error, but I cannot understand where it is...Thank you for any help
Daniele -
@Daniele can you try,
options.put("state", "Down");
I did some small script, Not complete!
var myImports = new JavaImporter(java.io, java.lang, java.util, java.text, java.util.HashMap); with(myImports) { // filters var filters = new HashMap(); filters.put("orderBy", "lastSeen"); filters.put("order", "desc"); filters.put("state", "Down"); }
Let me know if you need a complete solution, like how to ignore duplicate, etc.,
-
@jkandasa said in notification on node down status:
put("state", "Down");
I tried, but I still get the same error if I add this line to your script:
var nodes = mcApi.node().getAll(filters);```
-
@Daniele What version of MyController are you using? I will try to come up with a complete solution soon.
-
1.4.0, a snapshot of more or less one month ago
-
@jkandasa I managed to get the result using a slightly different approach. I saved previous node state in a repository variable, then looped over the nodes list and compared actual state with previous one, sending en email only when state changes.
I'm not a java programmer, so any suggestion on the code is more than welcome:
var myImports = new JavaImporter(java.io, java.lang, java.util,java.lang.Object, org.mycontroller.standalone.utils.McUtils); with(myImports) { var options = new HashMap(); options.put("orderBy", "Eui"); options.put("order", "asc"); options.put("pageLimit", new Long(-1)); // -1 to list all the nodes var nodes = mcApi.node().getAll(options); var nodesVar = mcApi.variable().get("Nodes state"); var nodesOU = JSON.parse(nodesVar.value); var nodesOD = JSON.parse(nodesVar.value2); var nodesU = []; var nodesD = []; var subject = "[MyController] Nodes status"; var messageU2D = ""; var messageD2U = ""; var message = ""; for (i = 0; i < nodes.data.length; i++) { var nodeStatus = new Object(); nodeStatus['key'] = nodes.data[i].getGatewayTable().getId() + '-' + nodes.data[i].getEui(); nodeStatus['state'] = nodes.data[i].getState(); if (nodeStatus['state'] == "UP") { nodesU.push(nodeStatus['key']); var prevState = ""; for (j = 0; j < nodesOU.length; j++){ if (nodesOU[j] == nodeStatus['key']) prevState = "UP"; } if (prevState != "UP") messageD2U += nodes.data[i].getEui() + " : " + nodes.data[i].getName() + " : " + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "<BR>"; } if (nodeStatus['state'] == "DOWN") { nodesD.push(nodeStatus['key']); var prevState = ""; for (j = 0; j < nodesOD.length; j++){ if (nodesOD[j] == nodeStatus['key']) prevState = "DOWN"; } if (prevState != "DOWN") messageU2D += nodes.data[i].getEui() + " : " + nodes.data[i].getName() + " : " + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "<BR>"; } } if (messageU2D != ""){ message += "Nodes DOWN: <BR>"; message += messageU2D; } if (messageD2U != ""){ message += "Nodes UP: <BR>"; message += messageD2U; } if (message != "") // mcApi.operation().sendPushbulletNote(null, "abc@xyz.com", null, subject, message); mcApi.operation().sendEmail("abc@xyz.com", subject, message); nodesVar.value = JSON.stringify(nodesU); nodesVar.value2 = JSON.stringify(nodesD); nodesVar.save(); }
-
@Daniele Your code looks super cool and awesome. I just added few HTML template style (you may not like ) and minor tune. Thanks for the awesome code!!
var myImports = new JavaImporter(java.io, java.lang, java.util,java.lang.Object, org.mycontroller.standalone.utils.McUtils); with(myImports) { var options = new HashMap(); options.put("orderBy", "Eui"); options.put("order", "asc"); options.put("pageLimit", new Long(-1)); // -1 to list all the nodes var nodes = mcApi.node().getAll(options); var nodesVar = mcApi.variable().get("nodes_state"); // It is good to have key in lowercase and no space var nodesOU = JSON.parse(nodesVar.value); var nodesOD = JSON.parse(nodesVar.value2); var nodesU = []; var nodesD = []; var subject = "[MyController] Nodes status"; var tableBody = ""; for (i = 0; i < nodes.data.length; i++) { var nodeStatus = { }; var _nodeId = nodes.data[i].getId(); // nodeId is unique and internal // but still if you want to refer node EUI and gateway id, use the following line and comment above line. //var _nodeId = nodes.data[i].getGatewayTable().getId() + '-' + nodes.data[i].getEui(); var _nodeState = nodes.data[i].getState(); nodeStatus[_nodeId] = _nodeState; // update in to our common map if (_nodeState == "UP") { nodesU.push(_nodeId); var prevState = ""; for (j = 0; j < nodesOU.length; j++){ if (nodesOU[j] == _nodeId) prevState = "UP"; } if (prevState != "UP") tableBody += "<tr style='color: green;'><td>" + _nodeId + "</td><td>" + nodes.data[i].getName() + "</td><td>" + nodes.data[i].getEui() + "</td><td>" + _nodeState + "</td><td>" + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "</td></tr>"; } if (_nodeState == "DOWN") { nodesD.push(_nodeId); var prevState = ""; for (j = 0; j < nodesOD.length; j++){ if (nodesOD[j] == nodeId) prevState = "DOWN"; } if (prevState != "DOWN") tableBody += "<tr style='color: red;'><td>" + _nodeId + "</td><td>" + nodes.data[i].getName() + "</td><td>" + nodes.data[i].getEui() + "</td><td>" + _nodeState + "</td><td>" + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "</td></tr>"; } } if (tableBody.length > 0){ var pageStart = "Hello,<BR><BR>"; var tableStyle = '<table border = "1" cellpadding = "5" style="border-collapse: collapse;border: none;"><tbody><tr><th align="left">Id</th><th align="left">Name</th><th align="left">EUI</th><th align="left">State</th><th align="left">Since</th></tr>'; var tableEnd = '</tbody></table>'; var pageEnd = "<BR><BR>--MyController.org<BR>"; // mcApi.operation().sendPushbulletNote(null, "abc@xyz.com", null, subject, message); mcApi.operation().sendEmail("abc@xyz.com", subject, pageStart + tableStyle + tableBody + tableEnd + pageEnd); } nodesVar.value = JSON.stringify(nodesU); nodesVar.value2 = JSON.stringify(nodesD); nodesVar.save(); }
Email will be looking like this,