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 {
- ![]() |
-