From 2b259491130eb521fd3795b4596332d23550a44d Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Thu, 31 Jan 2013 18:48:29 +0100 Subject: [PATCH] introduced HTTP server built-in --- lib/Monitorix.pm | 27 ++++++++++++++++++++++++++- monitorix | 21 +++++++++++++++------ monitorix.cgi | 18 ++++++++++-------- monitorix.conf | 15 ++++++++++++--- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/lib/Monitorix.pm b/lib/Monitorix.pm index 8522a96..d625527 100644 --- a/lib/Monitorix.pm +++ b/lib/Monitorix.pm @@ -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; diff --git a/monitorix b/monitorix index dca3464..cbfce67 100755 --- a/monitorix +++ b/monitorix @@ -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 { $config{title} - +
@@ -191,7 +195,7 @@ sub create_index { @@ -201,7 +205,7 @@ sub create_index {
- +

-

+ \n"; } @@ -145,7 +145,7 @@ sub multihost { print " \n"; print " \n"; print " \n"; print " \n"; print " \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("\n"); print(" \n"); print(" $config{title}\n"); - print(" \n"); + print(" \n"); if($config{refresh_rate}) { print(" \n"); } @@ -428,7 +430,7 @@ if(!$silent) { print(" \n"); print(" \n"); print("

\n"); - print(" \n"); + print(" \n"); print("
\n"); print(" \n"); print("Copyright © 2005-2013 Jordi Sanfeliu\n"); diff --git a/monitorix.conf b/monitorix.conf index e6e9030..d39e0c7 100644 --- a/monitorix.conf +++ b/monitorix.conf @@ -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 = / + + + enabled = y + port = 8080 + user = nobody + group = nobody + # 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 canvas = 000000

@@ -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}); diff --git a/monitorix.cgi b/monitorix.cgi index 14684a6..24e5104 100755 --- a/monitorix.cgi +++ b/monitorix.cgi @@ -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 " \n"; - print " \n"; + print " \n"; print "
\n"; - print " \n"; + print " \n"; print "