@ echo off
setlocal

rem curl-heartbeat.bat - Tests connectivity to xMatters via proxy server.  For proxy-specific troubleshooting.
rem Copyright xMatters, inc, 2016
rem Requirements: <IAHome>\tools\curl.exe : cURL executable to perform HTTP requests for testing; <IAHome>\tools\ca-bundle.crt : SSL certificate bundle for curl.exe

rem Directions: read the five "Step" comments below and follow the instructions to configure the required variables.  Then invoke this script from a cmd window.


if not exist .\curl.exe (echo. & echo ERROR: please execute this command from ^<IAHome^>\tools and ensure that curl.exe is present.  Exiting... & goto :theEnd)

rem Step 1: Un-comment the appropriate provide "set xm_url" line and provide the URL for your xMatters instance
rem hosted instance example
rem set xm_url=https://companyName.hosted.xmatters.com/api/services/AlarmPointWebService
rem on-premise instance example
rem set xm_url=http://192.168.34.56:8888/api/services/AlarmPointWebService

rem Step 2: Un-comment the appropriate lines and provide the access details for your proxy server
rem set proxy=192.168.12.34:80
rem Un-comment only if your proxy requires authentication
rem set proxyUser=userID
rem set proxyPwd=secret
rem Un-comment only if your proxy requires NTLM authentication
rem set proxyNtlmDomain=someDomain.com

rem Step 3: Extract the RegisterIntegrationAgent request body from the IA log and paste it in the "set payload" line below.
rem Be sure to escape any " (doublequote) characters in the request body with a \ (backslash) - ie, replace " with \"
rem There should be only two doublequotes, in xmlns:ns1=\"http://www.alarmpoint.com/webservices/schema\"...
rem To get the RegisterIntegrationAgent request, edit <IAHome>\config\log4j2.xml and change the log level to "DEBUG" for the "com.alarmpoint.integrationagent.heartbeat" logger.  Save the file, wait a minute, and then search for "<ns1:RegisterIntegrationAgent" in <IAHome>\log\alarmopoint.log
set payload="<ns1:RegisterIntegrationAgent ... </ns1:RegisterIntegrationAgent>"

rem Step 4: un-comment the appropriate cURL command below

rem Use this command for testing with _un_authenticated proxy
rem curl -v  -H "Content-Type: application/xml" --proxy %proxy%  --request POST --data %payload% %xm_url%

rem Use this one for authenticated proxy
rem curl -v  -H "Content-Type: application/xml" --proxy %proxyUser%:%proxyPwd%@%proxy%  --request POST --data %payload% %xm_url%

rem Use this one for NTLM proxy
rem curl -v  -H "Content-Type: application/xml" --proxy-ntlm --proxy-user  %proxyUser%:%proxyPwd% --proxy %proxy%  --request POST --data %payload% %xm_url%

rem step 5: Save your changes and invoke this script from a command prompt in <IAHome>\tools

if NOT DEFINED xm_url goto :usage
goto :theEnd

:usage
echo Before invoking the curl-heartbeat script, open it in a text editor and configure it as described in the "rem Step" comment lines.

: theEnd
