Minecraft log viewer

This commit is contained in:
Jamie Cameron
2012-12-07 18:09:14 -08:00
parent 16abebcb74
commit 707b52b641
5 changed files with 86 additions and 2 deletions

View File

@@ -49,5 +49,5 @@ print &ui_buttons_row("atboot.cgi",
print &ui_buttons_end();
&ui_print_footer("/", $text{'index_return'});
&ui_print_footer("/", $text{'index'});

View File

@@ -19,5 +19,8 @@ conf_title=Server Configuration
users_title=Users and Operators
logs_title=View Log File
logs_lines=Show last
logs_matching=lines matching
logs_ok=Search
manual_title=Edit Configuration File

View File

@@ -1,4 +1,8 @@
# Functions for editing the minecraft config
#
# XXX java param options
# XXX plugins?
# XXX world reset
BEGIN { push(@INC, ".."); };
use strict;
@@ -28,6 +32,41 @@ return undef;
# If the minecraft server is running, return the PID
sub is_minecraft_server_running
{
&foreign_require("proc");
my @procs = &proc::list_processes();
my $jar = $config{'minecraft_jar'} ||
$config{'minecraft_dir'}."/"."minecraft_server.jar";
my $shortjar = $jar;
$shortjar =~ s/^.*\///;
foreach my $p (@procs) {
if ($p->{'args'} =~ /\Q$config{'java_cmd'}\E.*(\Q$jar\E|\Q$shortjar\E)/) {
return $p->{'pid'};
}
}
return undef;
}
# get_minecraft_config()
# Parses the config into an array ref of hash refs
sub get_minecraft_config
{
my @rv;
my $fh = "CONFIG";
my $lnum = 0;
&open_readfile($fh, $config{'minecraft_dir'}."/server.properties") ||
return [ ];
while(<$fh>) {
s/\r|\n//g;
s/#.*$//;
if (/^([^=]+)=(.*)/) {
push(@rv, { 'name' => $1,
'value' => $2,
'line' => $lnum });
}
$lnum++;
}
close($fh);
return \@rv;
}
1;

View File

@@ -1,3 +1,3 @@
desc=Minecraft Server
depends=init
depends=init proc
category=servers

42
minecraft/view_logs.cgi Normal file
View File

@@ -0,0 +1,42 @@
#!/usr/local/bin/perl
# Show the log file, with searching
use strict;
use warnings;
require './minecraft-lib.pl';
our (%in, %text, %config);
my $logfile = $config{'minecraft_dir'}."/server.log";
&ReadParse();
$in{'lines'} = undef if ($in{'lines'} !~ /^\d+$/);
$in{'lines'} ||= 20;
&ui_print_header(undef, $text{'logs_title'}, "");
# Search form
print &ui_form_start("view_logs.cgi");
print "<b>$text{'logs_lines'}</b> ",
&ui_textbox("lines", $in{'lines'}, 5)." ".
"<b>$text{'logs_matching'}</b> ",
&ui_textbox("search", $in{'search'}, 20)." ".
&ui_submit($text{'logs_ok'})."<br>\n";
print &ui_form_end()."<p>\n";
# Results
my $cmd;
if ($in{'search'}) {
$cmd = "grep ".quotemeta($in{'search'})." ".quotemeta($logfile)." | ".
"tail -".quotemeta($in{'lines'});
}
else {
$cmd = "tail -".quotemeta($in{'lines'})." ".quotemeta($logfile);
}
print "<pre>";
my $fh = "OUT";
&open_execute_command($fh, $cmd, 1, 1);
while(<$fh>) {
print &html_escape($_);
}
print "</pre>";
&ui_print_footer("", $text{'index_return'});