提交 fa05d6cc authored 作者: Raymond Chandler's avatar Raymond Chandler

scripts for debugging from logs instead of cli

上级 eb325df1
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use DateTime;
use DateTime::Format::DateParse;
use Date::Calc qw( Delta_YMDHMS );
sub usage {
my $msg = shift;
print "USAGE:\n\t$0 '<start timestamp>' '<end timestamp>' [log file]\n";
print "$msg\n" if $msg;
exit 1;
}
my $tmp;
if ( scalar(@ARGV) < 2) {
usage('missing required args');
}
my $call_start_date_str = shift(@ARGV);
my $call_end_date_str = shift(@ARGV);
my $log_file = shift(@ARGV) || '/usr/local/freeswitch/log/freeswitch.log';
usage('log file doesn\'t exist') unless -f $log_file;
$call_start_date_str = DateTime->now if $call_start_date_str eq 'now';
$call_end_date_str = DateTime->now if $call_end_date_str eq 'now';
print "Call Was From: $call_start_date_str - $call_end_date_str\n";
my $start_dt = DateTime::Format::DateParse->parse_datetime( $call_start_date_str );
my $end_dt = DateTime::Format::DateParse->parse_datetime( $call_end_date_str );
$tmp = $start_dt->subtract( minutes => 2 );
$tmp = $end_dt->add( minutes => 2 );
print "find stuff between ";
printf("%s-%02s-%02s %02s:%02s:%02s", $start_dt->year, $start_dt->month, $start_dt->day, $start_dt->hour, $start_dt->minute, $start_dt->second);
print " and ";
printf("%s-%02s-%02s %02s:%02s:%02s", $end_dt->year, $end_dt->month, $end_dt->day, $end_dt->hour, $end_dt->minute, $end_dt->second);
print "\n";
my $limit = 10;
my $i = 0;
open(LOG_FILE, '<', $log_file);
my %times_seen;
my $start_printing = 0;
while (<LOG_FILE>) {
#print "LOG LINE: $_";
if ($_ =~ m/^(?:[a-z\d-]{36}\s)?(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/) {
if ( !$times_seen{$1} ) {
my $datetime = $1;
$times_seen{$1} = 1;
my $current_dt = DateTime::Format::DateParse->parse_datetime($datetime);
my @delta;
if ( !$start_printing ) {
@delta = Delta_YMDHMS(
$start_dt->year, $start_dt->month, $start_dt->day, $start_dt->hour, $start_dt->minute, $start_dt->second,
$current_dt->year, $current_dt->month, $current_dt->day, $current_dt->hour, $current_dt->minute, $current_dt->second,
);
} else {
@delta = Delta_YMDHMS(
$end_dt->year, $end_dt->month, $end_dt->day, $end_dt->hour, $end_dt->minute, $end_dt->second,
$current_dt->year, $current_dt->month, $current_dt->day, $current_dt->hour, $current_dt->minute, $current_dt->second,
);
}
my $sum = unpack "l", pack( "l", unpack( "%32d*", pack( "d*", @delta)));
if ( $sum > 0 ) {
last if $start_printing;
$start_printing = 1;
}
}
}
print $_ if $start_printing;
#exit if $i == $limit;
$i++;
}
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use LWP::UserAgent;
use Term::ANSIColor;
sub usage {
my $msg = shift;
print "USAGE:\n\t$0 <file> [regex]\n";
print "$msg\n" if $msg;
exit;
}
my %colors = (
CRIT => 'red',
ERR => 'red',
ALERT => 'red',
WARNING => 'magenta',
NOTICE => 'cyan',
INFO => 'green',
DEBUG => 'yellow',
CONSOLE => 'white',
);
if ( !scalar(@ARGV) ) {
usage('you must pass a file');
}
my $file = shift(@ARGV);
my $re = shift(@ARGV);
my @lines;
if ( $file =~ m/^http/) {
my $ua = LWP::UserAgent->new;
my $response = $ua->get( $file );
if ( $response->is_success) {
@lines = split("\n", $response->decoded_content);
} else {
usage($response->status_line);
}
} elsif ( $file eq '-') {
while (<STDIN>) {
chomp;
push(@lines, $_);
}
} else {
open(FILE, '<', $file) or usage($!);
while (<FILE>) {
chomp;
push(@lines, $_);
}
close FILE;
}
my $color;
foreach my $line ( @lines ) {
if ( $re ) {
next unless $line =~ $re;
}
chomp($line);
if ( $line =~ m/^.*\[(DEBUG|INFO|NOTICE|WARNING|ERROR)].*$/ && defined $colors{$1} ) {
$color = $colors{$1};
} elsif ( $line =~ m/^([a-z\d-]{36})\s?(Dialplan|EXECUTE)/ ) {
$color = 'yellow';
} elsif ( $line =~ m/^(recv|send|nua|nta|soa|su|tport|nth|nea|su|url|sresolv|sdp)/ ) {
$color = 'white';
}
print colored ("$line\n", $color);
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论