-
Notifications
You must be signed in to change notification settings - Fork 6
/
mdl-tail
executable file
·107 lines (82 loc) · 2.39 KB
/
mdl-tail
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
#!/usr/bin/perl
#
# From a moodle dir, auto detect the log file and tail it
use Cwd;
use Getopt::Long;
my $help;
my $access;
my $info = <<EOF;
Auto discover and live tail the moodle logs
Usage:
/var/www/moodle > mdl-tail
Options:
-h --help Show this help
-a --access Tail access instead of error logs
EOF
GetOptions(
"help|h" => \$help,
"access|a" => \$access,
) or die ("Error in cli args: $info");
if ($help) {
print $info;
die;
}
my $LogConfig = 'ErrorLog';
if ($access) {
$LogConfig = 'CustomLog';
}
if (!-e 'config.php'){
die "Can't find a config.php";
}
# Find current workig dir
my $cwd = getcwd;
# Look in some typical places for site definitions which match
# the current directory
my $apache = `grep '$cwd' /etc/apache2/sites-enabled/* /etc/httpd/conf/*`;
$apache =~ /^(.*):/;
# If we found some, then grep these for the Apache config for where the
# error logs hangout
my $conf = $1;
my $log = `grep $LogConfig $conf`;
$log =~ /$LogConfig(.*)$/;
$log = $1;
$log =~ s/\$\{APACHE_LOG_DIR\}/\/var\/log\/apache2/;
# If apache is setup using the VHost template we need more magic
if (!$apache){
$cwd =~ /.*\/(.*?)$/;
$cwd = $1;
$log = "/var/log/apache2/$cwd/error.log";
} elsif ($log =~ /^|/) {
# Not a file, is piped to a command lik logger
if ($log =~ /logger/) {
$log =~ /-p(\S*?)\s/;
$facility = $1;
$log =~ /-t\s(\S*)\b/;
$tag = $1;
print "Facility: $facility Tag: $tag\n";
if ($facility) {
print "Found logger with facility: $facility\n";
my $syslogfile = `grep '^$facility -?' /etc/rsyslog.d/*`;
if ($syslogfile) {
$syslogfile =~ /(.*):.*\-\?(.*)/;
my $logfileconf = `grep '$2' $1`;
print "Conf: $logfileconf\n";
$logfileconf =~ /$2,"(.*)"/;
$log = $1;
$log =~ s/%syslogtag.*?%/$tag/;
print "File: $file \n";
} else {
print "Can't find matcing rsyslog conf file\n";
}
} else {
print "Found logger but not sure what to do: $log\n";
}
} else {
print "Unknown log method: $log\n";
}
} else {
}
print "Found log file: $log \n";
#system("tail -f $log | sed 's/\\\\n/\\n/g' | sed 's/<\\/li>/<\\/li>\\n/g' ");
system("tail -f $log | sed 's/\\\\n/\\n/g' ");
#system("tail -f $log ");