Hallo,
ich teile heute mein Watchdog-Skript, das ich benutze, um zu prüfen ob IPS „lebt“. Das Skript ist sehr einfach zu verwenden und leistet bei mir seit vielen Monaten gute Dienste. Es funktioniert folgendermaßen:
Ein CMD-Skript wird erstellt und als eigener Prozess gestartet. Das CMD-Skript erstellt regelmäßig eine Datei und wartet darauf, dass diese vom IPS-Skript aus umbenannt wird. Bleibt dies aus, so wird zunächst der IPS-Dienst beendet. Nach einer Weile wird der Prozess, falls er noch läuft, abgeschossen. Dann wird versucht, ihn neu zu starten. Das Skript wartet auf ein Lebenszeichen. Stellt sich dieses nicht ein, d.h. der Neustart des IPS-Dienstes schlug fehl, so startet das Skript das ganze System neu.
Alle Aktionen des Watchdog-Skripts werden in der Datei watchdog.log im Log-Ordner protokolliert.
Zum Installieren muss einfach der Quellcode unten in ein leeres Skript kopiert und dieses einmal manuell gestartet werden. Es wird daraufhin eine Variable „Active“ vom Typ Boolean angelegt, über die sich das Watchdog-Skript jederzeit manuell starten bzw. beenden lässt.
Im Shutdown-Skript sollte diese Variable auf false, im Startup-Skript auf true gesetzt werden!
Bitte teilt mir mit, falls es Probleme gibt.
<?
$vid = @IPS_GetVariableIdByName('Active', $_IPS['SELF']);
if($vid === FALSE)
{
$vid = IPS_CreateVariable(0);
IPS_SetName($vid, 'Active');
IPS_SetParent($vid, $_IPS['SELF']);
$eid = IPS_CreateEvent(0);
IPS_SetEventTrigger($eid, 1, $vid);
IPS_SetParent($eid, $_IPS['SELF']);
IPS_SetEventActive($eid, true);
SetValue($vid, false);
}
if($_IPS['SENDER'] == 'Execute')
{
$hdl = @fopen('watchdog.cmd', 'r');
if($hdl)
{
echo "watchdog.cmd already exists. Checking content...";
if(filesize('watchdog.cmd') == 0)
$currentFileContent = '';
else
$currentFileContent = fread($hdl, filesize('watchdog.cmd'));
fclose($hdl);
if($currentFileContent != scriptFileContent())
{
echo "Content needs to be updated!\r";
$hdl = @fopen('watchdog.cmd', 'w');
if($hdl)
{
fwrite($hdl, scriptFileContent());
fclose($hdl);
echo "watchdog.cmd file updated successfully!\r";
}
else echo "Error: Unable to access watchdog.cmd file.\r";
}
else echo "Content is up to date.\r";
}
else
{
echo "Creating watchdog.cmd...";
$hdl = @fopen('watchdog.cmd', 'w');
if($hdl)
{
fwrite($hdl, scriptFileContent());
fclose($hdl);
echo "file created successfully!\r";
}
else echo "unable to create file!\r";
}
IPS_SetScriptTimer($_IPS['SELF'], 60);
$hdl = @fopen('watchdog.kill', 'w');
if($hdl) fclose($hdl);
}
else if($_IPS['SENDER'] == 'Variable')
{
if(!$_IPS['VALUE'])
{
$hdl = @fopen('watchdog.kill', 'w');
if($hdl) fclose($hdl);
}
}
else
{
if(file_exists('alive.check'))
{
@rename('alive.check', 'alive.ok');
}
else if(file_exists('alive.ok'))
{
@unlink('alive.ok');
}
else
{
if(GetValue($vid))
{
echo "watchdog.cmd not running, starting script file now...\r";
IPS_Execute('cmd.exe', '/c c:\IP-Symcon\scripts\watchdog.cmd', false, false);
}
else
{
$hdl = @fopen('watchdog.kill', 'w');
if($hdl) fclose($hdl);
}
}
}
function scriptFileContent()
{
$scriptFileContent = <<<HEREDOC
@echo off
cd C:\IP-Symcon\scripts
if "%1"=="" goto callWithLog
echo %DATE% %TIME% IP-Symcon Watchdog CMD Script started!
if exist watchdog.kill del watchdog.kill>nul
set firstPass=1
:beginCheck
if exist alive.ok del alive.ok>nul
echo %DATE% %TIME% Checking (90 Sec. Timeout)...
echo %DATE% %TIME%>alive.check
ping 1.1.1.1 -n 1 -w 90000>nul
if exist watchdog.kill goto watchdogKill
if exist alive.check goto noResponse
goto aliveOk
:aliveOk
echo %DATE% %TIME% IP-Symcon is responding correctly!
set firstPass=0
goto beginCheck
:noResponse
if "%firstPass%"=="1" goto noWatchdog
if "%firstPass%"=="2" goto restartFailed
echo %DATE% %TIME% Warning: IP-Symcon is not responding. Shutting down Service (300 Sec. Timeout)...
start /b net stop IPSServer>nul
ping 1.1.1.1 -n 1 -w 300000>nul
echo %DATE% %TIME% Killing process (if still running)...
taskkill /F /IM ips.exe>nul
echo %DATE% %TIME% Starting Service (120 Sec. Delay)...
net start IPSServer>nul
ping 1.1.1.1 -n 1 -w 120000>nul
if exist watchdog.kill del watchdog.kill>nul
set firstPass=2
goto beginCheck
:restartFailed
echo %DATE% %TIME% Error: Starting of Service failed. Rebooting.
rem using /f can lead to unstable system state!
rem shutdown /r /t 60 /f>nul
shutdown /r /t 60>nul
goto end
:noWatchdog
echo %DATE% %TIME% Error: IP-Symcon did not respond to the first check.
goto end
:watchdogKill
echo %DATE% %TIME% Kill request received from IP-Symcon!
if exist alive.check del alive.check>nul
if exist alive.ok del alive.ok>nul
if exist watchdog.kill del watchdog.kill>nul
goto end
:callWithLog
call watchdog.cmd log>>..\logs\watchdog.log
goto end
:end
echo %DATE% %TIME% Script execution ended.
HEREDOC;
return $scriptFileContent;
}
?>
Ergänzung: Shutdown-Skript.
Falls noch keines existiert, Skript erstellen und „Kern Instanzen / Event Handler“ als Shudown-Skript wählen.
Im Shutdown-Skript sollte der Watchdog beendet werden:
SetValue(12345 , false); // statt 12345 die ID der Active-Variable einsetzen
$hdl = @fopen('watchdog.kill', 'w');
if($hdl) fclose($hdl);