Changeset 30662
- Timestamp:
- Feb 17, 2011, 8:53:01 AM (15 years ago)
- Location:
- trunk/tools/log_parsing
- Files:
-
- 1 added
- 1 edited
-
nebulous_activity.py (added)
-
parse_apache_log.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/log_parsing/parse_apache_log.py
r30522 r30662 4 4 import re 5 5 import datetime 6 import fileinput 6 7 7 8 nebulousPattern = re.compile('POST /nebulous HTTP/1.1') 9 errorPattern = re.compile('exit signal Segmentation fault') 10 8 11 # Resolution: 10 minutes of 60 seconds of 1000000 microseconds 9 12 datetime.resolution = 10*60*1000000 10 13 11 14 def message(size): 15 """ 16 Guess the message type according to the message size 17 """ 12 18 if size == 488: 13 19 # delete == 488 … … 34 40 35 41 def show_results(counts, output=sys.stderr): 42 """ 43 Display results 44 """ 36 45 output.write('# Epoch | Create | FindIn | Delete | FailSt | Stat | Unkn. | NonNeb\n') 37 46 for count in sorted(counts.iterkeys()): … … 45 54 counts[count][6])) 46 55 47 if __name__ == '__main__': 56 def process(linesInFile = None): 57 """ 58 Process Nebulous entries in apache2 log file 59 """ 48 60 counts = dict() 49 50 61 linesCount = 1 51 for line in sys.stdin: 52 if linesCount % 10000 == 0: 53 sys.stderr.write('Analyzed lines: %d\n' % linesCount) 54 if linesCount % 100000 == 0: 62 if linesInFile == None: 63 input = fileinput.input() 64 else: 65 input = linesInFile 66 for line in input: 67 if linesCount % 100000 == 0: 68 sys.stderr.write('\tAnalyzed lines: %d\n' % linesCount) 69 if linesCount % 500000 == 0: 55 70 show_results(counts) 56 71 linesCount += 1 … … 85 100 counts[dt.isoformat()] = [0, 0, 0, 0, 0, 0, 0] 86 101 counts[dt.isoformat()][6] += 1 102 return counts 87 103 88 show_results(counts, sys.stdout) 104 def show_errors(counts, output=sys.stderr): 105 """ 106 Display error results 107 """ 108 output.write('# Epoch | Errors\n') 109 for count in sorted(counts.iterkeys()): 110 output.write('%19s | %6d\n' % (count, counts[count])) 111 112 def processErrorLog(linesInFile = None): 113 """ 114 Process errors in apache2 error log file 115 """ 116 counts = dict() 117 linesCount = 1 118 if linesInFile == None: 119 input = fileinput.input() 120 else: 121 input = linesInFile 122 for line in input: 123 if linesCount % 100000 == 0: 124 sys.stderr.write('\tAnalyzed lines: %d\n' % linesCount) 125 if linesCount % 500000 == 0: 126 show_errors(counts) 127 linesCount += 1 128 if errorPattern.search(line): 129 elements = line.split(']') 130 date = elements[0] 131 date = date[1:] 132 # print '[%s]' % (date) 133 dt = datetime.datetime.strptime(date, 134 '%a %b %d %H:%M:%S %Y') 135 dt = dt.replace(minute = int(dt.minute/10)*10, 136 second = 0) 137 # print dt.isoformat() 138 try: 139 counts[dt.isoformat()] += 1 140 except KeyError: 141 counts[dt.isoformat()] = 1 142 return counts 143 144 def usage(): 145 sys.stderr.write(main.__doc__) 146 147 def main(arguments): 148 """ 149 Usage: parse_apache_log [-a <nebulous apache access_log file>] [-e <nebulous apache error_log file>] 150 """ 151 mainName = arguments.pop() 152 if len(arguments) == 0: 153 sys.stderr.write('Reading from /dev/stdin\n') 154 counts = process() 155 show_results(counts, sys.stdout) 156 elif len(arguments) % 2 != 0: 157 sys.stderr.write('Arguments length: %d\n' % (len(arguments))) 158 usage() 159 sys.exit(1) 160 else: 161 accessFilenames = [] 162 errorFilenames = [] 163 while len(arguments) != 0: 164 flag = arguments.pop() 165 filename = arguments.pop() 166 if flag == '-a': 167 accessFilenames.append(filename) 168 elif flag == '-e': 169 errorFilenames.append(filename) 170 else: 171 sys.stderr.write('Unknown flag: [%s]' % (flag)) 172 usage() 173 sys.exit(1) 174 for accessFilename in accessFilenames: 175 sys.stderr.write('Processing: [%s]' % (accessFilename)) 176 inFile = open(accessFilename) 177 counts = process(inFile) 178 show_results(counts, sys.stdout) 179 for errorFilename in errorFilenames: 180 counts = processErrorLog(errorFilename) 181 show_errors(counts, sys.stdout) 182 183 if __name__ == '__main__': 184 main(sys.argv)
Note:
See TracChangeset
for help on using the changeset viewer.
