@ECHO OFF REM ************************************************************ REM REM Filename: CheckPower.cmd REM Version: 1.4.1 REM Language: Windows batch file for Windows NT4/2K/XP/2003 REM Copyright: SysCo systèmes de communication sa REM Created: 2005-07-15 SysCo/al REM Last modified: 2008-07-19 SysCo/al REM Web site: http://developer.sysco.ch/hobbit/ REM Email: developer@sysco.ch REM REM SpecialThanks: "aZLAn2000" for his feedback REM "trosenquist" for his feedback REM REM REM Description REM REM External batch script to monitor the power using an REM APC UPS with a network card or with an APC Powerchute REM Business Edition software installed on a computer with REM SNMP features enabled and linked with the UPS using a REM data cable. REM REM REM Usage REM REM The script has to be called by the Big Brother Client. It must be added REM in the Externals list (near the bottom of the Configuration Editor REM window of the Big Brother Client). The "Saved Logs Location" option of REM the client must be set to the same directory as the _bblogdir variable REM of the script. REM If you are using this script to connect to a UPS linked to a computer REM using a data cable, be sure that the SNMP stack is working correctly REM on this computer. On Windows 2003 server for example,you will have to REM add a read-only community name. This can be done by editing the REM properties of the SNMP Service and by adding the desired community name. REM REM REM External file needed REM REM SNMPutil (http://www.dynawell.com/reskit/microsoft/win2000/snmputil.zip) REM REM REM External file created REM REM One log file in the configured location REM REM REM Special issues REM REM - The free Microsoft tool SNMPutil MUST be somewhere included in the REM PATH, C:\Windows\System32 is a good choice REM REM - The alarm name (currently power) MUST be added in the REM svcerrlist list of the bbwarnsetup.cfg file of the server. REM REM - If UPS has no network card, APC PowerChute Business Edition Server REM must be installed on the computer that is connected to the UPS. REM REM - The script should be called in the "Externals list" of the client. REM REM - hostname and snmpread environment variables can be used (see below) REM REM REM Licence REM REM Copyright (c) 2005-06, SysCo systèmes de communication sa REM SysCo (tm) is a trademark of SysCo systèmes de communication sa REM (http://www.sysco.ch/) REM All rights reserved. REM REM This script is free software; you can redistribute it and/or REM modify it under the terms of the GNU Lesser General Public REM License as published by the Free Software Foundation; either REM version 2.1 of the License, or (at your option) any later version. REM REM This library is distributed in the hope that it will be useful, REM but WITHOUT ANY WARRANTY; without even the implied warranty of REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU REM Lesser General Public License for more details. REM REM You should have received a copy of the GNU Lesser General Public REM License along with this library; if not, write to the Free Software REM Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA REM (http://www.fsf.org/) REM REM REM Change Log REM REM 2008-07-19 1.4.1 SysCo/al Support for BBWin client added REM 2006-10-04 1.3.1 SysCo/al Adding UPS model, serial number, battery last change REM Thinking about hobbit (http://hobbitmon.sourceforge.net/) REM 2006-03-09 1.1.4 SysCo/al More information about usage in the comments REM 2005-10-09 1.1.3 SysCo/al Changed PUBLIC to public for default SNMP community name REM 2005-08-13 1.1.2 SysCo/al More information about usage in the comments REM 2005-07-18 1.1 SysCo/al Generic release REM 2005-07-15 1.0 SysCo/al Initial release REM REM ************************************************************ REM ================================================== REM ========== OPTIONS MUST BE CHANGED HERE ========== REM Define if you are still using the "regular" Big Brother client or the REM GPL BBWin client (http://sourceforge.net/projects/bbwin) SET _BBWin=0 REM Define if you are still using a Big Brother Server or the REM GPL Hobbit Monitor (http://hobbitmon.sourceforge.net/) SET _Hobbit=0 REM Hostname sent to the server (string) REM If not defined, the environment variable hostname will be used SET _hostname= REM IP address of the device (ip address, could be 127.0.0.1 if the REM APC software with SNMP support is installed on this machine) SET _deviceip=127.0.0.1 REM Community name of the device (string) REM If not defined, the environment variable snmpread will be used SET _community= REM Logs location for external scripts (WITH the ending \) REM This foldermust be the same has the one defined in the option REM "Saved Logs Location" of the Big Brother Client. SET _bblogdir=C:\Logs\bbnt\ REM Auto-extraction of the tmppath of BBWin REM We are using the environment variable _bblogdir for backward compatibility REM If you are still running under Windows 2000 or before and you didn't copy REM the REG.EXE file, please comments these three lines and add an extra line REM SET _bblogdir=path_of_your_tmp_dir_for_BBWin IF NOT "%_BBWin%"=="1" GOTO NoBBWin FOR /f "tokens=2*" %%i, IN ('REG QUERY HKLM\SOFTWARE\BBWin /v tmppath ^| FIND "tmppath"') do SET _temp=%%j> NUL IF NOT "%_temp%"=="" SET _bblogdir=%_temp%\ :NoBBWin REM ========== END OF THE OPTIONS HERE ========== REM ============================================= REM ************************************************************ REM ************************************************************ REM REM Main part of the script, please do not change anything REM without knowing what you are doing !!! REM REM ************************************************************ REM ************************************************************ REM Alarm name (should not be changed) REM This name MUST be added in the svcerrlist list of the bbwarnsetup.cfg file of the server SET _alarmname=power IF "%_hostname%"=="" SET _hostname=%hostname% IF "%_community%"=="" SET _community=%snmpread% IF "%_community%"=="" SET _community=public SET _bblogfile="%_bblogdir%-%_alarmname%" IF "%_BBWin%"=="1" SET _bblogfile="%_bblogdir%%_alarmname%" SET _alarm= SET _host_separator= IF NOT "%_hostname%"=="" SET _host_separator=: REM Extracting date and time information for /f "tokens=1,2*" %%i, in ('DATE /T') do SET _date=%%j for /f "tokens=1*" %%i, in ('TIME /T') do SET _time=%%i REM The UPS model for /f "tokens=1* delims=g" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.1.1.1.0 ^| find /I "String"') do SET s=%%j IF "%s%"=="" GOTO Error for /f "tokens=* delims= " %%s, in ('ECHO %s%') do SET upsmodel=%%s REM The UPS identifier for /f "tokens=1* delims=g" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.1.1.2.0 ^| find /I "String"') do SET s=%%j IF "%s%"=="" GOTO Error for /f "tokens=* delims= " %%s, in ('ECHO %s%') do SET upsident=%%s REM The UPS manufacture date for /f "tokens=1* delims=g" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.1.2.2.0 ^| find /I "String"') do SET s=%%j IF "%s%"=="" GOTO Error for /f "tokens=* delims= " %%s, in ('ECHO %s%') do SET manufdate=%%s REM The serial number of the UPS for /f "tokens=1* delims=g" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.1.2.3.0 ^| find /I "String"') do SET s=%%j IF "%s%"=="" GOTO Error for /f "tokens=* delims= " %%s, in ('ECHO %s%') do SET serialnumber=%%s REM The current state of the UPS. If the UPS is unable to determine REM the state of the UPS this variable is set to unknown(1). REM unknown(1) REM onLine(2) REM onBattery(3) REM onSmartBoost(4) REM timedSleeping(5) REM softwareBypass(6) REM off(7) REM rebooting(8) REM switchedBypass(9) REM hardwareFailureBypass(10) REM sleepingUntilPowerReturn(11) REM onSmartTrim(12) for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.4.1.1.0 ^| find /I "Integer32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET state=%%i REM Last replaced battery for /f "tokens=1* delims=g" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.2.1.3.0 ^| find /I "String"') do SET s=%%j IF "%s%"=="" GOTO Error for /f "tokens=* delims= " %%s, in ('ECHO %s%') do SET lastreplacedbat=%%s REM Indicates whether the UPS batteries need replacing. for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.2.2.4.0 ^| find /I "Integer32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET needreplacing=%%i REM The current utility line voltage in VAC. for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.3.2.1.0 ^| find /I "Gauge32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET input=%%i REM The output voltage of the UPS system in VAC. for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.4.2.1.0 ^| find /I "Gauge32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET output=%%i REM The remaining battery capacity expressed in percent of full capacity. for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.2.2.1.0 ^| find /I "Gauge32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET capacity=%%i REM The current UPS load expressed in percent of rated capacity. for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.4.2.3.0 ^| find /I "Gauge32"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET load=%%i REM The UPS battery run time remaining before battery exhaustion. (1/100 of seconds) for /f "tokens=1* delims=s" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.2.2.3.0 ^| find /I "TimeTicks"') do SET i=%%j IF "%i%"=="" GOTO Error for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET timeticks=%%i SET /A runtime = %timeticks% / 6000 IF %needreplacing% EQU 1 GOTO NoReplace SET BBCOLOR=red SET BBMESSAGE=UPS batteries need replacing GOTO TestEnd :NoReplace IF NOT %state% EQU 1 GOTO Not1 SET BBCOLOR=red SET BBMESSAGE=State of the UPS is reported as unknown GOTO TestEnd :Not1 IF NOT %state% EQU 2 GOTO Not2 SET BBCOLOR=green SET BBMESSAGE=Power is provided by the network (online) GOTO TestEnd :Not2 IF NOT %state% EQU 3 GOTO Not3 SET BBCOLOR=red SET BBMESSAGE=Power is provided by the battery of the UPS GOTO TestEnd :Not3 IF NOT %state% EQU 4 GOTO Not4 SET BBCOLOR=yellow SET BBMESSAGE=Power is provided by the UPS in Smart Boost mode GOTO TestEnd :Not4 IF NOT %state% EQU 5 GOTO Not5 SET BBCOLOR=yellow SET BBMESSAGE=The UPS is in timed sleeping state GOTO TestEnd :Not5 IF NOT %state% EQU 6 GOTO Not6 SET BBCOLOR=yellow SET BBMESSAGE=The UPS is bypassed by software GOTO TestEnd :Not6 IF NOT %state% EQU 7 GOTO Not7 SET BBCOLOR=red SET BBMESSAGE=The UPS is off GOTO TestEnd :Not7 IF NOT %state% EQU 8 GOTO Not8 SET BBCOLOR=yellow SET BBMESSAGE=The UPS is rebooting GOTO TestEnd :Not8 IF NOT %state% EQU 9 GOTO Not9 SET BBCOLOR=yellow SET BBMESSAGE=The UPS is switched to be bypassed GOTO TestEnd :Not9 IF NOT %state% EQU 10 GOTO Not10 SET BBCOLOR=red SET BBMESSAGE=The UPS is bypassed due to a hardware failure GOTO TestEnd :Not10 IF NOT %state% EQU 11 GOTO Not11 SET BBCOLOR=yellow SET BBMESSAGE=The UPS is sleeping until the power will return GOTO TestEnd :Not11 IF NOT %state% EQU 12 GOTO Not12 SET BBCOLOR=yellow SET BBMESSAGE=Power is provided by the UPS in Smart Trim mode GOTO TestEnd :Not12 IF NOT %state% GTR 1 GOTO NotG1 SET BBCOLOR=red SET BBMESSAGE=State of the UPS is reported as #%state%,which is unknown GOTO TestEnd :NotG1 :TestEnd GOTO End :Error SET BBCOLOR=red SET BBMESSAGE=Communication is lost with the UPS. SET SNMPERROR=true GOTO End :End ECHO %_hostname%%_host_separator%%BBCOLOR% %_date% %_time% [%_hostname%] > %_bblogfile% ECHO %BBMESSAGE% >> %_bblogfile% IF NOT "%SNMPERROR%"=="" GOTO NoMoreInfo ECHO. >> %_bblogfile% ECHO UPS model: %upsmodel%, %manufdate% >> %_bblogfile% ECHO Serial number: %serialnumber% - %upsident% >> %_bblogfile% ECHO. >> %_bblogfile% ECHO. >> %_bblogfile% ECHO Current utility line voltage: %input%V >> %_bblogfile% ECHO Current UPS output voltage: %output%V >> %_bblogfile% ECHO Current UPS load: %load%%% >> %_bblogfile% ECHO. >> %_bblogfile% ECHO Battery capacity: %capacity%%% >> %_bblogfile% ECHO UPS battery run time remaining: %runtime% minutes >> %_bblogfile% ECHO Battery last replaced date: %lastreplacedbat% >> %_bblogfile% ECHO. >> %_bblogfile% ECHO This script is running on %COMPUTERNAME%.%USERDNSDOMAIN% >> %_bblogfile% :NoMoreInfo ECHO. >> %_bblogfile%