airtime-log.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. exitIfNotRoot();
  3. date_default_timezone_set("UTC");
  4. $airtimeIni = getAirtimeConf();
  5. $airtime_base_dir = $airtimeIni['general']['airtime_dir'];
  6. set_include_path("$airtime_base_dir/library" . PATH_SEPARATOR . get_include_path());
  7. //Zend framework
  8. if (file_exists('/usr/share/php/libzend-framework-php')){
  9. set_include_path('/usr/share/php/libzend-framework-php' . PATH_SEPARATOR . get_include_path());
  10. }
  11. require_once('Zend/Loader/Autoloader.php');
  12. $autoloader = Zend_Loader_Autoloader::getInstance();
  13. $log_files = array("media-monitor" => "/var/log/airtime/media-monitor/media-monitor.log",
  14. "recorder" => "/var/log/airtime/pypo/show-recorder.log",
  15. "playout" => "/var/log/airtime/pypo/pypo.log",
  16. "liquidsoap" => "/var/log/airtime/pypo-liquidsoap/ls_script.log",
  17. "web" => "/var/log/airtime/zendphp.log");
  18. array_filter($log_files, "file_exists");
  19. /**
  20. * Ensures that the user is running this PHP script with root
  21. * permissions. If not running with root permissions, causes the
  22. * script to exit.
  23. */
  24. function exitIfNotRoot()
  25. {
  26. // Need to check that we are superuser before running this.
  27. if(posix_geteuid() != 0){
  28. echo "Must be root user.\n";
  29. exit(1);
  30. }
  31. }
  32. function printUsage($userMsg = "")
  33. {
  34. global $opts;
  35. $msg = $opts->getUsageMessage();
  36. if (strlen($userMsg)>0)
  37. echo $userMsg;
  38. echo PHP_EOL."Usage: airtime-log [options]";
  39. echo substr($msg, strpos($msg, "\n")).PHP_EOL;
  40. }
  41. function isKeyValid($key){
  42. global $log_files;
  43. return array_key_exists($key, $log_files);
  44. }
  45. function viewSpecificLog($key){
  46. global $log_files;
  47. if (isKeyValid($key)){
  48. echo "Viewing $key log\n";
  49. pcntl_exec(exec("which less"), array($log_files[$key]));
  50. pcntl_wait($status);
  51. } else printUsage();
  52. }
  53. function dumpAllLogs(){
  54. $dateStr = gmdate("Y-m-d-H-i-s");
  55. $dir = getcwd();
  56. $filename = "$dir/airtime-log-all-$dateStr.tgz";
  57. echo "Creating Airtime logs tgz file at $filename";
  58. $command = "tar cfz $filename /var/log/airtime 2>/dev/null";
  59. exec($command);
  60. }
  61. function dumpSpecificLog($key){
  62. global $log_files;
  63. if (isKeyValid($key)){
  64. $dateStr = gmdate("Y-m-d-H-i-s");
  65. $dir = getcwd();
  66. $filename = "$dir/airtime-log-$key-$dateStr.tgz";
  67. echo "Creating Airtime logs tgz file at $filename";
  68. $dir = dirname($log_files[$key]);
  69. $command = "tar cfz $filename $dir 2>/dev/null";
  70. exec($command);
  71. } else printUsage();
  72. }
  73. function tailAllLogs(){
  74. global $log_files;
  75. echo "Tail all Airtime logs";
  76. pcntl_exec(exec("which multitail"), $log_files);
  77. pcntl_wait($status);
  78. }
  79. function tailSpecificLog($key){
  80. global $log_files;
  81. if (isKeyValid($key)){
  82. echo "Tail $key log";
  83. pcntl_exec(exec("which tail"), array("-F", $log_files[$key]));
  84. pcntl_wait($status);
  85. } else printUsage();
  86. }
  87. function getAirtimeConf()
  88. {
  89. $ini = parse_ini_file("/etc/airtime/airtime.conf", true);
  90. if ($ini === false){
  91. echo "Error reading /etc/airtime/airtime.conf.".PHP_EOL;
  92. exit;
  93. }
  94. return $ini;
  95. }
  96. try {
  97. $keys = implode("|", array_keys($log_files));
  98. $opts = new Zend_Console_Getopt(
  99. array(
  100. 'view|v=s' => "Display log file\n"
  101. ."\t\t$keys",
  102. 'dump|d-s' => "Collect all log files and compress into a tarball\n"
  103. ."\t\t$keys (ALL by default)",
  104. 'tail|t-s' => "View any new entries appended to log files in real-time\n"
  105. ."\t\t$keys (ALL by default)"
  106. )
  107. );
  108. $opts->parse();
  109. }
  110. catch (Zend_Console_Getopt_Exception $e) {
  111. print $e->getMessage() .PHP_EOL;
  112. printUsage();
  113. exit(1);
  114. }
  115. if (isset($opts->v)){
  116. if ($opts->v === true){
  117. //Should never get here. Zend_Console_Getopt requires v to provide a string parameter.
  118. } else {
  119. viewSpecificLog($opts->v);
  120. }
  121. } else if (isset($opts->d)){
  122. if ($opts->d === true){
  123. dumpAllLogs();
  124. } else {
  125. dumpSpecificLog($opts->d);
  126. }
  127. } else if (isset($opts->t)){
  128. if ($opts->t === true){
  129. tailAllLogs();
  130. } else {
  131. tailSpecificLog($opts->t);
  132. }
  133. } else {
  134. printUsage();
  135. exit;
  136. }
  137. echo PHP_EOL;