logger.pl
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use ESL;
my $host = "localhost";
my $port = "8021";
my $pass = "ClueCon";
my $profile = "internal";
my $file = "";
my $debug = "";
my $paste = "";
my $i;
my $argc = @ARGV;
my $e;
my $running = 1;
my $con;
my $USAGE = "
FreeSWITCH Logger Utility
USAGE:
-h --help This help
-H --host Choose host
-p --port <port> Choose port
-P -pass <pass> Choose password
-f --file <file> Output file
-pb --paste-bin <name> Post to FreeSWITCH Paste Bin
-sp --sip-profiles <list> List of SIP profiles to trace
-sd --sip-debug <level> Set SIP debug level
No arguments given will trace profile 'internal' to STDOUT
";
$SIG{INT} = sub { $running = 0 };
sub parse(\$\$$) {
my ($index, $ref, $regex) = @_;
if ($ARGV[$$index] =~ $regex) {
die "missing arg!" if (!$ARGV[$$index+1]);
$$ref = $ARGV[++$$index];
return 1;
}
return 0;
}
for($i = 0; $i < $argc; $i++) {
if ($ARGV[$i] =~ /^\-h$|^\-\-help$/) {
print $USAGE;
exit;
}
if (! (parse($i, $host, '^-H$|^--host$') ||
parse($i, $port, '^-p$|^--port$') ||
parse($i, $pass, '^-P$|^--pass$') ||
parse($i, $file, '^-f$|^--file$') ||
parse($i, $paste, '^-pb$|^--paste-bin$') ||
parse($i, $profile, '^-sp$|^--sip-profile$') ||
parse($i, $debug, '^-sd$|^--sip-debug$')
)) {
die "invalid arg!";
}
}
if ($paste) {
if (!$file) {
$file = "./logger_post.log";
}
}
if ($file) {
open (F, ">$file");
select F;
}
if ($paste) {
print F "paste=Send&remember=0&poster=${paste}&format=fslog&code2=";
}
$con = new ESL::ESLconnection($host, $port, $pass);
sub do_api($) {
my ($cmd, $args) = split(" ", $_[0], 2);
my $e = $con->api($cmd, $args);
if ($e) {
print STDERR $e->getBody() . "\n";
}
}
foreach (split(",", $profile)) {
do_api("sofia profile $_ siptrace on");
}
if ($debug) {
do_api("sofia loglevel all $debug");
}
$e = $con->sendRecv("log 7");
print STDERR $e->getBody() . "\n" if ($e);
while($con->connected() && $running) {
$e = $con->recvEventTimed(100);
if ($e and $e->getHeader("content-type") eq "log/data") {
print $e->getBody();
}
}
print STDERR "Stopping\n";
foreach (split(",", $profile)) {
do_api("sofia profile $_ siptrace off");
}
if ($debug) {
do_api("sofia loglevel all 0");
}
$e = $con->sendRecv("log 4");
print STDERR $e->getBody() . "\n" if ($e);
print STDERR "Done.....\n";
if ($file) {
close F;
print STDERR "Data written to $file\n";
}
if ($paste) {
my $path;
system("mkdir -p .fs_logger");
chdir(".fs_logger") or die "I/O Error!";
if ($file =~ /^\/.*/) {
$path = $file;
} else {
$path = "../$file";
}
print STDERR "Posting to pastebin, please wait...\n";
system("wget --output-file=/dev/null --http-user=pastebin --http-password=freeswitch http://pastebin.freeswitch.org --post-file=$path");
$pb = `ls [0-9]*`;
print STDERR "Data posted to pastebin [$paste] http://pastebin.freeswitch.org/$pb\n";
chdir("..") or die "I/O Error!";
system("rm -fr .fs_logger");
}