Send event using java
Subscribe

From OpenNMS

Jump to: navigation, search

The Need

I've recent had the need to write my own custom java program, in order to monitor very specific items, on a very proprietary system. We couldn't install Perl on the box, due to strict vendor requirements, so the included send-event.pl script could not be used, but we were allowed to install Java.

This presented me with an easy solution. However, since Perl wasn't installed, I couldn't just fire-off send-event.pl. So, I had to write my own java code to send the event. Unfortunately, there isn't any such object or framework out there, so here is a quick class, that should do "most" of what you need it to do. First, here's the class (Obviously, you won't want to hardcode your data, as I have for this prototype):

The Code

   package com.company.onms.monitor;
   import java.io.IOException;
   import java.io.PrintWriter;
   import java.net.Socket;
   import java.net.UnknownHostException;
   import java.text.DateFormat;
   import java.util.Calendar;
   import java.util.Date;
   import java.util.HashMap;
   import java.util.Map;
   /**
    *
    * @author apaxson
    */
   public class OnmsEvent {
       private String uei;
       private String source;
       private int nodeid;
       private String time;
       private String host;
       private Map parms = new HashMap();
       public OnmsEvent(String uei, int nodeid, String host, String source) {
           setUei(uei);
           setNodeid(nodeid);
           setHost(host);
           setSource(source);
           Date currentTime = Calendar.getInstance().getTime();
           DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
           dformat.setTimeZone(TimeZone.getTimeZone("GMT"));
           setTime(dformat.format(currentTime));
       }
       protected String getUei() {
           return uei;
       }
       protected void setUei(String uei) {
           this.uei = uei;
       }
       protected String getSource() {
           return source;
       }
       protected void setSource(String source) {
           this.source = source;
       }
       protected int getNodeid() {
           return nodeid;
       }
       protected void setNodeid(int nodeid) {
           this.nodeid = nodeid;
       }
       protected String getTime() {
           return time;
       }
       protected void setTime(String time) {
           this.time = time;
       }
       protected String getHost() {
           return host;
       }
       protected void setHost(String host) {
           this.host = host;
       }
       public void addParm(String key, String value) {
           parms.put(key, value);
       }
       @SuppressWarnings(value = "unchecked")
       protected Map<String, String> getParms() {
           return parms;
       }
       protected String toXml() {
           StringBuffer data = new StringBuffer();
           data.append("<log>");
           data.append("<events>");
           data.append("<event>");
           data.append("<uei>" + getUei() + "</uei>");
           data.append("<source>" + getSource() + "</source>");
           data.append("<nodeid>" + getNodeid() + "</nodeid>");
           data.append("<time>" + getTime() + "</time>");
           data.append("<host>" + getHost() + "</host>");
           data.append("<parms>");
           // Cycle through each parameter
           for (Map.Entry<String, String> e : getParms().entrySet()) {
               data.append("<parm>");
               data.append("<parmName>");
               data.append("<![CDATA[" + e.getKey() + "]]></parmName>");
               data.append("<value type=\"string\" encoding=\"text\"><![CDATA[" + e.getValue() + "]]></value>");
               data.append("</parm>");
           }
           data.append("</parms>");
           data.append("</event>");
           data.append("</events>");
           data.append("</log>");
           return data.toString();
       }
       /**
        * TODO Remember to change the hardcoded address 2.3.4.5 to your address for onms.
        *  Preferrably, place this in a config file somewhere
        */
       public void sendEvent(String xmlData) {
           //TODO check to make sure xmlData isn't null before creating socket
           //TODO make some useful logging functions here, rather than println
           Socket socket = null;
           try {
               System.out.println("Sending data");
               socket = new Socket("2.3.4.5", 5817);
               PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
               out.println(xmlData);
               System.out.println("Sending data complete");
           } catch (UnknownHostException ex) {
               System.out.println("Unknown host");
           } catch (IOException ex) {
               System.out.println("I/O Exception while creating socket");
           } finally {
               try {
                   socket.close();
               } catch (IOException ex) {
                   System.out.println("I/O Exception when closing socket");
               }
           }
       }
   }

The Usage

Okay, so to use this new class, you create the object, and set the data. As such:

       OnmsEvent event = new OnmsEvent("uei.company.com/application/logFileTableLoadError",26,"test.host.com", "ServerLogMonitor");
       event.addParm("file", "logfile001.log");
       event.addParm("content","Disregard... testing only");
       event.sendEvent(event.toXml());