From 8a5c989cac0da4eaccdc29c8f8e574221b632263 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 18 Sep 2018 08:52:43 -0400 Subject: [PATCH 1/5] fix warning when editing users after a monitor has been deleted --- web/skins/classic/views/options.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web/skins/classic/views/options.php b/web/skins/classic/views/options.php index 37d7c75c4..aafeaacb7 100644 --- a/web/skins/classic/views/options.php +++ b/web/skins/classic/views/options.php @@ -166,12 +166,15 @@ foreach ( array_map('basename', glob('skins/'.$current_skin.'/css/*',GLOB_ONLYDI $userMonitors = array(); if ( !empty($row['MonitorIds']) ) { foreach ( explode(',', $row['MonitorIds']) as $monitorId ) { + // A deleted monitor will cause an error since we don't update + // the user monitors list on monitor delete + if ( ! isset($monitors[$monitorId]) ) continue; $userMonitors[] = $monitors[$monitorId]['Name']; } } ?> - + From 3a9f96e8e85b478068d0b5f0e7cf724138ea7feb Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 19 Sep 2018 12:03:51 -0400 Subject: [PATCH 2/5] Skip deleting event if event is archived. And event not having a scheme is now an error --- scripts/zmaudit.pl.in | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index f0f534a5b..81e87fcca 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -363,7 +363,7 @@ MAIN: while( $loop ) { } if ( ! $$Storage{Scheme} ) { - Debug("Storage Scheme not set on $$Storage{Name}"); + Error("Storage Scheme not set on $$Storage{Name}"); if ( ! chdir( $monitor_dir ) ) { Error( "Can't chdir directory '$$Storage{Path}/$monitor_dir': $!" ); next; @@ -477,6 +477,10 @@ MAIN: while( $loop ) { 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() ) { From 284d622464f83ec7898501c97daeb2d848c45aa7 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Wed, 19 Sep 2018 12:06:09 -0500 Subject: [PATCH 3/5] update the way we access mapped mem in zm-alarm.pl --- utils/zm-alarm.pl | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/utils/zm-alarm.pl b/utils/zm-alarm.pl index 528328c61..d68c82663 100755 --- a/utils/zm-alarm.pl +++ b/utils/zm-alarm.pl @@ -14,9 +14,17 @@ $| = 1; my @monitors; my $dbh = zmDbConnect(); -my $sql = "SELECT * FROM Monitors"; -my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); -my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() ); + +my $sql = "SELECT * FROM Monitors + WHERE find_in_set( Function, 'Modect,Mocord,Nodect' )". + ( $Config{ZM_SERVER_ID} ? 'AND ServerId=?' : '' ) + ; + +my $sth = $dbh->prepare_cached( $sql ) + or die( "Can't prepare '$sql': ".$dbh->errstr() ); + +my $res = $sth->execute() + or die( "Can't execute '$sql': ".$sth->errstr() ); while ( my $monitor = $sth->fetchrow_hashref() ) { push( @monitors, $monitor ); @@ -24,6 +32,12 @@ while ( my $monitor = $sth->fetchrow_hashref() ) { while (1) { foreach my $monitor (@monitors) { + # Check shared memory ok + if ( !zmMemVerify( $monitor ) ) { + zmMemInvalidate( $monitor ); + next; + } + my $monitorState = zmGetMonitorState($monitor); printState($monitor->{Id}, $monitor->{Name}, $monitorState); } From c8b10b2e3b04c6c0bda7b007a7a0436e5fd6a442 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 19 Sep 2018 19:50:26 -0400 Subject: [PATCH 4/5] factor out some common code when creating the event directory and add a mkdir for creating the monitor directory. Should make event writing more robust when monitor dirs do not exist for some reason. --- src/zm_event.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index 11fff6a76..2ee5247d4 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -120,9 +120,15 @@ Event::Event( char id_file[PATH_MAX]; + char *path_ptr = path; + path_ptr += snprintf(path_ptr, sizeof(path), "%s/%d", storage->Path(), monitor->Id()); + // Try to make the Monitor Dir. Normally this would exist, but in odd cases might not. + if ( mkdir(path, 0755) ) { + if ( errno != EEXIST ) + Error("Can't mkdir %s: %s", path, strerror(errno)); + } + if ( storage->Scheme() == Storage::DEEP ) { - char *path_ptr = path; - path_ptr += snprintf(path_ptr, sizeof(path), "%s/%d", storage->Path(), monitor->Id()); int dt_parts[6]; dt_parts[0] = stime->tm_year-100; @@ -155,28 +161,24 @@ Event::Event( if ( symlink(time_path, id_file) < 0 ) Error("Can't symlink %s -> %s: %s", id_file, path, strerror(errno)); } else if ( storage->Scheme() == Storage::MEDIUM ) { - char *path_ptr = path; path_ptr += snprintf( - path_ptr, sizeof(path), "%s/%d/%04d-%02d-%02d", - storage->Path(), monitor->Id(), stime->tm_year+1900, stime->tm_mon+1, stime->tm_mday + path_ptr, sizeof(path), "/%04d-%02d-%02d", + stime->tm_year+1900, stime->tm_mon+1, stime->tm_mday ); if ( mkdir(path, 0755) ) { - // FIXME This should not be fatal. Should probably move to a different storage area. if ( errno != EEXIST ) Error("Can't mkdir %s: %s", path, strerror(errno)); } path_ptr += snprintf(path_ptr, sizeof(path), "/%" PRIu64, id); if ( mkdir(path, 0755) ) { - // FIXME This should not be fatal. Should probably move to a different storage area. if ( errno != EEXIST ) Error("Can't mkdir %s: %s", path, strerror(errno)); } } else { - snprintf(path, sizeof(path), "%s/%d/%" PRIu64, storage->Path(), monitor->Id(), id); + path_ptr += snprintf(path, sizeof(path), "/%" PRIu64, id); if ( mkdir(path, 0755) ) { - if ( errno != EEXIST ) { + if ( errno != EEXIST ) Error("Can't mkdir %s: %s", path, strerror(errno)); - } } // Create empty id tag file From 95bcac4c5acdcabd45ba2f550ca96e194b24eb40 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 19 Sep 2018 20:00:26 -0400 Subject: [PATCH 5/5] Implement delete_empty_subdirs that uses delete_empty_directories. The idea is to use it on the monitor dir so that the monitor dir gets left behind and not deleted when there are no events --- scripts/zmaudit.pl.in | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 81e87fcca..20b3f78bf 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -386,7 +386,7 @@ MAIN: while( $loop ) { } # if USE_DEEP_STORAGE Debug( 'Got '.int(keys(%$fs_events))." filesystem events for monitor $monitor_dir\n" ); - delete_empty_directories($$Storage{Path}.'/'.$monitor_dir); + delete_empty_subdirs($$Storage{Path}.'/'.$monitor_dir); } # end foreach monitor if ( $cleaned ) { @@ -885,6 +885,24 @@ sub deleteSwapImage { } } +# Deletes empty sub directories of the given path. +# Does not delete the path if empty. Is not meant to be recursive. +sub delete_empty_subdirs { + my $DIR; + if ( !opendir($DIR, $_[0]) ) { + Error("delete_empty_directories: Can't open directory '".getcwd()."/$_[0]': $!" ); + return; + } + my @contents = map { ( $_ eq '.' or $_ eq '..' ) ? () : $_ } readdir( $DIR ); + Debug("delete_empty_subdirectories $_[0] has " . @contents .' entries:' . ( @contents < 2 ? join(',',@contents) : '' )); + my @dirs = map { -d $_[0].'/'.$_ ? $_ : () } @contents; + Debug("Have " . @dirs . " dirs"); + foreach ( @dirs ) { + delete_empty_directories( $_[0].'/'.$_ ); + } + closedir($DIR); +} + sub delete_empty_directories { my $DIR; if ( !opendir($DIR, $_[0]) ) {