forked from roramirez/qpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_queuelog.py
60 lines (48 loc) · 1.59 KB
/
parser_queuelog.py
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
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2020 Rodrigo Ramírez Norambuena <a@rodrigoramirez.com>
#
# Parse queue_log Asterisk file and add records into database.
#
import sys
import tailer
from qpanel import model
import click
@click.command()
@click.option('--file', default='/var/log/asterisk/queue_log',
help='Queue Log file.')
@click.option('--lines', default=None, help='Get the last lines from file.')
@click.option('--verbose', default=False)
def parse(file, verbose, lines):
inserted, not_inserted = 0, 0
try:
if lines is None:
fb = open(file)
content = fb.read().splitlines()
else:
nlines = int(lines)
content = tailer.tail(open(file), nlines)
print('Reading file %s ...' % file)
except IOError:
print('File file %s not exits or not can read.' % file)
sys.exit(1)
for idx, line in enumerate(content):
record = line.split('|')
if len(record) < 4:
continue
if not exist_record(record) and insert_record(record):
inserted += 1
if verbose:
print(('Insert record ', record))
else:
if verbose:
print(('Not insert record ', record))
not_inserted += 1
print('Insert record: %i\nNo inserted record: %i' % (inserted,
not_inserted))
def exist_record(record):
return model.queuelog_exists_record(record)
def insert_record(record):
return model.queuelog_insert(record)
if __name__ == '__main__':
parse()