#!/usr/bin/env perl # # Copyright: 2013 Sipwise Development Team # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This package is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # On Debian systems, the complete text of the GNU General # Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". # use 5.014; use strict; use warnings; use JSON; use YAML; use File::Spec; use Cwd 'abs_path'; use Data::Dumper; use Tie::File; use Getopt::Long; my $filename = "/var/log/ngcp/kamailio-proxy.log"; my $output_dir = "log"; sub usage { my $output = "usage: ulog_parser.pl [-h] [kamailio_log] [dest_dir]\n"; $output .= "Options:\n"; $output .= "\t-h: this help\n"; $output .= "\tkamailio_log default:$filename\n"; $output .= "\tdest_dir default:$output_dir\n"; return $output } my $help = 0; GetOptions ("h|help" => \$help) or die("Error in command line arguments\n".usage()); if($#ARGV>=2 || $help) { die(usage()) } my $path; my $data = { msgid => '', callid => '', flow => [], sip_in => '', sip_out => [], }; sub save_data { if(!$data->{'msgid'}) { print "data with no msgid\n"; print Dumper $data; exit; } else { if (!$data->{'sip_out'}) { print "no sip_out\n"; } $path = File::Spec->catfile( $output_dir, (sprintf "%04i", $data->{'msgid'}).".yml"); YAML::DumpFile($path, $data); #print "$data->{'msgid'} saved\n"; # This tries to fix problems with string values '-' being saved # without quotes. tie my @array, 'Tie::File', $path or die ('Can not open $path'); for (@array) { s/(.*\$\w+\(\w+\):) -/$1 '-'/g } untie @array; } $data = { msgid => '', callid => '', flow => [], sip_in => '', sip_out => [], }; } my $pid; my $log; my $line; sub first_line { $pid="unknown"; my $pid_read; do { $line = <$log>; #print "read line\n"; if(!$line) { $line = ''; return ($line ne '');} else { ($pid_read) = ($line =~ m/.+proxy\[(\d+)\]: DEBUG: