[32] | 1 | /** |
---|
| 2 | * Program: LogParserGo.java |
---|
| 3 | * Editor: Waue Chen |
---|
| 4 | * From : NCHC. Taiwn |
---|
| 5 | * Last Update Date: 07/02/2008 |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * Purpose : |
---|
| 9 | * This program will parse your apache log and store it into Hbase. |
---|
| 10 | * |
---|
| 11 | * HowToUse : |
---|
| 12 | * Make sure two thing : |
---|
| 13 | * 1. Upload apache logs ( /var/log/apache2/access.log* ) to \ |
---|
| 14 | * hdfs (default: /user/waue/apache-log) \ |
---|
| 15 | * $ bin/hadoop dfs -put /var/log/apache2/ apache-log |
---|
| 16 | * 2. parameter "dir" in main contains the logs. |
---|
| 17 | * 3. you should filter the exception contents manually, \ |
---|
| 18 | * ex: ::1 - - [29/Jun/2008:07:35:15 +0800] "GET / HTTP/1.0" 200 729 "... |
---|
| 19 | * |
---|
| 20 | * Check Result: |
---|
| 21 | * Go to hbase console, type : |
---|
| 22 | * hql > select * from apache-log; |
---|
| 23 | |
---|
[45] | 24 | +-------------------------+-------------------------+-------------------------+ |
---|
| 25 | | Row | Column | Cell | |
---|
| 26 | +-------------------------+-------------------------+-------------------------+ |
---|
| 27 | | 118.170.101.250 | http:agent | Mozilla/4.0 (compatible;| |
---|
| 28 | | | | MSIE 4.01; Windows 95) | |
---|
| 29 | +-------------------------+-------------------------+-------------------------+ |
---|
| 30 | | 118.170.101.250 | http:bytesize | 318 | |
---|
| 31 | +-------------------------+-------------------------+-------------------------+ |
---|
| 32 | ..........(skip)........ |
---|
| 33 | +-------------------------+-------------------------+-------------------------+ |
---|
| 34 | | 87.65.93.58 | http:method | OPTIONS | |
---|
| 35 | +-------------------------+-------------------------+-------------------------+ |
---|
| 36 | | 87.65.93.58 | http:protocol | HTTP/1.1 | |
---|
| 37 | +-------------------------+-------------------------+-------------------------+ |
---|
| 38 | | 87.65.93.58 | referrer:- | * | |
---|
| 39 | +-------------------------+-------------------------+-------------------------+ |
---|
| 40 | | 87.65.93.58 | url:* | - | |
---|
| 41 | +-------------------------+-------------------------+-------------------------+ |
---|
| 42 | 31 row(s) in set. (0.58 sec) |
---|
[32] | 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | */ |
---|
| 47 | package tw.org.nchc.code; |
---|
| 48 | |
---|
| 49 | import java.io.IOException; |
---|
[46] | 50 | import java.text.ParsePosition; |
---|
[45] | 51 | import java.text.SimpleDateFormat; |
---|
[46] | 52 | import java.util.Date; |
---|
[45] | 53 | import java.util.Locale; |
---|
[32] | 54 | |
---|
| 55 | import org.apache.hadoop.fs.FileStatus; |
---|
| 56 | import org.apache.hadoop.fs.FileSystem; |
---|
| 57 | import org.apache.hadoop.fs.Path; |
---|
| 58 | import org.apache.hadoop.hbase.HBaseAdmin; |
---|
| 59 | import org.apache.hadoop.hbase.HBaseConfiguration; |
---|
| 60 | import org.apache.hadoop.hbase.HColumnDescriptor; |
---|
| 61 | import org.apache.hadoop.hbase.HTable; |
---|
| 62 | import org.apache.hadoop.hbase.HTableDescriptor; |
---|
| 63 | import org.apache.hadoop.io.Text; |
---|
| 64 | import org.apache.hadoop.io.Writable; |
---|
| 65 | import org.apache.hadoop.io.WritableComparable; |
---|
| 66 | import org.apache.hadoop.mapred.ClusterStatus; |
---|
| 67 | import org.apache.hadoop.mapred.JobClient; |
---|
| 68 | import org.apache.hadoop.mapred.JobConf; |
---|
| 69 | import org.apache.hadoop.mapred.MapReduceBase; |
---|
| 70 | import org.apache.hadoop.mapred.Mapper; |
---|
| 71 | import org.apache.hadoop.mapred.OutputCollector; |
---|
| 72 | import org.apache.hadoop.mapred.Reporter; |
---|
| 73 | |
---|
[45] | 74 | class Log { |
---|
[47] | 75 | String gid, sid, version; |
---|
[45] | 76 | |
---|
[47] | 77 | String alert_name, class_type, priority; |
---|
[45] | 78 | |
---|
[47] | 79 | String source, destination, type; |
---|
| 80 | |
---|
| 81 | // String ttl, tos, id, iplen, dgmlen; |
---|
| 82 | |
---|
| 83 | String srcport, dstport,tmp; |
---|
| 84 | public Log(String data) { |
---|
| 85 | |
---|
[45] | 86 | String[] arr = data.split(";"); |
---|
| 87 | this.gid = arr[0]; |
---|
| 88 | this.sid = arr[1]; |
---|
| 89 | this.version = arr[2]; |
---|
| 90 | this.alert_name = arr[3]; |
---|
| 91 | this.class_type = arr[4]; |
---|
| 92 | this.priority = arr[5]; |
---|
[47] | 93 | this.timestamp = getTime(arr[7] + "/" + arr[6] + ":" + arr[8] + ":" |
---|
| 94 | + arr[9] + ":" + arr[10]); |
---|
| 95 | this.source = getIP(arr[11]); |
---|
| 96 | this.srcport = this.tmp; |
---|
| 97 | this.destination = getIP(arr[12]); |
---|
| 98 | this.dstport = this.tmp; |
---|
[45] | 99 | this.type = arr[13]; |
---|
[47] | 100 | // this.ttl = arr[14]; |
---|
| 101 | // this.tos = arr[15]; |
---|
| 102 | // this.id = arr[16]; |
---|
| 103 | // this.iplen = arr[17]; |
---|
| 104 | // this.dgmlen = arr[18]; |
---|
| 105 | |
---|
[45] | 106 | } |
---|
| 107 | long timestamp; |
---|
| 108 | |
---|
| 109 | |
---|
[47] | 110 | String getIP(String str){ |
---|
| 111 | String res; |
---|
| 112 | int n = str.indexOf(":"); |
---|
| 113 | if (n == -1) { |
---|
| 114 | res = str; |
---|
| 115 | this.tmp = "0"; |
---|
| 116 | } else { |
---|
| 117 | String[] vec = str.split(":"); |
---|
| 118 | res = vec[0]; |
---|
| 119 | this.tmp = vec[1]; |
---|
| 120 | } |
---|
| 121 | return res; |
---|
| 122 | } |
---|
[45] | 123 | |
---|
[46] | 124 | long getTime(String str) { |
---|
| 125 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM:HH:mm:ss", |
---|
[45] | 126 | Locale.TAIWAN); |
---|
[47] | 127 | Long timestamp = sdf.parse(str, new ParsePosition(0)).getTime(); |
---|
[45] | 128 | return timestamp; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
[32] | 132 | // import AccessLogParser |
---|
| 133 | public class SnortBase { |
---|
| 134 | static HBaseConfiguration conf = new HBaseConfiguration(); |
---|
| 135 | |
---|
| 136 | public static final String TABLE = "table.name"; |
---|
| 137 | |
---|
[47] | 138 | static String tableName = "flex"; |
---|
[32] | 139 | |
---|
| 140 | static HTable table = null; |
---|
[45] | 141 | |
---|
[32] | 142 | public static class MapClass extends MapReduceBase implements |
---|
| 143 | Mapper<WritableComparable, Text, Text, Writable> { |
---|
| 144 | |
---|
| 145 | @Override |
---|
[45] | 146 | // MapReduceBase.configure(JobConf job) |
---|
[32] | 147 | // Default implementation that does nothing. |
---|
| 148 | public void configure(JobConf job) { |
---|
[45] | 149 | // String get(String name,String defaultValue) |
---|
[32] | 150 | // Get the value of the name property. If no such property exists,\ |
---|
[45] | 151 | // then defaultValue is returned. |
---|
[32] | 152 | } |
---|
| 153 | |
---|
| 154 | public void map(WritableComparable key, Text value, |
---|
| 155 | OutputCollector<Text, Writable> output, Reporter reporter) |
---|
[47] | 156 | throws IOException { |
---|
[45] | 157 | |
---|
[47] | 158 | // try { |
---|
[45] | 159 | |
---|
[47] | 160 | Log log = new Log(value.toString()); |
---|
[45] | 161 | |
---|
[47] | 162 | // 查看value的值 |
---|
| 163 | // FileWriter out = new FileWriter(new File( |
---|
| 164 | // "/home/waue/Desktop/snort-result.txt")); |
---|
| 165 | // out.write(value.toString() + "_time=" + log.timestamp + "\n"); |
---|
| 166 | // out.flush(); |
---|
| 167 | // out.close(); |
---|
[45] | 168 | |
---|
[47] | 169 | if (table == null) |
---|
| 170 | table = new HTable(conf, new Text(tableName)); |
---|
[45] | 171 | |
---|
[47] | 172 | long lockId = table.startUpdate(new Text(log.destination)); |
---|
| 173 | table.put(lockId, new Text("id:gid"), log.gid.getBytes()); |
---|
| 174 | table.put(lockId, new Text("id:sid"), log.sid.getBytes()); |
---|
| 175 | table.put(lockId, new Text("id:version"), log.version.getBytes()); |
---|
| 176 | table.put(lockId, new Text("name:name"), log.alert_name.getBytes()); |
---|
| 177 | table |
---|
| 178 | .put(lockId, new Text("name:class"), log.class_type |
---|
| 179 | .getBytes()); |
---|
| 180 | table.put(lockId, new Text("id:priority"), log.priority |
---|
| 181 | .getBytes()); |
---|
| 182 | table.put(lockId, new Text("direction:soure"), log.source.getBytes()); |
---|
| 183 | table.put(lockId, new Text("direction:srcport"), log.srcport.getBytes()); |
---|
| 184 | table.put(lockId, new Text("direction:dstport"), log.dstport.getBytes()); |
---|
| 185 | table.put(lockId, new Text("payload:type"), log.type.getBytes()); |
---|
| 186 | // table.put(lockId, new Text("payload:ttl"), log.ttl.getBytes()); |
---|
| 187 | // table.put(lockId, new Text("payload:tos"), log.tos.getBytes()); |
---|
| 188 | // table.put(lockId, new Text("payload:id"), log.id.getBytes()); |
---|
| 189 | // table.put(lockId, new Text("payload:iplen"), log.iplen |
---|
| 190 | // .getBytes()); |
---|
| 191 | // table.put(lockId, new Text("payload:dgmlen"), log.dgmlen |
---|
| 192 | // .getBytes()); |
---|
| 193 | table.commit(lockId, log.timestamp); |
---|
[45] | 194 | |
---|
[47] | 195 | // } catch (Exception e) { |
---|
| 196 | // e.printStackTrace(); |
---|
| 197 | // } |
---|
| 198 | |
---|
[32] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | // do it to resolve warning : FileSystem.listPaths |
---|
| 203 | static public Path[] listPaths(FileSystem fsm, Path path) |
---|
| 204 | throws IOException { |
---|
| 205 | FileStatus[] fss = fsm.listStatus(path); |
---|
| 206 | int length = fss.length; |
---|
| 207 | Path[] pi = new Path[length]; |
---|
| 208 | for (int i = 0; i < length; i++) { |
---|
| 209 | pi[i] = fss[i].getPath(); |
---|
| 210 | } |
---|
| 211 | return pi; |
---|
| 212 | } |
---|
| 213 | |
---|
[46] | 214 | public static void runMapReduce(String tableName, String inpath) |
---|
[32] | 215 | throws IOException { |
---|
| 216 | Path tempDir = new Path("/tmp/Mylog/"); |
---|
[45] | 217 | Path InputPath = new Path(inpath); |
---|
[32] | 218 | FileSystem fs = FileSystem.get(conf); |
---|
| 219 | JobConf jobConf = new JobConf(conf, SnortBase.class); |
---|
[45] | 220 | jobConf.setJobName("Snort Parse"); |
---|
[46] | 221 | jobConf.set(TABLE, tableName); |
---|
[45] | 222 | // 先省略 自動搜尋目錄的功能 |
---|
[47] | 223 | // Path InputDir = new Path(inpath); |
---|
| 224 | // Path[] in = listPaths(fs, InputDir); |
---|
| 225 | // if (fs.isFile(InputDir)) |
---|
| 226 | // { |
---|
| 227 | // jobConf.setInputPath(InputDir); |
---|
| 228 | // } |
---|
| 229 | // else{ |
---|
| 230 | // for (int i = 0; i < in.length; i++){ |
---|
| 231 | // if (fs.isFile(in[i])){ |
---|
| 232 | // jobConf.addInputPath(in[i]); |
---|
| 233 | // } else |
---|
| 234 | // { |
---|
| 235 | // Path[] sub = listPaths(fs, in[i]); |
---|
| 236 | // for (int j = 0; j < sub.length; j++) |
---|
| 237 | // { |
---|
| 238 | // if (fs.isFile(sub[j])) |
---|
| 239 | // { |
---|
| 240 | // jobConf.addInputPath(sub[j]); |
---|
| 241 | // } } } } } |
---|
| 242 | |
---|
[45] | 243 | jobConf.setInputPath(InputPath); |
---|
[32] | 244 | jobConf.setOutputPath(tempDir); |
---|
| 245 | jobConf.setMapperClass(MapClass.class); |
---|
| 246 | JobClient client = new JobClient(jobConf); |
---|
| 247 | ClusterStatus cluster = client.getClusterStatus(); |
---|
| 248 | jobConf.setNumMapTasks(cluster.getMapTasks()); |
---|
| 249 | jobConf.setNumReduceTasks(0); |
---|
[46] | 250 | fs.delete(tempDir); |
---|
[32] | 251 | JobClient.runJob(jobConf); |
---|
| 252 | fs.delete(tempDir); |
---|
| 253 | fs.close(); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | public static void creatTable(String table) throws IOException { |
---|
| 257 | HBaseAdmin admin = new HBaseAdmin(conf); |
---|
| 258 | if (!admin.tableExists(new Text(table))) { |
---|
| 259 | System.out.println("1. " + table |
---|
| 260 | + " table creating ... please wait"); |
---|
| 261 | HTableDescriptor tableDesc = new HTableDescriptor(table); |
---|
[45] | 262 | tableDesc.addFamily(new HColumnDescriptor("id:")); |
---|
| 263 | tableDesc.addFamily(new HColumnDescriptor("name:")); |
---|
[47] | 264 | tableDesc.addFamily(new HColumnDescriptor("direction:")); |
---|
[45] | 265 | tableDesc.addFamily(new HColumnDescriptor("payload:")); |
---|
[32] | 266 | admin.createTable(tableDesc); |
---|
| 267 | } else { |
---|
| 268 | System.out.println("1. " + table + " table already exists."); |
---|
| 269 | } |
---|
| 270 | System.out.println("2. access_log files fetching using map/reduce"); |
---|
| 271 | } |
---|
| 272 | |
---|
[45] | 273 | public static void main(String[] args) throws IOException, Exception { |
---|
[46] | 274 | |
---|
[47] | 275 | String path = "/user/waue/snort-log/alert_flex_parsed.txt"; |
---|
[32] | 276 | |
---|
[45] | 277 | // 先省略掉 parse完後自動上傳部份 |
---|
| 278 | /* |
---|
| 279 | * SnortParser sp = new |
---|
| 280 | * SnortParser("/tmp/alert","/tmp/alert_SnortBase"); sp.parseToLine(); |
---|
| 281 | */ |
---|
[46] | 282 | creatTable(tableName); |
---|
[32] | 283 | |
---|
[46] | 284 | runMapReduce(tableName, path); |
---|
[45] | 285 | |
---|
[32] | 286 | } |
---|
| 287 | |
---|
| 288 | } |
---|