[yaala] Sendmail log parser

qMax yaala@verplant.org
Tue, 23 Nov 2004 18:15:28 +0600


------------A3E21F3017530A
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

From=20module POD:

This is a Parser for sendmail log.

Note:
 Each message in sendmail log forms several records (lines):
 one record with 'from=3D' field, and one or several with 'to=3D' field.

 Parser joins each 'from'-part with 'to'-part by message log id.
 However, if there're several recipients, result will be several
 records for the same message: one per recipient. When message is
 first time countd, datafield 'uniq' is set to 1. This is usefull to
 calculate total count/traffic or traffic by type. But if you count
 total by recipients, using this key (as WHERE uniq=3D=3D'1') will make
 yaala ignore all recipients of a message, but the first.

Config options:
 sendmail_aliases     - aliases file used to resolve (local senders) adress=
es
 sendmail_localdomain - local domain to remove from adresses
 sendmail_localrelay  - IP regexp to determine incoming/outgoing/local traf=
fic, egg '192.168.1.\d+'

Data fields:

 everything found in sendmail log, with:
 timedate is splited to 'date' and 'hour', as usual, year is taken from cur=
rent date.
 rrelay is relay field from 'to'-part

 uniq =3D set to 1 when message first time counted.

 type =3D "I","O","L","R" for incoming, outgoing, local and relay traffic.
        It is determined using fields 'mailer' and 'relay'.
        (Thus, only applied to actually sent/recieved messages)

Aggregations: size, count, nrcpts

TODO:
 - Properly handle multiple aliases.
 - Split non-local multiple recipients
 - resolve hosts in relay fields.

--=20
 qMax
------------A3E21F3017530A
Content-Type: application/octet-stream; name="Sendmail.pm"
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="Sendmail.pm"

cGFja2FnZSBZYWFsYTo6UGFyc2VyOwoKPWhlYWQxIFlhYWxhOjpQYXJzZXI6OlNlbmRtYWls
CgpQYXJzZXIgZm9yIHNlbmRtYWlsIGxvZy4KCk5vdGU6CgpFYWNoIG1lc3NhZ2UgaW4gc2Vu
ZG1haWwgbG9nIGZvcm1zIHNldmVyYWwgcmVjb3JkcyAobGluZXMpOgpvbmUgcmVjb3JkIHdp
dGggJ2Zyb209JyBmaWVsZCwgYW5kIG9uZSBvciBzZXZl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