logger.pl 3.0 KB

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