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

    Help to send email when node goes down

    Scheduled Pinned Locked Moved General Discussion
    scriptjavascriptemail
    8 Posts 3 Posters 2.0k Views 2 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.
    • jkandasaJ Offline
      jkandasa
      last edited by jkandasa

      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 toAddress = "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

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

        Hi Jeeva,

        Thank You for the forum article. It's really helpful.

        I need you help with respect to the JavaScript related to Node Last Seen Status. I have followed your forum article with the latest snapshot of MyController. But I get "400: Bad Request" Error when I try to run the script from Script Webpage. The mycontroller log is as follows (I have removed the actual value in fromAddress and smtpUsername below, they are updated with the required correct details)

        2017-07-10 00:20:13,293 INFO [Acme.Utils.ThreadPool(1)-PooledThread: Acme.Serve.Serve$ServeConnection@1b14d08] [org.mycontroller.standalone.email.EmailUtils:57] EmailSettings(smtpHost=smtp.live.com, smtpPort=587, fromAddress=abc@outlook.com, enableSsl=true, useStartTLS=true, smtpUsername=abc@outlook.com)
        2017-07-10 00:20:13,295 ERROR [Acme.Utils.ThreadPool(1)-PooledThread: Acme.Serve.Serve$ServeConnection@1b14d08] [org.mycontroller.standalone.utils.McScriptFileUtils:236] Exception:McScript(engineName=null, mimeType=null, extension=js, name=NodesLastSeen, canonicalPath=/home/pi/mycontroller/conf/resources/scripts/operations/NodesLastSeen.js, type=OPERATION, size=1849, lastModified=1499626206000, bindings={}),
        java.lang.NullPointerException: null
                at org.mycontroller.standalone.email.EmailUtils.sendSimpleEmail(EmailUtils.java:50)
                at org.mycontroller.standalone.email.EmailUtils.sendSimpleEmail(EmailUtils.java:40)
                at org.mycontroller.standalone.scripts.api.UtilsApi.sendEmail(UtilsApi.java:113)
                at jdk.nashorn.internal.scripts.Script$6$\^eval\_.:program(<eval>:44)
                at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
                at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
                at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
                at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:446)
                at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:403)
                at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
                at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
                at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
                at org.mycontroller.standalone.scripts.McScriptEngine.executeScript(McScriptEngine.java:78)
                at org.mycontroller.standalone.utils.McScriptFileUtils.executeScript(McScriptFileUtils.java:233)
                at org.mycontroller.standalone.api.jaxrs.ScriptsHandler.runNow(ScriptsHandler.java:139)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
                at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
                at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
                at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
                at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)
                at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202)
                at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
                at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
                at org.jboss.resteasy.plugins.server.tjws.TJWSServletDispatcher.service(TJWSServletDispatcher.java:40)
                at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
                at Acme.Serve.Serve$ServeConnection.runServlet(Serve.java:2328)
                at Acme.Serve.Serve$ServeConnection.parseRequest(Serve.java:2282)
                at Acme.Serve.Serve$ServeConnection.run(Serve.java:2054)
                at Acme.Utils$ThreadPool$PooledThread.run(Utils.java:1402)
                at java.lang.Thread.run(Thread.java:745)
        

        Can you please help me in figuring out where the error could be?
        Thank You.

        Regards,
        Nagu.

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

          @naguraob Are you using single to address and more than one email address on to?

          Try with the following scenario, It will help me to fix this issue.

          • multiple address: var toAddress= "abc@xyz.com,abcd@xyz.com";
          • Single address: var toAddress = "abc@xyz.com";
          1 Reply Last reply Reply Quote 0
          • D Offline
            dr_pub
            last edited by

            Hi all,

            small typo in the script: the variable is defined as toAddresses but used later toAddress.

            That caused the "400: Bad REquest" Error (at least on my side).

            It took me several hours to find this basic error 🙂 anyway I learned a lot with it.

            with regards,

            ps.: for all who participated in this software: thank you very much, it's a great tool and running in my home since months without any problems.

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

              @dr_pub Thank you! And sorry for the typo 😞
              I have updated the script.

              1 Reply Last reply Reply Quote 0
              • R Offline
                RMF69
                last edited by

                I'm still getting "400 Bad request" even without this type. Is there something else I have to do to enable the email feature ?

                I've tried just mcApi.utils().sendEmail("username@gmail.com", "subject", "message"); and get the same 400 Bad request.

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

                  @RMF69
                  Have you configured email server?
                  can you post your complete script?

                  R 1 Reply Last reply Reply Quote 0
                  • R Offline
                    RMF69 @jkandasa
                    last edited by

                    @jkandasa 🙂 No I hadn't, thanks, of course now its working. Great tool I'm enjoying getting to grips with the scripts and templates.

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

                    0

                    Online

                    587

                    Users

                    529

                    Topics

                    3.4k

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