Commit 2927811f authored by Dietmar Maurer's avatar Dietmar Maurer

pvestatd: use new Daemon class from pve-common

parent 4dbb80bb
...@@ -43,6 +43,9 @@ all: ${MANS} pvemailforward ...@@ -43,6 +43,9 @@ all: ${MANS} pvemailforward
%.1.pod: % %.1.pod: %
podselect $*>$@ podselect $*>$@
pvestatd.1.pod: pvestatd
perl -I.. ./pvestatd printmanpod >$@
pvectl.1.pod: pvectl pvectl.1.pod: pvectl
perl -I.. ./pvectl printmanpod >$@ perl -I.. ./pvectl printmanpod >$@
......
...@@ -18,37 +18,35 @@ DESC="PVE Status Daemon" ...@@ -18,37 +18,35 @@ DESC="PVE Status Daemon"
PIDFILE=/var/run/pvestatd.pid PIDFILE=/var/run/pvestatd.pid
# Exit if the package is not installed
test -f $DAEMON || exit 0 test -f $DAEMON || exit 0
[ -e /proxmox_install_mode ] && exit 0
# avoid warnings about uninstalled locales
export LC_ALL="C"
case "$1" in case "$1" in
start) start)
log_daemon_msg "Starting $DESC" "$NAME" log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON $DAEMON start
log_end_msg $? log_end_msg $?
;; ;;
stop) stop)
log_daemon_msg "Stopping $DESC" "$NAME" log_daemon_msg "Stopping $DESC" "$NAME"
start-stop-daemon --stop --quiet --retry TERM/5/TERM/10/KILL/2 --pidfile $PIDFILE $DAEMON stop
log_end_msg $? log_end_msg $?
;; ;;
reload) status)
log_daemon_msg "Reloading $DESC" "$NAME" $DAEMON status
if ( [ -e $PIDFILE ] && kill -0 `cat $PIDFILE`) then
start-stop-daemon --stop --quiet --pidfile $PIDFILE --signal HUP
else
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON
fi
log_end_msg $?
;; ;;
restart|force-reload) reload|restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME" log_daemon_msg "Restarting $DESC" "$NAME"
start-stop-daemon --stop --quiet --retry TERM/5/TERM/10/KILL/2 --pidfile $PIDFILE $DAEMON restart
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON
log_end_msg $? log_end_msg $?
;; ;;
*) *)
N=/etc/init.d/$NAME N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2 echo "Usage: $N {start|stop|status|reload|restart|force-reload}" >&2
exit 1 exit 1
;; ;;
esac esac
......
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
use strict; use strict;
use warnings; use warnings;
use PVE::SafeSyslog; use PVE::SafeSyslog;
use POSIX ":sys_wait_h"; use PVE::Daemon;
use Fcntl ':flock';
use Getopt::Long;
use Time::HiRes qw (gettimeofday); use Time::HiRes qw (gettimeofday);
use PVE::Tools qw(dir_glob_foreach file_read_firstline); use PVE::Tools qw(dir_glob_foreach file_read_firstline);
use PVE::ProcFSTools; use PVE::ProcFSTools;
...@@ -19,120 +18,50 @@ use PVE::RPCEnvironment; ...@@ -19,120 +18,50 @@ use PVE::RPCEnvironment;
use PVE::API2::Subscription; use PVE::API2::Subscription;
use PVE::AutoBalloon; use PVE::AutoBalloon;
$SIG{'__WARN__'} = sub { use base qw(PVE::Daemon);
my $err = $@;
my $t = $_[0];
chomp $t;
syslog('warning', "WARNING: %s", $t);
$@ = $err;
};
initlog('pvestatd');
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
die "please run as root\n" if $> != 0;
my $nodename = PVE::INotify::nodename();
my $opt_debug; my $opt_debug;
if (!GetOptions ('debug' => \$opt_debug)) { my $cmdline = [$0, @ARGV];
die "USAGE: $0 [--debug]\n";
}
my $opt_pidfile = "/var/run/pvestatd.pid"; my %daemon_options = (restart_on_error => 5, stop_wait_time => 5);
sub lockpidfile { my $daemon = __PACKAGE__->new('pvestatd', $cmdline, %daemon_options);
my $pidfile = shift;
my $lkfn = "$pidfile.lock";
if (!open (FLCK, ">>$lkfn")) { my $rpcenv = PVE::RPCEnvironment->init('cli');
my $msg = "can't aquire lock on file '$lkfn' - $!";
syslog ('err', $msg);
die "ERROR: $msg\n";
}
if (!flock (FLCK, LOCK_EX|LOCK_NB)) { $rpcenv->init_request();
close (FLCK); $rpcenv->set_language($ENV{LANG});
my $msg = "can't aquire lock '$lkfn' - $!"; $rpcenv->set_user('root@pam');
syslog ('err', $msg);
die "ERROR: $msg\n";
}
}
sub writepidfile {
my $pidfile = shift;
if (!open (PIDFH, ">$pidfile")) { my $nodename = PVE::INotify::nodename();
my $msg = "can't open pid file '$pidfile' - $!"; my $restart_request = 0;
syslog ('err', $msg);
die "ERROR: $msg\n";
}
print PIDFH "$$\n";
close (PIDFH);
}
# try to get the lock
lockpidfile($opt_pidfile);
# run in background
my $spid;
my $restart = $ENV{RESTART_PVESTATD};
if (!$opt_debug) { sub init {
open STDIN, '</dev/null' || die "can't read /dev/null"; my ($self) = @_;
open STDOUT, '>/dev/null' || die "can't write /dev/null";
}
if (!$restart && !$opt_debug) { PVE::Cluster::cfs_update();
$spid = fork();
if (!defined ($spid)) {
my $msg = "can't put server into background - fork failed";
syslog('err', $msg);
die "ERROR: $msg\n";
} elsif ($spid) { #parent
exit (0);
}
} }
writepidfile($opt_pidfile); sub shutdown {
my ($self) = @_;
open STDERR, '>&STDOUT' || die "can't close STDERR\n";
sub cleanup {
unlink "$opt_pidfile.lock";
unlink "$opt_pidfile";
}
$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub {
syslog('info' , "server closing"); syslog('info' , "server closing");
$SIG{INT} = 'DEFAULT';
# wait for children # wait for children
1 while (waitpid(-1, POSIX::WNOHANG()) > 0); 1 while (waitpid(-1, POSIX::WNOHANG()) > 0);
cleanup(); $self->exit_daemon(0);
}
exit (0);
};
PVE::INotify::inotify_init(); sub hup {
my ($self) = @_;
my $reload_config; syslog('info' , "received signal HUP");
if ($restart) { $restart_request = 1;
syslog('info' , "restarting server");
} else {
syslog('info' , "starting server");
} }
$SIG{HUP} = sub {
$reload_config = 1;
};
sub update_node_status { sub update_node_status {
my ($avg1, $avg5, $avg15) = PVE::ProcFSTools::read_loadavg(); my ($avg1, $avg5, $avg15) = PVE::ProcFSTools::read_loadavg();
...@@ -272,6 +201,7 @@ sub find_vzctl_console_pids { ...@@ -272,6 +201,7 @@ sub find_vzctl_console_pids {
return $res; return $res;
} }
sub remove_stale_openvz_consoles { sub remove_stale_openvz_consoles {
my $vmstatus = PVE::OpenVZ::vmstatus(); my $vmstatus = PVE::OpenVZ::vmstatus();
...@@ -383,36 +313,18 @@ my $next_update = 0; ...@@ -383,36 +313,18 @@ my $next_update = 0;
my $cycle = 0; my $cycle = 0;
my $updatetime = 10; my $updatetime = 10;
my $commandline = [$0, @ARGV];
$0 = "pvestatd";
sub restart_server {
my $waittime = shift;
syslog('info', "server shutdown (restart)");
$ENV{RESTART_PVESTATD} = 1;
sleep($waittime) if $waittime; # avoid high server load due to restarts
PVE::INotify::inotify_close();
exec (@$commandline);
exit (-1); # never reached?
}
my $initial_memory_usage; my $initial_memory_usage;
for (;;) { # forever sub run {
my ($self) = @_;
for (;;) { # forever
eval {
$next_update = time() + $updatetime; $next_update = time() + $updatetime;
if ($cycle) { if ($cycle) {
my ($ccsec, $cusec) = gettimeofday (); my ($ccsec, $cusec) = gettimeofday ();
eval { eval {
$reload_config = 0;
# syslog('info', "start status update"); # syslog('info', "start status update");
PVE::Cluster::cfs_update(); PVE::Cluster::cfs_update();
update_status(); update_status();
...@@ -441,25 +353,35 @@ for (;;) { # forever ...@@ -441,25 +353,35 @@ for (;;) { # forever
if ($diff > 5*1024*1024) { if ($diff > 5*1024*1024) {
syslog ('info', "restarting server after $cycle cycles to " . syslog ('info', "restarting server after $cycle cycles to " .
"reduce memory usage (free $mem->{resident} ($diff) bytes)"); "reduce memory usage (free $mem->{resident} ($diff) bytes)");
restart_server (); $self->restart_daemon();
} }
} }
my $wcount = 0; my $wcount = 0;
while ((time() < $next_update) && while ((time() < $next_update) &&
($wcount < $updatetime) && # protect against time wrap ($wcount < $updatetime) && # protect against time wrap
!$reload_config) { $wcount++; sleep (1); }; !$restart_request) { $wcount++; sleep (1); };
};
my $err = $@; $self->restart_daemon() if $restart_request;
if ($err) {
syslog ('err', "ERROR: $err");
restart_server(5);
exit (0);
} }
} }
$daemon->register_start_command(__PACKAGE__);
$daemon->register_restart_command(__PACKAGE__);
$daemon->register_stop_command(__PACKAGE__);
$daemon->register_status_command(__PACKAGE__);
my $cmddef = {
start => [ __PACKAGE__, 'start', []],
restart => [ __PACKAGE__, 'restart', []],
stop => [ __PACKAGE__, 'stop', []],
status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
};
my $cmd = shift;
PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
exit (0); exit (0);
__END__ __END__
...@@ -470,11 +392,14 @@ pvestatd - PVE Status Daemon ...@@ -470,11 +392,14 @@ pvestatd - PVE Status Daemon
=head1 SYNOPSIS =head1 SYNOPSIS
pvestatd =include synopsis
=head1 DESCRIPTION =head1 DESCRIPTION
Documentation is available at www.proxmox.com This daemom queries the status of VMs, storages and containers at
regular intervals. The result is sent to all nodes in the cluster.
=include pve_copyright
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment