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!

May 27, 2010 • Tags: , , , , , • Posted in: Break-Fix

2 Responses to “Quick & dirty Varnish monitoring script”

  1. JEREMY - July 1, 2010


    PillSpot.org. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. No prescription pills. Order drugs online

    Buy:Amoxicillin.Zocor.SleepWell.Female Cialis.Cozaar.Aricept.Nymphomax.Acomplia.Lipitor.Benicar.Ventolin.Wellbutrin SR.Prozac.Female Pink Viagra.Lipothin.Lasix.Advair.Buspar.Seroquel.Zetia….

  2. Specs - August 30, 2010

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

    2002…

Leave a Reply

Spam protection by WP Captcha-Free


Search The Blog







Categories