The Core Technologies Blog

Our Software // Windows Services // 24×7 Operation


Hey scripters, http-ping now returns the HTTP status code

Savvy programmers and administrators often use our free http-ping command line utility to check that their web servers up and serving pages. http-ping’s return code (the percentage of page downloads that succeeded) lets them know when the web server is not responding, enabling them to take the appropriate action (for example sending an email or rebooting the server). This works very well when a web server fails to respond but it can’t detect more subtle problems, such as when the server is responding but returning an error.

http-ping version 6.0 addresses that problem. By returning the HTTP status code from the server’s response, a script can detect 404’s (Not Found), 500’s (Internal Server Error) or any other non-standard replies.

Users of AlwaysUp (and Service Protector) can deploy http-ping to detect failures and restart their web servers when necessary. For example, this sample “sanity check plugin” script below signals a restart when it detects a server error (500-599).

Enjoy!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@echo off
:: This batch file uses the http-ping application to detect when
:: a web server is not responding, or responds with a 5XX status
:: code. It returns 0 if the server responds properly and 1 if not.
:: It is intended for use with AlwaysUp
:: (http://www.coretechnologies.com/products/AlwaysUp/)
:: or Service Protector
:: (http://www.coretechnologies.com/products/ServiceProtector/).
:: Copyright 2001-2013 Core Technologies Consulting, LLC. All
:: rights reserved.


:: Here are a few variables that you should adjust.
:: ** Please specify the URL you wish to ping, and the full path
:: to the http-ping executable on your system **
SET url=http://localhost:80
SET http-ping-exe-path="C:\Temp\http-ping.exe"
:: num-times-to-ping == The number of ping attempts that will be
:: made. The default of 5 is probably OK, but you can increase
:: it if errors are frequent (but tolerable).
SET num-times-to-ping=5
:: num-seconds-between-pings == The time to wait between ping
:: attempts. Increase this to give your server more time to get
:: back on its feet after an error.
SET num-seconds-between-pings=10


:: Loop "num-times-to-ping" times.
FOR /L %%X IN (1,1,%num-times-to-ping%) DO CALL :loopbody %%X


:: If we get here, then all ping attempts failed.
echo All %num-times-to-ping% attempts of http-ping failed^! ^
Exiting with 1.
exit 1


:loopbody
:: Ping your web server once, returning the HTTP status code
:: (or 0 if the ping failed to connect).
%http-ping-exe-path% -e -n 1 %url%
:: http-ping returns the HTTP status code or 0 if the ping
:: failed to connect. The list of HTTP status codes is
:: described here:
:: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
:: For this script, we'll panic on 0 or 5XX (server error)
:: and consider all other codes as a success.
IF %ERRORLEVEL% GTR 0 (
   IF %ERRORLEVEL% LSS 500 (
      echo http-ping returned %ERRORLEVEL% - not an error. ^
Exiting with 0.
      exit 0
   ) else IF %ERRORLEVEL% GTR 599 (
      echo http-ping returned %ERRORLEVEL% - not an error. ^
Exiting with 0.
      exit 0
   )
)
echo On attempt #%1, http-ping returned %ERRORLEVEL% - an error.
:: If this was not the last attempt, pause before trying again.
:: Since DOS doesn't have a "sleep" command, use ping with an
:: invalid address and a timeout (in milliseconds) to wait for
:: a while. You can adjust the timeout value
:: (num-seconds-between-pings) above.
IF %1 LSS %num-times-to-ping% (
	SET /A milliseconds=%num-seconds-between-pings% * 1000
	echo Pausing for %num-seconds-between-pings% second^(s^)^
before the next attempt...
	ping 1.1.1.1 -n 1 -w %milliseconds% > nul
)
Posted in http-ping | Tagged , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *