Don't calc DiskSpace if it is already defined

This commit is contained in:
Isaac Connor 2017-11-23 07:29:05 -08:00
parent 9d6b417b60
commit 485567349d
2 changed files with 27 additions and 20 deletions

View File

@ -79,8 +79,9 @@ 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),
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),
@ -94,13 +95,13 @@ TotalEvents=(SELECT COUNT(Id) FROM Events WHERE MonitorId=Monitors.Id),
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 );
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} : () )
or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() ) {
my $now = time();
next if $monitor->{Function} eq 'None';
my $restart = 0;
if ( zmMemVerify( $monitor ) ) {
@ -192,6 +193,8 @@ 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
@ -199,10 +202,12 @@ while( 1 ) {
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( $diskspace_sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
or Fatal( "Can't prepare '$diskspace_sql': ".$dbh->errstr() );
foreach my $Storage ( ZoneMinder::Storage->find() ) {
Error("Updating disk space for $$Storage{Name}");
Debug("Updating disk space for $$Storage{Name} was $$Storage{DiskSpace}");
$diskspace_sth->execute( $$Storage{Id} ) or Error( "Can't execute: ".$diskspace_sth->errstr() );
$Storage->load();
Debug("Updated disk space for $$Storage{Name} to $$Storage{DiskSpace}");
}
$diskspace_sth->finish();
sleep( $Config{ZM_WATCH_CHECK_INTERVAL} );

View File

@ -110,23 +110,25 @@ class Storage {
}
public function disk_used_space() {
# This isn't a function like this in php, so we have to add up the space used in each event.
if ( ! array_key_exists( 'disk_used_space', $this ) ) {
$used = 0;
if ( $this->{'Type'} == 's3fs' ) {
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) );
if ( ! array_key_exists( 'DiskSpace', $this ) ) {
$used = $this->{'DiskSpace'};
if ( ! $used ) {
if ( $this->{'Type'} == 's3fs' ) {
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) );
foreach ( Event::find_all( array( 'StorageId'=>$this->Id(), 'DiskSpace'=>null ) ) as $Event ) {
$Event->Storage( $this ); // Prevent further db hit
$used += $Event->DiskSpace();
}
} else {
$path = $this->Path();
$used = disk_total_space( $path ) - disk_free_space( $path );;
}
$this->{'disk_used_space'} = $used;
foreach ( Event::find_all( array( 'StorageId'=>$this->Id(), 'DiskSpace'=>null ) ) as $Event ) {
$Event->Storage( $this ); // Prevent further db hit
$used += $Event->DiskSpace();
}
} else {
$path = $this->Path();
$used = disk_total_space( $path ) - disk_free_space( $path );;
}
} # end if
$this->{'DiskSpace'} = $used;
}
return $this->{'disk_used_space'};
return $this->{'DiskSpace'};
}
}
?>