introduced HTTP server built-in

This commit is contained in:
Jordi Sanfeliu 2013-01-31 18:48:29 +01:00
parent eac62c839c
commit 2b25949113
4 changed files with 63 additions and 18 deletions

View File

@ -23,7 +23,8 @@ package Monitorix;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw(logger trim max get_nvidia_data flush_accounting_rules);
use POSIX qw(setuid setgid setsid);
our @EXPORT = qw(logger trim max httpd_setup get_nvidia_data flush_accounting_rules);
sub logger {
my ($msg) = @_;
@ -50,6 +51,30 @@ sub max {
return $max;
}
sub httpd_setup {
my ($config, $debug) = @_;
my $pid;
if($pid = fork()) {
$config->{httpd_pid} = $pid;
return; # parent returns
}
my (undef, undef, $uid) = getpwnam($config->{httpd_builtin}->{user});
my (undef, undef, $gid) = getgrnam($config->{httpd_builtin}->{group});
my $port = $config->{httpd_builtin}->{port};
setgid($gid);
setuid($uid);
setsid();
$SIG{$_} = 'DEFAULT' for keys %SIG; # reset all sighandlers
$0 = "monitorix-httpd listening on $port"; # change process' name
chdir($config->{base_dir});
my $server = HTTPServer->new($port);
$server->run(); # ->background
}
sub get_nvidia_data {
my ($gpu) = @_;
my $total = 0;

View File

@ -27,6 +27,7 @@ use FindBin qw($Bin);
use lib $Bin . "/lib", "/usr/lib/monitorix";
use Monitorix;
use HTTPServer;
use POSIX qw(WNOHANG LC_TIME setlocale uname pause setsid);
use Config::General;
use Getopt::Std;
@ -58,6 +59,9 @@ sub INT_handler {
logger("SIG$signal caught.");
flush_accounting_rules(\%config, $options{d});
if(lc($config{httpd_builtin}->{enabled} eq "y")) {
kill(15, $config{httpd_pid});
}
logger("Exiting.");
exit(0);
}
@ -181,7 +185,7 @@ sub create_index {
<html>
<head>
<title>$config{title}</title>
<link rel="shortcut icon" href="$config{base_url}/$config{favicon}">
<link rel="shortcut icon" href="$config{base_url}$config{favicon}">
</head>
<body bgcolor="$bgcolor" text="#888888" vlink="#888888" link="#888888">
<center>
@ -191,7 +195,7 @@ sub create_index {
<table>
<tr>
<td>
<a href="http://www.monitorix.org/"><img src="logo_top.png" border="0"></a>
<a href="http://www.monitorix.org/"><img src="$config{base_url}$config{logo_top}" border="0"></a>
</td>
</tr>
<tr>
@ -201,7 +205,7 @@ sub create_index {
</tr>
</table>
<p>
<form action="$config{base_cgi}/monitorix.cgi" method="get">
<form action="$config{base_cgi}monitorix.cgi" method="get">
<table cellspacing="5" cellpadding="0" bgcolor="$table_back_color" border="1">
<tr>
<td bgcolor="$title_back_color">
@ -402,7 +406,7 @@ $0 = sprintf("%s %s%s%s%s",
$options{d} ? " -d $options{d}" : "",
$options{v} ? " -v" : "");
daemonize();
#daemonize();
logger("Starting Monitorix version " . VERSION . " (pid $$).");
if($options{p}) {
@ -433,11 +437,16 @@ if($options{d}) {
}
# save the path of the configuration file
if(open(OUT, "> " . $config{base_dir} . "/cgi-bin/monitorix.conf.path")) {
if(open(OUT, "> " . $config{base_dir} . "/cgi/monitorix.conf.path")) {
print(OUT "$options{c}\n");
close(OUT);
} else {
logger("Unable to create the file '$config{base_dir}/cgi-bin/monitorix.conf.path'.");
logger("Unable to create the file '$config{base_dir}/cgi/monitorix.conf.path'.");
}
if(lc($config{httpd_builtin}->{enabled} eq "y")) {
httpd_setup(\%config, $options{d});
logger("HTTP built-in server pid is '$config{httpd_pid}'.");
}
flush_accounting_rules(\%config, $options{d});

View File

@ -108,7 +108,7 @@ sub multihost {
for($n2 = 0, $n = $n - $multihost->{graphs_per_row}; $n2 < $multihost->{graphs_per_row}; $n2++) {
if($n < scalar(@host)) {
print " <td bgcolor='$colors->{title_bg_color}' style='vertical-align: top; height: 10%; width: 10%;'>\n";
print " <iframe src=$url[$n]$config->{base_cgi}/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetag height=201 width=397 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
print " <iframe src=$url[$n]$config->{base_cgi}monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetag height=201 width=397 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
print " </td>\n";
}
@ -145,7 +145,7 @@ sub multihost {
print " </tr>\n";
print " <tr>\n";
print " <td bgcolor='$colors->{title_bg_color}' style='vertical-align: top; height: 10%; width: 10%;'>\n";
print " <iframe src=$url[$cgi->{val}]$config->{base_cgi}/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetagbig height=249 width=545 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
print " <iframe src=$url[$cgi->{val}]$config->{base_cgi}monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetagbig height=249 width=545 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
print " </td>\n";
print " </tr>\n";
print " <tr>\n";
@ -198,7 +198,7 @@ FATAL: Monitorix is unable to continue!
File 'monitorix.conf.path' not found.
Please make sure that 'base_dir' option is correctly configured and that
this CGI is located in the 'base_dir'/cgi-bin/ directory.
this CGI is located in the 'base_dir'/cgi/ directory.
And don't forget to restart Monitorix for the changes to take effect.
EOF
@ -216,7 +216,7 @@ if(!($config{hostname})) { # called from the command line
$config{hostname} = "127.0.0.1";
$config{url} = "http://127.0.0.1";
}
$config{url} .= $config{base_url} . "/";
$config{url} .= $config{base_url};
# get the current OS and kernel version
my $release;
@ -234,8 +234,10 @@ if($mode ne "localhost") {
($mode, $val) = split(/\./, $mode);
}
print("Content-Type: text/html\n");
print("\n");
if(lc($config{httpd_builtin}->{enabled} ne "y")) {
print("Content-Type: text/html\n");
print("\n");
}
# default white theme colors
$colors{graph_colors} = ();
@ -318,7 +320,7 @@ if(!$silent) {
print("<html>\n");
print(" <head>\n");
print(" <title>$config{title}</title>\n");
print(" <link rel='shortcut icon' href='" . $config{base_url} . "/" . $config{favicon} . "'>\n");
print(" <link rel='shortcut icon' href='" . $config{base_url} . $config{favicon} . "'>\n");
if($config{refresh_rate}) {
print(" <meta http-equiv='Refresh' content='" . $config{refresh_rate} . "'>\n");
}
@ -428,7 +430,7 @@ if(!$silent) {
print(" </font>\n");
print(" </center>\n");
print(" <p>\n");
print(" <a href='http://www.monitorix.org'><img src='" . $config{url} . "logo_bot.png' border='0'></a>\n");
print(" <a href='http://www.monitorix.org'><img src='" . $config{url} . $config{logo_bottom} . "' border='0'></a>\n");
print(" <br>\n");
print(" <font face='Verdana, sans-serif' color='" . $colors{fg_color} . "' size='-2'>\n");
print("Copyright &copy; 2005-2013 Jordi Sanfeliu\n");

View File

@ -14,8 +14,15 @@ disable_javascript_void = n
base_dir = /usr/share/monitorix/
base_lib = /var/lib/monitorix/
base_url = /monitorix
base_cgi = /monitorix-cgi
base_url = /
base_cgi = /
<httpd_builtin>
enabled = y
port = 8080
user = nobody
group = nobody
</httpd_builtin>
# Log files pathnames
@ -458,7 +465,9 @@ timeout = 15
imgs_dir = imgs/
usage_dir = usage/
report_dir = reports/
favicon = /monitorixico.png
favicon = monitorixico.png
logo_top = logo_top.png
logo_bottom = logo_bot.png
<black>
canvas = 000000