From 2b0df3e4e2c9b064088a02eac6d5ce190e9d0561 Mon Sep 17 00:00:00 2001 From: ratmole Date: Wed, 31 Oct 2018 10:17:36 +0200 Subject: [PATCH 01/37] API - Disable E_NOTICE from php error reporting in cake debug Using zmNinja, the API reports E_NOTICE errors Notice (8): compact(): Undefined variable: subject [CORE/Cake/Utility/ObjectCollection.php, line 128] Notice (8): compact() [function.compact]: Undefined variable: subject [CORE/Cake/Utility/ObjectCollection.php, line 128] Notice (8): compact() [function.compact]: Undefined variable: subject [CORE/Cake/Utility/ObjectCollection.php, line 128] Notice (8): compact() [function.compact]: Undefined variable: subject [CORE/Cake/Utility/ObjectCollection.php, line 128] and zmNinja will not work... there is a better way, but i think disabling E_NOTICE error is way easier see: https://github.com/ZoneMinder/zoneminder/pull/2269 --- web/api/app/Config/core.php.default | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/api/app/Config/core.php.default b/web/api/app/Config/core.php.default index 39a51690c..64f439420 100644 --- a/web/api/app/Config/core.php.default +++ b/web/api/app/Config/core.php.default @@ -50,7 +50,7 @@ */ Configure::write('Error', array( 'handler' => 'ErrorHandler::handleError', - 'level' => E_ALL & ~E_DEPRECATED, + 'level' => E_ALL & ~E_DEPRECATED & ~E_NOTICE, 'trace' => true )); From 37b2da59ce7047b1d6d57bd1e63a6f4824e24232 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 13:30:08 -0400 Subject: [PATCH 02/37] added streaming interface docs --- docs/api.rst | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index d4d4f3415..109569492 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -5,7 +5,6 @@ This document will provide an overview of ZoneMinder's API. This is work in prog Overview ^^^^^^^^ - In an effort to further 'open up' ZoneMinder, an API was needed. This will allow quick integration with and development of ZoneMinder. @@ -13,6 +12,78 @@ The API is built in CakePHP and lives under the ``/api`` directory. It provides a RESTful service and supports CRUD (create, retrieve, update, delete) functions for Monitors, Events, Frames, Zones and Config. +Streaming Interface +^^^^^^^^^^^^^^^^^^^ +Developers working on their application often ask if there is an "API" to receive live streams, or recorded event streams. +It is possible to stream both live and recorded streams. This isn't strictly an "API" per-se (that is, it is not integrated +into the Cake PHP based API layer discussed here) and also why we've used the term "Interface" instead of an "API". + +Live Streams +============= +What you need to know is that if you want to display "live streams", ZoneMinder sends you streaming JPEG images (MJPEG) +which can easily be rendered in a browser using an ``img src`` tag. + +For example: + +:: + + + +will display a live feed from monitor id 1, scaled down by 50% in quality and resized to 640x480px. + +* This assumes ``/zm/cgi-bin`` is your CGI_BIN path. Change it to what is correct in your system +* The "auth" token you see above is required if you use ZoneMinder authentication. To understand how to get the auth token, please read the "Login, Logout & API security" section below. +* The "connkey" parameter is essentially a random number which uniquely identifies a stream. If you don't specify a connkey, ZM will generate its own. It is recommended to generate a connkey because you can then use it to "control" the stream (pause/resume etc.) +* Instead of dealing with the "auth" token, you can also use ``&user=username&pass=password`` where "username" and "password" are your ZoneMinder username and password respectively. Note that this is not recommended because you are transmitting them in a URL and even if you use HTTPS, they may show up in web server logs. + + +PTZ on live streams +------------------- +PTZ commands are pretty cryptic in ZoneMinder. This is not meant to be an exhaustive guide, but just something to whet your appetite: + + +Lets assume you have a monitor, with ID=6. Let's further assume you want to pan it left. + +You'd need to send a: +``POST`` command to ``https://yourserver/zm/index.php`` with the following data payload in the command (NOT in the URL) + +``view=request&request=control&id=6&control=moveConLeft&xge=30&yge=30`` + +Obviously, if you are using authentication, you need to be logged in for this to work. + +Like I said, at this stage, this is only meant to get you started. Explore the ZoneMinder code and use "Inspect source" as you use PTZ commands in the ZoneMinder source code. +`control_functions.php `__ is a great place to start. + + +Pre-recorded (past event) streams +================================= + +Similar to live playback, if you have chosen to store events in JPEG mode, you can play it back using: + +:: + + + + +* This assumes ``/zm/cgi-bin`` is your CGI_BIN path. Change it to what is correct in your system +* This will playback event 293820, starting from frame 1 as an MJPEG stream +* Like before, you can add more parameters like ``scale`` etc. +* auth and connkey have the same meaning as before, and yes, you can replace auth by ``&user=usename&pass=password`` as before and the same security concerns cited above apply. + +If instead, you have chosen to use the MP4 (Video) storage mode for events, you can directly play back the saved video file: + +:: + + + +* This will play back the video recording for event 294690 + +What other parameters are supported? +===================================== +The best way to answer this question is to play with ZoneMinder console. Open a browser, play back live or recorded feed, and do an "Inspect Source" to see what parameters +are generated. Change and observe. + + Enabling API ^^^^^^^^^^^^ A default ZoneMinder installs with APIs enabled. You can explictly enable/disable the APIs @@ -396,6 +467,8 @@ PTZ Control APIs PTZ controls associated with a monitor are stored in the Controls table and not the Monitors table inside ZM. What that means is when you get the details of a Monitor, you will only know if it is controllable (isControllable:true) and the control ID. To be able to retrieve PTZ information related to that Control ID, you need to use the controls API +Note that these APIs only retrieve control data related to PTZ. They don't actually move the camera. + This returns all the control definitions: :: From 2953ff4db4afa15266917d9deffe28375108811e Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 13:40:07 -0400 Subject: [PATCH 03/37] header level fix --- docs/api.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 109569492..a06797cd1 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -19,7 +19,7 @@ It is possible to stream both live and recorded streams. This isn't strictly an into the Cake PHP based API layer discussed here) and also why we've used the term "Interface" instead of an "API". Live Streams -============= +~~~~~~~~~~~~~~ What you need to know is that if you want to display "live streams", ZoneMinder sends you streaming JPEG images (MJPEG) which can easily be rendered in a browser using an ``img src`` tag. @@ -56,7 +56,7 @@ Like I said, at this stage, this is only meant to get you started. Explore the Z Pre-recorded (past event) streams -================================= +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Similar to live playback, if you have chosen to store events in JPEG mode, you can play it back using: @@ -79,7 +79,7 @@ If instead, you have chosen to use the MP4 (Video) storage mode for events, you * This will play back the video recording for event 294690 What other parameters are supported? -===================================== +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The best way to answer this question is to play with ZoneMinder console. Open a browser, play back live or recorded feed, and do an "Inspect Source" to see what parameters are generated. Change and observe. @@ -490,3 +490,4 @@ ZM APIs have various APIs that help you in determining host (aka ZM) daemon stat curl -XGET http://server/zm/api/host/getLoad.json # returns current load of ZM curl -XGET http://server/zm/api/host/getDiskPercent.json # returns in GB (not percentage), disk usage per monitor (that is, space taken to store various event related information,images etc. per monitor) + From cd8d609e846eafa87b86e42fb59a4edf2ba1339c Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:00:09 -0400 Subject: [PATCH 04/37] initial multi-server/storage changes --- docs/api.rst | 101 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index a06797cd1..d70c7eabc 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -142,7 +142,21 @@ using CuRL like so: curl -b cookies.txt http://yourzmip/zm/api/monitors.json -This would return a list of monitors and pass on the authentication information to the ZM API layer. +This would return a list of monitors and pass on the authentication information to the ZM API layer. It is worthwhile noting, that starting ZM 1.32.3 and beyond, this API also returns a ``Monitor_Status`` object per monitor. It looks like this: + +:: + + "Monitor_Status": { + "MonitorId": "2", + "Status": "Connected", + "CaptureFPS": "1.67", + "AnalysisFPS": "1.67", + "CaptureBandwidth": "52095" + } + + +If you don't see this in your API, you are running an older version of ZM. This gives you a very convenient way to check monitor status without calling the ``daemonCheck`` API described later. + A deeper dive into the login process ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -486,8 +500,89 @@ ZM APIs have various APIs that help you in determining host (aka ZM) daemon stat :: - curl -XGET http://server/zm/api/host/daemonCheck.json # 1 = ZM running 0=not running curl -XGET http://server/zm/api/host/getLoad.json # returns current load of ZM - curl -XGET http://server/zm/api/host/getDiskPercent.json # returns in GB (not percentage), disk usage per monitor (that is, space taken to store various event related information,images etc. per monitor) + + # Note that ZM 1.32.3 onwards has the same information in Monitors.json which is more reliable and works for multi-server too. + curl -XGET http://server/zm/api/host/daemonCheck.json # 1 = ZM running 0=not running + + # The API below uses "du" to calculate disk space. We no longer recommend you use it if you have many events. Use the Storage APIs instead, described later + curl -XGET http://server/zm/api/host/getDiskPercent.json # returns in GB (not percentage), disk usage per monitor (that is,space taken to store various event related information,images etc. per monitor) + + +Storage and Server APIs +^^^^^^^^^^^^^^^^^^^^^^^ + +ZoneMinder introduced many new options that allowed you to configure multiserver/multistorage configurations. While a part of this was available in previous versions, a lot of rework was done as part of ZM 1.31 and 1.32. As part of that work, a lot of new and useful APIs were added. Some of these are part of ZM 1.32 and others will be part of ZM 1.32.3 (of course, if you build from master, you can access them right away, or wait till a stable release is out. + + + +This returns storage data for my single server install. If you are using multi-storage, you'll see many such "Storage" entries, one for each storage defined: + +:: + + curl http://server/zm/api/storage.json + +Returns: + +:: + + { + "storage": [ + { + "Storage": { + "Id": "0", + "Path": "\/var\/cache\/zoneminder\/events", + "Name": "Default", + "Type": "local", + "Url": null, + "DiskSpace": "364705447651", + "Scheme": "Medium", + "ServerId": null, + "DoDelete": true + } + } + ] + } + + + +"DiskSpace" is the disk used in bytes. While this doesn't return disk space data as rich as ``/host/getDiskPercent``, it is much more efficient. + +Similarly, + +:: + curl http://server/zm/api/server.json + +Returns: + +:: + + { + "servers": [ + { + "Server": { + "Id": "1", + "Name": "server1", + "Hostname": "sserver1.mydomain.com", + "State_Id": null, + "Status": "Running", + "CpuLoad": "0.9", + "TotalMem": "6186237952", + "FreeMem": "156102656", + "TotalSwap": "536866816", + "FreeSwap": "525697024", + "zmstats": false, + "zmaudit": false, + "zmtrigger": false + } + } + ] + } + +This only works if you have a multiserver setup in place. If you don't it will return an empty array. + + + + From 46cd2fc3ffb387762c8fa378cee96d117ea07ce6 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:04:51 -0400 Subject: [PATCH 05/37] relocate monitor_status to the right place --- docs/api.rst | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index d70c7eabc..ec4fe8d55 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -142,21 +142,7 @@ using CuRL like so: curl -b cookies.txt http://yourzmip/zm/api/monitors.json -This would return a list of monitors and pass on the authentication information to the ZM API layer. It is worthwhile noting, that starting ZM 1.32.3 and beyond, this API also returns a ``Monitor_Status`` object per monitor. It looks like this: - -:: - - "Monitor_Status": { - "MonitorId": "2", - "Status": "Connected", - "CaptureFPS": "1.67", - "AnalysisFPS": "1.67", - "CaptureBandwidth": "52095" - } - - -If you don't see this in your API, you are running an older version of ZM. This gives you a very convenient way to check monitor status without calling the ``daemonCheck`` API described later. - +This would return a list of monitors and pass on the authentication information to the ZM API layer. A deeper dive into the login process ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -217,6 +203,22 @@ Return a list of all monitors curl http://server/zm/api/monitors.json +It is worthwhile to note that starting ZM 1.32.3 and beyond, this API also returns a ``Monitor_Status`` object per monitor. It looks like this: + +:: + + "Monitor_Status": { + "MonitorId": "2", + "Status": "Connected", + "CaptureFPS": "1.67", + "AnalysisFPS": "1.67", + "CaptureBandwidth": "52095" + } + + +If you don't see this in your API, you are running an older version of ZM. This gives you a very convenient way to check monitor status without calling the ``daemonCheck`` API described later. + + Retrieve monitor 1 ^^^^^^^^^^^^^^^^^^^ @@ -551,6 +553,7 @@ Returns: Similarly, :: + curl http://server/zm/api/server.json Returns: From 536d9226e0575ecc34a6f324cd2180b69d823fd8 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:07:53 -0400 Subject: [PATCH 06/37] ptz - clarify this is meta-data --- docs/api.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index ec4fe8d55..647eedac0 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -478,12 +478,12 @@ Create a Zone &Zone[MaxBlobs]=\ &Zone[OverloadFrames]=0" -PTZ Control APIs -^^^^^^^^^^^^^^^^ +PTZ Control Meta-Data APIs +^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTZ controls associated with a monitor are stored in the Controls table and not the Monitors table inside ZM. What that means is when you get the details of a Monitor, you will only know if it is controllable (isControllable:true) and the control ID. To be able to retrieve PTZ information related to that Control ID, you need to use the controls API -Note that these APIs only retrieve control data related to PTZ. They don't actually move the camera. +Note that these APIs only retrieve control data related to PTZ. They don't actually move the camera. See the "PTZ on live streams" section to move the camera. This returns all the control definitions: :: From 14c30eac7682debb92dcc9633755d91bb61dba5c Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:08:09 -0400 Subject: [PATCH 07/37] typo --- docs/api.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index 647eedac0..e102851a7 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -566,7 +566,7 @@ Returns: "Server": { "Id": "1", "Name": "server1", - "Hostname": "sserver1.mydomain.com", + "Hostname": "server1.mydomain.com", "State_Id": null, "Status": "Running", "CpuLoad": "0.9", From 86a086c216647dc37b503a47be61cb97ea26a794 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:13:22 -0400 Subject: [PATCH 08/37] server json api typo --- docs/api.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index e102851a7..2c7ad1d16 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -554,7 +554,7 @@ Similarly, :: - curl http://server/zm/api/server.json + curl http://server/zm/api/servers.json Returns: From ace1134df137c1bb7bea35156158d57eafe50b0b Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 1 Nov 2018 14:40:31 -0400 Subject: [PATCH 09/37] further reading --- docs/api.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index 2c7ad1d16..2f90b7fdf 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -585,7 +585,14 @@ Returns: This only works if you have a multiserver setup in place. If you don't it will return an empty array. - +Further Reading +^^^^^^^^^^^^^^^^ +As described earlier, treat this document as an "introduction" to the important parts of the API and streaming interfaces. +There are several details that haven't yet been documented. Till they are, here are some resources: + +* zmNinja, the open source mobile app for ZoneMinder is 100% based on ZM APIs. Explore its `source code `__ to see how things work. +* Launch up ZM console in a browser, and do an "Inspect source". See how images are being rendered. Go to the networks tab of the inspect source console and look at network requests that are made when you pause/play/forward streams. +* If you still can't find an answer, post your question in the `forums `__ (not the github repo). From 3aee902a96daab320f6a1c17c81664d997916d75 Mon Sep 17 00:00:00 2001 From: Andy Bauer Date: Sun, 4 Nov 2018 17:11:19 -0600 Subject: [PATCH 10/37] update nginx support on redhat --- distros/redhat/nginx/README.Fedora | 168 +++++++++++------- distros/redhat/nginx/zoneminder.conf.in | 4 + .../redhat/nginx/zoneminder.php-fpm.conf.in | 12 +- distros/redhat/nginx/zoneminder.tmpfiles.in | 3 + distros/redhat/zoneminder.spec | 3 +- 5 files changed, 118 insertions(+), 72 deletions(-) diff --git a/distros/redhat/nginx/README.Fedora b/distros/redhat/nginx/README.Fedora index 0a5168231..013a502b0 100644 --- a/distros/redhat/nginx/README.Fedora +++ b/distros/redhat/nginx/README.Fedora @@ -1,39 +1,34 @@ What's New ========== -1. This is an *experimental* build of zoneminder which uses the - nginx web server. +1. See the ZoneMinder release notes for a list of new features: + https://github.com/ZoneMinder/zoneminder/releases -2. The Apache ScriptAlias has been changed from "/cgi-bin/zm/zms" to - "/cgi-bin-zm/zms". This has been to done to avoid this bug: - https://bugzilla.redhat.com/show_bug.cgi?id=973067 +2. The contents of the ZoneMinder Apache config file have changed. In + addition, this ZoneMinder package now requires you to manually symlink the + ZoneMinder Apache config file. See new install step 6 and upgrade step 3 + below for details. - IMPORTANT: You must manually inspect the value for PATH_ZMS under Options - and verify it is set to "/cgi-bin-zm/nph-zms". Failure to do so will result - in a broken system. You have been warned. - -3. Due to the active state of the ZoneMinder project, we now recommend granting - ALL permission to the ZoneMinder mysql account. This change must be done - manually before ZoneMinder will run. See the installation steps below. - -4. This package uses the HTTPS protocol by default to access the web portal. - Requests using HTTP will auto-redirect to HTTPS. See README.https for - more information. - -5. This package ships with the new ZoneMinder API enabled. +3. This is an experimental build of ZoneMinder supporting nginx, rather than + apache web server. +4. If you have installed ZoneMinder from the FedBerry repositories, this build + of ZoneMinder has support for Raspberry Pi hardware acceleration when using + ffmpeg. Unforunately, there is a problem with the same hardware acceleration + when using libvlc. Consequently, libvlc support in thie build of ZoneMinder + has been disabled until the problem is resolved. See the following bug + report for details: https://trac.videolan.org/vlc/ticket/18594 + New installs ============ -1. This package supports either community-mysql-server or mariadb-server with - mariadb being the preferred choice. Unless you are already using MariaDB or - Mysql server, you need to ensure that the server is configured to start - during boot and properly secured by running: +1. Unless you are already using MariaDB server, you need to ensure that the + server is configured to start during boot and properly secured by running: - sudo dnf install mariadb-server - sudo systemctl enable mariadb - sudo systemctl start mariadb.service - mysql_secure_installation + sudo dnf install mariadb-server + sudo systemctl enable mariadb + sudo systemctl start mariadb.service + mysql_secure_installation 2. Assuming the database is local and using the password for the root account set during the previous step, you will need to create the ZoneMinder @@ -48,13 +43,17 @@ New installs anything that suits your environment. 3. If you have chosen to change the zoneminder database account credentials to - something other than zmuser/zmpass, you must now edit /etc/zm/zm.conf. - Change ZM_DB_USER and ZM_DB_PASS to the values you created in the previous - step. + something other than zmuser/zmpass, you must now create a config file under + /etc/zm/conf.d and set your credentials there. For example, create the file + /etc/zm/conf.d/zm-db-user.conf and add the following content to it: + + ZM_DB_USER = {username of the sql account you want to use} + ZM_DB_PASS = {password of the sql account you want to use} - This version of zoneminder no longer requires you to make a similar change - to the credentials in /usr/share/zoneminder/www/api/app/Config/database.php - This now happens dynamically. Do *not* make any changes to this file. + Once the file has been saved, set proper file & ownership permissions on it: + + sudo chown root:apache *.conf + sudo chmod 640 *.conf 4. Edit /etc/php.ini, uncomment the date.timezone line, and add your local timezone. PHP will complain loudly if this is not set, or if it is set @@ -80,54 +79,87 @@ New installs SELINUX line from "enforcing" to "disabled". This change will take effect after a reboot. -6. This package comes preconfigured for HTTPS using the default self signed - certificate on your system. We recommend you keep this configuration. +6. Configure the web server - If this does not meet your needs, then read README.https to - learn about alternatives. + This package uses the HTTPS protocol by default to access the web portal, + using the default self signed certificate on your system. Requests using + HTTP will auto-redirect to HTTPS. -7. Edit /etc/sysconfig/fcgiwrap and set DAEMON_PROCS to the maximum number of + Inspect the web server configuration file and verify it meets your needs: + + /etc/zm/www/zoneminder.conf + + If you are running other web enabled services then you may need to edit + this file to suite. See README.https to learn about other alternatives. + + When in doubt, proceed with the default: + + sudo ln -s /etc/zm/www/zoneminder.conf /etc/nginx/default.d/ + +7. Fcgiwrap is required when using ZoneMinder with Nginx. At the time of this + writing, fcgiwrap is not yet available in the Fedora repos. Until it + becomes available, you may install it from my Copr repository: + + https://copr.fedorainfracloud.org/coprs/kni/fcgiwrap/ + + Follow the intructions on that site to enable the repo. Once enabled, + install fcgiwrap: + + sudo dnf install fcgiwrap + + After fcgiwrap is installed, it must be configured. Edit + /etc/sysconfig/fcgiwrap and set DAEMON_PROCS to the maximum number of simulatneous streams the server should support. Generally, a good minimum value for this equals the total number of cameras you expect to view at the same time. 8. Now start the web server: - sudo systemctl enable nginx - sudo systemctl start nginx + sudo systemctl enable nginx + sudo systemctl start nginx 9. Now start zoneminder: - sudo systemctl enable zoneminder - sudo systemctl start zoneminder + sudo systemctl enable zoneminder + sudo systemctl start zoneminder -10.The Fedora repos have a ZoneMinder package available, but it does not - support ffmpeg or libvlc, which many modern IP cameras require. Most users - will want to prevent the ZoneMinder package in the Fedora repos from - overwriting the ZoneMinder package in zmrepo, during a future dnf update. To - prevent that from happening you must edit /etc/yum.repos.d/fedora.repo - and /etc/yum.repos.d/fedora-updates.repo. Add the line "exclude=zoneminder*" - without the quotes under the [fedora] and [fedora-updates] blocks, - respectively. +10. Optionally configure the firewall + + All Redhat distros ship with the firewall enabled. That means you will not + be able to access the ZoneMinder web console from a remote machine until + changes are made to the firewall. + + What follows are a set of minimal commands to allow remote access to the + ZoneMinder web console and also allow ZoneMinder's ONVIF discovery to + work. The following commands do not put any restrictions on which remote + machine(s) have access to the listed ports or services. + + sudo firewall-cmd --permanent --zone=public --add-service=http + sudo firewall-cmd --permanent --zone=public --add-service=https + sudo firewall-cmd --permanent --zone=public --add-port=3702/udp + sudo firewall-cmd --reload + + Additional changes to the firewall may be required, depending on your + security requirements and how you use the system. It is up to you to verify + these commands are sufficient. + +11. Access the ZoneMinder web console + + You may now access the ZoneMinder web console from your web browser using + an appropriate url. Here are some examples: + + http://localhost/zm (works from the local machine only) + http://{machine name}/zm (works only if dns is configured for your network) + http://{ip address}/zm Upgrades ======== -1. Verify /etc/zm/zm.conf. - - If zm.conf was manually edited before running the upgrade, the installation - may not overwrite it. In this case, it will create the file - /etc/zm/zm.conf.rpmnew. - - For example, this will happen if you are using database account credentials - other than zmuser/zmpass. - - Compare /etc/zm/zm.conf to /etc/zm/zm.conf.rpmnew. Verify that zm.conf - contains any new config settings that may be in zm.conf.rpmnew. - - This version of zoneminder no longer requires you to make a similar change - to the credentials in /usr/share/zoneminder/www/api/app/Config/database.php - This now happens dynamically. Do *not* make any changes to this file. +1. Conf.d folder support has been added to ZoneMinder. Any custom + changes previously made to zm.conf must now be made in one or more custom + config files, created under the conf.d folder. Do this now. See + /etc/zm/conf.d/README for details. Once you recreate any custom config changes + under the conf.d folder, they will remain in place indefinitely. 2. Verify permissions of the zmuser account. @@ -139,12 +171,16 @@ Upgrades See step 2 of the Installation section to add missing permissions. -3. Verify the ZoneMinder Apache configuration file in the folder - /etc/httpd/conf.d. You will have a file called "zoneminder.conf" and there +3. Verify the ZoneMinder Nginx configuration file in the folder + /etc/zm/www. You will have a file called "zoneminder.conf" and there may also be a file called "zoneminder.conf.rpmnew". If the rpmnew file exists, inspect it and merge anything new in that file with zoneminder.conf. Verify the SSL REquirements meet your needs. Read README.https if necessary. + The contents of this file must be merged into your Nginx configuration. + See step 6 of the installation section if you have not already done this + during a previous upgrade. + 4. Upgrade the database before starting ZoneMinder. Most upgrades can be performed by executing the following command: diff --git a/distros/redhat/nginx/zoneminder.conf.in b/distros/redhat/nginx/zoneminder.conf.in index b8ffd816a..cca9af54f 100644 --- a/distros/redhat/nginx/zoneminder.conf.in +++ b/distros/redhat/nginx/zoneminder.conf.in @@ -22,6 +22,10 @@ location /cgi-bin-zm { fastcgi_pass unix:/run/fcgiwrap.sock; } +location /zm/cache { + alias "@ZM_CACHEDIR@"; +} + location /zm { gzip off; alias "@ZM_WEBDIR@"; diff --git a/distros/redhat/nginx/zoneminder.php-fpm.conf.in b/distros/redhat/nginx/zoneminder.php-fpm.conf.in index 26e8c62cf..ffc44bbe0 100644 --- a/distros/redhat/nginx/zoneminder.php-fpm.conf.in +++ b/distros/redhat/nginx/zoneminder.php-fpm.conf.in @@ -1,10 +1,12 @@ -# Change the user and group of the default pool to the web server account +; Change the user and group of the default pool to the web server account [www] user = @WEB_USER@ group = @WEB_GROUP@ -# Uncomment these on machines with little memory -#pm = ondemand -#pm.max_children = 10 -#pm.process_idle_timeout = 10s +; These parameters are typically a tradoff between performance and memory +; consumption. See the contents of www.conf for details. + +pm = ondemand +pm.max_children = 50 +pm.process_idle_timeout = 10s diff --git a/distros/redhat/nginx/zoneminder.tmpfiles.in b/distros/redhat/nginx/zoneminder.tmpfiles.in index 8040a7877..07bae0900 100644 --- a/distros/redhat/nginx/zoneminder.tmpfiles.in +++ b/distros/redhat/nginx/zoneminder.tmpfiles.in @@ -1,5 +1,8 @@ D @ZM_TMPDIR@ 0755 @WEB_USER@ @WEB_GROUP@ D @ZM_SOCKDIR@ 0755 @WEB_USER@ @WEB_GROUP@ +D @ZM_CACHEDIR@ 0755 @WEB_USER@ @WEB_GROUP@ +d @ZM_DIR_EVENTS@ 0755 @WEB_USER@ @WEB_GROUP@ +D @ZM_DIR_IMAGES@ 0755 @WEB_USER@ @WEB_GROUP@ D /var/lib/php/session 770 root @WEB_GROUP@ D /var/lib/php/wsdlcache 770 root @WEB_GROUP@ diff --git a/distros/redhat/zoneminder.spec b/distros/redhat/zoneminder.spec index de1d408ea..05d0cf653 100644 --- a/distros/redhat/zoneminder.spec +++ b/distros/redhat/zoneminder.spec @@ -84,7 +84,8 @@ BuildRequires: libmp4v2-devel BuildRequires: x264-devel %{?with_nginx:Requires: nginx} -%{?with_nginx:Requires: fcgiwrap} +# Enable only after fcgiwrap is in Fedora repos +#%{?with_nginx:Requires: fcgiwrap} %{?with_nginx:Requires: php-fpm} %{!?with_nginx:Requires: httpd} %{!?with_nginx:Requires: php} From 720505cbea163bf544b45cd861d6ae4592a174bb Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Sun, 4 Nov 2018 17:14:46 -0600 Subject: [PATCH 11/37] spelling --- distros/redhat/nginx/README.Fedora | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distros/redhat/nginx/README.Fedora b/distros/redhat/nginx/README.Fedora index 013a502b0..90d760952 100644 --- a/distros/redhat/nginx/README.Fedora +++ b/distros/redhat/nginx/README.Fedora @@ -15,7 +15,7 @@ What's New 4. If you have installed ZoneMinder from the FedBerry repositories, this build of ZoneMinder has support for Raspberry Pi hardware acceleration when using ffmpeg. Unforunately, there is a problem with the same hardware acceleration - when using libvlc. Consequently, libvlc support in thie build of ZoneMinder + when using libvlc. Consequently, libvlc support in this build of ZoneMinder has been disabled until the problem is resolved. See the following bug report for details: https://trac.videolan.org/vlc/ticket/18594 From b8066e66ed6ab1b824fd6d058505c292d4873911 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Mon, 5 Nov 2018 06:51:08 -0600 Subject: [PATCH 12/37] Update zoneminder.spec --- distros/redhat/zoneminder.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distros/redhat/zoneminder.spec b/distros/redhat/zoneminder.spec index 05d0cf653..4f51c73e4 100644 --- a/distros/redhat/zoneminder.spec +++ b/distros/redhat/zoneminder.spec @@ -307,7 +307,7 @@ EOF %{_libexecdir}/zoneminder/ %{_datadir}/zoneminder/ -%{_datadir}/applications/*%{name}.desktop +%{_datadir}/applications/*zoneminder.desktop %dir %attr(755,%{zmuid_final},%{zmgid_final}) %{_sharedstatedir}/zoneminder %dir %attr(755,%{zmuid_final},%{zmgid_final}) %{_sharedstatedir}/zoneminder/events From d3064e440296152e66974f89a773ef0751e8f0a0 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Mon, 5 Nov 2018 07:06:34 -0600 Subject: [PATCH 13/37] Update zoneminder.spec --- distros/redhat/zoneminder.spec | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/distros/redhat/zoneminder.spec b/distros/redhat/zoneminder.spec index 4f51c73e4..ecba0fb5a 100644 --- a/distros/redhat/zoneminder.spec +++ b/distros/redhat/zoneminder.spec @@ -84,8 +84,6 @@ BuildRequires: libmp4v2-devel BuildRequires: x264-devel %{?with_nginx:Requires: nginx} -# Enable only after fcgiwrap is in Fedora repos -#%{?with_nginx:Requires: fcgiwrap} %{?with_nginx:Requires: php-fpm} %{!?with_nginx:Requires: httpd} %{!?with_nginx:Requires: php} @@ -132,7 +130,7 @@ designed to support as many cameras as you can attach to your computer without too much degradation of performance. %prep -%autosetup -p 1 -a 1 -n ZoneMinder-%{version} +%autosetup -p 1 -a 1 %{__rm} -rf ./web/api/app/Plugin/Crud %{__mv} -f crud-%{crud_version} ./web/api/app/Plugin/Crud From ec951638105b320ac3a9940abe0b24f50f1d9b01 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Mon, 5 Nov 2018 19:35:05 -0600 Subject: [PATCH 14/37] Add fedora 29 support to buildsystem --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1ae0988d5..d46e0fc85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,7 @@ env: - OS=el DIST=7 - OS=fedora DIST=27 DOCKER_REPO=knnniggett/packpack - OS=fedora DIST=28 DOCKER_REPO=knnniggett/packpack + - OS=fedora DIST=29 DOCKER_REPO=knnniggett/packpack - OS=ubuntu DIST=trusty - OS=ubuntu DIST=xenial - OS=ubuntu DIST=trusty ARCH=i386 From c0c1247fa023e830345869fc86b9cff9abd817a0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 10:33:55 -0500 Subject: [PATCH 15/37] bump verion to 1.32.2 --- distros/redhat/zoneminder.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distros/redhat/zoneminder.spec b/distros/redhat/zoneminder.spec index 32f052ecd..867942e4e 100644 --- a/distros/redhat/zoneminder.spec +++ b/distros/redhat/zoneminder.spec @@ -25,7 +25,7 @@ %global _hardened_build 1 Name: zoneminder -Version: 1.32.1 +Version: 1.32.2 Release: 1%{?dist} Summary: A camera monitoring and analysis tool Group: System Environment/Daemons From 56bdd537575a3dd4531222ed6045707238266c70 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 10:40:44 -0500 Subject: [PATCH 16/37] Use the global dbh in ZoneMinder::Database instead of keeping our own copy of it in Logger --- scripts/ZoneMinder/lib/ZoneMinder/Logger.pm | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index 16535d9e3..2d15c045d 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -441,11 +441,11 @@ sub databaseLevel { $databaseLevel = $this->limit($databaseLevel); if ( $this->{databaseLevel} != $databaseLevel ) { if ( ( $databaseLevel > NOLOG ) and ( $this->{databaseLevel} <= NOLOG ) ) { - if ( !$this->{dbh} ) { - $this->{dbh} = ZoneMinder::Database::zmDbConnect(); + if ( ! ( $ZoneMinder::Database::dbh or ZoneMinder::Database::zmDbConnect() ) ) { + Warning("Failed connecting to db. Not using database logging."); + $this->{databaseLevel} = NOLOG; + return NOLOG; } - } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { - undef($this->{dbh}); } $this->{databaseLevel} = $databaseLevel; } @@ -558,12 +558,12 @@ sub logPrint { } if ( $level <= $this->{databaseLevel} ) { - if ( ! ( $this->{dbh} and $this->{dbh}->ping() ) ) { + if ( ! ( $ZoneMinder::Database::dbh and $ZoneMinder::Database::dbh->ping() ) ) { $this->{sth} = undef; # Turn this off because zDbConnect will do logging calls. my $oldlevel = $this->{databaseLevel}; $this->{databaseLevel} = NOLOG; - if ( ! ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) { + if ( ! ZoneMinder::Database::zmDbConnect() ) { #print(STDERR "Can't log to database: "); return; } @@ -571,10 +571,10 @@ sub logPrint { } my $sql = 'INSERT INTO Logs ( TimeKey, Component, ServerId, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, NULL )'; - $this->{sth} = $this->{dbh}->prepare_cached($sql) if ! $this->{sth}; + $this->{sth} = $ZoneMinder::Database::dbh->prepare_cached($sql) if ! $this->{sth}; if ( !$this->{sth} ) { $this->{databaseLevel} = NOLOG; - Error("Can't prepare log entry '$sql': ".$this->{dbh}->errstr()); + Error("Can't prepare log entry '$sql': ".$ZoneMinder::Database::dbh->errstr()); return; } @@ -590,7 +590,7 @@ sub logPrint { ); if ( !$res ) { $this->{databaseLevel} = NOLOG; - Error("Can't execute log entry '$sql': ".$this->{dbh}->errstr()); + Error("Can't execute log entry '$sql': ".$ZoneMinder::Database::dbh->errstr()); } } # end if doing db logging } # end if level < effectivelevel From 0ebcef732491aece126378c8306a551e6929f8b6 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 11:01:12 -0500 Subject: [PATCH 17/37] in poddoc and over needs a =back --- scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in b/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in index d48747703..92085b07b 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in +++ b/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in @@ -309,6 +309,8 @@ saving configuration is a convenient way to ensure that the configuration held in the database corresponds with the most recent definitions and that all components are using the same set of configuration. +=back + =head2 EXPORT None by default. From 4107082845e396fb3dff0cc762ec41c7cfc040ce Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 11:01:49 -0500 Subject: [PATCH 18/37] Don't delete default states if there are none --- scripts/zmpkg.pl.in | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/scripts/zmpkg.pl.in b/scripts/zmpkg.pl.in index b29235f65..2d6593619 100644 --- a/scripts/zmpkg.pl.in +++ b/scripts/zmpkg.pl.in @@ -315,17 +315,19 @@ sub isActiveSanityCheck { if ( $sth->rows != 1 ) { # PP - no row, or too many rows. Either case is an error Info( 'Fixing States table - either no default state or duplicate default states' ); - $sql = "DELETE FROM States WHERE Name='default'"; - $sth = $dbh->prepare_cached( $sql ) - or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); - $res = $sth->execute() - or Fatal( "Can't execute: ".$sth->errstr() ); - $sql = q`"INSERT INTO States (Name,Definition,IsActive) VALUES ('default','','1');`; + if ( $sth->rows ) { + $sql = q`DELETE FROM States WHERE Name='default'`; + $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + } + $sql = q`INSERT INTO States (Name,Definition,IsActive) VALUES ('default','','1');`; $sth = $dbh->prepare_cached($sql) or Fatal("Can't prepare '$sql': ".$dbh->errstr()); $res = $sth->execute() or Fatal("Can't execute: ".$sth->errstr()); - } + } # PP - Now make sure no two states have IsActive=1 $sql = 'SELECT Name FROM States WHERE IsActive = 1'; From 0e3eb0df17a64ddd09eef53bfae6c78ad508887f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 11:26:48 -0500 Subject: [PATCH 19/37] remove extra quotes, google code style, update pod docs --- scripts/ZoneMinder/lib/ZoneMinder/General.pm | 150 ++++++++----------- 1 file changed, 65 insertions(+), 85 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/General.pm b/scripts/ZoneMinder/lib/ZoneMinder/General.pm index 900a8985f..d16c248d5 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/General.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/General.pm @@ -1,27 +1,3 @@ -# ========================================================================== -# -# ZoneMinder General Utility Module, $Date$, $Revision$ -# Copyright (C) 2001-2008 Philip Coombes -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# -# ========================================================================== -# -# This module contains the common definitions and functions used by the rest -# of the ZoneMinder scripts -# package ZoneMinder::General; use 5.006; @@ -42,7 +18,7 @@ our @ISA = qw(Exporter ZoneMinder::Base); # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. our %EXPORT_TAGS = ( - 'functions' => [ qw( + functions => [ qw( executeShellCommand getCmdFormat runCommand @@ -56,7 +32,7 @@ our %EXPORT_TAGS = ( ); push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS; -our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); +our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } ); our @EXPORT = qw(); @@ -80,74 +56,74 @@ sub executeShellCommand { my $output = qx( $command ); my $status = $? >> 8; if ( $status || logDebugging() ) { - Debug( "Command: $command\n" ); + Debug("Command: $command"); chomp( $output ); - Debug( "Output: $output\n" ); + Debug("Output: $output"); } - return( $status ); + return $status; } sub getCmdFormat { - Debug( "Testing valid shell syntax\n" ); + Debug("Testing valid shell syntax"); my ( $name ) = getpwuid( $> ); if ( $name eq $Config{ZM_WEB_USER} ) { - Debug( "Running as '$name', su commands not needed\n" ); - return( "" ); + Debug("Running as '$name', su commands not needed"); + return ''; } - my $null_command = "true"; + my $null_command = 'true'; - my $prefix = "sudo -u ".$Config{ZM_WEB_USER}." "; - my $suffix = ""; + my $prefix = 'sudo -u '.$Config{ZM_WEB_USER}.' '; + my $suffix = ''; my $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); + Debug("Testing \"$command\""); my $output = qx($command 2>&1); my $status = $? >> 8; $output //= $!; if ( !$status ) { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + Debug("Test ok, using format \"$prefix$suffix\""); return( $prefix, $suffix ); } else { chomp( $output ); - Debug( "Test failed, '$output'\n" ); + Debug("Test failed, '$output'"); - $prefix = "su ".$Config{ZM_WEB_USER}." --shell=/bin/sh --command='"; - $suffix = "'"; + $prefix = 'su '.$Config{ZM_WEB_USER}.q` --shell=/bin/sh --command='`; + $suffix = q`'`; $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); + Debug("Testing \"$command\""); my $output = qx($command 2>&1); my $status = $? >> 8; $output //= $!; if ( !$status ) { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + Debug("Test ok, using format \"$prefix$suffix\""); return( $prefix, $suffix ); } else { - chomp( $output ); - Debug( "Test failed, '$output'\n" ); + chomp($output); + Debug("Test failed, '$output'"); $prefix = "su ".$Config{ZM_WEB_USER}." -c '"; $suffix = "'"; $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); + Debug("Testing \"$command\""); $output = qx($command 2>&1); $status = $? >> 8; $output //= $!; if ( !$status ) { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + Debug("Test ok, using format \"$prefix$suffix\""); return( $prefix, $suffix ); } else { - chomp( $output ); - Debug( "Test failed, '$output'\n" ); + chomp($output); + Debug("Test failed, '$output'"); } } } - Error( "Unable to find valid 'su' syntax\n" ); - exit( -1 ); -} + Error("Unable to find valid 'su' syntax"); + exit -1; +} # end sub getCmdFormat our $testedShellSyntax = 0; our ( $cmdPrefix, $cmdSuffix ); @@ -161,23 +137,23 @@ sub runCommand { } my $command = shift; - $command = $Config{ZM_PATH_BIN}."/".$command; + $command = $Config{ZM_PATH_BIN}.'/'.$command; if ( $cmdPrefix ) { $command = $cmdPrefix.$command.$cmdSuffix; } - Debug( "Command: $command\n" ); + Debug("Command: $command"); my $output = qx($command); my $status = $? >> 8; - chomp( $output ); + chomp($output); if ( $status || logDebugging() ) { if ( $status ) { - Error( "Unable to run \"$command\", output is \"$output\", status is $status\n" ); + Error("Unable to run \"$command\", output is \"$output\", status is $status"); } else { - Debug( "Output: $output\n" ); + Debug("Output: $output"); } } - return( $output ); -} + return $output; +} # end sub runCommand sub createEventPath { my $event = shift; @@ -210,7 +186,7 @@ sub _checkProcessOwner { $_setFileOwner = 0; } } - return( $_setFileOwner ); + return $_setFileOwner; } sub setFileOwner { @@ -219,7 +195,7 @@ sub setFileOwner { if ( _checkProcessOwner() ) { chown( $_ownerUid, $_ownerGid, $file ) or Fatal( "Can't change ownership of file '$file' to '" - .$Config{ZM_WEB_USER}.":".$Config{ZM_WEB_GROUP}."': $!" + .$Config{ZM_WEB_USER}.':'.$Config{ZM_WEB_GROUP}."': $!" ); } } @@ -234,13 +210,13 @@ sub _checkForImageInfo { }; $_hasImageInfo = $@?0:1; } - return( $_hasImageInfo ); + return $_hasImageInfo; } sub createEvent { my $event = shift; - Debug( "Creating event" ); + Debug('Creating event'); #print( Dumper( $event )."\n" ); _checkForImageInfo(); @@ -561,38 +537,33 @@ __END__ =head1 NAME -ZoneMinder::Database - Perl extension for blah blah blah +ZoneMinder::General - Utility Functions for ZoneMinder =head1 SYNOPSIS -use ZoneMinder::Database; +use ZoneMinder::General; blah blah blah =head1 DESCRIPTION -Stub documentation for ZoneMinder, created by h2xs. It looks like the -author of the extension was negligent enough to leave the stub -unedited. - -Blah blah blah. +This module contains the common definitions and functions used by the rest +of the ZoneMinder scripts =head2 EXPORT -None by default. + functions => [ qw( + executeShellCommand + getCmdFormat + runCommand + setFileOwner + createEventPath + createEvent + makePath + jsonEncode + jsonDecode + ) ] - -=head1 SEE ALSO - -Mention other useful documentation such as the documentation of -related modules or operating system documentation (such as man pages - in UNIX), or any relevant external documentation such as RFCs or -standards. - -If you have a mailing list set up for your module, mention it here. - -If you have a web site set up for your module, mention it here. - =head1 AUTHOR Philip Coombes, Ephilip.coombes@zoneminder.comE @@ -601,9 +572,18 @@ Philip Coombes, Ephilip.coombes@zoneminder.comE Copyright (C) 2001-2008 Philip Coombes -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself, either Perl version 5.8.3 or, -at your option, any later version of Perl 5 you may have available. +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. =cut From 230ce61dc417506ecf38ee6ef509e2900ae4b1db Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 11:54:18 -0500 Subject: [PATCH 20/37] fix #2296 by prepending bind with :: --- src/zm_stream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index 7ab844d78..461a6d74d 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -325,7 +325,7 @@ void StreamBase::openComms() { strncpy(loc_addr.sun_path, loc_sock_path, sizeof(loc_addr.sun_path)); loc_addr.sun_family = AF_UNIX; Debug(3, "Binding to %s", loc_sock_path); - if ( bind(sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path)+sizeof(loc_addr.sun_family)+1) < 0 ) { + if ( ::bind(sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path)+sizeof(loc_addr.sun_family)+1) < 0 ) { Fatal("Can't bind: %s", strerror(errno)); } From a066968acaf3dd61f484c71b32c4d90736d8e67c Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 12:33:18 -0500 Subject: [PATCH 21/37] fix dbError and cause it to return the error string instead of just logging it. Add error logging of db errors that don't throw exceptions. --- web/includes/database.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/web/includes/database.php b/web/includes/database.php index 55535659f..95fabee11 100644 --- a/web/includes/database.php +++ b/web/includes/database.php @@ -98,7 +98,14 @@ function dbLog( $sql, $update=false ) { } function dbError( $sql ) { - Error( "SQL-ERR '".$dbConn->errorInfo()."', statement was '".$sql."'" ); + global $dbConn; + $error = $dbConn->errorInfo(); + if ( ! $error[0] ) + return ''; + + $message = "SQL-ERR '".implode("\n",$dbConn->errorInfo())."', statement was '".$sql."'"; + Error($message); + return $message; } function dbEscape( $string ) { @@ -136,6 +143,10 @@ function dbQuery( $sql, $params=NULL ) { Logger::Debug("SQL: $sql values:" . ($params?implode(',',$params):'') ); } $result = $dbConn->query($sql); + if ( ! $result ) { + Error("SQL: Error preparing $sql: " . $pdo->errorInfo); + return NULL; + } } if ( defined('ZM_DB_DEBUG') ) { if ( $params ) From ec09a71ba0b6075b1daee93fe730d2f7ecf9f247 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 13:18:53 -0500 Subject: [PATCH 22/37] Include defaults for all the missing Monitor Columns --- web/includes/Monitor.php | 81 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index 2d84f5dae..e5b44682c 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -9,20 +9,95 @@ class Monitor { private $defaults = array( 'Id' => null, 'Name' => '', - 'StorageId' => 0, 'ServerId' => 0, + 'StorageId' => 0, 'Type' => 'Ffmpeg', 'Function' => 'None', 'Enabled' => 1, 'LinkedMonitors' => null, + 'Triggers' => null, + 'Device' => '', + 'Channel' => 0, + 'Format' => '0', + 'V4LMultiBuffer' => null, + 'V4LCapturesPerFrame' => null, + 'Protocol' => null, + 'Method' => '', + 'Host' => null, + 'Port' => '', + 'SubPath' => '', + 'Path' => null, + 'Options' => null, + 'User' => null, + 'Pass' => null, + // These are NOT NULL default 0 in the db, but 0 is not a valid value. FIXME 'Width' => null, 'Height' => null, + 'Colours' => 1, + 'Palette' => '0', 'Orientation' => null, + 'Deinterlacing' => 0, + 'SaveJPEGs' => 3, + 'VideoWriter' => '0', + 'OutputCodec' => null, + 'OutputContainer' => null, + 'EncoderParameters' => null, + 'RecordAudio' => 0, + 'RTSPDescribe' => null, + 'Brightness' => -1, + 'Contrast' => -1, + 'Hue' => -1, + 'Colour' => -1, + 'EventPrefix' => 'Event-', + 'LabelFormat' => null, + 'LabelX' => 0, + 'LabelY' => 0, + 'LabelSize' => 1, + 'ImageBufferCount' => 100, + 'WarmupCount' => 0, + 'PreEventCount' => 0, + 'PostEventCount' => 0, + 'StreamReplayBuffer' => 0, + 'AlarmFrameCount' => 1, + 'SectionLength' => 600, + 'FrameSkip' => 0, 'AnalysisFPSLimit' => null, - 'ZoneCount' => 0, - 'Triggers' => null, + 'AnalysisUpdateDelete' => 0, 'MaxFPS' => null, 'AlarmMaxFPS' => null, + 'FPSReportIneterval' => 100, + 'RefBlencPerc' => 6, + 'AlarmRefBlendPerc' => 6, + 'Controllable' => 0, + 'ControlId' => null, + 'ControlDevice' => null, + 'ControlAddress' => null, + 'AutoStopTimeout' => null, + 'TrackMotion' => 0, + 'TrackDelay' => null, + 'ReturnLocation' => -1, + 'ReturnDelay' => null, + 'DefaultView' => 'Events', + 'DefaultRate' => 100, + 'DefaultScale' => 100, + 'SignalCheckPoints' => 0, + 'SignalCheckColour' => '#0000BE', + 'WebColour' => 'red', + 'Exif' => 0, + 'Sequence' => null, + 'TotalEvents' => null, + 'TotalEventDiskSpace' => null, + 'HourEvents' => null, + 'HourEventDiskSpace' => null, + 'DayEvents' => null, + 'DayEventDiskSpace' => null, + 'WeekEvents' => null, + 'WeekEventDiskSpace' => null, + 'MonthEvents' => null, + 'MonthEventDiskSpace' => null, + 'ArchivedEvents' => null, + 'ArchivedEventDiskSpace' => null, + 'ZoneCount' => 0, 'Refresh' => null, ); private $status_fields = array( From 82abd04f363b6c3f4d21732ac07f4846111abd19 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 13:19:42 -0500 Subject: [PATCH 23/37] Add type=button to buttons so they don't act like submit buttons --- web/skins/classic/views/event.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/web/skins/classic/views/event.php b/web/skins/classic/views/event.php index 8671bfc03..12fd62c20 100644 --- a/web/skins/classic/views/event.php +++ b/web/skins/classic/views/event.php @@ -119,22 +119,22 @@ if ( ! $Event->Id() ) { @@ -214,8 +214,8 @@ if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
-
- +
+
From 1844a7eb292dec6188af0836feaea938b99537a7 Mon Sep 17 00:00:00 2001 From: Aktarus <11706557+aktarus82@users.noreply.github.com> Date: Fri, 9 Nov 2018 13:59:06 +0100 Subject: [PATCH 24/37] Update definemonitor.rst (#2299) --- docs/userguide/definemonitor.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/userguide/definemonitor.rst b/docs/userguide/definemonitor.rst index bf12a1bc5..bcd195570 100644 --- a/docs/userguide/definemonitor.rst +++ b/docs/userguide/definemonitor.rst @@ -164,6 +164,29 @@ Height (pixels) Web Site Refresh If the website in question has static content, optionally enter a time period in seconds for ZoneMinder to refresh the content. +Storage Tab +----------- + +The storage section allows for each monitor to configure if and how video and audio are recorded. + +Save JPEGs + Records video in individual JPEG frames. Storing JPEG frames requires more storage space than h264 but it allows to view an event anytime while it is being recorded. + + * Disabled – video is not recorded as JPEG frames. If this setting is selected, then "Video Writer" should be enabled otherwise there is no video recording at all. + * Frames only – video is recorded in individual JPEG frames. + * Analysis images only (if available) – video is recorded in invidual JPEG frames with an overlay of the motion detection analysis information. Note that this overlay remains permanently visible in the frames. + * Frames + Analysis images (if available) – video is recorded twice, once as normal individual JPEG frames and once in invidual JPEG frames with analysis information overlaid. + +Video Writer + Records video in real video format. It provides much better compression results than saving JPEGs, thus longer video history can be stored. + + * Disabled – video is not recorded in video format. If this setting is selected, then "Save JPEGs" should be enabled otherwise there is no video recording at all. + * X264 Encode – the video or picture frames received from the camera are transcoded into h264 and stored as a video. This option is useful if the camera cannot natively stream h264. + * H264 Camera Passthrough – this option assumes that the camera is already sending an h264 stream. Video will be recorded as is, without any post-processing in zoneminder. Video characteristics such as bitrate, encoding mode, etc. should be set directly in the camera. + +Recording Audio + Check the box labeled "Whether to store the audio stream when saving an event." in order to save audio (if available) when events are recorded. + Timestamp Tab ------------- From 4ddd328c6186b1e40286994efed0f24b46023ea0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 9 Nov 2018 10:39:57 -0500 Subject: [PATCH 25/37] Check and Fix Scheme and StorageArea --- scripts/zmaudit.pl.in | 110 ++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 495fdc031..66dab7ae4 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -508,68 +508,76 @@ MAIN: while( $loop ) { # Foreach database monitor and it's list of events. while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) ) { + Debug("Checking db events for monitor $db_monitor"); + if ( ! $db_events ) { + Debug("Skipping db events for $db_monitor because there are none"); + next; + } # If we found the monitor in the file system - if ( my $fs_events = $fs_monitors->{$db_monitor} ) { - next if ! $db_events; + my $fs_events = $fs_monitors->{$db_monitor}; - while ( my ( $db_event, $age ) = each( %$db_events ) ) { - if ( ! defined( $fs_events->{$db_event} ) ) { - my $Event = ZoneMinder::Event->find_one( Id=>$db_event ); - if ( ! $Event ) { - Debug("Event $db_event is no longer in db. Filter probably deleted it while we were auditing."); - next; + while ( my ( $db_event, $age ) = each( %$db_events ) ) { + if ( ! ($fs_events and defined( $fs_events->{$db_event} ) ) ) { + Debug("Don't have an fs event for $db_event"); + my $Event = ZoneMinder::Event->find_one( Id=>$db_event ); + if ( ! $Event ) { + Debug("Event $db_event is no longer in db. Filter probably deleted it while we were auditing."); + next; + } + Debug("Event $db_event is not in fs. Should have been at ".$Event->Path()); + if ( $Event->Archived() ) { + Warning("Event $$Event{Id} is Archived. Taking no further action on it."); + next; + } + if ( ! $Event->StartTime() ) { + Info("Event $$Event{Id} has no start time. deleting it."); + if ( confirm() ) { + $Event->delete(); + $cleaned = 1; } - Debug("Event $db_event is not in fs. Should have been at ".$Event->Path()); - if ( $Event->Archived() ) { - Warning("Event $$Event{Id} is Archived. Taking no further action on it."); - next; - } - if ( ! $Event->StartTime() ) { - Info("Event $$Event{Id} has no start time. deleting it."); + next; + } + if ( ! $Event->EndTime() ) { + if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) { + Info("Event $$Event{Id} has no end time and is $age seconds old. deleting it."); if ( confirm() ) { $Event->delete(); $cleaned = 1; } next; - } - if ( ! $Event->EndTime() ) { - if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) { - Info("Event $$Event{Id} has no end time and is $age seconds old. deleting it."); - if ( confirm() ) { - $Event->delete(); - $cleaned = 1; - } - next; - } } - if ( $Event->check_for_in_filesystem() ) { - Debug("Database event $$Event{Id} apparently exists at " . $Event->Path() ); - } else { - if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) { - aud_print( "Database event '$db_monitor/$db_event' does not exist at " . $Event->Path().' in filesystem, deleting' ); - if ( confirm() ) { - $Event->delete(); - $cleaned = 1; - } - } else { - aud_print( "Database event '".$Event->Path()." monitor:$db_monitor event:$db_event' does not exist in filesystem but too young to delete age: $age > MIN $Config{ZM_AUDIT_MIN_AGE}.\n" ); + } + if ( $Event->check_for_in_filesystem() ) { + Debug("Database event $$Event{Id} apparently exists at " . $Event->Path() ); + } else { + if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) { + aud_print( "Database event '$db_monitor/$db_event' does not exist at " . $Event->Path().' in filesystem, deleting' ); + if ( confirm() ) { + $Event->delete(); + $cleaned = 1; } - } # end if exists in filesystem - } # end if ! in fs_events - } # foreach db_event - #} else { - #my $Monitor = new ZoneMinder::Monitor( $db_monitor ); - #my $Storage = $Monitor->Storage(); - #aud_print( "Database monitor '$db_monitor' does not exist in filesystem, should have been at ".$Storage->Path().'/'.$Monitor->Id()."\n" ); -#if ( confirm() ) -#{ -# We don't actually do this in case it's new -#my $res = $deleteMonitorSth->execute( $db_monitor ) -# or Fatal( "Can't execute: ".$deleteMonitorSth->errstr() ); -#$cleaned = 1; -#} - } + } else { + aud_print( "Database event '".$Event->Path()." monitor:$db_monitor event:$db_event' does not exist in filesystem but too young to delete age: $age > MIN $Config{ZM_AUDIT_MIN_AGE}.\n" ); + } + } # end if exists in filesystem + } else { + Debug("Found fs event for $db_event, $age at " . $$fs_events{$db_event}->Path()); + my $Event = new ZoneMinder::Event( $db_event ); + if ( ! $Event->check_for_in_filesystem() ) { + Warning("Not found at " . $Event->Path() ); + if ( $$fs_events{$db_event}->Scheme() ne $Event->Scheme() ) { + Info("Updating scheme on event $$Event{Id} from $$Event{Scheme} to $$fs_events{$db_event}{Scheme}"); + $Event->Scheme($$fs_events{$db_event}->Scheme()); + } + if ( $$fs_events{$db_event}->StorageId() != $Event->StorageId() ) { + Info("Updating storage area on event $$Event{Id} from $$Event{StorageId} to $$fs_events{$db_event}{StorageId}"); + $Event->StorageId($$fs_events{$db_event}->StorageId()); + } + $Event->save(); + } + } # end if ! in fs_events + } # foreach db_event } # end foreach db_monitor if ( $cleaned ) { Debug("Have done some cleaning, restarting."); From 5f81e9111f6e8e4b4ba2030e983babf3fb9a6bdf Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 9 Nov 2018 12:14:05 -0500 Subject: [PATCH 26/37] Add Scheme and SaveJPEGs to Event fields, and fix setting Scheme --- scripts/ZoneMinder/lib/ZoneMinder/Event.pm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm index d52c9829e..67cd850b9 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm @@ -70,6 +70,7 @@ $serial = $primary_key = 'Id'; Frames AlarmFrames DefaultVideo + SaveJPEGs TotScore AvgScore MaxScore @@ -83,6 +84,7 @@ $serial = $primary_key = 'Id'; StateId Orientation DiskSpace + Scheme ); use POSIX; @@ -168,6 +170,8 @@ sub Path { sub Scheme { my $self = shift; + $$self{Scheme} = shift if @_; + if ( ! $$self{Scheme} ) { if ( $$self{RelativePath} ) { if ( $$self{RelativePath} =~ /^\d+\/\d{4}\-\d{2}\-\d{2}\/\d+$/ ) { From 1e98ecb5957a69970da2cadcea1412e0a217e220 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 9 Nov 2018 12:14:39 -0500 Subject: [PATCH 27/37] Sort keys in to_string --- scripts/ZoneMinder/lib/ZoneMinder/Object.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm index 8c28028a0..f779255b2 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm @@ -456,7 +456,7 @@ sub transform { sub to_string { my $type = ref($_[0]); my $fields = eval '\%'.$type.'::fields'; - return $type . ': '. join(' ' , map { $_[0]{$_} ? "$_ => $_[0]{$_}" : () } keys %$fields ); + return $type . ': '. join(' ' , map { $_[0]{$_} ? "$_ => $_[0]{$_}" : () } sort { $a cmp $b } keys %$fields ); } 1; From 967fe14b22dde95e07f7041655af2b2239aff218 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 11 Nov 2018 10:17:32 -0500 Subject: [PATCH 28/37] Add ability to update incorrect starttime. --- scripts/zmaudit.pl.in | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 66dab7ae4..ebd56229e 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -509,10 +509,10 @@ MAIN: while( $loop ) { # Foreach database monitor and it's list of events. while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) ) { Debug("Checking db events for monitor $db_monitor"); - if ( ! $db_events ) { - Debug("Skipping db events for $db_monitor because there are none"); - next; - } + if ( ! $db_events ) { + Debug("Skipping db events for $db_monitor because there are none"); + next; + } # If we found the monitor in the file system my $fs_events = $fs_monitors->{$db_monitor}; @@ -565,7 +565,9 @@ MAIN: while( $loop ) { Debug("Found fs event for $db_event, $age at " . $$fs_events{$db_event}->Path()); my $Event = new ZoneMinder::Event( $db_event ); if ( ! $Event->check_for_in_filesystem() ) { - Warning("Not found at " . $Event->Path() ); + Warning("Not found at " . $Event->Path() . ' was found at ' . $$fs_events{$db_event}->Path() ); + Warning($Event->to_string()); + Warning($$fs_events{$db_event}->to_string()); if ( $$fs_events{$db_event}->Scheme() ne $Event->Scheme() ) { Info("Updating scheme on event $$Event{Id} from $$Event{Scheme} to $$fs_events{$db_event}{Scheme}"); $Event->Scheme($$fs_events{$db_event}->Scheme()); @@ -574,6 +576,15 @@ MAIN: while( $loop ) { Info("Updating storage area on event $$Event{Id} from $$Event{StorageId} to $$fs_events{$db_event}{StorageId}"); $Event->StorageId($$fs_events{$db_event}->StorageId()); } + if ( $$fs_events{$db_event}->StartTime() ne $Event->StartTime() ) { + if ( $$Event{Scheme} eq 'Deep' ) { + Info("Updating StartTime on event $$Event{Id} from $$Event{StartTime} to $$fs_events{$db_event}{StartTime}"); + $Event->StartTime($$fs_events{$db_event}->StartTime()); + } else { + } + $Event->save(); + } + $Event->save(); } } # end if ! in fs_events @@ -962,7 +973,7 @@ sub delete_empty_directories { return; } my @contents = map { ( $_ eq '.' or $_ eq '..' ) ? () : $_ } readdir( $DIR ); - Debug("delete_empty_directories $_[0] has " . @contents .' entries:' . ( @contents <= 2 ? join(',',@contents) : '' )); + #Debug("delete_empty_directories $_[0] has " . @contents .' entries:' . ( @contents <= 2 ? join(',',@contents) : '' )); my @dirs = map { -d $_[0].'/'.$_ ? $_ : () } @contents; if ( @dirs ) { Debug("Have " . @dirs . " dirs"); From 246765ced975e4b5c4ef120c185c0bed91deb511 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 11 Nov 2018 16:23:19 -0500 Subject: [PATCH 29/37] Add tooltip for sorting monitors --- web/skins/classic/views/console.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/skins/classic/views/console.php b/web/skins/classic/views/console.php index 46575ffa0..1ebf49136 100644 --- a/web/skins/classic/views/console.php +++ b/web/skins/classic/views/console.php @@ -324,7 +324,7 @@ for( $monitor_i = 0; $monitor_i < count($displayMonitors); $monitor_i += 1 ) { ?> disabled="disabled"/> - + Date: Mon, 12 Nov 2018 06:18:13 -0800 Subject: [PATCH 30/37] Fix typos in README file (#2301) * Fix typos in README file * Improve clarity in introduction doc * Improved word selection * Revision of the README file * Finish editing introduction documentation --- README.md | 4 ++-- docs/userguide/introduction.rst | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 94774364c..b8e3f34fd 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Lastly, if you desire to build a development snapshot from the master branch, it Please visit our [ReadtheDocs site](https://zoneminder.readthedocs.org/en/stable/installationguide/index.html) for distro specific instructions. ### Package Maintainers -Many of the ZoneMinder configration variable default values are not configurable at build time through autotools or cmake. A new tool called *zmeditconfigdata.sh* has been added to allow package maintainers to manipulate any variable stored in ConfigData.pm without patching the source. +Many of the ZoneMinder configuration variable default values are not configurable at build time through autotools or cmake. A new tool called *zmeditconfigdata.sh* has been added to allow package maintainers to manipulate any variable stored in ConfigData.pm without patching the source. -For example, let's say I have created a new ZoneMinder package that contains the cambozola javascript file. However, by default cambozola support is turned off. To fix that, add this to the pacakging script: +For example, let's say I have created a new ZoneMinder package that contains the cambozola javascript file. However, by default cambozola support is turned off. To fix that, add this to the packaging script: ```bash ./utils/zmeditconfigdata.sh ZM_OPT_CAMBOZOLA yes ``` diff --git a/docs/userguide/introduction.rst b/docs/userguide/introduction.rst index 8da67a5e9..2d9985eb1 100644 --- a/docs/userguide/introduction.rst +++ b/docs/userguide/introduction.rst @@ -1,15 +1,14 @@ Introduction ============ -Welcome to ZoneMinder, the all-in-one Linux GPL'd security camera solution. +Welcome to ZoneMinder, the all-in-one security camera solution for Linux with GPL License. -Most commercial "security systems" are designed as a monitoring system that also records. Recording quality can vary from bad to unusable, locating the relevant video can range from challenging to impractical, and exporting can often only be done with the manual present. ZoneMinder was designed primarily to record, and allow easy searches and exporting. Recordings are of the best possible quality, easy to filter and find, and simple to export using any system with a web browser. It also monitors. +Commercial "security systems" are often designed as a monitoring system with little attention to recording quality. In such a system, locating and exporting relevant video can be challenging and often requires extensive human intervention. ZoneMinder was designed to provide the best possible record quality while allowing easy searching, filtering and exporting of security footage. -ZoneMinder is designed around a series of independent components that only function when necessary limiting any wasted resource and maximising the efficiency of your machine. A fairly ancient Pentium II PC should be able to track one camera per device at up to 25 frames per second with this dropping by half approximately for each additional camera on the same device. Additional cameras on other devices do not interact so can maintain this frame rate. Even monitoring several cameras still will not overload the CPU as frame processing is designed to synchronise with capture and not stall it. +ZoneMinder is designed around a series of independent components that only function when necessary, limiting any wasted resource and maximising the efficiency of your machine. An outdated Pentium II PC can have multiple recording devices connected to it, and it is able to track one camera per device at up to 25 frames per second, which drops by approximately half for each additional camera on the same device. Additional cameras on devices that do not interact with other devices can maintain the 25 frame rate per second. Monitoring several cameras will not overload the CPU as frame processing is designed to synchronise with capture. -As well as being fast ZoneMinder is designed to be friendly and even more than that, actually useful. As well as the fast video interface core it also comes with a user friendly and comprehensive PHP based web interface allowing you to control and monitor your cameras from home, at work, on the road, or even a web enabled cell phone. It supports variable web capabilities based on available bandwidth. The web interface also allows you to view events that your cameras have captured and archive them or review them time and again, or delete the ones you no longer wish to keep. The web pages directly interact with the core daemons ensuring full co-operation at all times. ZoneMinder can even be installed as a system service ensuring it is right there if your computer has to reboot for any reason. +A fast video interface core, a user-friendly and comprehensive PHP based web interface allows ZoneMinder to be efficient, friendly and most importantly useful. You can control and monitor your cameras from home, at work, on the road, or a web-enabled cell phone. It supports variable web capabilities based on available bandwidth. The web interface also allows you to view events that your cameras have captured, which can be archived, reviewed or deleted. The web application directly interacts with the core daemons ensuring full co-operation at all times. ZoneMinder can also be installed as a system service to reboot a system remotely. -The core of ZoneMinder is the capture and analysis of images and there is a highly configurable set of parameters that allow you to ensure that you can eliminate false positives whilst ensuring that anything you don't want to miss will be captured and saved. ZoneMinder allows you to define a set of 'zones' for each camera of varying sensitivity and functionality. This allows you to eliminate regions that you don't wish to track or define areas that will alarm if various thresholds are exceeded in conjunction with other zones. - -ZoneMinder is free, but if you do find it useful then please feel free to visit http://www.zoneminder.com/donate.html and help to fund future improvements to ZoneMinder. +The core of ZoneMinder is the capture and analysis of images and a highly configurable set of parameters that eliminate false positives whilst ensuring minimum loss of footage. For example, you can define a set of 'zones' for each camera of varying sensitivity and functionality. This eliminates zones that you don't wish to track or define areas that will alarm if various thresholds are exceeded in conjunction with other zones. +ZoneMinder is free under GPL License, but if you do find it useful, then please feel free to visit http://www.zoneminder.com/donate.html and help us fund our future improvements. From a95a012fa5dbe9da7b126d04be46f941c1d7f96d Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 12 Nov 2018 11:56:18 -0500 Subject: [PATCH 31/37] fix #2302 --- src/zm_monitor.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 2e1e55691..8ff0a0a64 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2110,6 +2110,7 @@ Monitor *Monitor::Load(MYSQL_ROW dbrow, bool load_zones, Purpose purpose) { Camera *camera = 0; if ( type == "Local" ) { +#if ZM_HAS_V4L int extras = (deinterlacing>>24)&0xff; camera = new LocalCamera( @@ -2132,6 +2133,9 @@ Monitor *Monitor::Load(MYSQL_ROW dbrow, bool load_zones, Purpose purpose) { record_audio, extras ); +#else + Fatal("ZoneMinder not built with Local Camera support"); +#endif } else if ( type == "Remote" ) { if ( protocol == "http" ) { camera = new RemoteCameraHttp( From d76d6bb9d13bcbbb98adb35122c0cda2d859e938 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 12 Nov 2018 15:09:15 -0500 Subject: [PATCH 32/37] include overlay.js when viewing the log, so that export works --- web/skins/classic/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 7847e37c7..b21157607 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -192,7 +192,7 @@ echo output_link_if_exists( array( From abeafe9ba6c26bfc44c8f7123e2fe073d2007a4d Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 12 Nov 2018 15:43:03 -0500 Subject: [PATCH 33/37] fix log export. minTime and maxTime were being cleared by the regexp to detect sub second time. Also use ZM_DIR_EXPORTS instead of ZM_PATH_SWAP --- web/ajax/log.php | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/web/ajax/log.php b/web/ajax/log.php index f0703f871..fca79fda4 100644 --- a/web/ajax/log.php +++ b/web/ajax/log.php @@ -164,14 +164,23 @@ switch ( $_REQUEST['task'] ) { $where = array(); $values = array(); if ( $minTime ) { - preg_match('/(.+)(\.\d+)/', $minTime, $matches); - $minTime = strtotime($matches[1]).$matches[2]; + Logger::Debug("MinTime: $minTime"); + if ( preg_match('/(.+)(\.\d+)/', $minTime, $matches) ) { + # This handles sub second precision + $minTime = strtotime($matches[1]).$matches[2]; + Logger::Debug("MinTime: $minTime"); + } else { + $minTime = strtotime($minTime); + } $where[] = 'TimeKey >= ?'; $values[] = $minTime; } if ( $maxTime ) { - preg_match('/(.+)(\.\d+)/', $maxTime, $matches); - $maxTime = strtotime($matches[1]).$matches[2]; + if ( preg_match('/(.+)(\.\d+)/', $maxTime, $matches) ) { + $maxTime = strtotime($matches[1]).$matches[2]; + } else { + $maxTime = strtotime($maxTime); + } $where[] = 'TimeKey <= ?'; $values[] = $maxTime; } @@ -209,8 +218,15 @@ switch ( $_REQUEST['task'] ) { } $exportKey = substr(md5(rand()),0,8); $exportFile = "zm-log.$exportExt"; - $exportPath = ZM_PATH_SWAP."/zm-log-$exportKey.$exportExt"; - if ( !($exportFP = fopen( $exportPath, "w" )) ) + if ( ! file_exists(ZM_DIR_EXPORTS) ) { + Logger::Debug('Creating ' . ZM_DIR_EXPORTS); + if ( ! mkdir(ZM_DIR_EXPORTS) ) { + Fatal("Can't create exports dir at '".ZM_DIR_EXPORTS."'"); + } + } + $exportPath = ZM_DIR_EXPORTS."/zm-log-$exportKey.$exportExt"; + Logger::Debug("Exporting to $exportPath"); + if ( !($exportFP = fopen($exportPath, 'w')) ) Fatal("Unable to open log export file $exportPath"); $logs = array(); foreach ( dbFetchAll($sql, NULL, $values) as $log ) { @@ -218,6 +234,8 @@ switch ( $_REQUEST['task'] ) { $log['Server'] = ( $log['ServerId'] and isset($servers_by_Id[$log['ServerId']]) ) ? $servers_by_Id[$log['ServerId']]->Name() : ''; $logs[] = $log; } + Logger::Debug(count($logs)." lines being exported by $sql " . implode(',',$values)); + switch( $format ) { case 'text' : { @@ -390,7 +408,7 @@ switch ( $_REQUEST['task'] ) { } $exportFile = "zm-log.$exportExt"; - $exportPath = ZM_PATH_SWAP."/zm-log-$exportKey.$exportExt"; + $exportPath = ZM_DIR_EXPORTS."/zm-log-$exportKey.$exportExt"; header('Pragma: public'); header('Expires: 0'); From 993da131f1b07940da5f8b91260d10cabe3eef6b Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 10:29:19 -0500 Subject: [PATCH 34/37] tidy up and use Error instead of Carp::croak --- scripts/ZoneMinder/lib/ZoneMinder/Database.pm | 108 +++++++++++------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm index b1fca1d16..dcc797bd4 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm @@ -41,17 +41,18 @@ our @ISA = qw(Exporter ZoneMinder::Base); # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. our %EXPORT_TAGS = ( - 'functions' => [ qw( + functions => [ qw( zmDbConnect zmDbDisconnect zmDbGetMonitors zmDbGetMonitor zmDbGetMonitorAndControl + zmDbDo ) ] ); push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS; -our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); +our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } ); our @EXPORT = qw(); @@ -66,8 +67,6 @@ our $VERSION = $ZoneMinder::Base::VERSION; use ZoneMinder::Logger qw(:all); use ZoneMinder::Config qw(:all); -use Carp; - our $dbh = undef; sub zmDbConnect { @@ -93,7 +92,7 @@ sub zmDbConnect { my $sslOptions = ''; if ( $Config{ZM_DB_SSL_CA_CERT} ) { - $sslOptions = ';'.join(';', + $sslOptions = join(';','', 'mysql_ssl=1', 'mysql_ssl_ca_file='.$Config{ZM_DB_SSL_CA_CERT}, 'mysql_ssl_client_key='.$Config{ZM_DB_SSL_CLIENT_KEY}, @@ -102,8 +101,9 @@ sub zmDbConnect { } eval { - $dbh = DBI->connect( 'DBI:mysql:database='.$Config{ZM_DB_NAME} - .$socket . $sslOptions . ($options?';'.join(';', map { $_.'='.$$options{$_} } keys %{$options} ) : '') + $dbh = DBI->connect( + 'DBI:mysql:database='.$Config{ZM_DB_NAME} + .$socket . $sslOptions . ($options?join(';', '', map { $_.'='.$$options{$_} } keys %{$options} ) : '') , $Config{ZM_DB_USER} , $Config{ZM_DB_PASS} ); @@ -125,7 +125,7 @@ sub zmDbConnect { sub zmDbDisconnect { if ( defined( $dbh ) ) { - $dbh->disconnect(); + $dbh->disconnect() or Error('Error disconnecting db? ' . $dbh->errstr()); $dbh = undef; } } @@ -141,7 +141,7 @@ sub zmDbGetMonitors { zmDbConnect(); my $function = shift || DB_MON_ALL; - my $sql = "select * from Monitors"; + my $sql = 'SELECT * FROM Monitors'; if ( $function ) { if ( $function == DB_MON_CAPT ) { @@ -156,26 +156,38 @@ sub zmDbGetMonitors { $sql .= " where Function = 'Nodect'"; } } - my $sth = $dbh->prepare_cached( $sql ) - or croak( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute() - or croak( "Can't execute '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ); + if ( ! $sth ) { + Error("Can't prepare '$sql': ".$dbh->errstr()); + return undef; + } + my $res = $sth->execute(); + if ( ! $res ) { + Error("Can't execute '$sql': ".$sth->errstr()); + return undef; + } my @monitors; while( my $monitor = $sth->fetchrow_hashref() ) { push( @monitors, $monitor ); } $sth->finish(); - return( \@monitors ); + return \@monitors; } sub zmSQLExecute { my $sql = shift; - - my $sth = $dbh->prepare_cached( $sql ) - or croak( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute( @_ ) - or croak( "Can't execute '$sql': ".$sth->errstr() ); + + my $sth = $dbh->prepare_cached( $sql ); + if ( ! $sth ) { + Error("Can't prepare '$sql': ".$dbh->errstr()); + return undef; + } + my $res = $sth->execute( @_ ); + if ( ! $res ) { + Error("Can't execute '$sql': ".$sth->errstr()); + return undef; + } return 1; } @@ -185,17 +197,22 @@ sub zmDbGetMonitor { my $id = shift; if ( !defined($id) ) { - croak("Undefined id in zmDbgetMonitor"); + Error('Undefined id in zmDbgetMonitor'); return undef ; } my $sql = 'SELECT * FROM Monitors WHERE Id = ?'; - my $sth = $dbh->prepare_cached($sql) - or croak("Can't prepare '$sql': ".$dbh->errstr()); - my $res = $sth->execute($id) - or croak("Can't execute '$sql': ".$sth->errstr()); + my $sth = $dbh->prepare_cached($sql); + if ( !$sth ) { + Error("Can't prepare '$sql': ".$dbh->errstr()); + return undef; + } + my $res = $sth->execute($id); + if ( $res ) { + Error("Can't execute '$sql': ".$sth->errstr()); + return undef; + } my $monitor = $sth->fetchrow_hashref(); - return $monitor; } @@ -204,25 +221,28 @@ sub zmDbGetMonitorAndControl { my $id = shift; - return( undef ) if ( !defined($id) ); + return undef if !defined($id); - my $sql = "SELECT C.*,M.*,C.Protocol + my $sql = 'SELECT C.*,M.*,C.Protocol FROM Monitors as M INNER JOIN Controls as C on (M.ControlId = C.Id) - WHERE M.Id = ?" + WHERE M.Id = ?' ; - my $sth = $dbh->prepare_cached( $sql ) - or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute( $id ) - or Fatal( "Can't execute '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached($sql); + if ( !$sth ) { + Error("Can't prepare '$sql': ".$dbh->errstr()); + return undef; + } + my $res = $sth->execute( $id ); + if ( !$res ) { + Error("Can't execute '$sql': ".$sth->errstr()); + return undef; + } my $monitor = $sth->fetchrow_hashref(); - - return( $monitor ); + return $monitor; } sub start_transaction { - #my ( $caller, undef, $line ) = caller; -#$openprint::log->debug("Called start_transaction from $caller : $line"); my $d = shift; $d = $dbh if ! $d; my $ac = $d->{AutoCommit}; @@ -231,20 +251,29 @@ sub start_transaction { } # end sub start_transaction sub end_transaction { - #my ( $caller, undef, $line ) = caller; - #$openprint::log->debug("Called end_transaction from $caller : $line"); my ( $d, $ac ) = @_; if ( ! defined $ac ) { Error("Undefined ac"); } $d = $dbh if ! $d; if ( $ac ) { - #$log->debug("Committing"); $d->commit(); } # end if $d->{AutoCommit} = $ac; } # end sub end_transaction +# Basic execution of $dbh->do but with some pretty logging of the sql on error. +# Returns 1 on success, 0 on error +sub zmDbDo { + my $sql = shift; + if ( ! $dbh->do($sql, undef, @_) ) { + $sql =~ s/\?/'%s'/; + Error(sprintf("Failed $sql :", @_).$dbh->errstr()); + return 0; + } + return 1; +} + 1; __END__ @@ -266,6 +295,7 @@ zmDbDisconnect zmDbGetMonitors zmDbGetMonitor zmDbGetMonitorAndControl +zmDbDo =head1 AUTHOR From 31b0ed107d8209b5d4b178e5b3274217cf0a0f71 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 14:02:44 -0500 Subject: [PATCH 35/37] remove epadding2, to restore the 64bit alignment of startup_time. A proper fix instead of #2258 --- scripts/ZoneMinder/lib/ZoneMinder/Memory.pm.in | 1 - src/zm_monitor.h | 11 ++++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Memory.pm.in b/scripts/ZoneMinder/lib/ZoneMinder/Memory.pm.in index 2529c8786..7c5292720 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Memory.pm.in +++ b/scripts/ZoneMinder/lib/ZoneMinder/Memory.pm.in @@ -161,7 +161,6 @@ our $mem_data = { format => { type=>'uint8', seq=>$mem_seq++ }, imagesize => { type=>'uint32', seq=>$mem_seq++ }, epadding1 => { type=>'uint32', seq=>$mem_seq++ }, - epadding2 => { type=>'uint32', seq=>$mem_seq++ }, startup_time => { type=>'time_t64', seq=>$mem_seq++ }, last_write_time => { type=>'time_t64', seq=>$mem_seq++ }, last_read_time => { type=>'time_t64', seq=>$mem_seq++ }, diff --git a/src/zm_monitor.h b/src/zm_monitor.h index b98f5953f..d7be9555e 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -127,24 +127,25 @@ protected: uint8_t format; /* +55 */ uint32_t imagesize; /* +56 */ uint32_t epadding1; /* +60 */ - uint32_t epadding2; /* +64 */ /* ** This keeps 32bit time_t and 64bit time_t identical and compatible as long as time is before 2038. ** Shared memory layout should be identical for both 32bit and 64bit and is multiples of 16. + ** Because startup_time is 64bit it may be aligned to a 64bit boundary. So it's offset SHOULD be a multiple + ** of 8. Add or delete epadding's to achieve this. */ - union { /* +68 */ + union { /* +64 */ time_t startup_time; /* When the zmc process started. zmwatch uses this to see how long the process has been running without getting any images */ uint64_t extrapad1; }; - union { /* +76 */ + union { /* +72 */ time_t last_write_time; uint64_t extrapad2; }; - union { /* +84 */ + union { /* +80 */ time_t last_read_time; uint64_t extrapad3; }; - uint8_t control_state[256]; /* +92 */ + uint8_t control_state[256]; /* +88 */ char alarm_cause[256]; From 798bf68df1ed1fef1031cc9a3595aabc2576e246 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 14:13:32 -0500 Subject: [PATCH 36/37] add function time_of_youngest_file to determine the youngest file in an event dir, so as to guess the starttime of the event. --- scripts/ZoneMinder/lib/ZoneMinder/Event.pm | 112 ++++++++++----------- 1 file changed, 52 insertions(+), 60 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm index 67cd850b9..5916f8cdb 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm @@ -320,11 +320,11 @@ sub GenerateVideo { my $file_size = 'S'.$size; push( @file_parts, $file_size ); } - my $video_file = "$video_name-".$file_parts[0]."-".$file_parts[1].".$format"; + my $video_file = join('-', $video_name, $file_parts[0], $file_parts[1] ).'.'.$format; if ( $overwrite || !-s $video_file ) { - Info( "Creating video file $video_file for event $self->{Id}\n" ); + Info("Creating video file $video_file for event $self->{Id}"); - my $frame_rate = sprintf( "%.2f", $self->{Frames}/$self->{FullLength} ); + my $frame_rate = sprintf('%.2f', $self->{Frames}/$self->{FullLength}); if ( $rate ) { if ( $rate != 1.0 ) { $frame_rate *= $rate; @@ -355,19 +355,19 @@ sub GenerateVideo { .$Config{ZM_FFMPEG_OUTPUT_OPTIONS} ." '$video_file' > ffmpeg.log 2>&1" ; - Debug( $command."\n" ); + Debug($command); my $output = qx($command); my $status = $? >> 8; if ( $status ) { - Error( "Unable to generate video, check $event_path/ffmpeg.log for details"); + Error("Unable to generate video, check $event_path/ffmpeg.log for details"); return; } - Info( "Finished $video_file\n" ); + Info("Finished $video_file"); return $event_path.'/'.$video_file; } else { - Info( "Video file $video_file already exists for event $self->{Id}\n" ); + Info("Video file $video_file already exists for event $self->{Id}"); return $event_path.'/'.$video_file; } return; @@ -375,57 +375,49 @@ sub GenerateVideo { sub delete { my $event = $_[0]; - if ( ! ( $event->{Id} and $event->{MonitorId} and $event->{StartTime} ) ) { - my ( $caller, undef, $line ) = caller; - Warning("Can't Delete event $event->{Id} from Monitor $event->{MonitorId} StartTime:$event->{StartTime} from $caller:$line\n"); - return; - } - if ( ! -e $event->Storage()->Path() ) { - Warning("Not deleting event because storage path doesn't exist"); - return; - } - Info("Deleting event $event->{Id} from Monitor $event->{MonitorId} StartTime:$event->{StartTime}\n"); - $ZoneMinder::Database::dbh->ping(); - $ZoneMinder::Database::dbh->begin_work(); - #$event->lock_and_load(); + my $in_zmaudit = ( $0 =~ 'zmaudit.pl$'); - { - my $sql = 'DELETE FROM Frames WHERE EventId=?'; - my $sth = $ZoneMinder::Database::dbh->prepare_cached($sql) - or Error( "Can't prepare '$sql': ".$ZoneMinder::Database::dbh->errstr() ); - my $res = $sth->execute($event->{Id}) - or Error( "Can't execute '$sql': ".$sth->errstr() ); - $sth->finish(); + if ( ! $in_zmaudit ) { + if ( ! ( $event->{Id} and $event->{MonitorId} and $event->{StartTime} ) ) { + # zmfilter shouldn't delete anything in an odd situation. zmaudit will though. + my ( $caller, undef, $line ) = caller; + Warning("$0 Can't Delete event $event->{Id} from Monitor $event->{MonitorId} StartTime:". + (defined($event->{StartTime})?$event->{StartTime}:'undef')." from $caller:$line"); + return; + } + if ( !($event->Storage()->Path() and -e $event->Storage()->Path()) ) { + Warning('Not deleting event because storage path doesn\'t exist'); + return; + } + } + + if ( $$event{Id} ) { + # Need to have an event Id if we are to delete from the db. + Info("Deleting event $event->{Id} from Monitor $event->{MonitorId} StartTime:$event->{StartTime}"); + $ZoneMinder::Database::dbh->ping(); + + $ZoneMinder::Database::dbh->begin_work(); + #$event->lock_and_load(); + + ZoneMinder::Database::zmDbDo('DELETE FROM Frames WHERE EventId=?', $$event{Id}); + if ( $ZoneMinder::Database::dbh->errstr() ) { + $ZoneMinder::Database::dbh->commit(); + return; + } + ZoneMinder::Database::zmDbDo('DELETE FROM Stats WHERE EventId=?', $$event{Id}); if ( $ZoneMinder::Database::dbh->errstr() ) { $ZoneMinder::Database::dbh->commit(); return; } - $sql = 'DELETE FROM Stats WHERE EventId=?'; - $sth = $ZoneMinder::Database::dbh->prepare_cached($sql) - or Error("Can't prepare '$sql': ".$ZoneMinder::Database::dbh->errstr()); - $res = $sth->execute($event->{Id}) - or Error("Can't execute '$sql': ".$sth->errstr()); - $sth->finish(); - if ( $ZoneMinder::Database::dbh->errstr() ) { - $ZoneMinder::Database::dbh->commit(); - return; - } + # Do it individually to avoid locking up the table for new events + ZoneMinder::Database::zmDbDo('DELETE FROM Events WHERE Id=?', $$event{Id}); + $ZoneMinder::Database::dbh->commit(); } -# Do it individually to avoid locking up the table for new events - { - my $sql = 'DELETE FROM Events WHERE Id=?'; - my $sth = $ZoneMinder::Database::dbh->prepare_cached($sql) - or Error("Can't prepare '$sql': ".$ZoneMinder::Database::dbh->errstr()); - my $res = $sth->execute($event->{Id}) - or Error("Can't execute '$sql': ".$sth->errstr()); - $sth->finish(); - } - $ZoneMinder::Database::dbh->commit(); - if ( (! $Config{ZM_OPT_FAST_DELETE}) and $event->Storage()->DoDelete() ) { - $event->delete_files( ); + if ( ( $in_zmaudit or (!$Config{ZM_OPT_FAST_DELETE})) and $event->Storage()->DoDelete() ) { + $event->delete_files(); } else { Debug('Not deleting event files from '.$event->Path().' for speed.'); } @@ -434,11 +426,11 @@ sub delete { sub delete_files { my $event = shift; - my $Storage = @_ ? $_[0] : new ZoneMinder::Storage( $$event{StorageId} ); + my $Storage = @_ ? $_[0] : new ZoneMinder::Storage($$event{StorageId}); my $storage_path = $Storage->Path(); if ( ! $storage_path ) { - Error("Empty storage path when deleting files for event $$event{Id} with storage id $$event{StorageId} "); + Error("Empty storage path when deleting files for event $$event{Id} with storage id $$event{StorageId}"); return; } @@ -470,14 +462,14 @@ sub delete_files { if ( $bucket->delete_key($event_path) ) { $deleted = 1; } else { - Error("Failed to delete from S3:".$s3->err . ": " . $s3->errstr); + Error('Failed to delete from S3:'.$s3->err . ': ' . $s3->errstr); } }; Error($@) if $@; } - if ( ! $deleted ) { + if ( !$deleted ) { my $command = "/bin/rm -rf $storage_path/$event_path"; - ZoneMinder::General::executeShellCommand( $command ); + ZoneMinder::General::executeShellCommand($command); } } @@ -486,7 +478,7 @@ sub delete_files { Debug("Deleting link for Event $$event{Id} from $storage_path/$link_path."); if ( $link_path ) { ( $link_path ) = ( $link_path =~ /^(.*)$/ ); # De-taint - unlink($storage_path.'/'.$link_path) or Error( "Unable to unlink '$storage_path/$link_path': $!" ); + unlink($storage_path.'/'.$link_path) or Error("Unable to unlink '$storage_path/$link_path': $!"); } } } # end sub delete_files @@ -506,7 +498,7 @@ sub check_for_in_filesystem { if ( $path ) { if ( -e $path ) { my @files = glob "$path/*"; - Debug("Checking for files for event $_[0]{Id} at $path using glob $path/* found " . scalar @files . " files"); + Debug("Checking for files for event $_[0]{Id} at $path using glob $path/* found " . scalar @files . ' files'); return 1 if @files; } else { Warning("Path not found for Event $_[0]{Id} at $path"); @@ -577,7 +569,7 @@ sub MoveTo { if ( $$OldStorage{Id} != $$self{StorageId} ) { $ZoneMinder::Database::dbh->commit(); - return "Old Storage path changed, Event has moved somewhere else."; + return 'Old Storage path changed, Event has moved somewhere else.'; } $$self{Storage} = $NewStorage; @@ -621,11 +613,11 @@ Debug("Files to move @files"); Debug("Moving file $file to $NewPath"); my $size = -s $file; if ( ! $size ) { - Info("Not moving file with 0 size"); + Info('Not moving file with 0 size'); } my $file_contents = File::Slurp::read_file($file); if ( ! $file_contents ) { - die "Loaded empty file, but it had a size. Giving up"; + die 'Loaded empty file, but it had a size. Giving up'; } my $filename = $event_path.'/'.File::Basename::basename($file); @@ -633,7 +625,7 @@ Debug("Files to move @files"); die "Unable to add key for $filename"; } my $duration = time - $starttime; - Debug("PUT to S3 " . Number::Bytes::Human::format_bytes($size) . " in $duration seconds = " . Number::Bytes::Human::format_bytes($duration?$size/$duration:$size) . '/sec'); + Debug('PUT to S3 ' . Number::Bytes::Human::format_bytes($size) . " in $duration seconds = " . Number::Bytes::Human::format_bytes($duration?$size/$duration:$size) . '/sec'); } # end foreach file. $moved = 1; From ca87936cd78e427511e57408460045b72ae0fef7 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 14:13:41 -0500 Subject: [PATCH 37/37] add function time_of_youngest_file to determine the youngest file in an event dir, so as to guess the starttime of the event. --- scripts/zmaudit.pl.in | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index ebd56229e..7a5017fbd 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -384,7 +384,7 @@ MAIN: while( $loop ) { Debug("Checking for Medium Scheme Events under $$Storage{Path}/$monitor_dir"); { my @event_dirs = glob("$monitor_dir/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/*"); - Debug(qq`glob("$monitor_dir/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/*") returned ` . scalar @event_dirs . " entries." ); + Debug(qq`glob("$monitor_dir/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/*") returned ` . scalar @event_dirs . ' entries.' ); foreach my $event_dir ( @event_dirs ) { if ( ! -d $event_dir ) { Debug( "$event_dir is not a dir. Skipping" ); @@ -403,6 +403,7 @@ MAIN: while( $loop ) { $$Event{RelativePath} = $event_dir; $Event->MonitorId( $monitor_dir ); $Event->StorageId( $Storage->Id() ); + $Event->StartTime( POSIX::strftime('%Y-%m-%d %H:%M:%S', gmtime(time_of_youngest_file($$Event{Path})) ) ); } # end foreach event } @@ -428,7 +429,7 @@ MAIN: while( $loop ) { } # end foreach event chdir( $Storage->Path() ); } # if USE_DEEP_STORAGE - Debug( 'Got '.int(keys(%$fs_events))." filesystem events for monitor $monitor_dir\n" ); + Debug( 'Got '.int(keys(%$fs_events))." filesystem events for monitor $monitor_dir" ); delete_empty_subdirs($$Storage{Path}.'/'.$monitor_dir); } # end foreach monitor @@ -446,7 +447,7 @@ MAIN: while( $loop ) { next; } my @event_ids = keys %$fs_events; - Debug("Have " .scalar @event_ids . " events for monitor $monitor_id"); + Debug('Have ' .scalar @event_ids . " events for monitor $monitor_id"); foreach my $fs_event_id ( sort { $a <=> $b } keys %$fs_events ) { @@ -490,7 +491,11 @@ MAIN: while( $loop ) { } } } # end foreach Storage Area - redo MAIN if ( $cleaned ); + + if ( $cleaned ) { + Debug("Events were deleted, starting again."); + redo MAIN; + } $cleaned = 0; my $deleteMonitorSql = 'DELETE LOW_PRIORITY FROM Monitors WHERE Id = ?'; @@ -531,7 +536,7 @@ MAIN: while( $loop ) { next; } if ( ! $Event->StartTime() ) { - Info("Event $$Event{Id} has no start time. deleting it."); + Info("Event $$Event{Id} has no start time."); if ( confirm() ) { $Event->delete(); $cleaned = 1; @@ -577,10 +582,11 @@ MAIN: while( $loop ) { $Event->StorageId($$fs_events{$db_event}->StorageId()); } if ( $$fs_events{$db_event}->StartTime() ne $Event->StartTime() ) { + Info("Updating StartTime on event $$Event{Id} from $$Event{StartTime} to $$fs_events{$db_event}{StartTime}"); if ( $$Event{Scheme} eq 'Deep' ) { - Info("Updating StartTime on event $$Event{Id} from $$Event{StartTime} to $$fs_events{$db_event}{StartTime}"); $Event->StartTime($$fs_events{$db_event}->StartTime()); } else { + $Event->StartTime($$fs_events{$db_event}->StartTime()); } $Event->save(); } @@ -994,6 +1000,25 @@ sub delete_empty_directories { } } # end sub delete_empty_directories +sub time_of_youngest_file { + my $dir = shift; + + if ( ! opendir(DIR, $dir) ) { + Error("Can't open directory '$dir': $!"); + return; + } + my $youngest = (stat($dir))[9]; + Debug("stat of $dir is $youngest"); + foreach my $file ( readdir( DIR ) ) { + next if $file =~ /^\./; + $_ = (stat($dir))[9]; + $youngest = $_ if $_ and ( $_ < $youngest ); + #Debug("stat of $dir is $_ < $youngest"); + } + Debug("stat of $dir is $youngest"); + return $youngest; +} # end sub time_of_youngest_file + 1; __END__