Live events counter on my website

Forum for questions and support relating to the 1.30.x releases only.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Live events counter on my website

Post by kmg454 »

We are using Zoneminder to monitor the traffic to the UNESCO World Heritage Kinderdijk and like to show a live events counter on our website so the visitors can see what they can aspect when coming to visit the site.
Showing the live video is no problem but we can't find how to show the real time events counter.

Maybe someone has wrote a php script to do this?
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

I am not sure what type of counter? A real time count of events detected by motion that are created? What is the "event"? I have a script that runs as a daemon written in PERL that checks continuously all the monitors status to test for 0 events across a set timeout period, which could be modified to just count "Alarm" states.....maybe of some use? Or perhaps a Filter which executes a script to count and display the event count? There are definitely ways of doing this.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

I want a counter to show the amount of alarm events of the day, just like in the web interface of zoneminder, this will indicate how many vehicles or pedestrians past our cam to World Heritage Kinderdijk

http://kinderdyk.nl/webcam-kinderdijk/
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

Is the website based on WordPress? There may be a API call one could make for a result showing the number events that you could display on the page with the stream.
I stream a ZM feed here TroutCam but do not display an event account.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

here is something from documents on the ZM api : http://zoneminder.readthedocs.io/en/stable/api.html

Return a list of events for a specific monitor Id =5

Code: Select all

curl -XGET http://server/zm/api/events/index/MonitorId:5.json
Note that the same pagination logic applies if the list is too long
Return a list of events for a specific monitor within a specific date/time range

Code: Select all

http://server/zm/api/events/index/MonitorId:5/StartTime >=:2015-05-15 18:43:56/EndTime <=:2015-05-16 18:43:56.json
To try this in CuRL, you need to URL escape the spaces like so:

Code: Select all

curl -XGET  "http://server/zm/api/events/index/MonitorId:5/StartTime%20>=:2015-05-15%2018:43:56/EndTime%20<=:2015-05-16%2018:43:56.json"
Return a list of events for all monitors within a specified date/time range

Code: Select all

curl -XGET "http://server/zm/api/events/index/StartTime%20>=:2015-05-15%2018:43:56/EndTime%20<=:208:43:56.json"
Return event count based on times and conditions

The API also supports a handy mechanism to return a count of events for a period of time.

This returns number of events per monitor that were recorded in the last one hour

Code: Select all

curl "http://server/zm/api/events/consoleEvents/1%20hour.json"
This returns number of events per monitor that were recorded in the last day where there were atleast 10 frames that were alarms”

Code: Select all

curl "http://server/zm/api/events/consoleEvents/1%20day.json/AlarmFrames >=: 10.json"
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

Did never some programming with curl before just PHP and Javascript but I will look into this api calls if I can use it.
The website is based on Wordpress mixed with my own PHP scripts.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

I managed to get something working on a WordPress test site using the API and then calling a php script up in an iframe on the wordpress page. There are much better ways of doing the display part with a template page with the php included and no iframe would be needed. Here is the code I am experimenting with:


the PHP code for proof of concept that gets the total number of events across all monitors
and is set to look for events since the last 72 hours.

Code: Select all

<?php

if (!file_exists("/tmp/cookies.txt")){
echo shell_exec("curl -d 'username=admin&password=somepassword&action=login&view=console' -c /tmp/cookies.txt  http://some.domain/zm/index.php > /dev/null");
}

$theurl = "http://some.domain/zm/api/events/consoleEvents/72%20hour.json";
$cookie_file = '/tmp/cookies.txt';

//$theurl = "http://some.domain/zm/api/host/getVersion.json";
$ch = curl_init($theurl);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie_file );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file );
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file );
 
$response = curl_exec($ch);

$r = json_decode($response);

foreach ($r as $bank) {
foreach ($bank as $key => $data){
//	echo $key." = ".$data."<br>";
    $x = $x + $data;
}
}
echo "Total Events =>".$x;
?>
I put the php script somewhere in the WordPress root and call it with an iframe in the page. With some work on setting the authorization cookie or session you will be able to do it.
Last edited by rockedge on Sun Apr 23, 2017 8:46 pm, edited 1 time in total.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

