in Break-Fix

Quick & dirty Varnish monitoring script

Depending on which version of Varnish you have running, there is a chance the code is still experimental and the whole Varnish daemon is prone to a fatal crash.

Here is a quick and dirty monitoring script for Varnish. It essentially runs as a crontab job and requests a PING response. Should it not receive a PONG from Varnish, it will attempt a restart. There is a second round of testing within the same script. This is a second level of PING request; just in case. Be sure to set the usual defaults like -T port and varnish runlevel script.

#!/bin/bash

result=$(echo -e "ping\n\r" | nc localhost 6082|grep PONG|wc -l);

if [ "${result}" -lt "1" ];
then
/etc/init.d/varnish stop;
sleep 5;
/etc/init.d/varnish start;
echo "$(hostname): Varnish restart" | mail -s "$(hostname): Restarting varnish" alerts@example.com;
fi

sleep 5;

results=$(echo -e "ping\n\r" | nc localhost 6082|grep PONG|wc -l);

if [ "${results}" -lt "1" ];
then
/etc/init.d/varnish stop;
sleep 5;
/etc/init.d/varnish start;
echo "$(hostname): Second Varnish restart" | mail -s "$(hostname): Restarting varnish second time" alerts@example.com;
fi

exit 0;

Finally load the job into crontab:

*/5 * * * * /root/varnish.sh

A cleaner way of accomplishing the same goal would be to establish a permanent connection to the Varnish admin port (6082 in this case) and issue a PING command every other second or so. The interval can be in the sub seconds as opposed to minutes as is the case with crontab.

Here is an example:

# telnet localhost 6082
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
ping
200 19
PONG 1274931430 1.0
ping
200 19
PONG 1274931434 1.0
ping
200 19
PONG 1274931437 1.0
ping
200 19
PONG 1274931443 1.0

We deploy Varnish and other tools on our clients’ server to get the most performance out of hardware. Our clients are happy with the performance boost and we are happy of the end result. Please do get in touch if you have any question or comment

That’s all folks!

Write a Comment

Comment

Comment moderation is enabled. Your comment may take some time to appear.

Webmentions

  • yankee candles cheap now April 15, 2011

    Buy Yankee Candles Cheap…

    […]below you’ll find the link to some sites that we think you should visit[…]…

  • greenville April 15, 2011

    motorcycle http://adz.3t9.o11.co : dealer…

    greenville…

  • Specs April 15, 2011

    2005 http://zavonp61hyrb.05KIAPARTS.US/tag/Specs+2005+2002/ : 2002…

    2002…