Create custom probe using java

Document created by luscanga Employee on Jan 10, 2017
Version 1Show Document
  • View in full screen mode

In the Infrastructure Manager, in the distribution server, under archive, select new

 

 

 

Add a section, one for each Operating System Unix and Windows.

 

 

 

 Then we need to add the files that will belong to the distribution package, so we compiled in eclipse a Jar file, Integrating Two libraries Nimsoft.jar and Twofish.jar. (These two libraries can be obtained installing the SDK_Java probe in the robot where eclipse is installed or another robot and copy just the .jar files).

 

 

 

We generated a java project, and a Normal class that uses static Main method. (The example code is at the end of the document)

 

 

Then we generated a jar file exporting the project in eclipse

 

 

 

  

 

Once that we have a jar compiled, we copied to the UIM package.

 

 

 

Included also the libraries nimsoft.jar and Twofish.jar

 

 

 

 

 

 

Aditionally we included log4j.jar to the package, because this library is required in the OrderedConfig.getValue method.

 

try {

                  ret = opc.getValue(seccion, key);

            } catch (NimException e) {

                  logger.log(NimLog.FATAL, e.toString());

            }

 

 

 

 

In the code we read the config from the .cfg file, so we included in the package

 

 

 

Verify that the path of the files is the same as the Workdir of the probe properties.

 

 

 

Command

<startup java>

Arguments

-classpath ./tp4.jar:./nimsoft.jar:./Twofish.jar:./log4j-1.2.15.jar testprobe4.tp1

Config File

The name of the config file. This value was configured hardcoded in the java class, and when you configure the probe this file is readed and saved.

Log file

The name of the logfile. This value was configured hardcoded in the java class, and when you view the log in the Infrastructure Manager this file is readed.

 

We distributed as any other probe.When you click configure appears the raw configuration. You can add sections and keys that you need

 

And View log displays the log that you defined

And here is the probe managed by the robot.

 

 

 

 

Java Code

 

Include in the project Nimsoft.jar and Twofish.jar

 

package testprobe4;

 

import com.nimsoft.nimbus.NimAlarm;

import com.nimsoft.nimbus.NimConfig;

import com.nimsoft.nimbus.NimException;

import com.nimsoft.nimbus.NimLog;

import com.nimsoft.nimbus.NimPost;

import com.nimsoft.nimbus.PDS;

import com.nimsoft.nimbus.extension.OrderedConfig;

 

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.util.Date;

 

public class tp1 {

 

      static String cfg_file = "tp1.cfg";

      static String log_file = "tp1.log";

      static int loglevel=5;

      static NimLog logger = NimLog.getLogger(tp1.class);

 

/*

* Routine to get a value from Ordered Config

* You specify all the path to the key and returns the value

* If the value is not found returns N/A as value

*/

      public static String cfg_get_value(OrderedConfig opc,String llave) {

            String ret="N/A";

            String key = llave.replaceAll(".*/", "");

            String seccion = llave.replaceAll("/"+key, "");

            try {

                  ret = opc.getValue(seccion, key);

            } catch (NimException e) {

                  logger.log(NimLog.FATAL, e.toString());

            }

            return(ret);

      }

     

      public static void send_alarm() {

           

      }

 

      /*

      * Routine read the .cfg file

      * and retrieve the configuration into local values to be used by the

      * class

      *

      * If all the values are found and read returns true else returns false

      */

      public static boolean read_config(String cfg_file) {

            boolean ret=true;

            OrderedConfig opc1;

            File f = new File(cfg_file);

            if (f.exists()) {

                  try {

                        opc1 = new OrderedConfig(cfg_file);

                        String sloglevel =cfg_get_value(opc1,"/setup/loglevel");

                        try {

                             loglevel=Integer.parseInt(sloglevel);

                        } catch (NumberFormatException e) {

                             ret=false;

                        }

                        /*

                  System.out.println(opc1);

                  String[] sl1 = opc1.getSectionList("/");

                  System.out.println("seclist:"+sl1.length);

                  System.out.println(sl1[0]);

                  String[] sl2 = opc1.getSectionList("/seccion1");

                  System.out.println(sl2.length);

                  System.out.println(sl2[0]);

                  String[] vals = opc1.getKeyList("/seccion1");

                  System.out.println("vals:"+vals.length+":"+vals[0]);

                  String valor1 = opc1.getValue("/seccion1", "llave1");

                  System.out.println("valor1:"+valor1);

                        */

                        /*

                  try {

                        ret = opc.getValue(seccion, key, "");

                  } catch (NimException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                  }

                        */

                  } catch (NimException e) {

                        logger.log(NimLog.FATAL,e.toString());

                        loglevel=5;

                  }

            }

            return(ret);

 

      }

 

      public static void main(String[] args) {

 

/*

            if (true)

                  return;

*/

            // define logger file class.log

            try {

                  logger.setFilename(log_file);

                  /*

                  * These are the posible values returned at specified loglevel

                  */

                  /*

                  logger.log(NimLog.TRACE, "trace"); //Level 5

                  logger.log(NimLog.DEBUG, "debug");// Level 4

                  logger.log(NimLog.INFO, "info");   //Level 3

                  logger.log(NimLog.WARN, "warn");   //Level 2

                  logger.log(NimLog.FATAL, "fatal"); //Level 0

                  */

            } catch (NimException e2) {

                  e2.printStackTrace();

            }

            // read configuration

            if (read_config(cfg_file)) {

                  logger.log(NimLog.INFO, ">Started loglevel:"+loglevel);

                  logger.setLogLevel(loglevel);

            } else {

                  logger.log(NimLog.FATAL, "No puedo leer el archivo de configuracion");

                 

            }

            while (true) {

                  try {

                        Thread.sleep(10*1000);

                  } catch (InterruptedException e) {

                        e.printStackTrace();

                  }

                  Date d = new Date();

                  logger.log(NimLog.INFO, d+"......");

                  try {

                        NimAlarm alarm = new NimAlarm(NimAlarm.NIML_WARNING, d+" Alarm generated from java", "JAVA", "JAVA", "hw");

                        String id = alarm.send();

                        /*

                        // Clear previous critical alarm

                        alarm.setSeverity(NimAlarm.NIML_CLEAR);

                        alarm.send();

                        */

                        alarm.close();

                  } catch (NimException ne) {

                        System.out.println("Hello world, alarm error: " + ne.getMessage());

                  }

            }

      }

 

}

2 people found this helpful

Attachments

    Outcomes