Help to send email when node goes down


  • ADMIN

    I received an email from @naguraob for example script to send an email with node last seen details. I thought when we post here, will helpful for other too.

    Original request:

    Can you please help me out with some suggestion on the following, I need to send out an email whenever a node goes down, and another email with the last seen details of the nodes. It would be handy if you can provide some direction on this and I will start with it and contact you in case I run into any issues. Thank You.

    @naguraob here is the solution to send an email:

    Use case #1: send an email when a node goes down
    This is straight forward. You have to create an operation to send an email. and setup a rule to trigger this operation when your node goes down.
    0_1496995510873_upload-93591893-a725-4a9c-811f-ba237aa0050c

    0_1496995484023_upload-5aea0eef-9776-417e-9caa-82020add80ef

    Use case #2: Send node last seen status often:
    For this, we have to create a script

    JavaScript
    // Import required packages for our coding. JavaImporter is used to import packages. 
    // You can import any number of packages separated with comma.
    var myImports = new JavaImporter(java.io, java.lang, java.util,java.lang.Object);
    
    // Add our imports with loop
    with(myImports) {
      // get list of nodes
      // Create a HashMap to add our query filter values.
      var options = new HashMap();
      //Sort by lastSeen.
      options.put("orderBy", "lastSeen");
      //Order by descending
      options.put("order", "desc");
      //Page limit
      options.put("pageLimit", new Long(-1)); // -1 to list all the nodes
      //Get nodes
      var nodes = mcApi.node().getAll(options);
      
      //Email details
      var toAddresses = "abc@xyz.com,abcd@xyz.com";
      var subject = "[MyController] Nodes last seen status";
      var message = "\
              <!DOCTYPE html> \
              <html>\
                <style>body {font-size: 12px;}</style>\
                <body>\
                  <b>Dear User,</b>\
                  <br>\
                  <br>Nodes last seen details\
                  <br>\
                  <br>\
                    <table border='0'>\
                    <thead><th>Gateway</th><th>Name</th><th>EUI</th><th>State</th><th>Battery level</th><th>Last seen</th></thead>";
      for (index = 0; index < nodes.data.length; index++) {
        message += "<tr><td>"+nodes.data[index].getGatewayTable().getName()+"</td><td>"+nodes.data[index].getName()+"</td><td>"+nodes.data[index].getEui()+"</td><td>"+nodes.data[index].getState()+"</td><td>"+nodes.data[index].getBatteryLevel()+"</td><td>"+mcApi.utils().friendlyTime(nodes.data[index].getLastSeen())+"</td><tr>";
      }
    
      message += "</table>\
                  <br>\
                  <br>-- Powered by <a href='http://www.MyController.org'>www.MyController.org</a>\
                </body>\
              </html>";
      //send email
      mcApi.utils().sendEmail(toAddress, subject, message);
    }
    

    Note: Kindly use latest snapshot version from here


Log in to reply