| 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 | |
|---|
| 25 | import sys |
|---|
| 26 | import re |
|---|
| 27 | |
|---|
| 28 | ## |
|---|
| 29 | ## Set mode to... |
|---|
| 30 | ## "COPY": give PostGres-compatible COPY-syntax output. |
|---|
| 31 | ## other : give usual (slow) SQL-output |
|---|
| 32 | ## |
|---|
| 33 | mode = "COPY" |
|---|
| 34 | |
|---|
| 35 | faillog = "/var/log/updatedblog/zevents-failed.log" |
|---|
| 36 | |
|---|
| 37 | ## |
|---|
| 38 | ## The format of a zope event.log line is like this... |
|---|
| 39 | ## The expression looks for the following fields: |
|---|
| 40 | ## |
|---|
| 41 | ## Fieldcontent | Fieldnum |
|---|
| 42 | ## --------------------+--------- |
|---|
| 43 | ## DATE (dd-mm-yyyy) | 0 |
|---|
| 44 | ## TIME | 1 |
|---|
| 45 | ## LOGLEVEL | 2 |
|---|
| 46 | ## MSG_SRC | 3 |
|---|
| 47 | ## MSG_TEXT | 4 |
|---|
| 48 | ## ERR_TYPE | -- |
|---|
| 49 | ## ERR_MSG | -- |
|---|
| 50 | ## TRACEBACK | 5 |
|---|
| 51 | ## |
|---|
| 52 | ## It was hard work. Please do not touch! |
|---|
| 53 | ## |
|---|
| 54 | expr = re.compile("^([0-9]+\-[0-9]+\-[0-9]+)T([0-9]+\:[0-9]+\:[0-9]+) ([^ ]+) ([^ ]+) ?(.*) ?(Traceback \(innermost last\)\: .*)?") |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | ## Number of line |
|---|
| 58 | lnum = 1 |
|---|
| 59 | |
|---|
| 60 | if mode == "COPY": |
|---|
| 61 | print 'COPY "ZEVENTS" FROM stdin;' |
|---|
| 62 | |
|---|
| 63 | def process_dataset(data): |
|---|
| 64 | data = data.strip() |
|---|
| 65 | data = data.replace("\n", " ") |
|---|
| 66 | data = data.replace("\t", " ") |
|---|
| 67 | return data |
|---|
| 68 | |
|---|
| 69 | def print_dataset(data, lnum, mode): |
|---|
| 70 | m = expr.match(data) |
|---|
| 71 | if not m: |
|---|
| 72 | # Line did not match... |
|---|
| 73 | #print data |
|---|
| 74 | fd = open( faillog, "a") |
|---|
| 75 | fd.write( str( data ) + '\n' ) |
|---|
| 76 | fd.close() |
|---|
| 77 | return |
|---|
| 78 | |
|---|
| 79 | res = m.groups() |
|---|
| 80 | val = { |
|---|
| 81 | 'id' : lnum, |
|---|
| 82 | 'date' : res[0], |
|---|
| 83 | 'time' : res[1], |
|---|
| 84 | 'loglevel' : res[2], |
|---|
| 85 | 'msg_src' : res[3] or '', |
|---|
| 86 | 'msg_text' : res[4] or '', |
|---|
| 87 | 'err_type' : '', |
|---|
| 88 | 'err_msg' : '', |
|---|
| 89 | 'traceback' : res[5] or '', |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if mode == "COPY": |
|---|
| 93 | print "%07d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % ( |
|---|
| 94 | val['id'], val['date'], val['time'], val['loglevel'], |
|---|
| 95 | val['msg_src'], val['msg_text'], val['err_type'], val['err_msg'], |
|---|
| 96 | val['traceback'] |
|---|
| 97 | ) |
|---|
| 98 | else: |
|---|
| 99 | print "INSERT INTO \"ZEVENTS\" (\"ID\",\"DATE\",\"TIME\",\"LOGLEVEL\",\"MSG_SRC\",\"MSG_TEXT\",\"ERR_TYPE\",\"ERR_MSG\",\"TRACEBACK\") VALUES ('%07d','%s','%s','%s','%s','%s','%s','%s','%s');" % ( |
|---|
| 100 | val['id'], val['date'], val['time'], val['loglevel'], |
|---|
| 101 | val['msg_src'], val['msg_text'], val['err_type'], val['err_msg'], |
|---|
| 102 | val['traceback'] |
|---|
| 103 | ) |
|---|
| 104 | return |
|---|
| 105 | |
|---|
| 106 | ## Read line for line... |
|---|
| 107 | data=sys.stdin.readline().strip() |
|---|
| 108 | dataset = '' |
|---|
| 109 | while data: |
|---|
| 110 | |
|---|
| 111 | if data.strip() == '------': |
|---|
| 112 | lnum += 1 |
|---|
| 113 | dataset = process_dataset(dataset) |
|---|
| 114 | print_dataset(dataset, lnum, mode) |
|---|
| 115 | dataset = "" |
|---|
| 116 | else: |
|---|
| 117 | dataset += data |
|---|
| 118 | |
|---|
| 119 | data=sys.stdin.readline() |
|---|
| 120 | continue |
|---|
| 121 | |
|---|
| 122 | dataset = process_dataset(dataset) |
|---|
| 123 | print_dataset(dataset, lnum, mode) |
|---|
| 124 | |
|---|