source: WAeUP_SysConf/logging/trunk/usr/local/lib/zevents.py @ 2880

Last change on this file since 2880 was 2880, checked in by uli, 17 years ago

Fixed INSERT mode. Added usage hints.

File size: 4.4 KB
RevLine 
[2838]1##
2## zevents.py
3## Read Zope event log from stdin and output SQL data.
4##
5## Login : <uli@pu.smp.net>
6## Started on  Wed Feb  7 03:02:47 2007 Uli Fouquet
7## $Id$
8##
9## Copyright (C) 2007 Uli Fouquet
10## This program is free software; you can redistribute it and/or modify
11## it under the terms of the GNU General Public License as published by
12## the Free Software Foundation; either version 2 of the License, or
13## (at your option) any later version.
14##
15## This program is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with this program; if not, write to the Free Software
22## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23##
24
[2880]25##
26## Usage: python zevents.py [-INSERT]
27##
28## Reads Zope event logs from stdin and turn it into PostGres compatible
29## output on stdout.
30##
31## If no options are given, output is formatted as a COPY command, which is
32## *much* (50 times or more) faster in PostGres.
33##
34## If -INSERT is given, output is formatted as a list of INSERT statements,
35## which will need much more time and resources.
36##
37
[2838]38import sys
39import re
40
41##
42## Set mode to...
43##   "COPY":  give PostGres-compatible COPY-syntax output.
44##   other : give usual (slow) SQL-output
45##
46mode = "COPY"
47
[2880]48if '-INSERT' in sys.argv:
49    mode = "INSERT"
50
[2838]51faillog = "/var/log/updatedblog/zevents-failed.log"
52
53##
54## The format of a zope event.log line is like this...
55## The expression looks for the following fields:
56##
57## Fieldcontent        | Fieldnum
58## --------------------+---------
59## DATE (dd-mm-yyyy)   | 0
60## TIME                | 1
61## LOGLEVEL            | 2
62## MSG_SRC             | 3
63## MSG_TEXT            | 4
64## ERR_TYPE            | --
65## ERR_MSG             | --
66## TRACEBACK           | 5
67##
68## It was hard work. Please do not touch!
69##
70expr = re.compile("^([0-9]+\-[0-9]+\-[0-9]+)T([0-9]+\:[0-9]+\:[0-9]+) ([^ ]+) ([^ ]+) ?(.*) ?(Traceback \(innermost last\)\: .*)?")
71
72
73## Number of line
74lnum = 1
75
76if mode == "COPY":
77    print 'COPY "ZEVENTS" FROM stdin;'
78
79def process_dataset(data):
80    data = data.strip()
81    data = data.replace("\n", " ")
82    data = data.replace("\t", "    ")
83    return data
84
[2880]85def q(text):
86    return text.replace("'","\\\'")
87
[2838]88def print_dataset(data, lnum, mode):
89    m = expr.match(data)
90    if not m:
91        # Line did not match...
92        #print data
93        fd = open( faillog, "a")
94        fd.write( str( data ) + '\n' )
95        fd.close()
96        return
97
98    res = m.groups()
99    val = {
100        'id' : lnum,
101        'date' : res[0],
102        'time' : res[1],
103        'loglevel' : res[2],
104        'msg_src' : res[3] or '',
105        'msg_text' : res[4] or '',
106        'err_type' : '',
107        'err_msg' : '',
108        'traceback' : res[5] or '',
109        }
110
[2879]111    # If msg_text is too long, append it to traceback...
112    if len(val['msg_text']) > 1024:
113        val['traceback'] = val['msg_text'] + " " + val['traceback']
114        val['msg_text'] = val['msg_text'][:1024]
115
116    # Shorten overlong fields...
117    for name,maxlen in [('loglevel',32), ('msg_src',1024), ('msg_text', 1024),
118                 ('err_type', 32), ('err_msg', 1024)]:
119        val[name]=val[name][:maxlen]
120
[2838]121    if mode == "COPY":
122        print "%07d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (
[2879]123            val['id'], val['date'], val['time'], val['msg_src'],
124            val['msg_text'], val['err_type'], val['err_msg'], val['traceback'],
125            val['loglevel']
[2838]126        )
127    else:
[2880]128        print "INSERT INTO \"ZEVENTS\" (\"ID\",\"DATESTR\",\"TIMESTR\",\"MSG_SRC\",\"MSG\",\"ERR_TYPE\",\"ERR_MSG\",\"TRACEBACK\",\"LOGLEVEL\") VALUES ('%07d','%s','%s','%s','%s','%s','%s','%s','%s');" % (
129            val['id'], q(val['date']), q(val['time']), q(val['msg_src']),
130            q(val['msg_text']), q(val['err_type']), q(val['err_msg']),
131            q(val['traceback']), q(val['loglevel'])
[2838]132        )
133    return
134
135## Read line for line...
136data=sys.stdin.readline().strip()
137dataset = ''
138while data:
139
140    if data.strip() == '------':
141        lnum += 1
142        dataset = process_dataset(dataset)
143        print_dataset(dataset, lnum, mode)
144        dataset = ""
145    else:
146        dataset += data
147
148    data=sys.stdin.readline()
149    continue
150
151dataset = process_dataset(dataset)
152print_dataset(dataset, lnum, mode)
153
Note: See TracBrowser for help on using the repository browser.