| 1 | |
| 2 | |
| 3 | = rawDataCheck = |
| 4 | |
| 5 | * uploadHDFS 待實做 |
| 6 | * 其他單元已測試完成 |
| 7 | {{{ |
| 8 | #!java |
| 9 | /** |
| 10 | * Program: LogParser.java |
| 11 | * Editor: Waue Chen |
| 12 | * From : GTD. NCHC. Taiwn |
| 13 | * Last Update Date: 10/20/2009 |
| 14 | * support version : java 6 upper |
| 15 | * |
| 16 | * How to Use : |
| 17 | * see as SnortProduce.java |
| 18 | */ |
| 19 | |
| 20 | package tw.org.nchc.icas; |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.text.ParseException; |
| 25 | import java.text.SimpleDateFormat; |
| 26 | import java.util.Calendar; |
| 27 | |
| 28 | public class RawDataCheck { |
| 29 | // TODO improve sourcepath include Directory |
| 30 | String sourcepath; |
| 31 | String out; |
| 32 | String tmp = "/home/waue/tmp"; |
| 33 | String bak = "/home/waue/bak"; |
| 34 | |
| 35 | File[] src_files; |
| 36 | |
| 37 | RawDataCheck(String in, String out) { |
| 38 | this.sourcepath = in; |
| 39 | this.out = out; |
| 40 | |
| 41 | } |
| 42 | |
| 43 | public String now() { |
| 44 | |
| 45 | Calendar cal = Calendar.getInstance(); |
| 46 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
| 47 | return sdf.format(cal.getTime()); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | boolean checkData() { |
| 52 | File srcpath = new File(sourcepath); |
| 53 | File outpath = new File(out); |
| 54 | File tmppath = new File(tmp); |
| 55 | File bakpath = new File(bak); |
| 56 | // check path is ready |
| 57 | if (!srcpath.exists()) { |
| 58 | System.err |
| 59 | .print("Error(RawDataCheck.checkData): sourcepath is not exists."); |
| 60 | return false; |
| 61 | } |
| 62 | if (!outpath.exists()) { |
| 63 | System.err |
| 64 | .print("Error(RawDataCheck.checkData): outpathpath is not exists."); |
| 65 | return false; |
| 66 | } |
| 67 | if (!tmppath.exists()) { |
| 68 | System.err |
| 69 | .print("Error(RawDataCheck.checkData): tmp path is not exists."); |
| 70 | return false; |
| 71 | } |
| 72 | if (!bakpath.exists()) { |
| 73 | System.err |
| 74 | .print("Error(RawDataCheck.checkData): bak path is not exists."); |
| 75 | return false; |
| 76 | } |
| 77 | // check src path is a director |
| 78 | if (srcpath.isDirectory()) { |
| 79 | // point src path files |
| 80 | src_files = srcpath.listFiles(); |
| 81 | |
| 82 | if (src_files.length != 0) { |
| 83 | // list src path files |
| 84 | for (File fi : src_files) { |
| 85 | System.err |
| 86 | .println("Message(RawDataCheck.checkData): detect " |
| 87 | + fi); |
| 88 | } |
| 89 | } else { |
| 90 | System.err |
| 91 | .println("Message(RawDataCheck.checkData): There is no data in " |
| 92 | + sourcepath + "!"); |
| 93 | return false; |
| 94 | } |
| 95 | } else { |
| 96 | System.err |
| 97 | .println("Error(RawDataCheck.checkData): Input path is not a directory."); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | boolean regular() throws IOException, ParseException, Exception { |
| 105 | |
| 106 | for (File src_fi : src_files) { |
| 107 | File tmppath = new File(tmp); |
| 108 | String filename = now() + "_" + src_fi.getName() + ".log"; |
| 109 | File tmpfile = new File(tmppath + "/" + filename); |
| 110 | SnortRegular snr = new SnortRegular(src_fi, tmpfile); |
| 111 | if (snr.parseToLine()) { |
| 112 | |
| 113 | System.err.println("Message(RawDataCheck.regular): " |
| 114 | + src_fi.toString() + " -> " + tmpfile.toString() |
| 115 | + " done ."); |
| 116 | } else { |
| 117 | System.err |
| 118 | .println("Error(RawDataCheck.regular): parseToLine() error."); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | boolean uploadHDFS() { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | boolean purgeData() { |
| 131 | |
| 132 | // mv tmp data to bak path |
| 133 | File[] tmp_files = (new File(tmp)).listFiles(); |
| 134 | for (File old_file : tmp_files) { |
| 135 | // mkdir |
| 136 | File dir = new File(bak + "/parsed/"); |
| 137 | if (!dir.exists()) |
| 138 | dir.mkdir(); |
| 139 | File new_file = new File(bak + "/parsed/" + old_file.getName()); |
| 140 | if (old_file.renameTo(new_file)) { |
| 141 | System.err.println("Message(RawDataCheck.purgeData): mv " |
| 142 | + old_file.toString() + " to " + new_file.toString() |
| 143 | + " ."); |
| 144 | } else { |
| 145 | System.err |
| 146 | .println("Error(RawDataCheck.purgeData): mv file error."); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | // mv src data to bak path |
| 151 | |
| 152 | for (File old_file : src_files) { |
| 153 | // mkdir |
| 154 | File dir = new File(bak + "/raw/"); |
| 155 | if (!dir.exists()) |
| 156 | dir.mkdir(); |
| 157 | // mv file |
| 158 | File new_file = new File(bak + "/raw/" + old_file.getName()); |
| 159 | if (old_file.renameTo(new_file)) { |
| 160 | System.err.println("Message(RawDataCheck.purgeData): mv " |
| 161 | + old_file.toString() + " to " + new_file.toString() |
| 162 | + " ."); |
| 163 | } else { |
| 164 | System.err |
| 165 | .println("Error(RawDataCheck.purgeData): mv file error."); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | public static void main(String[] args) throws ParseException, Exception { |
| 174 | long Start_Time = Util.getTime(); |
| 175 | RawDataCheck rdc = new RawDataCheck("/home/waue/in", "/home/waue/out"); |
| 176 | if (!rdc.checkData()) { |
| 177 | System.err.println("Error(RawDataCheck.Main): checkData() error."); |
| 178 | return; |
| 179 | } |
| 180 | if (!rdc.regular()) { |
| 181 | System.err.println("Error(RawDataCheck.Main): regular() error ."); |
| 182 | return; |
| 183 | } |
| 184 | // if (! rdc.uploadHDFS()){ |
| 185 | // System.err.println("Error(RawDataCheck.Main): uploadHDFS() error ."); |
| 186 | // return; |
| 187 | // } |
| 188 | if (!rdc.purgeData()) { |
| 189 | System.err.println("Error(RawDataCheck.Main): purgeData() error ."); |
| 190 | return; |
| 191 | } |
| 192 | Util.calcuTime("RawDataCheck", Start_Time); |
| 193 | |
| 194 | } |
| 195 | } |
| 196 | }}} |