package MPCServices; use strict; use Win32; use Win32::API; use Win32::Service; use Win32::OLE('in'); use constant LOG_DIR => "$ENV{SVP_HOME}\\wk\\supervisor\\setup\\log\\"; use constant LOG => ".log"; use constant LOG_CONFLICT => "-conflict"; use constant LOG_OLD => "-old"; use constant TRUE => 1; use constant FALSE => 0; use constant ERR_NOT_FOUND_FILE => 300; use constant MSG_SUCCESS => "Success"; use constant MSG_FAILED => "Failed"; use constant MAX_SIZE => 65536; use constant START => 0; use constant STOP => 1; use constant ERROR_SERVICE_ALREADY_RUNNING => 1056; use constant ERROR_SERVICE_NOT_ACTIVE => 1062; use constant ERROR_ACCESS_DENIED => 5; use constant ERROR_SUCCESS => 0; use constant ERROR => 1; my %funcMap = ( 0x100 => "getSupervisorIni", 0x200 => "stopServiceByBat", 0x300 => "stopServices", 0x400 => "changeStartup", 0x500 => "startCheckService", 0x600 => "startServiceByBat"); my %stateCodes = ( 1 => 'STOPPED', 2 => 'START PENDING', 3 => 'STOP PENDING', 4 => 'RUNNING'); use constant START_FILE => "startAllMPCServices"; use constant STOP_FILE => "stopAllMPCServices"; our $mode; sub checkServiceName { my $code = $_[0]; my $key; my $dval; my %services; Win32::Service::GetServices("",\%services); while(($key,$dval)=each(%services)) { if($dval eq $code){ return TRUE; } } return FALSE; } sub getServiceStatusCodeStr { my $code = $_[0]; my $ret; if(! defined($code)) { $ret = "UNKNOWN"; } elsif(exists($stateCodes{$code})) { $ret = $stateCodes{$code}; } else { $ret = "UNKNOWN(" . $code . ")"; } return $ret; } sub openLog() { my $name = $_[0]; my $oldFile; my $size; my $outLog; if(! -d LOG_DIR){ printMessage(ERR_NOT_FOUND_FILE); exit(ERROR); } my $logFile = LOG_DIR . $name . LOG; my $oldFile = LOG_DIR . $name . LOG_OLD . LOG; $size = -s $logFile; if($size >= MAX_SIZE){ rename $logFile, $oldFile; } if(!open($outLog, '>>', $logFile)) { $logFile = LOG_DIR . $name . LOG_CONFLICT . LOG; $oldFile = LOG_DIR . $name . LOG_CONFLICT . LOG_OLD . LOG; $size = -s $logFile; if($size >= MAX_SIZE){ rename $logFile, $oldFile; } if(!open($outLog, '>>', $logFile)) { printMessage(ERR_NOT_FOUND_FILE); exit(ERROR); } } return $outLog; } sub closeLog() { my $outLog = $_[0]; close($outLog) or die(qq/Can not close file: $!/); } sub writeLog() { my ($funcCode, $errCode, $message, $outLog) = @_; my $result; my $func; my ($sec,$min,$hour,$mday,$mon,$year,$wno) = localtime(time); my $time = sprintf("[%04d-%02d-%02d %02d:%02d:%02d]",$year+1900,$mon+1,$mday,$hour,$min, $sec); if($errCode != ERROR_SUCCESS && $errCode != ERROR_SERVICE_NOT_ACTIVE && $errCode != ERROR_SERVICE_ALREADY_RUNNING) { $result = MSG_FAILED; } else { $result = MSG_SUCCESS; } $func = $funcMap{$funcCode}; printf($outLog "%s[%s][%s][%s]\n", $time, $func, $message, $result); } sub printMessage{ my $errCode = $_[0]; my $msgNormal; my $msgErr; if($mode == STOP) { $msgNormal = "stopped"; $msgErr = "stop"; } elsif($mode == START){ $msgNormal = "started"; $msgErr = "start"; } if($errCode == ERROR_SUCCESS){ sleep(30); print "Storage Navigator services have $msgNormal.\n"; } else { print"ERROR: Failed to $msgErr Storage Navigator services. Code:$errCode\n"; $errCode = ERROR; } print"Press any key.\n"; return $errCode; } 1;