2014-05-13 Ramon Novoa <rnovoa@artica.es>

* bin/pandora_server: Added support to run as a native win32 service.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@9924 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2014-05-13 13:34:57 +00:00
parent 72a4336b50
commit 828a23432b
2 changed files with 277 additions and 108 deletions

View File

@ -1,3 +1,7 @@
2014-05-13 Ramon Novoa <rnovoa@artica.es>
* bin/pandora_server: Added support to run as a native win32 service.
2014-05-10 Junichi Satoh <junichi@rworks.jp>
* util/recon_script/ipmi-recon.pl, util/recon_script/snmpdevices.pl,

View File

@ -19,7 +19,9 @@
use strict;
use warnings;
use Getopt::Std;
use POSIX qw(strftime);
use threads;
# Default lib dir for RPM and DEB packages
use lib '/usr/lib/perl5';
@ -37,10 +39,15 @@ use PandoraFMS::WMIServer;
use PandoraFMS::PluginServer;
use PandoraFMS::PredictionServer;
# Constants for Win32 services.
use constant WIN32_SERVICE_STOPPED => 0x01;
use constant WIN32_SERVICE_RUNNING => 0x04;
# Global vars
my %Config;
my %Config :shared;
my @Servers;
my $DBH;
my $RUN :shared = 1;
########################################################################################
# Server shutdown. Handler to do a controlled shutdown.
@ -243,7 +250,7 @@ sub pandora_server_tasks ($) {
$pa_config->{'dbuser'}, $pa_config->{'dbpass'});
my $counter = 0;
while (1) {
while ($RUN == 1) {
eval{
# TASKS EXECUTED EVERY 5 SECONDS (Low latency tasks)
# --------------------------------------------------
@ -331,6 +338,154 @@ sub pandora_server_tasks ($) {
}
}
################################################################################
## Install the Windows service.
################################################################################
sub win32_install_service() {
# Load Win32::Daemon.
eval "use Win32::Daemon";
die($@) if ($@);
# Configure and install the service.
my $service_path = $0;
my $service_params = "-S run \"$ARGV[0]\"";
my %service_hash = (
machine => '',
name => 'PANDORAFMSSRV',
display => 'Pandora FMS Server',
path => $service_path,
user => '',
pwd => '',
description => 'Pandora FMS Server http://pandorafms.com/',
parameters => $service_params
);
if (Win32::Daemon::CreateService(\%service_hash)) {
print "Successfully added.\n";
exit 0;
} else {
print "Failed to add service: " . Win32::FormatMessage(Win32::Daemon::GetLastError()) . "\n";
exit 1;
}
}
################################################################################
## Install the Windows service.
################################################################################
sub win32_uninstall_service() {
# Load Win32::Daemon.
eval "use Win32::Daemon";
die($@) if ($@);
# Uninstall the service.
if (Win32::Daemon::DeleteService('', 'PANDORAFMSSRV')) {
print "Successfully deleted.\n";
exit 0;
} else {
print "Failed to delete service: " . Win32::FormatMessage(Win32::Daemon::GetLastError()) . "\n";
exit 1;
}
}
################################################################################
## Windows service callback function for the running event.
################################################################################
sub callback_running {
if (Win32::Daemon::State() == WIN32_SERVICE_RUNNING) {
}
}
################################################################################
## Windows service callback function for the start event.
################################################################################
sub callback_start {
no strict;
# Accept_connections ();
my $thr = threads->create(\&main);
if (!defined($thr)) {
Win32::Daemon::State(WIN32_SERVICE_STOPPED);
Win32::Daemon::StopService();
return;
}
$thr->detach();
Win32::Daemon::State(WIN32_SERVICE_RUNNING);
}
################################################################################
## Windows service callback function for the stop event.
################################################################################
sub callback_stop {
$RUN = 0;
Win32::Daemon::State(WIN32_SERVICE_STOPPED);
Win32::Daemon::StopService();
}
################################################################################
# Run as a Windows service.
################################################################################
sub win32_service_run() {
# Load Win32::Daemon.
eval "use Win32::Daemon";
die($@) if ($@);
# Run the Pandora FMS Server as a Windows service.
Win32::Daemon::RegisterCallbacks({
start => \&callback_start,
running => \&callback_running,
stop => \&callback_stop,
});
Win32::Daemon::StartService();
}
################################################################################
## Parse command line options.
################################################################################
sub parse_options {
my %opts;
my $tmp;
my @t_addresses_tmp;
# Get options
if (getopts('S:', \%opts) == 0 || defined ($opts{'h'})) {
print_help ();
exit 1;
}
# Win32 service management
if (defined ($opts{'S'})) {
my $service_action = $opts{'S'};
if ($^O ne 'MSWin32') {
error ("Windows services are only available on Win32.");
} else {
eval "use Win32::Daemon";
die($@) if ($@);
if ($service_action eq 'install') {
win32_install_service();
} elsif ($service_action eq 'uninstall') {
win32_uninstall_service();
} elsif ($service_action eq 'run') {
win32_service_run;
} else {
error("Unknown action: $service_action");
}
}
}
}
################################################################
################################################################
## Main.
################################################################
################################################################
sub main() {
$SIG{'TERM'} = 'pandora_shutdown';
$SIG{'INT'} = 'pandora_shutdown';
@ -413,7 +568,7 @@ if ($Config{'verbosity'} == 10) {
# Main loop
my $time_ref = time ();
while (1) {
while ($RUN == 1) {
eval {
@ -462,3 +617,13 @@ while (1) {
threads->yield;
sleep ($Config{'server_threshold'});
}
pandora_shutdown();
}
# Parse command line options.
parse_options;
# Run as a regular process.
main();