#!/usr/bin/python

timer = 10
threshold = 1000
var = 'n_wrk_overflow'

import os
import time

def get_stats():
	stats={}
	for line in os.popen("varnishstat -1"):
		key,value,=line.split()[0:2]
		stats[key]=int(value)
	return stats

def restart():
	os.system("pkill -10 -U nobody varnishd")

o = get_stats()
while True:
	time.sleep(timer)
	s = get_stats()
	if (s[var] - o[var]) / timer > threshold:
		restart()
		# Give some time to normalize the workload, start threads and such
		time.sleep(60)
		o = get_stats()
	else:
		o = s