Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / contrib / debug / parse_log.pl
blobfb5466f2ee5f3c72e545485fe69f3b7c357293dc
1 #!/usr/bin/perl -w
2 use 5.010;
3 use strict;
4 use warnings;
6 use File::Spec;
7 use Getopt::Long;
8 use Pod::Usage;
10 # Command line option
11 my %Options = (
12 directory => ".",
14 GetOptions(\%Options,
15 "directory=s",
16 "callid",
17 "from",
18 "method",
19 "transport",
20 "filter",
21 "help|h|?")
22 or pod2usage(2);
23 pod2usage(-verbose => 2) if $Options{help};
25 ###############################################################################
27 # Message parsing
29 ###############################################################################
30 my %callid;
31 my %from;
32 my %method;
33 my %transport;
34 sub AddMessage($$$$@)
36 my($direction, $type, $transport, $time, @message) = @_;
38 # extract additional information from SIP messages
39 if ($type eq "SIP") {
40 my($index, $callid, $from, $method);
41 foreach my $line (@message) {
42 next if $index++ < 1;
43 last if $line =~ /^\s+$/;
45 next unless my($keyword, $value) = $line =~ /^([^:]+):\s+(.+)/;
46 $callid = $value if $keyword =~ /^call-id$/i;
47 ($from) = $value =~ /^<sip:([^;>]+)/ if $keyword =~ /^from$/i;
48 ($method) = $value =~ /^\d+\s+(\S+)/ if $keyword =~ /^cseq$/i;
50 push(@{$callid{$callid}}, \@message) if $Options{callid} && defined $callid;
51 push(@{$from{lc($from)}}, \@message) if $Options{from} && defined $from;
52 push(@{$method{uc($method)}}, \@message) if $Options{method} && defined $method;
55 # information available for all message types
56 push(@{$transport{$transport}}, \@message) if $Options{transport};
59 sub DumpMessages()
61 foreach my $callid (keys %callid) {
62 if (open(my $fh, ">",
63 File::Spec->catfile($Options{directory}, "callid-${callid}.txt"))) {
64 print $fh @{$_} foreach (@{$callid{$callid}});
65 close($fh);
68 foreach my $from (keys %from) {
69 if (open(my $fh, ">",
70 File::Spec->catfile($Options{directory}, "from-${from}.txt"))) {
71 print $fh @{$_} foreach (@{$from{$from}});
72 close($fh);
75 foreach my $method (keys %method) {
76 if (open(my $fh, ">",
77 File::Spec->catfile($Options{directory}, "method-${method}.txt"))) {
78 print $fh @{$_} foreach (@{$method{$method}});
79 close($fh);
82 foreach my $transport (keys %transport) {
83 if (open(my $fh, ">",
84 File::Spec->catfile($Options{directory}, "transport-${transport}.txt"))) {
85 print $fh @{$_} foreach (@{$transport{$transport}});
86 close($fh);
91 ###############################################################################
93 # Main program
95 ###############################################################################
97 # For all lines from command line files or STDIN
98 my @message;
99 my $counter;
100 while (<>) {
102 # Start of message?
103 if (my ($direction, $type, $transport, $time) =
104 /^MESSAGE START\s+([<>]+)\s+([^(]+)\((0x[\da-f]+)\)\s+-\s+(.+)/) {
105 push(@message,
106 "------------- NEXT MESSAGE: " .
107 (($direction =~ /^>/) ? "outgoing" : "incoming") .
108 " $type($transport) at $time\n");
110 # End of message?
111 } elsif (($direction, $type, $transport, $time) =
112 /^MESSAGE END\s+([<>]+)\s+([^(]+)\((0x[\da-f]+)\)\s+-\s+(.+)/) {
114 if ($Options{filter}) {
115 print @message;
116 } else {
117 print STDERR "." if (++$counter % 10 == 0);
118 AddMessage($direction, $type, $transport, $time, @message);
121 # Done with the current message
122 undef @message;
124 # All other lines
125 } else {
127 # Collect message information
128 push(@message, $_) if @message;
132 unless ($Options{filter}) {
133 print STDERR "\n" unless $Options{filter};
134 DumpMessages();
137 # That's all folks...
138 exit 0;
140 __END__
142 =head1 NAME
144 parse_log.pl - parse pidgin-sipe debug log
146 =head1 SYNOPSIS
148 [perl} parse_log.pl --help|-h|-?
150 [perl} parse_log.pl --filter
151 [file ...]
153 [perl} parse_log.pl [--directory <dir>]
154 [--callid]
155 [--from]
156 [--method]
157 [--transport]
158 [file ...]
160 =head1 OPTIONS
162 =over 8
164 =item B<--callid>
166 Dump all SIP messages belonging to one Call-ID to the same file.
168 =item B<--directory>
170 Directory for output files. Default is the current directory.
172 =item B<--filter>
174 Enable filter mode. Messages are simply printed to stdout.
176 =item B<--from>
178 Dump all SIP messages sent from the same SIP URI to the same file.
180 =item B<--help>
182 =item B<--h>
184 =item B<--?>
186 Print a brief help message and exits.
188 =item B<--method>
190 Dump all SIP messages with the same method to the same file.
192 =item B<--transport>
194 Dump all messages from one transport to the same file.
196 =back
198 =head1 DESCRIPTION
200 B<This program> extracts SIP/HTTP messages from pidgin-sipe debug logs. If
201 no file is specified then it reads from STDIN.
203 =cut