From 9d6b417b60c2952480be56689943cb0de8ee7b5e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 22 Nov 2017 17:03:37 -0800 Subject: [PATCH] make zmwatch.pl update columns in the Monitors table for Event And DiskSpace counts --- db/zm_create.sql.in | 2 ++ db/zm_update-1.31.14.sql | 26 +++++++++++++++++++ scripts/zmwatch.pl.in | 23 ++++++++++++++++- web/skins/classic/views/console.php | 40 +++++++++++++---------------- 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index 7a394c7be..46e0dad63 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -444,6 +444,8 @@ CREATE TABLE `Monitors` ( `WeekEventDiskSpace` bigint unsigned, `MonthEvents` int(10) unsigned, `MonthEventDiskSpace` bigint unsigned, + `ArchivedEvents` int(10) unsigned, + `ArchivedEventDiskSpace` bigint unsigned, PRIMARY KEY (`Id`) ) ENGINE=@ZM_MYSQL_ENGINE@; diff --git a/db/zm_update-1.31.14.sql b/db/zm_update-1.31.14.sql index 859b28162..29e0f4a42 100644 --- a/db/zm_update-1.31.14.sql +++ b/db/zm_update-1.31.14.sql @@ -96,6 +96,7 @@ SET @s = (SELECT IF( PREPARE stmt FROM @s; EXECUTE stmt; + SET @s = (SELECT IF( (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() AND table_name = 'Monitors' @@ -120,3 +121,28 @@ SET @s = (SELECT IF( PREPARE stmt FROM @s; EXECUTE stmt; + +SET @s = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() + AND table_name = 'Monitors' + AND column_name = 'ArchivedEvents' + ) > 0, +"SELECT 'Column ArchivedEvents already exists in Monitors'", +"ALTER TABLE `Monitors` ADD `ArchivedEvents` INT(10) AFTER `MonthEvents`" +)); + + +PREPARE stmt FROM @s; +EXECUTE stmt; + +SET @s = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() + AND table_name = 'Monitors' + AND column_name = 'ArchivedEventDiskSpace' + ) > 0, +"SELECT 'Column ArchivedEventDiskSpace already exists in Monitors'", +"ALTER TABLE `Monitors` ADD `ArchivedEventDiskSpace` BIGINT AFTER `ArchivedEvents`" +)); + +PREPARE stmt FROM @s; +EXECUTE stmt; diff --git a/scripts/zmwatch.pl.in b/scripts/zmwatch.pl.in index c1a2d78e7..575dee560 100644 --- a/scripts/zmwatch.pl.in +++ b/scripts/zmwatch.pl.in @@ -79,6 +79,23 @@ my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'S my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $eventcounts_sql = q`UPDATE Monitors SET +TotalEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id), + TotalEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND DiskSpace IS NOT NULL), + HourEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB( NOW(), INTERVAL 1 hour) ), + HourEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 hour) AND DiskSpace IS NOT NULL), + DayEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day)), + DayEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 day) AND DiskSpace IS NOT NULL), + WeekEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week)), + WeekEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 week) AND DiskSpace IS NOT NULL), + MonthEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB( NOW(), INTERVAL 1 month)), + MonthEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND StartTime > DATE_SUB(NOW(), INTERVAL 1 month) AND DiskSpace IS NOT NULL), + ArchivedEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id AND Archived=1), + ArchivedEventDiskSpace=(SELECT SUM(DiskSpace) FROM Events WHERE MonitorId=Monitors.Id AND Archived=1 AND DiskSpace IS NOT NULL) + WHERE Id=?`; + + my $eventcounts_sth = $dbh->prepare_cached( $eventcounts_sql ); + while( 1 ) { my $now = time(); my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () ) @@ -175,9 +192,13 @@ while( 1 ) { } # 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 + $eventcounts_sth->execute( $$monitor{Id} ) or Error( "Can't execute: ".$eventcounts_sth->errstr() ); + } # end foreach monitor + $eventcounts_sth->finish(); + my $diskspace_sql = 'UPDATE Storage SET DiskSpace =(SELECT SUM(DiskSpace) FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL)'; - my $diskspace_sth = $dbh->prepare_cached( $sql ) + my $diskspace_sth = $dbh->prepare_cached( $diskspace_sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); foreach my $Storage ( ZoneMinder::Storage->find() ) { Error("Updating disk space for $$Storage{Name}"); diff --git a/web/skins/classic/views/console.php b/web/skins/classic/views/console.php index 126421eb0..570f44112 100644 --- a/web/skins/classic/views/console.php +++ b/web/skins/classic/views/console.php @@ -22,7 +22,7 @@ if ( $running == null ) $running = daemonCheck(); $eventCounts = array( - array( +'Total'=> array( 'title' => translate('Events'), 'filter' => array( 'Query' => array( @@ -31,7 +31,7 @@ $eventCounts = array( ), 'total' => 0, ), - array( + 'Hour'=>array( 'title' => translate('Hour'), 'filter' => array( 'Query' => array( @@ -42,7 +42,7 @@ $eventCounts = array( ), 'total' => 0, ), - array( + 'Day'=>array( 'title' => translate('Day'), 'filter' => array( 'Query' => array( @@ -53,7 +53,7 @@ $eventCounts = array( ), 'total' => 0, ), - array( + 'Week'=>array( 'title' => translate('Week'), 'filter' => array( 'Query' => array( @@ -64,7 +64,7 @@ $eventCounts = array( ), 'total' => 0, ), - array( + 'Month'=>array( 'title' => translate('Month'), 'filter' => array( 'Query' => array( @@ -75,7 +75,7 @@ $eventCounts = array( ), 'total' => 0, ), - array( + 'Archived'=>array( 'title' => translate('Archived'), 'filter' => array( 'Query' => array( @@ -113,22 +113,16 @@ for ( $i = 0; $i < count($displayMonitors); $i++ ) { $zoneCount += $monitor['ZoneCount']; $counts = array(); - for ( $j = 0; $j < count($eventCounts); $j += 1 ) { + foreach ( array_keys( $eventCounts ) as $j ) { $filter = addFilterTerm( $eventCounts[$j]['filter'], count($eventCounts[$j]['filter']['Query']['terms']), array( 'cnj' => 'and', 'attr' => 'MonitorId', 'op' => '=', 'val' => $monitor['Id'] ) ); parseFilter( $filter ); - $counts[] = 'count(if(1'.$filter['sql'].",1,NULL)) AS EventCount$j, SUM(if(1".$filter['sql'].",DiskSpace,NULL)) As DiskSpace$j"; + #$counts[] = 'count(if(1'.$filter['sql'].",1,NULL)) AS EventCount$j, SUM(if(1".$filter['sql'].",DiskSpace,NULL)) As DiskSpace$j"; $monitor['eventCounts'][$j]['filter'] = $filter; - } - $sql = 'SELECT '.join($counts,', ').' FROM Events as E where MonitorId = ?'; - $counts = dbFetchOne( $sql, NULL, array($monitor['Id']) ); - if ( $counts ) - $monitor = array_merge( $monitor, $counts ); - for ( $j = 0; $j < count($eventCounts); $j += 1 ) { - $eventCounts[$j]['total'] += $monitor['EventCount'.$j]; + $eventCounts[$j]['total'] = $monitor[$j.'Events']; } unset($monitor); } // end foreach display monitor @@ -169,9 +163,11 @@ xhtmlHeaders( __FILE__, translate('Console') ); - - - + + + + + disabled="disabled"/> @@ -242,10 +238,10 @@ for( $monitor_i = 0; $monitor_i < count($displayMonitors); $monitor_i += 1 ) { ' . human_filesize($monitor['DiskSpace'.$i]), canView( 'Events' ) ) ?> + $monitor[$i.'Events'] . '
' . human_filesize($monitor[$i.'EventDiskSpace']), canView( 'Events' ) ) ?> @@ -276,8 +272,8 @@ for( $monitor_i = 0; $monitor_i < count($displayMonitors); $monitor_i += 1 ) {