I tried to use a php snippet plugin, but got lots of errors I have to look into
Last edited by kmg454 on Mon Apr 24, 2017 7:27 am, edited 1 time in total.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

ahhh I see!
Appears that shell_exec() has been disabled and also cURL has been restricted with file_exists(): open_basedir restrictions in effect! On my servers I can of course run this because I have securities set for development, not production. I am interested in seeing if I can use HASH authorization and skip the cookies all together which would solve the file_exists and shell_exec errors because they will not be used. A reliable way of authorization for the single API call is what is needed. I think running PHP code using a WordPress plugin will not allow some of these commands. Can you just load the code in the WordPress root directory and call it with an iframe and see if it works that way? You could comment out the if file exists line to try to streamline it. I will see what I can find and will post what I find out.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

Tried to load with an Iframe and got the same errors, maybe the simple way is to read the database direct and use the refresh Meta to update the page.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

here are 2 versions of the same thing that worked, using your suggestion of going straight to the db, the scripts connect to the ZM db and count the events and displays that number:
Version 1 (simple)->

Code: Select all

<?php
  $server = "localhost";
  $username = "zmuser";
  $password =  "zmpass";
  $database =  "zm";        
  $con = mysql_connect($server, $username, $password)or die("Could not connect to database");
         mysql_select_db($database)or die("Could not select database");

   $sql = "SELECT * FROM `Events` ";
	 $result = mysql_query($sql,$con) or die("query:" . mysql_error());
	   $num = mysql_numrows($result);
   
   echo "Total # of Events :".$num;

?>
Version 2 (as functions, $which_monitor selects which monitor to count)->

Code: Select all

<?php
$which_monitor = 2;
echo "Total # of Events :".geteventtotals($which_monitor);

function conx(){
  $server = "localhost";
  $username = "zmuser";
  $password =  "zmpass";
  $database =  "zm";        
  $con = mysql_connect($server, $username, $password)or die("Could not connect to database");
         mysql_select_db($database)or die("Could not select database");
   return $con;
}
function geteventtotals($mon_id){
  
    $sql = "SELECT * FROM `Events` WHERE `MonitorId`=".$mon_id;
	 $result = mysql_query($sql,conx()) or die("query:" . mysql_error());
	   $num = mysql_numrows($result);
    return $num;
}  
?>
Hope it helps!
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

Tested on a WordPress test site using a Minamaze child theme streaming a ZM feed and using this WorpPress plugin https://wordpress.org/plugins/insert-php-code-snippet/ and by opening the script with an iframe,
both versions of the above code works.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

For testing I made a standalone php page and added this in the /var/www/html directory of the zoneminder server and call this url by the browser.
But both scripts gave me a internal server error 500 an I can't find out why.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Live events counter on my website

Post by rockedge »

wow. What version of PHP are you running? I will try to duplicate the internal server error.
So far that I have not encountered on either of the zoneminder systems I am testing with. One runs with Apache 2.4 MySQL 5.5.54-0ubuntu0.14.04.1 and PHP Version 5.5.9-1ubuntu4.21. The other is running with the same php and mysql with Hiawatha 10.5 as the web server. ZoneMinder 1.30.2 is the model on both. Are there any signs in the PHP logs?
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Live events counter on my website

Post by kmg454 »

Server version: 5.7.17-0ubuntu0.16.04.2 (Ubuntu) Zoneminder is 1.30.2

[Tue Apr 25 16:26:58.487397 2017] [:error] [pid 22489] [client 84.82.234.240:43400] PHP Fatal error: Uncaught Error: Call to undefined function mysql_query() in /var/www/html/teller.php:32\nStack trace:\n#0 /var/www/html/teller.php(18): geteventtotals(1)\n#1 {main}\n thrown in /var/www/html/teller.php on line 32
Locked