#!/bin/bash
#
# Copyright 2009 Klavs Klavsen <klavs@EnableIT.dk>
# This script is licensed under the GPLv3 or any later version
# See more on fsf.org
#
# fetches status from mythweb - and checks for ok output - otherwise restarts myth-backend
#
rm -f /tmp/status
DATE=`date +%Y%m%d-%H%M%S`
wget -t 1 --timeout=20 http://localhost/status -O /tmp/status 1>/dev/null 2>/dev/null
REC=`find /home/mythtv/storage -mmin -2 | wc -l`
if [ $REC -gt 0 ]
then
	exit 0
fi 
RES=`grep "Unable to connect to the master backend" /tmp/status | wc -l`
if [ ! -s /tmp/status ] || [ $RES -gt 0 ]
then
	#myth-backend down - restart
	/etc/init.d/mythtv-backend restart 1>/dev/null
	echo "$DATE Restarted myth backend" >>/var/log/mythtv/check_mythtv.log
fi

