2015-12-16 00:12:21 +08:00
|
|
|
<?php
|
2019-02-22 22:19:07 +08:00
|
|
|
namespace ZM;
|
2018-04-11 04:05:37 +08:00
|
|
|
require_once('database.php');
|
2019-02-22 22:19:07 +08:00
|
|
|
require_once('Event.php');
|
2019-07-23 21:58:05 +08:00
|
|
|
require_once('Object.php');
|
2017-10-11 01:39:17 +08:00
|
|
|
|
2019-07-23 21:58:05 +08:00
|
|
|
class Storage extends ZM_Object {
|
|
|
|
protected static $table = 'Storage';
|
|
|
|
protected $defaults = array(
|
2018-09-15 21:42:59 +08:00
|
|
|
'Id' => null,
|
|
|
|
'Path' => '',
|
|
|
|
'Name' => '',
|
|
|
|
'Type' => 'local',
|
|
|
|
'Url' => '',
|
|
|
|
'DiskSpace' => null,
|
|
|
|
'Scheme' => 'Medium',
|
|
|
|
'ServerId' => 0,
|
|
|
|
'DoDelete' => 1,
|
2020-03-04 23:46:16 +08:00
|
|
|
'Enabled' => 1,
|
2018-09-15 21:42:59 +08:00
|
|
|
);
|
2019-11-27 03:36:39 +08:00
|
|
|
public static function find($parameters = array(), $options = array()) {
|
2019-07-23 21:58:05 +08:00
|
|
|
return ZM_Object::_find(get_class(), $parameters, $options);
|
|
|
|
}
|
2018-09-15 21:42:59 +08:00
|
|
|
|
2019-11-27 03:36:39 +08:00
|
|
|
public static function find_one($parameters = array(), $options = array()) {
|
2019-07-23 21:58:05 +08:00
|
|
|
return ZM_Object::_find_one(get_class(), $parameters, $options);
|
2016-09-21 00:16:49 +08:00
|
|
|
}
|
2015-12-16 00:12:21 +08:00
|
|
|
|
2016-09-21 00:16:49 +08:00
|
|
|
public function Path() {
|
2019-05-06 22:49:18 +08:00
|
|
|
if ( isset($this->{'Path'}) and ( $this->{'Path'} != '' ) ) {
|
2016-09-21 00:16:49 +08:00
|
|
|
return $this->{'Path'};
|
|
|
|
} else if ( ! isset($this->{'Id'}) ) {
|
2016-09-22 04:00:23 +08:00
|
|
|
$path = ZM_DIR_EVENTS;
|
|
|
|
if ( $path[0] != '/' ) {
|
|
|
|
$this->{'Path'} = ZM_PATH_WEB.'/'.ZM_DIR_EVENTS;
|
|
|
|
} else {
|
|
|
|
$this->{'Path'} = ZM_DIR_EVENTS;
|
|
|
|
}
|
|
|
|
return $this->{'Path'};
|
2015-12-16 00:12:21 +08:00
|
|
|
}
|
2016-09-21 00:16:49 +08:00
|
|
|
return $this->{'Name'};
|
|
|
|
}
|
|
|
|
public function Name() {
|
2019-05-06 22:49:18 +08:00
|
|
|
if ( isset($this->{'Name'}) and ( $this->{'Name'} != '' ) ) {
|
2016-09-21 00:16:49 +08:00
|
|
|
return $this->{'Name'};
|
|
|
|
} else if ( ! isset($this->{'Id'}) ) {
|
|
|
|
return 'Default';
|
|
|
|
}
|
|
|
|
return $this->{'Name'};
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:36:39 +08:00
|
|
|
public function Events() {
|
|
|
|
if ( $this->{'Id'} and ! isset($this->{'Events'}) ) {
|
|
|
|
$this->{'Events'} = Event::find(array('StorageId'=>$this->{'Id'}));
|
|
|
|
}
|
|
|
|
if ( ! isset($this->{'Events'}) ) {
|
|
|
|
$this->{'Events'} = array();
|
|
|
|
}
|
|
|
|
return $this->{'Events'};
|
|
|
|
}
|
|
|
|
|
2020-01-14 01:53:50 +08:00
|
|
|
public function EventCount() {
|
|
|
|
if ( (! property_exists($this, 'EventCount')) or (!$this->{'EventCount'}) ) {
|
|
|
|
$this->{'EventCount'} = dbFetchOne('SELECT COUNT(*) AS EventCount FROM Events WHERE StorageId=?', 'EventCount', array($this->Id()));
|
|
|
|
}
|
|
|
|
return $this->{'EventCount'};
|
|
|
|
}
|
|
|
|
|
2016-09-21 00:16:49 +08:00
|
|
|
public function disk_usage_percent() {
|
|
|
|
$path = $this->Path();
|
2017-01-31 06:24:41 +08:00
|
|
|
if ( ! $path ) {
|
2018-09-08 04:31:11 +08:00
|
|
|
Warning('Storage::disk_usage_percent: path is empty');
|
2017-01-31 06:24:41 +08:00
|
|
|
return 0;
|
2018-09-08 04:31:11 +08:00
|
|
|
} else if ( ! file_exists($path) ) {
|
2017-01-31 06:24:41 +08:00
|
|
|
Warning("Storage::disk_usage_percent: path $path does not exist");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
$total = $this->disk_total_space();
|
2016-09-21 00:16:49 +08:00
|
|
|
if ( ! $total ) {
|
2019-05-06 22:49:18 +08:00
|
|
|
Error('disk_total_space returned false for ' . $path);
|
2016-09-21 00:16:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-23 01:02:49 +08:00
|
|
|
$used = $this->disk_used_space();
|
2019-05-06 22:49:18 +08:00
|
|
|
$usage = round(($used / $total) * 100);
|
2018-01-19 04:39:39 +08:00
|
|
|
//Logger::Debug("Used $usage = round( ( $used / $total ) * 100 )");
|
2016-09-21 00:16:49 +08:00
|
|
|
return $usage;
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
public function disk_total_space() {
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( !property_exists($this, 'disk_total_space') ) {
|
2018-07-12 05:21:44 +08:00
|
|
|
$path = $this->Path();
|
|
|
|
if ( file_exists($path) ) {
|
|
|
|
$this->{'disk_total_space'} = disk_total_space($path);
|
|
|
|
} else {
|
|
|
|
Error("Path $path does not exist.");
|
|
|
|
$this->{'disk_total_space'} = 0;
|
|
|
|
}
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
|
|
|
return $this->{'disk_total_space'};
|
|
|
|
}
|
2018-04-18 05:14:40 +08:00
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
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.
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( ( !property_exists($this, 'disk_used_space')) or !$this->{'disk_used_space'} ) {
|
2017-11-27 04:12:35 +08:00
|
|
|
if ( $this->{'Type'} == 's3fs' ) {
|
2018-10-31 23:56:08 +08:00
|
|
|
$this->{'disk_used_space'} = $this->event_disk_space();
|
2017-11-27 04:12:35 +08:00
|
|
|
} else {
|
|
|
|
$path = $this->Path();
|
2018-07-12 05:21:44 +08:00
|
|
|
if ( file_exists($path) ) {
|
|
|
|
$this->{'disk_used_space'} = disk_total_space($path) - disk_free_space($path);
|
|
|
|
} else {
|
|
|
|
Error("Path $path does not exist.");
|
|
|
|
$this->{'disk_used_space'} = 0;
|
|
|
|
}
|
2018-04-18 05:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->{'disk_used_space'};
|
|
|
|
} // end function disk_used_space
|
|
|
|
|
|
|
|
public function event_disk_space() {
|
|
|
|
# This isn't a function like this in php, so we have to add up the space used in each event.
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( (! property_exists($this, 'DiskSpace')) or (!$this->{'DiskSpace'}) ) {
|
2019-05-06 22:49:18 +08:00
|
|
|
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()));
|
2018-04-18 05:14:40 +08:00
|
|
|
|
2019-11-20 22:31:16 +08:00
|
|
|
do {
|
2020-01-14 01:53:05 +08:00
|
|
|
# Do in batches of 1000 so as to not useup all ram, Event will do caching though...
|
2019-11-20 22:31:16 +08:00
|
|
|
$events = Event::find(array('StorageId'=>$this->Id(), 'DiskSpace'=>null), array('limit'=>1000));
|
|
|
|
foreach ( $events as $Event ) {
|
|
|
|
$Event->Storage($this); // Prevent further db hit
|
|
|
|
# DiskSpace will update the event
|
|
|
|
$used += $Event->DiskSpace();
|
|
|
|
} #end foreach
|
2020-01-14 01:53:05 +08:00
|
|
|
Event::clear_cache();
|
2019-11-21 05:30:26 +08:00
|
|
|
} while ( count($events) == 1000 );
|
2017-11-23 23:29:05 +08:00
|
|
|
$this->{'DiskSpace'} = $used;
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
2017-11-23 23:29:05 +08:00
|
|
|
return $this->{'DiskSpace'};
|
2018-04-18 05:14:40 +08:00
|
|
|
} // end function event_disk_space
|
2018-05-08 05:08:32 +08:00
|
|
|
|
2018-04-18 00:37:30 +08:00
|
|
|
public function Server() {
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( ! property_exists($this, 'Server') ) {
|
|
|
|
if ( property_exists($this, 'ServerId') ) {
|
2019-10-12 05:29:47 +08:00
|
|
|
$this->{'Server'} = Server::find_one(array('Id'=>$this->{'ServerId'}));
|
2019-10-22 01:41:32 +08:00
|
|
|
|
2019-10-22 01:45:27 +08:00
|
|
|
if ( !$this->{'Server'} ) {
|
|
|
|
if ( $this->{'ServerId'} )
|
|
|
|
Error('No Server record found for server id ' . $this->{'ServerId'});
|
2019-10-12 05:29:47 +08:00
|
|
|
$this->{'Server'} = new Server();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->{'Server'} = new Server();
|
|
|
|
}
|
2018-04-18 00:37:30 +08:00
|
|
|
}
|
|
|
|
return $this->{'Server'};
|
|
|
|
}
|
2019-02-07 00:44:36 +08:00
|
|
|
|
2019-05-06 22:49:18 +08:00
|
|
|
} // end class Storage
|
2015-12-16 00:12:21 +08:00
|
|
|
?>
|