Python Script to reset Home DVR

My Home DVR system seems to be built on a Busybox Linux install with the DVR running from that OS.   The issue with my DVR is that the consumer/home dvr devices for under $500 seem to have issues with remote viewing.   I have a system that works well enough when you are using the client on the same LAN.  However, when trying to view remote video, the DVR viewing software would shutdown.  Sometimes the system will just lock up for no reason and stop all recording.    Error in the code maybe?  Buffer issue, possibly?   Whatever the reason, If I used a smartphone to peek in on my cameras or perhaps the wind blew the wrong way, I run the risk of locking up the DVR app and thus stop recording video.   The Linux OS is still up and running, it’s just the Recording and playback app that crashed.

After several tech calls to the company and getting the latest firmware for the unit, as well as trying the same process from various Android and iOS apps, the issue was still there. What I found is that my unit as well as most cheap comsumer units is built off the same busybox clone.

So I could remove the system and get another that would work with the remote viewers.    Not an attractive option.  Considering I might end up with the same flaky code.  I also can not afford more expensive units.

Since I have a home Linux server running various functions I including a small Check_mk/Nagios install for monitoring, I did the following:

The viewing app listens on 18004 by default for remote requests.   In nagios, I added the following check to the DVR system’s cfg file in nagios/conf.d.

define service{
use                     generic-service
host_name               <my dvr host name>
service_description     TCP_18004
check_command           check_tcp!18004
event_handler           restart-dvr
}

This will watch port 18004.  If the app locks up for any reason,  18004 stops listening.

Next I added the following item to nagios/conf.d/commands.cfg.

define command{
command_name    restart-dvr
command_line    /opt/scripts/resetdvr.py
}

In the event 18004 is not reachable (indicating a failure) nagios calls the command “restart-dvr”.   This command simply executes the python script I wrote at /opt/scripts/resetdvr.py

#!/usr/bin/python
import sys
import telnetlib
HOST = "<dvr IP address>"
user = "<root username>"
password = "<your password"
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "n")
tn.read_until("Password: ")
tn.write(password + "n")
tn.read_until("~ $")
tn.write("rebootn")
tn.write("exitn")
tn.read_all()

To help lock it up a bit:

chown root:root /opt/scripts/resetdvr.py
chmod 700 /opt/scripts/resetdvr.py

When the failure is detected, nagios runs the python script which simply issues a reboot.  The DVR comes back in in about 45 seconds or so and its back online.

Have a better suggestion?  Leave me a comment.

Bookmark the permalink.

One Response to Python Script to reset Home DVR

  1. mohamed says:

    Hello, I need to access the video stream of the cameras on my dvr using openCv, anyidea on how I can request them?

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
26 − 17 =