@echo off setlocal REM #################################### REM Description: This script is used to handle the different events for the REM apcupsd service on server.test.com. REM Required files: background, popup, apcupsd, shutdown, hostname, tr, blat REM Last modified: July 6, 2010 REM #################################### REM ################ REM Set variables REM ################ set command=%1 set background="C:\Program Files\apcupsd\bin\background.exe" set popup=%background% "C:\Program Files\apcupsd\bin\popup.exe" set apcupsd="C:\Program Files\apcupsd\bin\apcupsd.exe" set shutdown="C:\WINDOWS\system32\shutdown.exe" set hostname="C:\WINDOWS\system32\hostname.exe" set tr="C:\Program Files\System Utilities\bin\tr.exe" set blat="C:\WINDOWS\system32\blat.exe" %hostname% > %hostname_results%# for /f "tokens=1" %%x in (%hostname_results%#) do (set host=%%x) del /f /q # > nul 2>&1 echo %host% | %tr% '[:upper:]' '[:lower:]' > %lowercase_results%# for /f "tokens=1" %%x in (%lowercase_results%#) do (set host=%%x) del /f /q # > nul 2>&1 set host=server set month=%date:~-10,2% set day=%date:~-7,2% set year=%date:~-2,2% set current_day=%month%/%day%/%year% set hour=%time:~-11,2% REM put a leading zero for hours before 10 AM if "%time:~0,1%"==" " set hour=0%hour:~1,1% set minute=%time:~-8,2% set second=%time:~-5,2% set current_time=%hour%:%minute%:%second% set timestamp=%current_day% %current_time% set to=networkalerts@test.com set profile=test.com REM Use call here because event script might be a batch file itself call ".\%command%" 2> nul REM This is retarded. "if errorlevel 99" means greater-than-or- REM equal-to 99, so we have to synthesize an == using two ifs. REM Ahh, the glory of Windows batch programming. At least they REM gave us a NOT op. if not errorlevel 99 goto :events if not errorlevel 100 goto :done :events REM REM powerout, onbattery, offbattery, mainsback events occur REM in that order. REM if "%command%" == "commfailure" goto :commfailure if "%command%" == "commok" goto :commok if "%command%" == "powerout" goto :powerout if "%command%" == "onbattery" goto :onbattery if "%command%" == "offbattery" goto :offbattery if "%command%" == "mainsback" goto :mainsback if "%command%" == "failing" goto :failing if "%command%" == "timeout" goto :timeout if "%command%" == "loadlimit" goto :loadlimit if "%command%" == "runlimit" goto :runlimit if "%command%" == "doshutdown" goto :doshutdown if "%command%" == "annoyme" goto :annoyme if "%command%" == "emergency" goto :emergency if "%command%" == "changeme" goto :changeme if "%command%" == "remotedown" goto :remotedown if "%command%" == "startselftest" goto :startselftest if "%command%" == "endselftest" goto :endselftest if "%command%" == "battdetach" goto :battdetach if "%command%" == "battattach" goto :battattach echo Unknown command '%command%' echo. echo Usage: %0 command echo. echo Warning: this script is intended to be launched by echo apcupsd and should never be launched by users. goto :done :commfailure set description=Warning communications lost with UPS %2. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :commok set description=Communications restored with UPS %2. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :powerout goto :done :onbattery set description=Power failure on UPS %2. Running on batteries. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :offbattery set description=Power has returned on UPS %2. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :mainsback goto :done :failing set description=Battery power exhausted on UPS %2. Doing shutdown. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :timeout set description=Battery time limit exceeded on UPS %2. Doing shutdown. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :loadlimit set description=Remaining battery charge below limit on UPS %2. Doing shutdown. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :runlimit set description=Remaining battery runtime below limit on UPS %2. Doing shutdown. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :doshutdown set description=Apccontrol doing: %apcupsd% /kill on UPS %2. %popup% "%description% [%timestamp%]" %apcupsd% /kill %shutdown% /s /f /t 15 /c "Shutting down due to apcupsd event." goto :done :annoyme set description=Power problems with UPS %2. Please logoff. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :emergency set description=Emergency shutdown. Possible battery failure on UPS %2. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :changeme set description=Emergency! Batteries have failed on UPS %2. Change them NOW. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :remotedown set description=Remote shutdown. Beginning shutdown sequence. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :startselftest set description=Self-test starting %popup% "%description% [%timestamp%]" goto :done :endselftest set description=Self-test completed %popup% "%description% [%timestamp%]" goto :done :battdetach set description=Battery disconnected. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :battattach set description=Battery reattached. %popup% "%description% [%timestamp%]" set subject="Network Alert: Apcupsd event (%1) on %host% [%timestamp%]" set body="%timestamp% %description%" %blat% -to %to% -subject %subject% -body %body% -p %profile% -q > nul 2>&1 goto :done :done