File: smtpserver-log-parser.pl

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (104 lines) | stat: -rwxr-xr-x 1,963 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/perl -T
#
# A parser to glob in smtpserver's log (via 'tail -f', for example)
# and to print out those sessions which have errors logged in them.
#

#
# NEW servers make log which has two kinds of session termination
# conditions:
#    #####w \t 221 ...
#    #####- \t ...
#
# That is, either a "w"-line with code 221, or a "-"-line.
#


#
#  You wonder why this utility was written ?
#  Well, "technical monitoring with tail -f" is something which propably
#  is not allowed for people in Telecom Carrier business in Finland.
#  
#


#
#  We collect streams of inputs of all pids; we discard
#  such sessions which end happily without presenting
#  any instance of $haserror.
#

%pidstore    = (); # Key: $pid.".".$sernro
%sernrostore = (); # Key: $pid
%iserr       = (); # key: $pid


while (<STDIN>) {
    $line = $_;
    chomp $line;

    #print "line='".$line."'\n";

    if ($line =~ m/^([0-9]*)([^0-9].*)$/) {
	$pid  = $1;
	$rest = $2;
    } else {
	$pid = "**";
	$rest = "**";
    }

    #print "pid='".$pid."'\n";
    #print "rest='".$rest."'\n";

    $isend = 0;
    if ($rest =~ m/^-/) {
	$isend = 1;
    }
    if ($rest =~ m/^w\t221/) {
	$isend = 1;
    }

    $haserror = 0;
    if ($rest =~ m/^w\t[45]/) {
	$haserror = 1;
    }

    if (!defined($sernrostore{$pid})) {
	# New PID
	$sernrostore{$pid} = 1;
	$iserr{$pid} = 0;
    }


    if ($haserror) {
	$iserr{$pid} = 1;

	local($hi,$i);
	$hi = $sernrostore{$pid};
	for ($i = 1; $i < $hi; ++$i) {
	    $key = $pid.".".$i;
	    printf "%s\n",$pidstore{$key};
	    delete $pidstore{$key};
	}
	$sernrostore{$pid} = 0;
    }

    if (!$iserr{$pid}) {
	$key = $pid.".".$sernrostore{$pid};
	$sernrostore{$pid} += 1;
	$pidstore{$key} = $line;
    } else {
	printf "%s\n",$line;
    }

    if ($isend) {
	local($hi,$i);
	$hi = $sernrostore{$pid};
	for ($i = 1; $i < $hi; ++$i) {
	    $key = $pid.".".$i;
	    delete $pidstore{$key};
	}
	delete $sernrostore{$pid};
	delete $iserr{$pid};
    }
}