Monday, May 18, 2020

MachineMonitor.py

A few months ago I repurposed my 2010 gaming PC into a Zoneminder server to monitor some network cameras and make timelapse video. The PC has more than enough power to do the job but it has a problem with uptime, occasionally it will lock up so completely that it doesn't even respond to a ping. Troubleshooting a problem that happens less than once a month is hard but I still would like to know when it has happens so I can reboot manually. I looked at what I had available and decided on my RaspberryPi ZeroW (currently hosting a webserver), and the cheapest thermal printer on the internet.



The printer is driven by a python scrip that takes a couple arguments and sends the correct bytes to the printer to get formatted text. It had only a partial implementation of the ESC/P protocol so it is no good for images, making it a perfect choice for printing human friendly notifications. Over a couple of days I was able to map the functions that were available and come up with a workable script, as well as some really long glitch-art.



To monitor the Zoneminder and other PCs, I used another python script, it reads a list of machines from a file, pings them and then generates a notification based on whether the machine has changed state since the last time it was checked. To begin, the user (me) fills out a config file that contains a list of the IPs/Domains to be checked, and their human readable name. At regular intervals the monitoring script is run by cron, the script then pings each machine and appends "up" or "down" to each line in the config file accordingly. The next time it runs, the script compares the new up/down status to the old one and generates an appropriate message, if there has been no change in state nothing will be printed, eliminating spam. Messages are passed to the printer script along with the required formatting arguments and a messages like the following appears.



I have published the code but keep in mind I am not, by any means, a programmer. I start every project with about 15 tabs of docs.python and w3schools, I know that I am 'done' when I close the last tab. https://github.com/nik282000/Network-Machine-Monitor