Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package parse;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Objects;
  5. import org.javatuples.Pair;
  6.  
  7. public class AuParser {
  8.  
  9.     private final static String libraryName = "au";
  10.  
  11.     static {
  12.         System.loadLibrary(libraryName);
  13.     }
  14.  
  15.     private long address;
  16.  
  17.     public AuParser() {
  18.     }
  19.  
  20.     private native int firstField();
  21.     private native int firstRecord();
  22.     private native int nextField();
  23.     private native int reset();
  24.     private native int getMilliSeconds();
  25.     private native long getSerial();
  26.     private native long getSeconds();
  27.     private native long init(String log);
  28.     private native String getHost();
  29.     private native String getFieldName();
  30.     private native String getFieldStr();
  31.     private native String findField(String name);
  32.     private native String interpretField();
  33.  
  34.     public HashMap parseEvent(String log) {
  35.         HashMap<String, Object> event = new HashMap<>();
  36.         Pair<String, String> parsedLine = parseFlumeLine(log);
  37.         String line = parsedLine.getValue0();
  38.         String host = parsedLine.getValue1();
  39.         address = init(line);
  40.         firstRecord();
  41.         firstField();
  42.         while (true) {
  43.             String field = getFieldName();
  44.             String fieldValue = getFieldStr();
  45.             if (Objects.equals(field, "hostname")) {
  46.                 fieldValue = fieldValue.substring(1, fieldValue.length());
  47.             }
  48.             event.put(field, fieldValue);
  49.             if (nextField() == 0)
  50.                 break;
  51.         }
  52.         reset();
  53.         firstRecord();
  54.         if (!event.containsKey("type")) {
  55.             event.put("type", "TRASH");
  56.         }
  57.         if (event.get("type").equals("SYSCALL")) {
  58.             findField("syscall");
  59.             event.put("syscall", interpretField());
  60.         }
  61.         int milliSeconds = getMilliSeconds();
  62.         long seconds = getSeconds();
  63.         int timestamp = (int)(1000*seconds + (float)milliSeconds);
  64.         event.put("timestamp", timestamp);
  65.         event.put("event_id", getSerial());
  66.         event.putIfAbsent("hostname", host);
  67.         return event;
  68.     }
  69.  
  70.     private Pair<String, String> parseFlumeLine(String log) {
  71.         char tenth = log.charAt(10);
  72.         int ord = (int) tenth;
  73.         int hostIndex= 11 + ord/2;
  74.         int k = 3;
  75.         if (log.charAt(hostIndex + 2) == 't') {
  76.             k = 2;
  77.         }
  78.         int logLength = log.length();
  79.         String line = log.substring(hostIndex + k, logLength);
  80.         String host = log.substring(11, hostIndex);
  81.         return new Pair<>(line, host);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement