f888bb610f0e71572263279529bca211215c7151
1 #!/usr/bin/perl
2 #===============================================================================
3 #
4 # FILE: log-parser.pl
5 #
6 # USAGE: ./log-parser.pl
7 #
8 # DESCRIPTION:
9 #
10 # OPTIONS: ---
11 # REQUIREMENTS: ---
12 # BUGS: ---
13 # NOTES: ---
14 # AUTHOR: (), <>
15 # COMPANY:
16 # VERSION: 1.0
17 # CREATED: 13.03.2008 14:51:03 CET
18 # REVISION: ---
19 #===============================================================================
21 use strict;
22 use warnings;
23 use Getopt::Long;
25 my $log_file = "/var/log/gosa-si-server.log";
26 my $within_session = 0;
27 my $within_incoming = 0;
28 my $within_header = 0;
29 my $session;
30 my $incoming;
31 my $header;
33 sub check_header {
34 my ($line) = @_ ;
35 my @line_list = split(" ", $line);
37 # new header, set all values back to default
38 if ($line =~ /INFO: Incoming msg with header/ ) {
39 $within_header = 0;
40 }
42 if ($line =~ /INFO: Incoming msg with header '$header'/) {
43 $within_header = 1;
44 return $line;
45 } else {
46 if ($within_header) {
47 return $line;
48 } else {
49 return;
50 }
51 }
52 }
54 sub check_incoming {
55 my ($line) = @_ ;
56 my @line_list = split(" ", $line);
58 # new incoming msg, set all values back to default
59 if ($line =~ /INFO: Incoming msg with session ID \d+ from/ ) {
60 $within_incoming = 0;
61 }
63 if ($line =~ /INFO: Incoming msg with session ID \d+ from '$incoming'/) {
64 $within_incoming = 1;
65 return $line;
66 } else {
67 if ($within_incoming) {
68 return $line;
69 } else {
70 return;
71 }
72 }
73 }
75 sub check_session {
76 my ($line) = @_ ;
77 my @line_list = split(" ", $line);
79 if (not $line_list[4]) {
80 if ($within_session) {
81 return $line;
82 }
83 return;
84 }
86 if($line_list[4] eq $session) {
87 $within_session = 1;
88 return $line;
89 } else {
90 $within_session = 0;
91 }
92 return;
93 }
95 ### MAIN ######################################################################
97 GetOptions(
98 "s|session=s" => \$session,
99 "i|incoming=s" => \$incoming,
100 "h|header=s" => \$header,
101 );
103 # check script pram
104 my $script_pram = {};
105 if (defined $session) {
106 print "session: $session\n";
107 $script_pram->{'session'} = $session;
108 }
109 if (defined $incoming) {
110 print "incoming: $incoming\n";
111 $script_pram->{'incoming'} = $incoming;
112 }
113 if (defined $header) {
114 print "header: $header\n";
115 $script_pram->{'header'} = $header;
116 }
118 if (keys(%$script_pram) == 0) {
119 # print usage and die
120 print "exiting script\n";
121 exit(0);
122 }
124 open(FILE, "<$log_file") or die "\t can not open log-file";
125 my @lines;
126 my $positive_msg = 0;
127 # Read lines
128 while ( my $line = <FILE>){
129 chomp($line);
131 # start of a new message, plot saved log lines
132 if ($line =~ /INFO: Incoming msg with session ID \d+ from / ) {
133 if ($positive_msg) {
134 print join("\n", @lines)."\n";
135 $positive_msg = 0;
136 }
138 $within_session = 0;
139 $within_header = 0;
140 $within_incoming = 0;
141 @lines = ();
142 }
144 push (@lines, $line);
146 my $positiv_counter = 0;
147 while (my ($pram, $val) = each %$script_pram) {
148 if ($pram eq 'session') {
149 my $l = &check_session($line);
150 if (defined $l) { $positiv_counter++; }
151 }
153 elsif ($pram eq 'incoming') {
154 my $l = &check_incoming($line);
155 if (defined $l) { $positiv_counter++; }
156 }
158 elsif ($pram eq 'header') {
159 my $l = &check_header($line);
160 if (defined $l) { $positiv_counter++; }
161 }
162 }
164 if (keys(%$script_pram) == $positiv_counter) {
165 $positive_msg = 1;
166 }
168 }