Python 日志处理与图形生成[Pygal的初步使用]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import re
import os
from datetime import datetime
import pygal


def logTimeStatistics(reDictIn, dictStatisticIn):
    '13/Jul/2017:16:27:11 +0800'
    timeHourGet = datetime.strptime(reDictIn['time'], '%d/%b/%Y:%H:%M:%S %z')
    dictStatisticIn[timeHourGet.strftime('%H')] += 1
    return dictStatisticIn


def main():
    if not os.path.exists('access_log2'):
        print('flie dose not exists!!')
        exit(1)
    r = re.compile(r'(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) ' +
                   r'.* .* \[(?P<time>.*)\] "(?P<method>\w+) ' +
                   r'(?P<url>[^\s]*) (?P<version>[\w|/\.\d]*)" (?P<status>\d{3}) ' +
                   r'(?P<length>\d+) "(?P<referer>[^\s]*)" "(?P<ua>.*)"')
    dictStatistic = {}
    for x in range(24):
        if x < 10:
            dictStatistic['0{0}'.format(x)] = 0
        else:
            dictStatistic[str(x)] = 0

    with open('access_log2') as f:
        fileContext = f.readlines()
        for x in fileContext:
            if r.search(x):
                dictStatistic = logTimeStatistics(r.search(x).groupdict(), dictStatistic)

    picOut(dictStatistic.values())


def picOut(listIn):
    'line PicOut'
    chart_line = pygal.Line()
    chart_line.x_labels = map(str, range(0,24))
    chart_line.add('visit statistics', listIn)
    chart_line.render_to_file('visit.svg')
    print('pic out now')


if __name__ == '__main__':
    main()