From d2dc23b4e98090803ed516b3c5e77c03c2b1fc80 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 16:35:09 -0500 Subject: [PATCH 1/7] fixes, better logging --- scripts/ZoneMinder/lib/ZoneMinder/Event.pm | 18 ++---------- scripts/zmrecover.pl.in | 33 ++++++++++++++++------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm index 07b005aa1..8c8bf4944 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm @@ -342,19 +342,6 @@ sub GenerateVideo { sub delete { my $event = $_[0]; -<<<<<<< HEAD - 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"); - 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}"); - $ZoneMinder::Database::dbh->ping(); -======= my $in_zmaudit = ( $0 =~ 'zmaudit.pl$'); @@ -371,7 +358,6 @@ sub delete { return; } } ->>>>>>> master if ( $$event{Id} ) { # Need to have an event Id if we are to delete from the db. @@ -680,9 +666,9 @@ sub recover_timestamps { my ( $Event, $path ) = @_; $path = $Event->Path() if ! $path; - if ( ! opendir(DIR, $path) ) { + if ( !opendir(DIR, $path) ) { Error("Can't open directory '$path': $!"); - next; + return; } my @contents = readdir(DIR); Debug('Have ' . @contents . " files in $path"); diff --git a/scripts/zmrecover.pl.in b/scripts/zmrecover.pl.in index 6b8f3831f..56ee724fb 100644 --- a/scripts/zmrecover.pl.in +++ b/scripts/zmrecover.pl.in @@ -231,9 +231,12 @@ Debug("@Monitors"); $Event->Height( $Monitor->Height() ); $Event->Orientation( $Monitor->Orientation() ); $Event->recover_timestamps(); - - $Event->save({}, 1); - Info("Event resurrected as " . $Event->to_string() ); + if ( $$Event{StartTime} ) { + $Event->save({}, 1); + Info("Event resurrected as " . $Event->to_string() ); + } else { + Warning("Unable to determine starttime. Not resurrecting this event."); + } next; } # end if resurrection } # event path exists @@ -291,8 +294,12 @@ Debug("@Monitors"); $Event->StorageId( $Storage->Id() ); $Event->DiskSpace( undef ); $Event->recover_timestamps(); - $Event->save({}, 1); - Debug("Event resurrected as " . $Event->to_string() ); + if ( $$Event{StartTime} ) { + $Event->save({}, 1); + Info("Event resurrected as " . $Event->to_string() ); + } else { + Warning("Unable to determine starttime. Not resurrecting this event."); + } next; } } # end if event found @@ -351,8 +358,12 @@ Debug("@Monitors"); $Event->Orientation( $Monitor->Orientation() ); $Event->StorageId( $Storage->Id() ); $Event->recover_timestamps(); - $Event->save({}, 1); - Info("Event resurrected as " . $Event->to_string() ); + if ( $$Event{StartTime} ) { + $Event->save({}, 1); + Info("Event resurrected as " . $Event->to_string() ); + } else { + Warning("Unable to determine starttime. Not resurrecting this event."); + } } } # end foreach event } # end search for Medium @@ -389,8 +400,12 @@ Debug("@Monitors"); $Event->Orientation( $Monitor->Orientation() ); $Event->StorageId( $Storage->Id() ); $Event->recover_timestamps(); - $Event->save({}, 1); - Debug("Event resurrected as " . $Event->to_string() ); + if ( $$Event{StartTime} ) { + $Event->save({}, 1); + Info("Event resurrected as " . $Event->to_string() ); + } else { + Warning("Unable to determine starttime. Not resurrecting this event."); + } } } # end foreach event chdir( $Storage->Path() ); From 381071eb0f880a9d90425d266723d6ae54241f01 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 16:36:39 -0500 Subject: [PATCH 2/7] Fix log message to say recovering instead of auditing --- scripts/zmrecover.pl.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/zmrecover.pl.in b/scripts/zmrecover.pl.in index 56ee724fb..02083d757 100644 --- a/scripts/zmrecover.pl.in +++ b/scripts/zmrecover.pl.in @@ -120,7 +120,7 @@ if ( defined $storage_id ) { Error("No Storage Area found with Id $storage_id"); Term(); } - Info("Auditing Storage Area $Storage_Areas[0]{Id} $Storage_Areas[0]{Name} at $Storage_Areas[0]{Path}"); + Info("Recovering from Storage Area $Storage_Areas[0]{Id} $Storage_Areas[0]{Name} at $Storage_Areas[0]{Path}"); } elsif ( $server_id ) { @Storage_Areas = ZoneMinder::Storage->find( ServerId => $server_id ); if ( ! @Storage_Areas ) { @@ -128,11 +128,11 @@ if ( defined $storage_id ) { Term(); } foreach my $Storage ( @Storage_Areas ) { - Info('Auditing ' . $Storage->Name() . ' at ' . $Storage->Path() . ' on ' . $Storage->Server()->Name() ); + Info('Recovering from ' . $Storage->Name() . ' at ' . $Storage->Path() . ' on ' . $Storage->Server()->Name() ); } } else { @Storage_Areas = ZoneMinder::Storage->find(); - Info("Auditing All Storage Areas"); + Info("Recovering from All Storage Areas"); } my @Monitors = ZoneMinder::Monitor->find(); From 5120c6f540a06c7c57ee77bad775c92ced7b466e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 13 Nov 2018 18:13:55 -0500 Subject: [PATCH 3/7] remove compile warning by assigning AV_CODEC_NONE to the codec_id. --- src/zm_rtsp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zm_rtsp.cpp b/src/zm_rtsp.cpp index 897859435..703328e2e 100644 --- a/src/zm_rtsp.cpp +++ b/src/zm_rtsp.cpp @@ -388,7 +388,7 @@ int RtspThread::run() { std::string trackUrl = mUrl; std::string controlUrl; - _AVCODECID codecId; + _AVCODECID codecId = AV_CODEC_ID_NONE; if ( mFormatContext->nb_streams >= 1 ) { for ( unsigned int i = 0; i < mFormatContext->nb_streams; i++ ) { From d671761a35262f1e326665e881f48fbe746ade66 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 14 Nov 2018 12:54:10 -0500 Subject: [PATCH 4/7] simplify params to daemonControl since they really aren't being used anyways. Return the status text --- web/api/app/Controller/MonitorsController.php | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index 185a06c84..9d5631043 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -182,6 +182,9 @@ class MonitorsController extends AppController { ($Monitor['ServerId']==ZM_SERVER_ID) ) ) { + if ( !defined('ZM_SERVER_ID')) { + Logger::Debug("Not defined ZM_SERVER_ID"); + } $this->daemonControl($this->Monitor->id, 'start'); } } else { @@ -349,17 +352,15 @@ class MonitorsController extends AppController { )); } - public function daemonControl($id, $command, $monitor=null, $daemon=null) { + public function daemonControl($id, $command) { $daemons = array(); - if ( !$monitor ) { - // Need to see if it is local or remote - $monitor = $this->Monitor->find('first', array( - 'fields' => array('Type', 'Function', 'Device'), - 'conditions' => array('Id' => $id) - )); - $monitor = $monitor['Monitor']; - } + // Need to see if it is local or remote + $monitor = $this->Monitor->find('first', array( + 'fields' => array('Type', 'Function', 'Device'), + 'conditions' => array('Id' => $id) + )); + $monitor = $monitor['Monitor']; if ( $monitor['Function'] == 'Monitor' ) { array_push($daemons, 'zmc'); @@ -369,6 +370,7 @@ class MonitorsController extends AppController { $zm_path_bin = Configure::read('ZM_PATH_BIN'); + $status_text = ''; foreach ( $daemons as $daemon ) { $args = ''; if ( $daemon == 'zmc' and $monitor['Type'] == 'Local' ) { @@ -378,7 +380,14 @@ class MonitorsController extends AppController { } $shellcmd = escapeshellcmd("$zm_path_bin/zmdc.pl $command $daemon $args"); - $status = exec( $shellcmd ); + Logger::Debug("Command $shellcmd"); + $status = exec($shellcmd); + $status_text .= $status."\n"; } - } + $this->set(array( + 'status' => 'ok', + 'statustext' => $status_text, + '_serialize' => array('status','statustext'), + )); + } // end function daemonControl } // end class MonitorsController From 9d8f0fef0c420c90055e13e20e0044c46259dd35 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 14 Nov 2018 12:54:40 -0500 Subject: [PATCH 5/7] add templates for daemonControl to api --- web/api/app/View/Monitors/json/daemon_control.ctp | 1 + web/api/app/View/Monitors/xml/daemon_control.ctp | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 web/api/app/View/Monitors/json/daemon_control.ctp create mode 100644 web/api/app/View/Monitors/xml/daemon_control.ctp diff --git a/web/api/app/View/Monitors/json/daemon_control.ctp b/web/api/app/View/Monitors/json/daemon_control.ctp new file mode 100644 index 000000000..4dbb0a055 --- /dev/null +++ b/web/api/app/View/Monitors/json/daemon_control.ctp @@ -0,0 +1 @@ +echo json_encode($status_text); diff --git a/web/api/app/View/Monitors/xml/daemon_control.ctp b/web/api/app/View/Monitors/xml/daemon_control.ctp new file mode 100644 index 000000000..8e7d3a290 --- /dev/null +++ b/web/api/app/View/Monitors/xml/daemon_control.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $status_text)); +echo $xml->asXML(); From 51d8c0ea73932e7ee3bf037555d9e09b68d14a5b Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 14 Nov 2018 12:59:44 -0500 Subject: [PATCH 6/7] add back daemon parameter, but make it actually work --- web/api/app/Controller/MonitorsController.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index 9d5631043..69799a820 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -352,8 +352,7 @@ class MonitorsController extends AppController { )); } - public function daemonControl($id, $command) { - $daemons = array(); + public function daemonControl($id, $command, $daemon=null) { // Need to see if it is local or remote $monitor = $this->Monitor->find('first', array( @@ -362,10 +361,14 @@ class MonitorsController extends AppController { )); $monitor = $monitor['Monitor']; - if ( $monitor['Function'] == 'Monitor' ) { - array_push($daemons, 'zmc'); + if ( ! $daemon ) { + if ( $monitor['Function'] == 'Monitor' ) { + array_push($daemons, 'zmc'); + } else { + array_push($daemons, 'zmc', 'zma'); + } } else { - array_push($daemons, 'zmc', 'zma'); + array_push($daemons, $daemon); } $zm_path_bin = Configure::read('ZM_PATH_BIN'); From 786ca5b22a404f6377675349ea3893bf91618b8e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 14 Nov 2018 13:00:19 -0500 Subject: [PATCH 7/7] implement remove service restart for zma. Use daemonControl instead of saving the monitor when restarting zmc --- web/includes/Monitor.php | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index e5b44682c..977c14993 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -460,7 +460,7 @@ private $control_fields = array( } else if ( $this->ServerId() ) { $Server = $this->Server(); - $url = ZM_BASE_PROTOCOL . '://'.$Server->Hostname().'/zm/api/monitors/'.$this->{'Id'}.'.json'; + $url = ZM_BASE_PROTOCOL . '://'.$Server->Hostname().'/zm/api/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json'; if ( ZM_OPT_USE_AUTH ) { if ( ZM_AUTH_RELAY == 'hashed' ) { $url .= '?auth='.generateAuthHash( ZM_AUTH_HASH_IPS ); @@ -472,17 +472,8 @@ private $control_fields = array( } } Logger::Debug("sending command to $url"); - $data = array('Monitor[Function]' => $this->{'Function'} ); - // use key 'http' even if you send the request to https://... - $options = array( - 'http' => array( - 'header' => "Content-type: application/x-www-form-urlencoded\r\n", - 'method' => 'POST', - 'content' => http_build_query($data) - ) - ); - $context = stream_context_create($options); + $context = stream_context_create(); try { $result = file_get_contents($url, false, $context); if ($result === FALSE) { /* Handle error */ @@ -518,6 +509,33 @@ private $control_fields = array( daemonControl( 'reload', 'zma', '-m '.$this->{'Id'} ); } } + } else if ( $this->ServerId() ) { + $Server = $this->Server(); + + $url = ZM_BASE_PROTOCOL . '://'.$Server->Hostname().'/zm/api/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zma.json'; + if ( ZM_OPT_USE_AUTH ) { + if ( ZM_AUTH_RELAY == 'hashed' ) { + $url .= '?auth='.generateAuthHash( ZM_AUTH_HASH_IPS ); + } elseif ( ZM_AUTH_RELAY == 'plain' ) { + $url = '?user='.$_SESSION['username']; + $url = '?pass='.$_SESSION['password']; + } elseif ( ZM_AUTH_RELAY == 'none' ) { + $url = '?user='.$_SESSION['username']; + } + } + Logger::Debug("sending command to $url"); + + $context = stream_context_create(); + try { + $result = file_get_contents($url, false, $context); + if ($result === FALSE) { /* Handle error */ + Error("Error restarting zma using $url"); + } + } catch ( Exception $e ) { + Error("Except $e thrown trying to restart zma"); + } + } else { + Error("Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor."); } // end if we are on the recording server } // end public function zmaControl