Ignore:
Timestamp:
Jul 22, 2008, 5:55:42 PM (16 years ago)
Author:
waue
Message:

little tunning SnortParser?.java

new SnortUploadHbase? to parse data to hbase, testing

Location:
sample/hadoop-0.16/tw/org/nchc/code
Files:
1 added
9 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • sample/hadoop-0.16/tw/org/nchc/code/SnortBase.java

    r32 r33  
    206206
    207207  public static void main(String[] args) throws IOException {
    208     String table_name = "apache-log2";
     208    String table_name = "snort";
    209209    String dir = "/user/waue/apache-log";
    210210   
  • sample/hadoop-0.16/tw/org/nchc/code/SnortParser.java

    r32 r33  
    1515import java.io.IOException;
    1616import java.text.ParseException;
    17 import java.util.StringTokenizer;
    1817import java.util.regex.Matcher;
    1918import java.util.regex.Pattern;
    2019
    2120public class SnortParser {
    22   private String logData = new String();
    23   private BufferedReader fi ;
    24   private BufferedWriter fw ;
    25   public SnortParser(String in, String ou) throws IOException {
    26     fi = new BufferedReader(new FileReader(new File(in)));
    27     fw = new BufferedWriter(new FileWriter(new File(ou)));
     21  private String logData = new String();
     22
     23  private String in;
     24
     25  private String ou;
     26
     27  public SnortParser(String in, String ou) {
     28    this.in = in;
     29    this.ou = ou;
    2830  }
    2931
    30   public static boolean isIpAddress(String inputString) {
    31     StringTokenizer tokenizer = new StringTokenizer(inputString, ".");
    32     if (tokenizer.countTokens() != 4) {
    33       return false;
    34     }
    35     try {
    36       for (int i = 0; i < 4; i++) {
    37         String t = tokenizer.nextToken();
    38         int chunk = Integer.parseInt(t);
    39         if ((chunk & 255) != chunk) {
    40           return false;
    41         }
    42       }
    43     } catch (NumberFormatException e) {
    44       return false;
    45     }
    46     if (inputString.indexOf("..") >= 0) {
    47       return false;
    48     }
    49     return true;
     32  public SnortParser() {
     33    this.in = "/var/log/snort/alert";
     34    this.ou = "~/parseSnort.log";
    5035  }
    5136
    5237  public void snortParser(String line, int i) throws ParseException,
    5338      Exception {
    54     String[] data ;
     39    String[] data;
    5540    Pattern patten_line;
    56     Matcher matcher;   
     41    Matcher matcher;
    5742    switch (i) {
    5843    case 1:
     
    8166      data = new String[number];
    8267      for (int j = 0; j < number; j++) {
    83         data[j] = matcher.group(j+1);
    84         this.logData += (data[j]+";");
     68        data[j] = matcher.group(j + 1);
     69        this.logData += (data[j] + ";");
    8570      }
    86      
     71
    8772    }
    88    
     73
    8974  }
    90   void parseToLine() throws IOException,ParseException,Exception {
    9175
     76  void parseToLine() throws IOException, ParseException, Exception {
     77    BufferedReader fi = new BufferedReader(new FileReader(new File(in)));
     78    BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou)));
    9279    String line = null;
    9380    int count = 0;
    94 
    9581    do {
    96       String tmp = this.fi.readLine();
     82      String tmp = fi.readLine();
    9783      if (tmp == null) {
    9884        break;
    99       }else if(count < 4){
     85      } else if (count < 4) {
    10086        line = tmp;
    101 //        System.out.println(line);
    102         snortParser(line, count+1);
    103         count ++;
    104       }else if(count ==4 ){
    105         count ++;
    106       }else if (count == 5){
    107         this.fw.write(this.logData.toString() + "\n");
    108         this.logData = "" ;
     87        // System.out.println(line);
     88        snortParser(line, count + 1);
     89        count++;
     90      } else if (count == 4) {
     91        count++;
     92      } else if (count == 5) {
     93        fw.write(this.logData.toString() + "\n");
     94        this.logData = "";
    10995        count = 0;
    110       }
    111       else
    112       {
     96      } else {
    11397        System.err.print(" Error ! ");
    114         return ;
     98        return;
    11599      }
    116100    } while (true);
    117     this.fw.flush();
    118     this.fw.close();
     101    fw.flush();
     102    fw.close();
    119103
    120104  }
     105
    121106  public static void main(String[] args) throws ParseException, Exception {
    122107    String in = new String("/home/waue/Desktop/alert");
    123108    String ou = new String("/home/waue/Desktop/bb");
    124     SnortParser a = new SnortParser(in,ou);
     109    SnortParser a = new SnortParser(in, ou);
    125110    a.parseToLine();
    126111  }
Note: See TracChangeset for help on using the changeset viewer.