Code

extension to log-parser
[gosa.git] / gosa-si / tests / log-parser.pl
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;
24 use Data::Dumper;
26 my $log_file = "/var/log/gosa-si-server.log"; 
27 my $within_session = 0;
28 my $within_incoming = 0;
29 my $within_header = 0;
30 my $session;
31 my $incoming;
32 my $header;
34 sub check_header {
35         my ($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) { return $line; }
47         }
48         return;
49 }
51 sub check_incoming {
52         my ($line) = @_ ;
54         # new incoming msg, set all values back to default
55         if ($line =~ /INFO: Incoming msg with session ID \d+ from/ ) {
56                 $within_incoming = 0;
57         } 
59         if ($line =~ /INFO: Incoming msg with session ID \d+ from '$incoming'/) {
60                 $within_incoming = 1;
61                 return $line;
62         } else {
63                 if ($within_incoming) { return $line; } 
64         }
65         return;
66 }
68 sub check_session {
69         my ($line) = @_ ;
70         
71         if ($line =~ /gosa-si-server (\d+) / ) {
72                 if ((defined $1) && ($1 eq $session)) {
73                         $within_session = 1;    
74                         return $line;
75                 } else { $within_session = 0; }
76         } else {
77                 if ($within_session == 1) {     return $line; } 
78         }
79         return;
80 }
84 ### MAIN ######################################################################
86 GetOptions(
87                 "s|session=s"  => \$session,
88                 "i|incoming=s" => \$incoming,
89                 "h|header=s"   => \$header,
90                 );
92 # check script pram
93 my $script_pram = {};
94 if (defined $session) { 
95         print "session: $session\n";
96         $script_pram->{'session'} = $session;
97         }
98 if (defined $incoming) { 
99         print "incoming: $incoming\n";
100         $script_pram->{'incoming'} = $incoming;
101
102 if (defined $header) {
103         print "header: $header\n";
104         $script_pram->{'header'} = $header;
105 }       
107 if (keys(%$script_pram) == 0) {
108         # print usage and die
109         print "exiting script\n"; 
110         exit(0);
113 open(FILE, "<$log_file") or die "\t can not open log-file"; 
114 my @lines;
115 my $positive_msg = 0;
116 # Read lines
117 while ( my $line = <FILE>){
118     chomp($line);
120         # start of a new message, plot saved log lines
121         if ($line =~ /INFO: Incoming msg with session ID \d+ from / ) {
122                 if ($positive_msg) {
123                         print join("\n", @lines)."\n"; 
124                 }
126                 $positive_msg = 0;
127                 $within_session = 0;
128                 $within_header = 0;
129                 $within_incoming = 0;
130                 @lines = ();
131         }
133         push (@lines, $line); 
135         my $positiv_counter = 0;
136         while (my ($pram, $val) = each %$script_pram) {
137                 if ($pram eq 'session') {
138                         my $l = &check_session($line);
139                         if (defined $l) { $positiv_counter++; } 
140                 }
142                 elsif ($pram eq 'incoming') {
143                         my $l = &check_incoming($line);
144                         if (defined $l) { $positiv_counter++; } 
145                 }
146                 
147                 elsif ($pram eq 'header') {
148                         my $l = &check_header($line);
149                         if (defined $l) { $positiv_counter++; } 
150                 }
151         }
153         if (keys(%$script_pram) == $positiv_counter) {
154                 $positive_msg = 1;
155         }