|
| 1 | +package io.covert.binary.analysis; |
| 2 | + |
| 3 | +import io.covert.util.Utils; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Map.Entry; |
| 13 | + |
| 14 | +import org.apache.hadoop.conf.Configuration; |
| 15 | +import org.apache.hadoop.io.BytesWritable; |
| 16 | +import org.apache.hadoop.io.Text; |
| 17 | +import org.apache.hadoop.mapred.JobConf; |
| 18 | +import org.apache.hadoop.mapreduce.Mapper; |
| 19 | +import org.apache.log4j.Logger; |
| 20 | + |
| 21 | +public class BinaryAnalysisMapper<K, V> extends Mapper<Text, BytesWritable, K, V> |
| 22 | +{ |
| 23 | + Logger LOG = Logger.getLogger(getClass()); |
| 24 | + |
| 25 | + OutputParser<K, V> parser = null; |
| 26 | + String program; |
| 27 | + String[] args; |
| 28 | + int[] exitCodes; |
| 29 | + Map<String, Object> substitutionMap = new HashMap<String, Object>(); |
| 30 | + long uuid = 0; |
| 31 | + File workingDir; |
| 32 | + String fileExtention; |
| 33 | + long timeoutMS; |
| 34 | + |
| 35 | + protected void setup(org.apache.hadoop.mapreduce.Mapper<Text,BytesWritable,K,V>.Context context) throws java.io.IOException ,InterruptedException |
| 36 | + { |
| 37 | + |
| 38 | + Configuration conf = context.getConfiguration(); |
| 39 | + try { |
| 40 | + parser = (OutputParser<K, V>) Class.forName(conf.get("binary.analysis.output.parser")).newInstance(); |
| 41 | + } catch (Exception e) { |
| 42 | + throw new IOException("Could create parser", e); |
| 43 | + } |
| 44 | + |
| 45 | + fileExtention = conf.get("binary.analysis.file.extention", ".dat"); |
| 46 | + timeoutMS = conf.getLong("binary.analysis.execution.timeoutMS", Long.MAX_VALUE); |
| 47 | + program = conf.get("binary.analysis.program"); |
| 48 | + args = conf.get("binary.analysis.program.args").split(conf.get("binary.analysis.program.args.delim", ",")); |
| 49 | + String[] codes = conf.get("binary.analysis.program.exit.codes").split(","); |
| 50 | + exitCodes = new int[codes.length]; |
| 51 | + for(int i = 0; i < codes.length; ++i) |
| 52 | + { |
| 53 | + exitCodes[i] = Integer.parseInt(codes[i]); |
| 54 | + } |
| 55 | + |
| 56 | + JobConf job = new JobConf(conf); |
| 57 | + |
| 58 | + // setup working dir |
| 59 | + workingDir = new File(new File(job.getJobLocalDir()).getParent(), context.getTaskAttemptID().toString()); |
| 60 | + workingDir.mkdir(); |
| 61 | + |
| 62 | + LOG.info("Working dir: "); |
| 63 | + for(File f : workingDir.listFiles()) |
| 64 | + { |
| 65 | + LOG.info(f); |
| 66 | + } |
| 67 | + |
| 68 | + LOG.info("Working dir parent: "); |
| 69 | + for(File f : workingDir.getParentFile().listFiles()) |
| 70 | + { |
| 71 | + LOG.info(f); |
| 72 | + } |
| 73 | + |
| 74 | + LOG.info("job.getLocalDirs(): "); |
| 75 | + for(String f : job.getLocalDirs()) |
| 76 | + { |
| 77 | + LOG.info(f); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + // prepare binary for exec |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + protected void map(Text key, BytesWritable value, org.apache.hadoop.mapreduce.Mapper<Text,BytesWritable,K,V>.Context context) throws java.io.IOException ,InterruptedException |
| 86 | + { |
| 87 | + uuid++; |
| 88 | + |
| 89 | + long fileCreationOverheadMS = System.currentTimeMillis(); |
| 90 | + File binaryFile = new File(workingDir, uuid+fileExtention); |
| 91 | + FileOutputStream fileOut = new FileOutputStream(binaryFile); |
| 92 | + fileOut.write(value.getBytes()); |
| 93 | + fileOut.close(); |
| 94 | + fileCreationOverheadMS = System.currentTimeMillis() - fileCreationOverheadMS; |
| 95 | + context.getCounter("STATS", "fileCreationOverheadMS").increment(fileCreationOverheadMS); |
| 96 | + |
| 97 | + substitutionMap.put("file", binaryFile.toString()); |
| 98 | + |
| 99 | + context.getCounter("STATS", "Process execution attempts").increment(1); |
| 100 | + LOG.info("Running: "+program+" args="+Arrays.toString(args)+", substitutionMap="+substitutionMap+", exitCodes="+Arrays.toString(exitCodes)+", workingDir="+workingDir); |
| 101 | + |
| 102 | + long programExecutionTimeMS = System.currentTimeMillis(); |
| 103 | + ExecutorThread exec = new ExecutorThread(program, args, substitutionMap, exitCodes, timeoutMS, workingDir); |
| 104 | + exec.start(); |
| 105 | + while(exec.isAlive()) |
| 106 | + { |
| 107 | + context.progress(); |
| 108 | + Utils.sleep(100); |
| 109 | + } |
| 110 | + exec.join(); |
| 111 | + programExecutionTimeMS = System.currentTimeMillis() - programExecutionTimeMS; |
| 112 | + context.getCounter("STATS", "programExecutionTimeMS").increment(programExecutionTimeMS); |
| 113 | + |
| 114 | + LOG.info("Process completed, elapsed="+programExecutionTimeMS+"ms, ExitCode="+exec.getExitCode()+", processStarted="+exec.isProcessStarted()+", processDestroyed="+exec.isProcessDestroyed()); |
| 115 | + if(exec.getExecuteException() != null) |
| 116 | + { |
| 117 | + LOG.error(exec.getExecuteException()); |
| 118 | + } |
| 119 | + |
| 120 | + if(exec.isProcessDestroyed()) |
| 121 | + { |
| 122 | + context.getCounter("STATS", "Process destroyed").increment(1); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + context.getCounter("STATS", "Process NOT destroyed").increment(1); |
| 127 | + } |
| 128 | + |
| 129 | + context.getCounter("EXIT CODES", ""+exec.getExitCode()).increment(1); |
| 130 | + |
| 131 | + if(exec.isProcessStarted()) |
| 132 | + { |
| 133 | + context.getCounter("STATS", "Process started").increment(1); |
| 134 | + long parseOverheadMS = System.currentTimeMillis(); |
| 135 | + // hand stdOut and stdErr to the parser |
| 136 | + parser.parse(key, value, exec.getStdOut(), exec.getStdErr()); |
| 137 | + Collection<Entry<K, V>> values = parser.getResults(); |
| 138 | + for(Entry<K, V> e : values) |
| 139 | + { |
| 140 | + context.write(e.getKey(), e.getValue()); |
| 141 | + } |
| 142 | + parseOverheadMS = System.currentTimeMillis() - parseOverheadMS; |
| 143 | + context.getCounter("STATS", "parseOverheadMS").increment(parseOverheadMS); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + context.getCounter("STATS", "Process NOT started").increment(1); |
| 148 | + } |
| 149 | + |
| 150 | + long fileDeletionOverheadMS = System.currentTimeMillis(); |
| 151 | + binaryFile.delete(); |
| 152 | + fileDeletionOverheadMS = System.currentTimeMillis() - fileDeletionOverheadMS; |
| 153 | + context.getCounter("STATS", "fileDeletionOverheadMS").increment(fileDeletionOverheadMS); |
| 154 | + } |
| 155 | + |
| 156 | + protected void cleanup(org.apache.hadoop.mapreduce.Mapper<Text,BytesWritable,K,V>.Context context) throws java.io.IOException ,InterruptedException |
| 157 | + { |
| 158 | + // cleanup working dir |
| 159 | + } |
| 160 | +} |
0 commit comments