@ECHO OFF

REM ************************************************************
REM
REM      Filename: CheckHeat.cmd
REM       Version: 1.3.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: 2006-04-10 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 check the temperature of an
REM   area using an APC UPS with a network card or with an
REM   APC Powerchute Business Edition software installed on
REM   a computer with SNMP features enabled and linked with
REM   the UPS using a 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. Do not forget to put "quotes" around your log file path
REM   name if spaces are included.
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 heat) 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   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.2.6 SysCo/al More information about usage in the comments
REM   2005-10-09 1.2.5 SysCo/al Changed PUBLIC to public for default SNMP community name
REM   2005-08-13 1.2.4 SysCo/al More information about usage in the comments
REM   2005-07-18 1.2.3 SysCo/al Comments fixes
REM   2005-07-18 1.2.2 SysCo/al Supporting snmpread and hostname environment variable
REM   2005-07-18 1.2.1 SysCo/al Minor fixes
REM   2005-07-18 1.2   SysCo/al Modified to be generic
REM   2005-07-17 1.1   SysCo/al Some fixes
REM   2005-07-15 1.0   SysCo/al Initial release
REM
REM ************************************************************


REM ==================================================
REM ========== OPTIONS MUST BE CHANGED HERE ==========

REM Setting the different temperature threshold values in °C (integer values)
SET _alarmmax=27
SET _warnmax=25
SET _warnmin=18
SET _alarmmin=15

REM Environmental probe installed (0 or 1)
SET _probe1=1
SET _probe2=1

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 ========== 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=heat


REM Temperature unit (°C)
SET _tempunit=°C


IF "%_hostname%"=="" SET _hostname=%hostname%
IF "%_community%"=="" SET _community=%snmpread%
IF "%_community%"=="" SET _community=public


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 internal UPS temperature expressed in Celsius.
REM This information is required.
for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.1.2.2.2.0 ^| find /I "Gauge32"') do SET i=%%j
IF "%i%"=="" GOTO Error
for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET internaltemp=%%i
SET heatmax=%internaltemp%
SET heatmin=%internaltemp%


If NOT "%_probe1%"=="1" GOTO NoProbe1

REM The ambient temperature in Celsius for Probe 1.
REM This information is optional.
for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.2.1.1.0 ^| find /I "Gauge32"') do SET i=%%j
IF "%i%"=="" GOTO NoTemp1
for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET heat1=%%i
SET heatmax=%heat1%
SET heatmin=%heat1%
:NoTemp1


REM The relative humidity as a percentage for Probe 1.
REM This information is optional.
for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.2.1.2.0 ^| find /I "Gauge32"') do SET i=%%j
IF NOT "%i%"=="" for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET humidity1=%%i
:NoProbe1


If NOT "%_probe2%"=="1" GOTO NoProbe2

REM The ambient temperature in Celsius for Probe 2.
REM This information is optional.
for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.2.1.3.0 ^| find /I "Gauge32"') do SET i=%%j
IF "%i%"=="" GOTO NoTemp2
for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET heat2=%%i
IF %heat2% GTR %heat1% SET heatmax=%heat2%
IF %heat2% LSS %heat1% SET heatmin=%heat2%
:NoTemp2


REM The relative humidity as a percentage for Probe 2.
REM This information is optional.
for /f "tokens=1* delims=2" %%i, in ('snmputil get %_deviceip% %_community% .1.3.6.1.4.1.318.1.1.2.1.4.0 ^| find /I "Gauge32"') do SET i=%%j
IF NOT "%i%"=="" for /f "tokens=1* delims= " %%i, in ('ECHO %i%') do SET humidity2=%%i
:NoProbe2


IF %heatmin% LSS %_warnmin% SET _alarm=LOW_YELLOW
IF %heatmin% LSS %_alarmmin% SET _alarm=LOW_RED
IF %heatmax% GTR %_warnmax% SET _alarm=HIGH_YELLOW
IF %heatmax% GTR %_alarmmax% SET _alarm=HIGH_RED


SET BBCOLOR=green
SET BBMESSAGE=Temperature is ok, %heatmax%%_tempunit% is between %_alarmmin%%_tempunit% and %_alarmmax%%_tempunit%


IF NOT "%_alarm%"=="LOW_YELLOW" GOTO NoLY
SET BBMESSAGE=Temperature is falling, %heatmin%%_tempunit% and we have a minimum of %_alarmmin%%_tempunit% with an alert at %_warnmin%%_tempunit%
SET BBCOLOR=yellow
GOTO TestEnd
:NoLY


IF NOT "%_alarm%"=="LOW_RED" GOTO NoLR
SET BBMESSAGE=Temperature is to low, %heatmin%%_tempunit% and we have a minimum of %_alarmmin%%_tempunit%
SET BBCOLOR=red
GOTO TestEnd
:NoLR


IF NOT "%_alarm%"=="HIGH_YELLOW" GOTO NoHY
SET BBMESSAGE=Temperature is rising, %heatmax%%_tempunit% and we have a maximum of %_alarmmax%%_tempunit% with an alert at %_warnmax%%_tempunit%
SET BBCOLOR=yellow
GOTO TestEnd
:NoHY


IF NOT "%_alarm%"=="HIGH_RED" GOTO NoHR
SET BBCOLOR=red
SET BBMESSAGE=Temperature is to high, %heatmax%%_tempunit% and we have a maximum of %_alarmmax%%_tempunit%
GOTO TestEnd
:NoHR


: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%

IF NOT "%_probe1%%_probe2%"=="00" ECHO. >> %_bblogfile%
IF "%_probe1%"=="1" ECHO                      1st probe: %heat1%%_tempunit% (relative humidity: %humidity1%%%) >> %_bblogfile%
IF "%_probe2%"=="1" ECHO                      2nd probe: %heat2%%_tempunit% (relative humidity: %humidity2%%%) >> %_bblogfile%

ECHO. >> %_bblogfile%
SET _infotups=
IF NOT "%_probe1%%_probe2%"=="00" SET _infotups= (for information only, not checked)
ECHO       Internal UPS temperature: %internaltemp%%_tempunit%%_infotups% >> %_bblogfile%
ECHO. >> %_bblogfile%
ECHO                     Alarm high: %_alarmmax%%_tempunit% >> %_bblogfile%
ECHO                   Warning high: %_warnmax%%_tempunit% >> %_bblogfile%
ECHO                    Warning low: %_warnmin%%_tempunit% >> %_bblogfile%
ECHO                      Alarm low: %_alarmmin%%_tempunit% >> %_bblogfile%
:NoMoreInfo

ECHO. >> %_bblogfile%
