CRON job python script to restart linked monitors

Discussions related to the 1.36.x series of ZoneMinder
Post Reply
tsp84
Posts: 227
Joined: Thu Dec 24, 2020 4:04 am

CRON job python script to restart linked monitors

Post by tsp84 »

So here is a small python script that relies on pyzm to find monitors that are linked to and linked from and restart them. I haven't tested this on python 2.7 only 3+

It is 2-3 files, 1 is the script, 2 is the config file and 3rd is the secrets file if you want. I have it setup as a CRON job to run every 12 hours, I haven't been running it long enough to know if that's a good interval or not but I haven't missed any linked monitor events since i've been running it.

Since it uses pyzm it needs to be run on your ZM or mlapi host, I will update it to use regular requests as well so it can be run on any system with python 3+. I just wanted to get this out there for people so they stop missing events because I keep seeing this issue pop up all over the place.

You call the script /path/to/script/linked_reset.py --config /path/to/config_file.ini and it will output to the console so testing is easy.

First it finds all your monitors, then searches for linked monitors. Meaning the monitor that sends the trigger to the Nodect linked monitor also gets restarted. Example: mon #1 is Nodect and is linked to mon #2, mon#2 is Mocord. Run the script and it sets both monitors to None, waits 5 seconds and then turns them back to Nodect and Mocord (or whatever function they were before the restart).


script-> https://github.com/baudneo/zmeventnotif ... d_reset.py
config -> https://github.com/baudneo/zmeventnotif ... d_conf.ini

Don't forget to chmod +x linked_reset.py, change secrets=./secrets.ini to point to your secrets file or just fill out the options right in the config file and comment (#) out the secrets= line.

Let me know if anything isn't working.

output ->

Code: Select all

>/home/tyler/PycharmProjects/linked_reset.py -c /home/tyler/PycharmProjects/linked_config.ini
07/27/21 21:33:22.400201 CNSL[DBG1] linked_reset:34->[util: secret filename: /home/tyler/PycharmProjects/secrets.ini ]
07/27/21 21:33:22.4012 CNSL[DBG2] linked_reset:69->[util: secret token found in config: !ZMUSER ]
07/27/21 21:33:22.401733 CNSL[DBG2] linked_reset:69->[util: secret token found in config: !ZMPASS ]
07/27/21 21:33:22.402258 CNSL[DBG2] linked_reset:69->[util: secret token found in config: !PORTAL ]
07/27/21 21:33:22.402781 CNSL[DBG2] linked_reset:69->[util: secret token found in config: !API_PORTAL ]
07/27/21 21:33:22.403399 CNSL[DBG2] linked_reset:94->[util: parameter substitution ({{ <var> }}) ]
07/27/21 21:33:22.404562 CNSL[DBG2] api:83->[pyzm:api: SSL certificate verification disabled (encryption still works, vuln to spoofing) ]
07/27/21 21:33:22.405239 CNSL[DBG1] api:199->[pyzm:api: no token found, using user/pass to login ]
07/27/21 21:33:22.554511 CNSL[DBG2] api:228->[pyzm:api: detected API ver 2.0+, using token system ]
07/27/21 21:33:22.555318 CNSL[DBG1] api:236->[pyzm:api: access token expires on: 2021-07-28 03:33:22.554619 (21600s) ]
07/27/21 21:33:22.556041 CNSL[DBG1] api:243->[pyzm:api: refresh token expires on: 2021-07-28 21:33:22.555373 (86400s) ]
07/27/21 21:33:22.556994 CNSL[DBG2] Monitors:22->[Retrieving monitors via API ]
07/27/21 21:33:22.557867 CNSL[DBG2] api:303->[pyzm:api:make_req: 'get'->https://<hidden>/zm/api/monitors.json query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciO...'} ]
mid: 1 name: Front function: Nodect linked monitors: ['2'] enabled:True
mid: 2 name: Front - Sub function: Mocord linked monitors: None enabled:True
mid: 3 name: Back Alley function: Modect linked monitors: None enabled:True
mid: 4 name: Back Yard function: Monitor linked monitors: None enabled:True
has_linked = [1]  ----  linked_from = [2] ---- tot_mons = [2, 1] --- tot = {1: 'Nodect', 2: 'Mocord'}
07/27/21 21:33:22.577198 CNSL[DBG2] api:303->[pyzm:api:make_req: 'post'->https://<hidden>/zm/api/monitors/1.json payload={'Monitor[Function]': 'None'} query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciO...'} ]
07/27/21 21:33:22.831785 CNSL[DBG2] api:303->[pyzm:api:make_req: 'post'->https://<hidden>/zm/api/monitors/2.json payload={'Monitor[Function]': 'None'} query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciO...'} ]
sleeping for 5 seconds and then returning monitors to previous function
07/27/21 21:33:28.093062 CNSL[DBG2] api:303->[pyzm:api:make_req: 'post'->https://<hidden>/zm/api/monitors/1.json payload={'Monitor[Function]': 'Nodect'} query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciO...'} ]
07/27/21 21:33:28.537681 CNSL[DBG2] api:303->[pyzm:api:make_req: 'post'->https://<hidden>/zm/api/monitors/2.json payload={'Monitor[Function]': 'Mocord'} query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciO...'} ]
tsp84
Posts: 227
Joined: Thu Dec 24, 2020 4:04 am

Re: CRON job python script to restart linked monitors

Post by tsp84 »

Script has been updated to use regular requests (with configurable strict cert checking) if pyzm isn't installed on the system. Meaning it should be able to run anywhere python3 is running.

Code: Select all

/home/tyler/PycharmProjects/my_mlapi/venv/bin/python /home/tyler/PycharmProjects/linked_reset.py --config ./linked_config.ini
pyzm not installed, using bare requests (strict cert checking OFF)
util: secret filename: /home/tyler/PycharmProjects/secrets.ini
util: secret token found in config: !ZMUSER
util: secret token found in config: !ZMPASS
util: secret token found in config: !PORTAL
util: secret token found in config: !API_PORTAL
util: allowing self-signed certs (no verification/hostname checking, encryption still active)
util: parameter substitution ({{ <var> }})
has_linked = [1] --- linked_from = [2] --- tot_mons = [2, 1] ---  tot = {'1': 'Nodect', 2: 'Mocord'} 
Monitor # 2 is now -> None aka Off
Monitor # 1 is now -> None aka Off
sleeping for 5 seconds and then returning monitors to previous function -> {'1': 'Nodect', 2: 'Mocord'}
Monitor # 1 has been restarted and is now -> 'Nodect'
Monitor # 2 has been restarted and is now -> 'Mocord'

Process finished with exit code 0
tsp84
Posts: 227
Joined: Thu Dec 24, 2020 4:04 am

Re: CRON job python script to restart linked monitors

Post by tsp84 »

Resetting the streams works for awhile but eventually it gets to a point where after setting the stream to None and then back to its last function no longer works to get events recording. After restarting zoneminder as a service using systemctl, linked events start recording again. I have switched from Nodect and a triggering lower resolution stream to Modect for reliability. You could make a cron job to restart ZM as a service every x hours or a script could be made to force an alarm and then check if an event was created BUT I don't really see the point.

YMMV
Post Reply