Merge branch 'storageareas' into record_audio

This commit is contained in:
Isaac Connor 2016-09-20 12:38:47 -04:00
commit bf45f489f7
7 changed files with 167 additions and 164 deletions

View File

@ -28,29 +28,13 @@ use 5.006;
use strict;
use warnings;
require Exporter;
require ZoneMinder::Base;
require ZoneMinder::Object;
require ZoneMinder::Storage;
require ZoneMinder::Server;
our @ISA = qw(Exporter ZoneMinder::Base);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = (
'functions' => [ qw(
) ]
);
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our $VERSION = $ZoneMinder::Base::VERSION;
#our @ISA = qw(Exporter ZoneMinder::Base);
use parent qw(ZoneMinder::Object);
# ==========================================================================
#
@ -61,39 +45,11 @@ our $VERSION = $ZoneMinder::Base::VERSION;
use ZoneMinder::Config qw(:all);
use ZoneMinder::Logger qw(:all);
use ZoneMinder::Database qw(:all);
require ZoneMinder::Server;
require ZoneMinder::Storage;
use POSIX;
sub new {
my ( $parent, $id, $data ) = @_;
my $self = {};
bless $self, $parent;
if ( ( $$self{Id} = $id ) or $data ) {
#$log->debug("loading $parent $id") if $debug or DEBUG_ALL;
$self->load( $data );
}
return $self;
} # end sub new
sub load {
my ( $self, $data ) = @_;
my $type = ref $self;
if ( ! $data ) {
#$log->debug("Object::load Loading from db $type");
$data = $ZoneMinder::Database::dbh->selectrow_hashref( 'SELECT * FROM Monitors WHERE Id=?', {}, $$self{Id} );
if ( ! $data ) {
if ( $ZoneMinder::Database::dbh->errstr ) {
Error( "Failure to load Monitor record for $$self{id}: Reason: " . $ZoneMinder::Database::dbh->errstr );
} # end if
} # end if
} # end if ! $data
if ( $data and %$data ) {
@$self{keys %$data} = values %$data;
} # end if
} # end sub load
use vars qw/ $table $primary_key /;
$table = 'Monitors';
$primary_key = 'Id';
sub Server {
return new ZoneMinder::Server( $_[0]{ServerId} );

View File

@ -73,7 +73,7 @@ class Event {
}
public function LinkPath() {
public function Link_Path() {
if ( ZM_USE_DEEP_STORAGE ) {
return $this->{'MonitorId'} .'/'.strftime( "%y/%m/%d/.", $this->Time()).$this->{'Id'};
}
@ -82,6 +82,7 @@ class Event {
}
public function delete() {
# This wouldn't work with foreign keys
dbQuery( 'DELETE FROM Events WHERE Id = ?', array($this->{'Id'}) );
if ( !ZM_OPT_FAST_DELETE ) {
dbQuery( 'DELETE FROM Stats WHERE EventId = ?', array($this->{'Id'}) );
@ -90,15 +91,28 @@ class Event {
# Assumption: All events haev a start time
$start_date = date_parse( $this->{'StartTime'} );
if ( ! $start_date ) {
Error("Unable to parse start time for event " . $this->{'Id'} . ' not deleting files.' );
return;
}
$start_date['year'] = $start_date['year'] % 100;
$Storage = $this->Storage();
# So this is because ZM creates a link under teh day pointing to the time that the event happened.
if ( ! $this->Link_Path() ) {
Error("Unable to determine link path for event " . $this->{'Id'} . ' not deleting files.' );
return;
}
$eventlink_path = $Storage->Path().'/'.$this->Link_Path();
if ( $id_files = glob( $eventlink_path ) ) {
if ( ! $eventPath = readlink($id_files[0]) ) {
Error("Unable to read link at $id_files[0]");
return;
}
# I know we are using arrays here, but really there can only ever be 1 in the array
$eventPath = preg_replace( '/\.'.$event['Id'].'$/', readlink($id_files[0]), $id_files[0] );
$eventPath = preg_replace( '/\.'.$this->{'Id'}.'$/', $eventPath, $id_files[0] );
deletePath( $eventPath );
deletePath( $id_files[0] );
$pathParts = explode( '/', $eventPath );

View File

@ -31,6 +31,15 @@ class Storage {
}
return $this->{'Name'};
}
public function Name() {
if ( isset( $this->{'Name'} ) and ( $this->{'Name'} != '' ) ) {
return $this->{'Name'};
} else if ( ! isset($this->{'Id'}) ) {
return 'Default';
}
return $this->{'Name'};
}
public function __call( $fn, array $args= NULL){
if(isset($this->{$fn})){
return $this->{$fn};
@ -47,5 +56,19 @@ class Storage {
}
return $storage_areas;
}
public function disk_usage_percent() {
$path = $this->Path();
$total = disk_total_space( $path );
if ( ! $total ) {
Error("disk_total_space returned false for " . $path );
return 0;
}
$free = disk_free_space( $path );
if ( ! $free ) {
Error("disk_free_space returned false for " . $path );
}
$usage = round(($total - $free) / $total * 100);
return $usage;
}
}
?>

View File

@ -124,36 +124,31 @@ if ( !empty($action) ) {
}
// Event scope actions, view permissions only required
if ( canView( 'Events' ) )
{
if ( $action == "filter" )
{
if ( !empty($_REQUEST['subaction']) )
{
if ( canView( 'Events' ) ) {
if ( $action == 'filter' ) {
if ( !empty($_REQUEST['subaction']) ) {
if ( $_REQUEST['subaction'] == "addterm" )
$_REQUEST['filter'] = addFilterTerm( $_REQUEST['filter'], $_REQUEST['line'] );
elseif ( $_REQUEST['subaction'] == "delterm" )
$_REQUEST['filter'] = delFilterTerm( $_REQUEST['filter'], $_REQUEST['line'] );
}
elseif ( canEdit( 'Events' ) )
{
if ( !empty($_REQUEST['execute']) )
$tempFilterName = "_TempFilter".time();
if ( isset($tempFilterName) ) {
$filterName = $tempFilterName;
}
elseif ( !empty($_REQUEST['newFilterName']) ) {
$filterName = $_REQUEST['newFilterName'];
$sql = "replace into Filters set Name = ".dbEscape($filterName).",";
} elseif ( canEdit( 'Events' ) ) {
$sql = '';
$endSql = '';
$filterName = '';
if ( !empty($_REQUEST['execute']) ) {
// TempFilterName is used in event listing later on
$tempFilterName = $filterName = "_TempFilter".time();
} elseif ( !empty($_REQUEST['newFilterName']) ) {
$filterName = $_REQUEST['newFilterName'];
}
else {
$doUpdate = 1;
$sql = "update Filters set";
$endSql = "where Id = ".$_REQUEST['filterId'];
if ( $filterName ) {
$sql = "REPLACE INTO Filters SET Name = ".dbEscape($filterName).",";
} else {
$sql = 'UPDATE Filters SET';
$endSql = "where Id = ".$_REQUEST['Id'];
}
if ( !empty($filterName) || $doUpdate )
{
if ( !empty($filterName) || $endSql ) {
$_REQUEST['filter']['sort_field'] = validStr($_REQUEST['sort_field']);
$_REQUEST['filter']['sort_asc'] = validStr($_REQUEST['sort_asc']);
$_REQUEST['filter']['limit'] = validInt($_REQUEST['limit']);
@ -180,9 +175,9 @@ if ( !empty($action) ) {
dbQuery( $sql );
$refreshParent = true;
}
}
}
}
} // end if canedit events
} // end if action == filter
} // end if canview events
// Event scope actions, edit permissions required
if ( canEdit( 'Events' ) )
@ -223,20 +218,21 @@ if ( !empty($action) ) {
}
}
}
elseif ( $action == "delete" )
elseif ( $action == 'delete' )
{
foreach( getAffectedIds( 'markEid' ) as $markEid )
{
deleteEvent( $markEid );
$refreshParent = true;
}
if ( !empty($_REQUEST['fid']) )
{
dbQuery( 'DELETE FROM Filters WHERE Id=?', array( $_REQUEST['filterId'] ) );
if ( isset( $_REQUEST['object'] ) and ( $_REQUEST['object'] == 'filter' ) ) {
if ( !empty($_REQUEST['Id']) ) {
dbQuery( 'DELETE FROM Filters WHERE Id=?', array( $_REQUEST['Id'] ) );
//$refreshParent = true;
}
}
}
}
// Monitor control actions, require a monitor id and control view permissions for that monitor
if ( !empty($_REQUEST['mid']) && canView( 'Control', $_REQUEST['mid'] ) )

View File

@ -450,6 +450,7 @@ function getEventDefaultVideoPath( $event ) {
}
function deletePath( $path ) {
Error("deletePath $path");
if ( is_dir( $path ) ) {
system( escapeshellcmd( "rm -rf ".$path ) );
} else {
@ -1220,6 +1221,8 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&' ) {
$filter['sql'] = '';
$filter['fields'] = '';
$StorageArea = NULL;
if ( isset($filter['terms']) && count($filter['terms']) ) {
for ( $i = 0; $i < count($filter['terms']); $i++ ) {
if ( isset($filter['terms'][$i]['cnj']) ) {
@ -1270,10 +1273,27 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&amp;' ) {
$filter['sql'] .= 'E.'.$filter['terms'][$i]['attr'];
break;
case 'DiskPercent':
$filter['sql'] .= getDiskPercent();
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
if ( ! $StorageArea ) {
for ( $j = $i; $j < count($filter['terms']); $j++ ) {
if ( isset($filter['terms'][$i]['attr']) and $filter['terms'][$i]['attr'] == 'StorageId' ) {
$StorageArea = new Storage( $filter['terms'][$i]['val'] );
}
} // end foreach remaining term
} // end no StorageArea found yet
$filter['sql'] .= getDiskPercent( $StorageArea );
break;
case 'DiskBlocks':
$filter['sql'] .= getDiskBlocks();
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
if ( ! $StorageArea ) {
for ( $j = $i; $j < count($filter['terms']); $j++ ) {
if ( isset($filter['terms'][$i]['attr']) and $filter['terms'][$i]['attr'] == 'StorageId' ) {
$StorageArea = new Storage( $filter['terms'][$i]['val'] );
}
} // end foreach remaining term
} // end no StorageArea found yet
$filter['sql'] .= getDiskBlocks( $StorageArea );
break;
case 'SystemLoad':
$filter['sql'] .= getLoad();
@ -1294,6 +1314,10 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&amp;' ) {
} else {
$value = dbEscape($value);
}
case 'StorageId':
$StorageArea = new Storage( $value );
$value = dbEscape($value);
break;
case 'DateTime':
$value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'";
break;
@ -1460,35 +1484,15 @@ function getLoad() {
return( $load[0] );
}
function getDiskPercent() {
if ( !empty(ZM_DIR_EVENTS) ) {
$total = disk_total_space(ZM_DIR_EVENTS);
if ( ! $total ) {
Error("disk_total_space returned false for " . ZM_DIR_EVENTS );
return 0;
}
$free = disk_free_space(ZM_DIR_EVENTS);
if ( ! $free ) {
Error("disk_free_space returned false for " . ZM_DIR_EVENTS );
}
$space = round(($total - $free) / $total * 100);
$spaceString = 'Default '.$space.'%';
}
else {
$spaceString = '';
}
$storageAreas = Storage::find_all();
foreach($storageAreas as $storageArea) {
$storageTotal = disk_total_space($storageArea->Path);
$storageFree = disk_free_space($storageArea->Path);
$storageSpace = round(($storageTotal - $storageFree) / $storageTotal * 100);
$spaceString .= ', '.$storageArea->Name.' '.$storageSpace.'%';
}
return( $spaceString );
function getDiskPercent( $StorageArea = NULL ) {
if ( ! $StorageArea ) $StorageArea = new Storage();
return $StorageArea->disk_usage_percent();
}
function getDiskBlocks() {
$df = shell_exec( 'df '.ZM_DIR_EVENTS );
if ( ! $StorageArea ) $StorageArea = new Storage();
$df = shell_exec( 'df '.$StorageArea->Path() );
$space = -1;
if ( preg_match( '/\s(\d+)\s+\d+\s+\d+%/ms', $df, $matches ) )
$space = $matches[1];

View File

@ -249,7 +249,14 @@ function getNavBarHTML() {
</div>
<ul class="list-inline">
<li><?php echo translate('Load') ?>: <?php echo getLoad() ?></li>
<li><?php echo translate('Storage') ?>: <?php echo getDiskPercent() ?></li>
<li><?php echo translate('Storage') ?>: <?php
$storage_areas = Storage::find_all();
array_push( $storage_areas, new Storage() );
$func = function($S){ return $S->Name() . ': ' . $S->disk_usage_percent().'%'; };
echo implode( ', ', array_map ( $func, $storage_areas ) );
?></li>
</ul>
</div> <!-- End .footer -->

View File

@ -19,23 +19,26 @@
//
if ( !canView( 'Events' ) ) {
$view = "error";
$view = 'error';
return;
}
$selectName = "filterId";
$selectName = 'Id';
$filterNames = array( ''=>translate('ChooseFilter') );
$dbFilter = NULL;
foreach ( dbFetchAll( "select * from Filters order by Name" ) as $row ) {
$filterNames[$row['Id']] = $row['Name'];
if ( $row['Background'] )
$filterNames[$row['Id']] .= "*";
if ( $row['Concurrent'] )
$filterNames[$row['Id']] .= "&";
if ( !empty($_REQUEST['reload']) && isset($_REQUEST['filterId']) && $_REQUEST['filterId'] == $row['Id'] )
if ( !empty($_REQUEST['reload']) && isset($_REQUEST['Id']) && $_REQUEST['Id'] == $row['Id'] ) {
$dbFilter = $row;
}
}
$backgroundStr = "";
if ( isset($dbFilter) ) {
$backgroundStr = '';
if ( $dbFilter ) {
if ( $dbFilter['Background'] )
$backgroundStr = '['.strtolower(translate('Background')).']';
$_REQUEST['filter'] = jsonDecode( $dbFilter['Query'] );
@ -57,7 +60,7 @@ if ( isset( $_REQUEST['reload'] ) and ! $_REQUEST['reload'] ) {
$dbFilter['AutoUpload'] = isset( $_REQUEST['AutoUpload'] );
$dbFilter['AutoVideo'] = isset( $_REQUEST['AutoVideo'] );
$dbFilter['AutoDelete'] = isset( $_REQUEST['AutoDelete'] );
$dbFilter['Name'] = $_REQUEST['filterId'];
$dbFilter['Name'] = $_REQUEST['Id'];
}
$conjunctionTypes = array(
@ -163,7 +166,7 @@ xhtmlHeaders(__FILE__, translate('EventFilter') );
<input type="hidden" name="action" value=""/>
<input type="hidden" name="subaction" value=""/>
<input type="hidden" name="line" value=""/>
<input type="hidden" name="fid" value=""/>
<input type="hidden" name="object" value="filter"/>
<hr/>
<div id="filterSelector"><label for="<?php echo $selectName ?>"><?php echo translate('UseFilter') ?></label><?php if ( count($filterNames) > 1 ) { echo buildSelect( $selectName, $filterNames, "submitToFilter( this, 1 );" ); } else { ?><select disabled="disabled"><option><?php echo translate('NoSavedFilters') ?></option></select><?php } ?><?php echo $backgroundStr ?></div>
<hr/>