• Categories
    • Recent
    • Tags
    • Popular
    • Register
    • Login

    notification on node down status

    Scheduled Pinned Locked Moved Developers Zone
    8 Posts 2 Posters 1.0k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D Offline
      Daniele
      last edited by

      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:
      4cc9bd57-521b-47c1-a5f7-07e92f791e73-image.png
      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

      1 Reply Last reply Reply Quote 0
      • D Offline
        Daniele
        last edited by

        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

        jkandasaJ 1 Reply Last reply Reply Quote 0
        • jkandasaJ Offline
          jkandasa @Daniele
          last edited by jkandasa

          @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.,

          1 Reply Last reply Reply Quote 0
          • D Offline
            Daniele
            last edited by

            @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);```
            jkandasaJ 1 Reply Last reply Reply Quote 0
            • jkandasaJ Offline
              jkandasa @Daniele
              last edited by

              @Daniele What version of MyController are you using? I will try to come up with a complete solution soon.

              1 Reply Last reply Reply Quote 0
              • D Offline
                Daniele
                last edited by

                1.4.0, a snapshot of more or less one month ago

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Daniele
                  last edited by Daniele

                  @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();
                  }
                  
                  jkandasaJ 1 Reply Last reply Reply Quote 1
                  • jkandasaJ Offline
                    jkandasa @Daniele
                    last edited by jkandasa

                    @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,
                    310032a4-3e1b-4191-9c0e-c42d7a1ff5f0-image.png

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post

                    0

                    Online

                    587

                    Users

                    529

                    Topics

                    3.4k

                    Posts
                    Copyright © 2015-2025 MyController.org | Contributors | Localization