root/src/java/src/com/omniti/reconnoiter/StratconMessage.java

Revision d0a64b649e4eac288431ac20d986830b57fb044d, 6.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

Add support for riemann as the IEP subsystem.
Remove all traces of Esper.
Change the license on all our bits to simply match reconnoiter.
Cleanup copyrights and embelish auditing script.
Updated test 108 to check riemann iep results.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2013, Circonus, Inc. All rights reserved.
3  * Copyright (c) 2010, OmniTI Computer Consulting, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 package com.omniti.reconnoiter;
35
36 import java.io.StringReader;
37 import java.util.HashMap;
38 import java.lang.reflect.Constructor;
39 import java.lang.reflect.Method;
40 import java.lang.reflect.InvocationTargetException;
41 import java.security.MessageDigest;
42 import com.omniti.reconnoiter.IEventHandler;
43 import com.omniti.reconnoiter.event.*;
44
45 public abstract class StratconMessage {
46   // This is the text type in the noit_log.h
47   private static final ThreadLocal < MessageDigest > md_tls =
48     new ThreadLocal < MessageDigest > () {
49       @Override protected MessageDigest initialValue() {
50         MessageDigest md;
51         try { md = MessageDigest.getInstance("MD5"); }
52         catch(java.security.NoSuchAlgorithmException nsae) {
53           throw new RuntimeException(nsae);
54         }
55         return md;
56       }
57     };
58   public final static String METRIC_STRING = "s";
59   public final static HashMap<String,StratconMessageFactory> quicklookup = new HashMap<String,StratconMessageFactory>();
60   protected byte[] signature;
61
62   public static void ignorePrefix(String prefix) {
63     quicklookup.put(prefix, null);
64   }
65   @SuppressWarnings("unchecked")
66   public static boolean registerType(Class clazz) {
67     boolean success = false;
68     try {
69       Method meth = clazz.getMethod("getPrefix");
70       String prefix = (String)meth.invoke(clazz.newInstance());
71       StratconMessageFactory smf = new StratconMessageFactory(clazz);
72       quicklookup.put(prefix, smf);
73       success = true;
74     } catch (NoClassDefFoundError e) {
75     } catch (NoSuchMethodException e) {
76       e.printStackTrace();
77     } catch (IllegalAccessException e) {
78       e.printStackTrace();
79     } catch (InvocationTargetException e) {
80       e.printStackTrace();
81     } catch (InstantiationException e) {
82       e.printStackTrace();
83     }
84     return success;
85   } 
86   public static String[] parseToArray(String jlog, int num) {
87     // Get rid of the null parameter
88     String operand = jlog.endsWith("\n") ? jlog.substring(0, jlog.length()-1)
89                                          : jlog;
90     return operand.split("[\t]", num);
91   }
92
93   public String getPrefix() { return null; }
94
95   protected long timeToLong(String time) {
96     long ms = 0;
97     int off = time.lastIndexOf('.');
98     try {
99       if(off == -1) {
100         ms = Long.valueOf(time).longValue() * 1000;
101       }
102       else {
103         ms = Long.valueOf(time.substring(0,off)).longValue() * 1000;
104         ms = ms + Long.valueOf(time.substring(off+1)).longValue();
105       }
106     }
107     catch (NumberFormatException e) {
108       return 0;
109     }
110     return ms;
111   }
112
113   public int numparts() { return 0; }
114   public byte[] signature() { return signature; }
115   public abstract void handle(IEventHandler eh);
116
117   public StratconMessage() {}
118   // Check and make sure
119   public StratconMessage(String[] parts) throws Exception {
120     if (!parts[0].equals(this.getPrefix())) {
121       throw new Exception("Incorrect state prefix:" + getPrefix() + " not applicable for " + getClass());
122     }
123     if (parts.length != numparts()) {
124       throw new Exception("Incorrect amount of parts parsed, tossing message.");
125     }
126
127     MessageDigest md = md_tls.get();
128     for(int i=0; i<parts.length;i++) {
129       if(parts[i] != null) {
130         byte[] piece = parts[i].getBytes();
131         if(piece.length > 0) md.update(piece);
132       }
133     }
134     signature = md.digest();
135   }
136
137   public static StratconMessage makeMessage(String jlog) {
138     String[] parts;
139     if(jlog == null || jlog.length() == 0) return null;
140     int idxOftab = jlog.indexOf('\t');
141     if(idxOftab < 0) return null;
142     // The numbers of the parse are pulled from stratcon and
143     // +1 for the extra remote
144     try {
145       String prefix = jlog.substring(0, idxOftab);
146
147       StratconMessageFactory smf = quicklookup.get(prefix);
148       if(smf == null) {
149         return null;
150       }
151       parts = parseToArray(jlog, smf.numparts());
152       return smf.make(parts);
153     }
154     catch(Exception e) {
155       System.err.println("makeMessage: " + e);
156       e.printStackTrace();
157     }
158     return null;
159   }
160   public static StratconMessage[] makeMessages(String big) {
161     String[] lines = big.split("[\n]");
162     StratconMessage[] messages = new StratconMessage[lines.length];
163     for (int i = 0; i < messages.length; i++) {
164       messages[i] = makeMessage(lines[i]);
165     }
166     return messages;
167   }
168   static {
169     StratconMessage.registerType(NoitCheck.class);
170     StratconMessage.registerType(NoitStatus.class);
171     StratconMessage.registerType(NoitMetric.class);
172     StratconMessage.registerType(NoitBundlev1.class);
173     StratconMessage.registerType(NoitBundlev2.class);
174     StratconMessage.ignorePrefix("n");
175   }
176 }
Note: See TracBrowser for help on using the browser.