0efe9eab92a8b0cd9779c6bef175c6e4e0f4f909
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 = "/home/hickert/log.gosa-si-server";
27 #my $log_file = "/var/log/gosa-si-server.log";
29 my $within_session = 0;
30 my $within_incoming = 0;
31 my $within_header = 0;
32 my $session;
33 my $incoming;
34 my $header;
35 my $syslog_state;
37 sub check_syslog_state {
38 my ($line) = @_ ;
39 if($line =~ /$syslog_state:/ ) {
40 return $line;
41 }
42 return;
43 }
45 sub check_header {
46 my ($line) = @_ ;
48 # new header, set all values back to default
49 if ($line =~ /INFO: Incoming msg with header/ ) {
50 $within_header = 0;
51 }
53 if ($line =~ /INFO: Incoming msg with header '$header'/) {
54 $within_header = 1;
55 return $line;
56 } else {
57 if ($within_header) { return $line; }
58 }
59 return;
60 }
62 sub check_incoming {
63 my ($line) = @_ ;
65 # new incoming msg, set all values back to default
66 if ($line =~ /INFO: Incoming msg with session ID \d+ from/ ) {
67 $within_incoming = 0;
68 }
70 if ($line =~ /INFO: Incoming msg with session ID \d+ from '$incoming'/) {
71 $within_incoming = 1;
72 return $line;
73 } else {
74 if ($within_incoming) { return $line; }
75 }
76 return;
77 }
79 sub check_session {
80 my ($line) = @_ ;
82 if ($line =~ /gosa-si-server (\d+) / ) {
83 if ((defined $1) && ($1 eq $session)) {
84 return $line;
85 }
86 }
87 return;
88 }
92 ### MAIN ######################################################################
94 GetOptions(
95 "s|session=s" => \$session,
96 "i|incoming=s" => \$incoming,
97 "h|header=s" => \$header,
98 "l|log=s" => \$syslog_state,
99 );
101 # check script pram
102 my $script_pram = {};
103 if (defined $session) {
104 print "session: $session\n";
105 $script_pram->{'session'} = $session;
106 }
107 if (defined $incoming) {
108 print "incoming: $incoming\n";
109 $script_pram->{'incoming'} = $incoming;
110 }
111 if (defined $header) {
112 print "header: $header\n";
113 $script_pram->{'header'} = $header;
114 }
115 if (defined $syslog_state) {
116 print "syslog_state: $syslog_state\n";
117 $script_pram->{'syslog_state'} = $syslog_state;
118 }
120 if (keys(%$script_pram) == 0) {
121 # print usage and die
122 print "exiting script\n";
123 exit(0);
124 }
126 open(FILE, "<$log_file") or die "\t can not open log-file";
127 my @lines;
128 my $positive_msg = 0;
129 # Read lines
130 while ( my $line = <FILE>){
131 chomp($line);
133 # start of a new message, plot saved log lines
134 if ($line =~ /INFO: Incoming msg with session ID \d+ from / ) {
135 if ($positive_msg) {
136 print "#####################################################\n";
137 print join("\n", @lines)."\n";
138 }
140 $positive_msg = 0;
141 $within_session = 0;
142 $within_header = 0;
143 $within_incoming = 0;
144 @lines = ();
145 }
147 push (@lines, $line);
149 my $positiv_counter = 0;
150 while (my ($pram, $val) = each %$script_pram) {
151 if ($pram eq 'session') {
152 my $l = &check_session($line);
153 if (defined $l) { $positiv_counter++; }
154 }
156 elsif ($pram eq 'incoming') {
157 my $l = &check_incoming($line);
158 if (defined $l) { $positiv_counter++; }
159 }
161 elsif ($pram eq 'header') {
162 my $l = &check_header($line);
163 if (defined $l) { $positiv_counter++; }
164 }
166 elsif ($pram eq 'syslog_state') {
167 my $l = &check_syslog_state($line);
168 if (defined $l) { $positiv_counter++; }
169 }
170 }
172 if (keys(%$script_pram) == $positiv_counter) {
173 $positive_msg = 1;
174 }
176 }