make zmwatch.pl update columns in the Monitors table for Event And DiskSpace counts

This commit is contained in:
Isaac Connor 2017-11-22 17:03:37 -08:00
parent 1fe421ed9d
commit 9d6b417b60
4 changed files with 68 additions and 23 deletions

View File

@ -444,6 +444,8 @@ CREATE TABLE `Monitors` (
`WeekEventDiskSpace` bigint unsigned, `WeekEventDiskSpace` bigint unsigned,
`MonthEvents` int(10) unsigned, `MonthEvents` int(10) unsigned,
`MonthEventDiskSpace` bigint unsigned, `MonthEventDiskSpace` bigint unsigned,
`ArchivedEvents` int(10) unsigned,
`ArchivedEventDiskSpace` bigint unsigned,
PRIMARY KEY (`Id`) PRIMARY KEY (`Id`)
) ENGINE=@ZM_MYSQL_ENGINE@; ) ENGINE=@ZM_MYSQL_ENGINE@;

View File

@ -96,6 +96,7 @@ SET @s = (SELECT IF(
PREPARE stmt FROM @s; PREPARE stmt FROM @s;
EXECUTE stmt; EXECUTE stmt;
SET @s = (SELECT IF( SET @s = (SELECT IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE()
AND table_name = 'Monitors' AND table_name = 'Monitors'
@ -120,3 +121,28 @@ SET @s = (SELECT IF(
PREPARE stmt FROM @s; PREPARE stmt FROM @s;
EXECUTE stmt; 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;

View File

@ -79,6 +79,23 @@ my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'S
my $sth = $dbh->prepare_cached( $sql ) my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); 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 ) { while( 1 ) {
my $now = time(); my $now = time();
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () ) my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
@ -175,9 +192,13 @@ while( 1 ) {
} # end if check analysis daemon } # end if check analysis daemon
# Prevent open handles building up if we have connect to shared memory # 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
$eventcounts_sth->execute( $$monitor{Id} ) or Error( "Can't execute: ".$eventcounts_sth->errstr() );
} # end foreach monitor } # 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_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() ); or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
foreach my $Storage ( ZoneMinder::Storage->find() ) { foreach my $Storage ( ZoneMinder::Storage->find() ) {
Error("Updating disk space for $$Storage{Name}"); Error("Updating disk space for $$Storage{Name}");

View File

@ -22,7 +22,7 @@ if ( $running == null )
$running = daemonCheck(); $running = daemonCheck();
$eventCounts = array( $eventCounts = array(
array( 'Total'=> array(
'title' => translate('Events'), 'title' => translate('Events'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -31,7 +31,7 @@ $eventCounts = array(
), ),
'total' => 0, 'total' => 0,
), ),
array( 'Hour'=>array(
'title' => translate('Hour'), 'title' => translate('Hour'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -42,7 +42,7 @@ $eventCounts = array(
), ),
'total' => 0, 'total' => 0,
), ),
array( 'Day'=>array(
'title' => translate('Day'), 'title' => translate('Day'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -53,7 +53,7 @@ $eventCounts = array(
), ),
'total' => 0, 'total' => 0,
), ),
array( 'Week'=>array(
'title' => translate('Week'), 'title' => translate('Week'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -64,7 +64,7 @@ $eventCounts = array(
), ),
'total' => 0, 'total' => 0,
), ),
array( 'Month'=>array(
'title' => translate('Month'), 'title' => translate('Month'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -75,7 +75,7 @@ $eventCounts = array(
), ),
'total' => 0, 'total' => 0,
), ),
array( 'Archived'=>array(
'title' => translate('Archived'), 'title' => translate('Archived'),
'filter' => array( 'filter' => array(
'Query' => array( 'Query' => array(
@ -113,22 +113,16 @@ for ( $i = 0; $i < count($displayMonitors); $i++ ) {
$zoneCount += $monitor['ZoneCount']; $zoneCount += $monitor['ZoneCount'];
$counts = array(); $counts = array();
for ( $j = 0; $j < count($eventCounts); $j += 1 ) { foreach ( array_keys( $eventCounts ) as $j ) {
$filter = addFilterTerm( $filter = addFilterTerm(
$eventCounts[$j]['filter'], $eventCounts[$j]['filter'],
count($eventCounts[$j]['filter']['Query']['terms']), count($eventCounts[$j]['filter']['Query']['terms']),
array( 'cnj' => 'and', 'attr' => 'MonitorId', 'op' => '=', 'val' => $monitor['Id'] ) array( 'cnj' => 'and', 'attr' => 'MonitorId', 'op' => '=', 'val' => $monitor['Id'] )
); );
parseFilter( $filter ); 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; $monitor['eventCounts'][$j]['filter'] = $filter;
} $eventCounts[$j]['total'] = $monitor[$j.'Events'];
$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];
} }
unset($monitor); unset($monitor);
} // end foreach display monitor } // end foreach display monitor
@ -169,9 +163,11 @@ xhtmlHeaders( __FILE__, translate('Console') );
<?php if ( $show_storage_areas ) { ?> <?php if ( $show_storage_areas ) { ?>
<th class="colStorage"><?php echo translate('Storage') ?></th> <th class="colStorage"><?php echo translate('Storage') ?></th>
<?php } ?> <?php } ?>
<?php for ( $i = 0; $i < count($eventCounts); $i++ ) { ?> <th class="colEvents"><?php echo 'Total' ?></th>
<th class="colEvents"><?php echo $eventCounts[$i]['title'] ?></th> <th class="colEvents"><?php echo 'Hour' ?></th>
<?php } ?> <th class="colEvents"><?php echo 'Day' ?></th>
<th class="colEvents"><?php echo 'Week' ?></th>
<th class="colEvents"><?php echo 'Month' ?></th>
<th class="colZones"><a href="<?php echo $_SERVER['PHP_SELF'] ?>?view=zones_overview"><?php echo translate('Zones') ?></a></th> <th class="colZones"><a href="<?php echo $_SERVER['PHP_SELF'] ?>?view=zones_overview"><?php echo translate('Zones') ?></a></th>
<?php if ( canEdit('Monitors') ) { ?> <?php if ( canEdit('Monitors') ) { ?>
<th class="colMark"><input type="checkbox" name="toggleCheck" value="1" onclick="toggleCheckbox( this, 'markMids[]' );"<?php if ( !canEdit( 'Monitors' ) ) { ?> disabled="disabled"<?php } ?>/> <?php echo translate('All') ?></th> <th class="colMark"><input type="checkbox" name="toggleCheck" value="1" onclick="toggleCheckbox( this, 'markMids[]' );"<?php if ( !canEdit( 'Monitors' ) ) { ?> disabled="disabled"<?php } ?>/> <?php echo translate('All') ?></th>
@ -242,10 +238,10 @@ for( $monitor_i = 0; $monitor_i < count($displayMonitors); $monitor_i += 1 ) {
<?php <?php
} }
for ( $i = 0; $i < count($eventCounts); $i++ ) { foreach ( array('Total','Hour','Day','Week','Month') as $i ) {
?> ?>
<td class="colEvents"><?php echo makePopupLink( '?view='.ZM_WEB_EVENTS_VIEW.'&amp;page=1'.$monitor['eventCounts'][$i]['filter']['query'], $eventsWindow, ZM_WEB_EVENTS_VIEW, <td class="colEvents"><?php echo makePopupLink( '?view='.ZM_WEB_EVENTS_VIEW.'&amp;page=1'.$monitor['eventCounts'][$i]['filter']['query'], $eventsWindow, ZM_WEB_EVENTS_VIEW,
$monitor['EventCount'.$i] . '<br/>' . human_filesize($monitor['DiskSpace'.$i]), canView( 'Events' ) ) ?></td> $monitor[$i.'Events'] . '<br/>' . human_filesize($monitor[$i.'EventDiskSpace']), canView( 'Events' ) ) ?></td>
<?php <?php
} }
?> ?>
@ -276,7 +272,7 @@ for( $monitor_i = 0; $monitor_i < count($displayMonitors); $monitor_i += 1 ) {
<input type="button" name="deleteBtn" value="<?php echo translate('Delete') ?>" onclick="deleteMonitor( this )" disabled="disabled"/> <input type="button" name="deleteBtn" value="<?php echo translate('Delete') ?>" onclick="deleteMonitor( this )" disabled="disabled"/>
</td> </td>
<?php <?php
for ( $i = 0; $i < count($eventCounts); $i++ ) { foreach ( array('Total','Hour','Day','Week','Month') as $i ) {
parseFilter( $eventCounts[$i]['filter'] ); parseFilter( $eventCounts[$i]['filter'] );
?> ?>
<td class="colEvents"><?php echo makePopupLink( '?view='.ZM_WEB_EVENTS_VIEW.'&amp;page=1'.$eventCounts[$i]['filter']['query'], $eventsWindow, ZM_WEB_EVENTS_VIEW, $eventCounts[$i]['total'], canView( 'Events' ) ) ?></td> <td class="colEvents"><?php echo makePopupLink( '?view='.ZM_WEB_EVENTS_VIEW.'&amp;page=1'.$eventCounts[$i]['filter']['query'], $eventsWindow, ZM_WEB_EVENTS_VIEW, $eventCounts[$i]['total'], canView( 'Events' ) ) ?></td>