From c98339e152e4b813b5459a9f140e2ccffb6c7ed5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 24 May 2018 10:49:20 -0400 Subject: [PATCH 1/7] Google code style, list monitor in debug line listing LastWriteTime --- scripts/zmwatch.pl.in | 67 ++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/scripts/zmwatch.pl.in b/scripts/zmwatch.pl.in index 61104f55f..b98b9d22a 100644 --- a/scripts/zmwatch.pl.in +++ b/scripts/zmwatch.pl.in @@ -69,41 +69,44 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; logInit(); logSetSignal(); -Info( "Watchdog starting, pausing for ".START_DELAY." seconds\n" ); -sleep( START_DELAY ); +Info('Watchdog starting, pausing for '.START_DELAY.' seconds'); +sleep(START_DELAY); my $dbh = zmDbConnect(); my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'SELECT * FROM Monitors'; -my $sth = $dbh->prepare_cached( $sql ) - or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); +my $sth = $dbh->prepare_cached($sql) + or Fatal("Can't prepare '$sql': ".$dbh->errstr()); while( 1 ) { my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () ) - or Fatal( "Can't execute: ".$sth->errstr() ); + or Fatal('Can\'t execute: '.$sth->errstr()); while( my $monitor = $sth->fetchrow_hashref() ) { - my $now = time(); next if $monitor->{Function} eq 'None'; next if $monitor->{Type} eq 'WebSite'; + my $now = time(); my $restart = 0; - if ( zmMemVerify( $monitor ) ) { + if ( zmMemVerify($monitor) ) { # Check we have got an image recently - my $capture_time = zmGetLastWriteTime( $monitor ); + my $capture_time = zmGetLastWriteTime($monitor); if ( !defined($capture_time) ) { # Can't read from shared data - Debug( "LastWriteTime is not defined." ); - zmMemInvalidate( $monitor ); + Debug('LastWriteTime is not defined.'); + zmMemInvalidate($monitor); next; } - Debug( "LastWriteTime is = $capture_time." ); + Debug("Monitor $$monitor{Id} LastWriteTime is $capture_time."); if ( !$capture_time ) { - my $startup_time = zmGetStartupTime( $monitor ); + my $startup_time = zmGetStartupTime($monitor); if ( $now - $startup_time > $Config{ZM_WATCH_MAX_DELAY} ) { - Info( "Restarting capture daemon for ".$monitor->{Name}.", no image since startup. Startup time was $startup_time - now $now > $Config{ZM_WATCH_MAX_DELAY}\n" ); + Info( + "Restarting capture daemon for $$monitor{Name}, no image since startup. ". + "Startup time was $startup_time - now $now > $Config{ZM_WATCH_MAX_DELAY}" + ); $restart = 1; } else { # We can't get the last capture time so can't be sure it's died, it might just be starting up. - zmMemInvalidate( $monitor ); + zmMemInvalidate($monitor); next; } } @@ -115,44 +118,44 @@ while( 1 ) { : $Config{ZM_WATCH_MAX_DELAY} ; my $image_delay = $now-$capture_time; - Debug( "Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay\n" ); + Debug("Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay"); if ( $image_delay > $max_image_delay ) { - Info( "Restarting capture daemon for " - .$monitor->{Name}.", time since last capture $image_delay seconds ($now-$capture_time)\n" + Info("Restarting capture daemon for " + .$monitor->{Name}.", time since last capture $image_delay seconds ($now-$capture_time)" ); $restart = 1; } } # end if ! restart } else { - Info( "Restarting capture daemon for ".$monitor->{Name}.", shared data not valid\n" ); + Info("Restarting capture daemon for $monitor->{Name}, shared data not valid"); $restart = 1; } if ( $restart ) { # Because zma depends on zmc, and zma can hold the shm in place, preventing zmc from using the space in /dev/shm, # we need to stop zma before restarting zmc. - runCommand( "zmdc.pl stop zma -m $$monitor{Id}" ) if $monitor->{Function} ne 'Monitor'; + runCommand("zmdc.pl stop zma -m $$monitor{Id}") if $monitor->{Function} ne 'Monitor'; my $command; if ( $monitor->{Type} eq 'Local' ) { $command = "zmdc.pl restart zmc -d $monitor->{Device}"; } else { $command = "zmdc.pl restart zmc -m $monitor->{Id}"; } - runCommand( $command ); - runCommand( "zmdc.pl start zma -m $$monitor{Id}" ) if $monitor->{Function} ne 'Monitor'; + runCommand($command); + runCommand("zmdc.pl start zma -m $$monitor{Id}") if $monitor->{Function} ne 'Monitor'; } elsif ( $monitor->{Function} ne 'Monitor' ) { # Now check analysis daemon $restart = 0; # Check we have got an image recently - my $image_time = zmGetLastReadTime( $monitor ); + my $image_time = zmGetLastReadTime($monitor); if ( !defined($image_time) ) { # Can't read from shared data $restart = 1; - Error("Error reading shared data for $$monitor{Id} $$monitor{Name}\n"); + Error("Error reading shared data for $$monitor{Id} $$monitor{Name}"); } elsif ( !$image_time ) { # We can't get the last capture time so can't be sure it's died. $restart = 1; - Error("Last analyse time for $$monitor{Id} $$monitor{Name} was zero.\n"); + Error("Last analyse time for $$monitor{Id} $$monitor{Name} was zero."); } else { my $max_image_delay = ( $monitor->{MaxFPS} @@ -162,29 +165,29 @@ while( 1 ) { : $Config{ZM_WATCH_MAX_DELAY} ; my $image_delay = $now-$image_time; - Debug( "Monitor $monitor->{Id} last analysed $image_delay seconds ago, max is $max_image_delay\n" ); + Debug("Monitor $monitor->{Id} last analysed $image_delay seconds ago, max is $max_image_delay"); if ( $image_delay > $max_image_delay ) { - Info( "Analysis daemon for $$monitor{Id} $$monitor{Name} needs restarting," - ." time since last analysis $image_delay seconds ($now-$image_time)\n" + Info("Analysis daemon for $$monitor{Id} $$monitor{Name} needs restarting," + ." time since last analysis $image_delay seconds ($now-$image_time)" ); $restart = 1; } } if ( $restart ) { - Info( "Restarting analysis daemon for $$monitor{Id} $$monitor{Name}\n"); + Info("Restarting analysis daemon for $$monitor{Id} $$monitor{Name}"); my $command = 'zmdc.pl restart zma -m '.$monitor->{Id}; - runCommand( $command ); + runCommand($command); } # end if restart } # end if check analysis daemon # Prevent open handles building up if we have connect to shared memory - zmMemInvalidate( $monitor ); # Close our file handle to the zmc process we are about to end + zmMemInvalidate($monitor); # Close our file handle to the zmc process we are about to end } # end foreach monitor - sleep( $Config{ZM_WATCH_CHECK_INTERVAL} ); + sleep($Config{ZM_WATCH_CHECK_INTERVAL}); } # end while (1) -Info( "Watchdog exiting\n" ); +Info("Watchdog exiting"); exit(); 1; From b7129578c1d8a24902d114212a040aecefb7819f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 24 May 2018 10:59:15 -0400 Subject: [PATCH 2/7] fix case where pre_event_count=0 and analysis_fps is set because there are no pre_event_buffers --- src/zm_monitor.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index e8e14b3c4..20f9c9421 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1397,9 +1397,12 @@ bool Monitor::Analyse() { int motion_score = last_motion_score; if ( !(image_count % (motion_frame_skip+1) ) ) { // Get new score. - motion_score = DetectMotion( *snap_image, zoneSet ); + motion_score = DetectMotion(*snap_image, zoneSet); - Debug( 3, "After motion detection, last_motion_score(%d), new motion score(%d)", last_motion_score, motion_score ); + Debug(3, + "After motion detection, last_motion_score(%d), new motion score(%d)", + last_motion_score, motion_score + ); // Why are we updating the last_motion_score too? last_motion_score = motion_score; } @@ -1542,7 +1545,7 @@ bool Monitor::Analyse() { } // end if ! event } if ( score ) { - if ( (state == IDLE || state == TAPE || state == PREALARM ) ) { + if ( state == IDLE || state == TAPE || state == PREALARM ) { if ( (!pre_event_count) || (Event::PreAlarmCount() >= alarm_frame_count) ) { Info("%s: %03d - Gone into alarm state %u > %u", name, image_count, Event::PreAlarmCount(), alarm_frame_count); @@ -1551,7 +1554,7 @@ bool Monitor::Analyse() { int pre_index; int pre_event_images = pre_event_count; - if ( analysis_fps ) { + if ( analysis_fps && pre_event_count ) { // If analysis fps is set, // compute the index for pre event images in the dedicated buffer pre_index = pre_event_buffer_count ? image_count%pre_event_buffer_count : 0; @@ -1564,7 +1567,7 @@ bool Monitor::Analyse() { pre_event_images--; } - event = new Event( this, *(pre_event_buffer[pre_index].timestamp), cause, noteSetMap ); + event = new Event(this, *(pre_event_buffer[pre_index].timestamp), cause, noteSetMap); } else { // If analysis fps is not set (analysis performed at capturing framerate), // compute the index for pre event images in the capturing buffer @@ -1581,7 +1584,7 @@ bool Monitor::Analyse() { pre_event_images--; } - event = new Event( this, *(image_buffer[pre_index].timestamp), cause, noteSetMap ); + event = new Event(this, *(image_buffer[pre_index].timestamp), cause, noteSetMap); } shared_data->last_event = event->Id(); // lets construct alarm cause. It will contain cause + names of zones alarmed From e831c1563d93e31575f354d1810287b25518e468 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 24 May 2018 10:59:26 -0400 Subject: [PATCH 3/7] pretty the code structure --- src/zm_event.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index f55f4f1a5..bc039a2f5 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -69,7 +69,8 @@ Event::Event( untimedEvent = true; start_time = now; } else if ( start_time.tv_sec > now.tv_sec ) { - Error("StartTime in the future %u.%u > %u.%u", + Error( + "StartTime in the future %u.%u > %u.%u", start_time.tv_sec, start_time.tv_usec, now.tv_sec, now.tv_usec ); start_time = now; From 2eb3c8ef6d2ffaa08575976cd4daedad0333209a Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 31 May 2018 10:24:54 -0400 Subject: [PATCH 4/7] use a stored prepared sth instead of re-preparing it --- scripts/ZoneMinder/lib/ZoneMinder/Logger.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index a455bf71c..aec308b65 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -544,8 +544,9 @@ sub logPrint { if ( $level <= $this->{databaseLevel} ) { if ( ( $this->{dbh} and $this->{dbh}->ping() ) or ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) { + my $sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, NULL )'; - $this->{sth} = $this->{dbh}->prepare_cached($sql); + $this->{sth} = $this->{dbh}->prepare_cached($sql) if ! $this->{sth}; if ( !$this->{sth} ) { $this->{databaseLevel} = NOLOG; Error("Can't prepare log entry '$sql': ".$this->{dbh}->errstr()); From 21e26c8c14fd72c855cfb164981621086be8aaa8 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 31 May 2018 10:25:16 -0400 Subject: [PATCH 5/7] Spacing and parenthesis and some debug lines --- scripts/zmdc.pl.in | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index 3ddf07e15..fee01658f 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -119,13 +119,13 @@ if ( $command eq 'version' ) { } my $needs_daemon = $command !~ /(?:startup|shutdown|status|check|logrot|version)/; my $daemon = shift @ARGV; -if ( $needs_daemon && ! $daemon ) { +if ( $needs_daemon && !$daemon ) { print(STDERR "No daemon given\n"); pod2usage(-exitstatus => -1); } my @args; -my $daemon_patt = '('.join( '|', @daemons ).')'; +my $daemon_patt = '('.join('|', @daemons).')'; if ( $needs_daemon ) { if ( $daemon =~ /^${daemon_patt}$/ ) { $daemon = $1; @@ -139,7 +139,7 @@ foreach my $arg ( @ARGV ) { # Detaint arguments, if they look ok #if ( $arg =~ /^(-{0,2}[\w]+)/ ) if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ ) { - push( @args, $1 ); + push @args, $1; } else { print(STDERR "Bogus argument '$arg' found"); exit(-1); @@ -306,7 +306,7 @@ sub run { $dbh = zmDbConnect(); } my @cpuload = CpuLoad(); - Debug("UPdating Server record @cpuload"); + Debug("Updating Server record @cpuload"); if ( ! defined $dbh->do(q{UPDATE Servers SET Status=?,CpuLoad=?,TotalMem=?,FreeMem=?,TotalSwap=?,FreeSwap=? WHERE Id=?}, undef, 'Running', $cpuload[0], &totalmem, &freemem, &totalswap, &freeswap, $Config{ZM_SERVER_ID} ) ) { Error("Failed Updating status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr()); @@ -382,7 +382,7 @@ Debug("check_for_processes_to_kill"); ."\n" ); if ( $Config{ZM_SERVER_ID} ) { - $dbh = zmDbConnect() if ! $dbh->ping(); + $dbh = zmDbConnect() if ! ($dbh and $dbh->ping()); if ( ! defined $dbh->do(q{UPDATE Servers SET Status='NotRunning' WHERE Id=?}, undef, $Config{ZM_SERVER_ID}) ) { Error("Failed Updating status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr()); } @@ -399,7 +399,9 @@ sub cPrint { # I think the purpose of this is to echo the logs to the client process so it can then display them. sub dPrint { my $logLevel = shift; + # One thought here, if no client exists to read these... does it block? if ( fileno(CLIENT) ) { + Debug("Have fileno for CLIENT, printing "); print CLIENT @_ } if ( $logLevel == ZoneMinder::Logger::DEBUG ) { @@ -439,8 +441,10 @@ sub start { sigprocmask(SIG_BLOCK, $blockset, $sigset) or Fatal("Can't block SIGCHLD: $!"); Debug("forking"); if ( my $cpid = fork() ) { + Debug("before logReinit"); # This logReinit is required. Not sure why. logReinit(); + Debug("aftere logReinit"); $process->{pid} = $cpid; $process->{started} = time(); @@ -808,7 +812,7 @@ sub status { foreach my $process ( values %cmd_hash ) { if ( $process->{pending} ) { dPrint(ZoneMinder::Logger::DEBUG, "'$process->{command}' pending at " - .strftime( '%y/%m/%d %H:%M:%S', localtime($process->{pending})) + .strftime('%y/%m/%d %H:%M:%S', localtime($process->{pending})) ."\n" ); } From 405b1f92edb2479266b690b38758160dbcc01fab Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 31 May 2018 10:25:53 -0400 Subject: [PATCH 6/7] add a .fail to log the errors given when a navbar ajax call fails --- web/skins/classic/js/skin.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index 7f960cc90..5da53d0d2 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -192,7 +192,13 @@ if ( currentView != 'none' ) { }); function getNavBar() { - $j.getJSON(thisUrl + '?view=request&request=status&entity=navBar', setNavBar); + $j.getJSON(thisUrl + '?view=request&request=status&entity=navBar') + .done(setNavBar) + .fail(function( jqxhr, textStatus, error ) { + var err = textStatus + ", " + error; + console.log( "Request Failed: " + err ); + window.location.href = thisUrl; + }); } function setNavBar(data) { From 87d9fc447ec0dffb5265aa222c194d27c70c4880 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 1 Jun 2018 11:27:53 -0400 Subject: [PATCH 7/7] google code style --- src/zms.cpp | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/zms.cpp b/src/zms.cpp index e8d20f72f..c785485da 100644 --- a/src/zms.cpp +++ b/src/zms.cpp @@ -74,45 +74,44 @@ int main( int argc, const char *argv[] ) { unsigned int playback_buffer = 0; bool nph = false; - const char *basename = strrchr( argv[0], '/' ); - if (basename) //if we found a / lets skip past it + const char *basename = strrchr(argv[0], '/'); + if ( basename ) //if we found a / lets skip past it basename++; else //argv[0] will not always contain the full path, but rather just the script name basename = argv[0]; const char *nph_prefix = "nph-"; - if ( basename && !strncmp( basename, nph_prefix, strlen(nph_prefix) ) ) { + if ( basename && !strncmp(basename, nph_prefix, strlen(nph_prefix)) ) { nph = true; } zmLoadConfig(); - - const char *query = getenv( "QUERY_STRING" ); + const char *query = getenv("QUERY_STRING"); if ( query ) { - Debug( 1, "Query: %s", query ); + Debug(1, "Query: %s", query); char temp_query[1024]; - strncpy( temp_query, query, sizeof(temp_query) ); + strncpy(temp_query, query, sizeof(temp_query)); char *q_ptr = temp_query; char *parms[16]; // Shouldn't be more than this int parm_no = 0; - while( (parm_no < 16) && (parms[parm_no] = strtok( q_ptr, "&" )) ) { + while( (parm_no < 16) && (parms[parm_no] = strtok(q_ptr, "&")) ) { parm_no++; q_ptr = NULL; } for ( int p = 0; p < parm_no; p++ ) { - char *name = strtok( parms[p], "=" ); - char *value = strtok( NULL, "=" ); + char *name = strtok(parms[p], "="); + char *value = strtok(NULL, "="); if ( !value ) value = (char *)""; - if ( !strcmp( name, "source" ) ) { - source = !strcmp( value, "event" )?ZMS_EVENT:ZMS_MONITOR; - } else if ( !strcmp( name, "mode" ) ) { - mode = !strcmp( value, "jpeg" )?ZMS_JPEG:ZMS_MPEG; - mode = !strcmp( value, "raw" )?ZMS_RAW:mode; - mode = !strcmp( value, "zip" )?ZMS_ZIP:mode; - mode = !strcmp( value, "single" )?ZMS_SINGLE:mode; + if ( !strcmp(name, "source") ) { + source = !strcmp(value, "event")?ZMS_EVENT:ZMS_MONITOR; + } else if ( !strcmp(name, "mode") ) { + mode = !strcmp(value, "jpeg")?ZMS_JPEG:ZMS_MPEG; + mode = !strcmp(value, "raw")?ZMS_RAW:mode; + mode = !strcmp(value, "zip")?ZMS_ZIP:mode; + mode = !strcmp(value, "single")?ZMS_SINGLE:mode; } else if ( !strcmp( name, "format" ) ) { strncpy( format, value, sizeof(format) ); } else if ( !strcmp( name, "monitor" ) ) { @@ -182,32 +181,32 @@ int main( int argc, const char *argv[] ) { if ( config.opt_use_auth ) { User *user = 0; - if ( strcmp( config.auth_relay, "none" ) == 0 ) { + if ( strcmp(config.auth_relay, "none") == 0 ) { if ( username.length() ) { - user = zmLoadUser( username.c_str() ); + user = zmLoadUser(username.c_str()); } } else { //if ( strcmp( config.auth_relay, "hashed" ) == 0 ) { if ( *auth ) { - user = zmLoadAuthUser( auth, config.auth_hash_ips ); + user = zmLoadAuthUser(auth, config.auth_hash_ips); } } //else if ( strcmp( config.auth_relay, "plain" ) == 0 ) { if ( username.length() && password.length() ) { - user = zmLoadUser( username.c_str(), password.c_str() ); + user = zmLoadUser(username.c_str(), password.c_str()); } } } if ( !user ) { - Error( "Unable to authenticate user" ); + Error("Unable to authenticate user"); logTerm(); zmDbClose(); - return( -1 ); + return -1; } - ValidateAccess( user, monitor_id ); - } + ValidateAccess(user, monitor_id); + } // end if config.opt_use_auth hwcaps_detect(); zmSetDefaultTermHandler();