[SOLVED] Finding out current run state

Forum for questions and support relating to the 1.28.x releases only.
Locked
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

[SOLVED] Finding out current run state

Post by asker »

Is there a way for me to find out the current running state of zone minder? Not just 'running' or 'stopped' but assuming I've created 2-3 run states, find out that state (let's say I have 3 states: in, out, vacation), I'd like to know which state ZM is in (while running)

Use-case: I have a cron that switches between in/out depending on time of day. However, if I am on an extended vacation, I want it to be in out mode without switching to in. There are may ways to accomplish this, but the neatest way is to create a new running state called vacation and if its set, cron will not switch states unless I switch it back. Then I don't need any external files to manage and I can switch to/from vacation mode within the ZM interface, even while on travel. This is useful because I might forget to set 'vacation mode' and then when on travel, I log into ZM and enable 'vacation' mode run state and my cron will know not to switch. Using external mechanisms will mean I need to open another point of access to my linux box.

I took a look directly at the DB records and did not find any such data that is maintained (it does maintain a list of states, but not current running state)

I thought about checking the state of every individual monitor in my cron script and map it to a running state, but that won't really work because my 'vacation' state is the same as 'out'

Code: Select all

mysql> select * from States;

+----------+---------------------------------------------------------+
| Name     | Definition                                              |
+----------+---------------------------------------------------------+
| in       | 4:Nodect:1,5:Nodect:1,8:Nodect:0,9:Nodect:1,10:Nodect:0 |
| out      | 4:Nodect:1,5:Nodect:1,8:Nodect:0,9:Nodect:1,10:Nodect:1 |
| vacation | 4:Nodect:1,5:Nodect:1,8:Nodect:0,9:Nodect:1,10:Nodect:1 |
+----------+---------------------------------------------------------+
If ZM indeed does not store running state name, I am thinking of modifying zmpkg.pl to write current state to a file. But I am not sure if zmpkg is invoked all the time when ZM shuts down/starts/restarts - if not, I'll have a wrong state.
Last edited by asker on Fri Mar 13, 2015 6:50 pm, edited 1 time in total.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
sime
Posts: 26
Joined: Thu Jan 01, 2015 8:45 pm

Re: Finding our current run state

Post by sime »

I had the same problem as you...

My cron, checks every 5 minutes to see if I am home...
I don't want it to change state if it is already in the good state..


you may try this...
Let's say state Vacation is all monitors to Modect,
we wanna test if monitor 2 is on Modect

Code: Select all

#!/bin/bash
STATE=$(mysql -D zm -u******** -p******* -s -N -e "SELECT Function FROM Monitors WHERE Id=2;")

if [[ $STATE -eq "Modect"  ]] ;
    then
    echo "System is already in state Vacation"
    else 
    /usr/local/bin/zmpkg.pl Vacation
fi
I'm not sure about my bashscript syntax but i hope you get the idea.. You can check many monitors and store them fonctions and compare to figure out wich state you are in...

Simon
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Finding our current run state

Post by asker »

Thank you, Simon. Unfortunately, checking monitor states won't work as my "out" and "vacation" states are exactly the same - they both put all my cameras on nodect. The difference is when in vacation mode, I don't want cron switching back to disable at specific times.

I've gone ahead and changed both zmpkg.pl and console.php to get what I wanted - that is, a way to set the ZM run state to vacation mode via the web interface and have cron honor that and not switch states if vacation is set.


The logic I've implemented:
a) When zmpkg changes to a new state, it writes the state string to a text file. When the system is stopped or restarted, the state remains (because you only want to change to a different state when I make a change - for example, if I set it to vacation, and for whatever reason, the system gets stopped and restarted, I want it to go back to vacation when its back to running)

b) I did a search in the skin to see where it was displaying state, and it seems its in a file called Console.php - so in that file, I read the text file of step a and display it next to "Running". So now in addition to just knowing that it's running, I also know current run state.

Yay !
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
dukie
Posts: 1
Joined: Thu Apr 14, 2016 6:53 am

Re: [SOLVED] Finding out current run state

Post by dukie »

Thx for all your post it was helping me in my solution. I solved this with the ZonerMinder's API.

http://zoneminder.readthedocs.org/en/stable/api.html

First I used curl to do the login and received the cookie file.

curl -d "username=XXXX&password=YYYY&action=login&view=console" -c cookies.txt http://yourzmip/zm/index.php

Next I grabed the active state with the states.json.

curl -b cookies.txt http://yourzmip/zm/api/states.json

Then I use an if statement and some filtering in crontab to check for vacation before switching Home or Away.

Henrik
Locked