Sam Nazarko

Cleaning your cameras and knowing when to do so

I’ve had a CCTV setup for several years using Hikvision and Zoneminder.

I recently bolstered security and set up HKVS so I can view cameras outside of my LAN and also send recordings immediately to iCloud.

Last year, I was getting frustrated at checking cameras and finding that they needed cleaning. So using the Zoneminder API, I hacked up a quick solution to send me an email when a clean was due. This is just based on the average events I experienced on a daily basis around the outside of the house.

	#!/bin/bash

	function check_threshold() {

	# $1 - API output
	# $2 - camera ID
	# $3 - threshold
	# $4 - camera name

	RESULT=$(echo $1 | jq ".results[\"${2}\"]" | tr -d '"')
	if [ "$RESULT" == "null" ]; then RESULT=0; fi

	echo "$4 has had $RESULT events in the last hour"

	if [ "$RESULT" -gt "$3" ]
	then
		echo "$4 has exceed the threshold of $3 events per hour"
		CAMERAS="$CAMERAS $4"
	fi
	}

	CAMERAS=""
	API_ENDPOINT="http://camera-server/zm/api/events/consoleEvents/1%20hour.json"
	API_OUTPUT=$(wget $API_ENDPOINT -O-)

	# Threshold of maximum expected events per hour.
	# Checking at 7PM, 10PM, 1AM, 4AM, 7AM

	THRESHOLD_A=10
	THRESHOLD_B=75
	THRESHOLD_C=5

	# Check A
	check_threshold $API_OUTPUT 6 $THRESHOLD_A Area A
	# Check B
	check_threshold $API_OUTPUT 4 $THRESHOLD_B Area B
	# Check C
	check_threshold $API_OUTPUT 5 $THRESHOLD_C Area C

	if [ "$CAMERAS" == "" ]; then echo "There is nothing to report"; exit 0; fi

	TO="email@......"
	/usr/sbin/sendmail -i $TO << MSG
	Subject: Cameras need cleaning
	To: $TO

	The following cameras may need cleaning: $CAMERAS

	MSG

Spiders love their cameras…

Posted May 26, 2022

Written by Sam Nazarko, a 20s London chap who is an open source developer by night. Follow me on Twitter