Skip to content

Instantly share code, notes, and snippets.

@stesee
Created July 29, 2020 06:43
Show Gist options
  • Select an option

  • Save stesee/48d737360e0e8e784342993858b17cf4 to your computer and use it in GitHub Desktop.

Select an option

Save stesee/48d737360e0e8e784342993858b17cf4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# install stuff before using this script
# sudo apt install libxml-xpath-perl curl
#configure your kodi settings
URL=localhost
PORT=9981
USERNAME=kodi
PASSWORD=kodi
WARNINGTIMEOUT=65
TIMEOUT=75
TIMEBEFORENEXTRECORD=15
POLLINGDURATION=60
LOGFILE="/home/pi/health.log"
# 2 days in minutes
NEXTRECORD=2880
#set start conditions
SECONDS=0
echo $(date && echo "Start polling") | tee -a "$LOGFILE"
while [ 1 ]; do
# check if tvheadend is recording
OUTPUT=$(curl --anyauth -u $USERNAME:$PASSWORD --silent --max-time 5 "http://$URL:$PORT/status.xml")
if [ -z "$OUTPUT" ]; then
echo "Tvheadend is not responding" | tee -a "$LOGFILE"
else
if echo $OUTPUT | grep "<status>Recording</status>" >/dev/null; then
echo "Tvheadend is recording" | tee -a "$LOGFILE"
#reset timer
SECONDS=0
fi
if echo $OUTPUT | grep "<next>" >/dev/null; then
echo "Tvheadend waiting for an upcomming record" | tee -a "$LOGFILE"
echo $OUTPUT
NEXTRECORD=$(echo $OUTPUT | xpath -q -e "/currentload/recordings[1]/recording/next/text()")
echo Next recording starts in $NEXTRECORD minutes | tee -a "$LOGFILE"
SECONDS=0
if [ "$TIMEBEFORENEXTRECORD" -gt "$NEXTRECORD" ]; then
echo "Tvheadend will start record in short time" | tee -a "$LOGFILE"
SECONDS=0
fi
fi
if echo $OUTPUT | grep "<subscriptions>" >/dev/null; then
SUBS=$(echo $OUTPUT | xpath -q -e "/currentload/subscriptions/text()")
if [ "$SUBS" -gt 1 ]; then
echo "$SUBS subscriptions running" | tee -a "$LOGFILE"
SECONDS=0
elif [ "$SUBS" -eq 0 ]; then
echo "No subscriptions" | tee -a "$LOGFILE"
fi
fi
fi
duration=$SECONDS
#calc if timeout ocured -> restart service
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed since last succesfull poll." | tee -a "$LOGFILE"
if [ "$duration" -gt "$WARNINGTIMEOUT" ]; then
echo "Warning: tvheadend is not resonding! Waiting grace time before restarting service." | tee -a "$LOGFILE"
if [ "$duration" -gt "$TIMEOUT" ]; then
#Convert $NEXTRECORD to seconds
RECNS=$(($NEXTRECORD * 60))
echo Next recording starts in $RECNS seconds | tee -a "$LOGFILE"
echo $(date && echo "resarting tvheadend") | tee -a "$LOGFILE"
/etc/init.d/tvheadend restart | tee -a "$LOGFILE"
SECONDS=0
fi
fi
sleep $POLLINGDURATION
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment