3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004 Juan Luis Francés Jiménez |
5 | Copyright 2010-2011, Davide Franco |
7 | This program is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public License |
9 | as published by the Free Software Foundation; either version 2 |
10 | of the License, or (at your option) any later version. |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 +-------------------------------------------------------------------------+
21 public $db_link; // Database link
23 private $config_file; // Config filename
24 private $config; // Loaded config from bacula.conf
25 private $catalogs = array(); // Catalog array
27 private $catalog_current_id;
30 function __construct()
32 $this->bwcfg = new Config();
34 // Loading configuration from config file
35 $this->bwcfg->Load_Config();
36 $this->catalog_nb = $this->bwcfg->Count_Catalogs();
38 // Initialize smarty template classe
40 // Initialize smarty gettext function
41 $this->init_gettext();
44 if( isset($_POST['catalog_id']) ) {
45 $this->catalog_current_id = $_POST['catalog_id'];
46 $_SESSION['catalog_id'] = $this->catalog_current_id;
48 elseif( isset( $_SESSION['catalog_id'] ) )
49 $this->catalog_current_id = $_SESSION['catalog_id'];
51 $this->catalog_current_id = 0;
52 $_SESSION['catalog_id'] = $this->catalog_current_id;
55 $this->tpl->assign( 'catalog_current_id', $this->catalog_current_id );
57 // Database connection
59 $this->db_link = new CDB( $this->bwcfg->getDSN($this->catalog_current_id),
60 $this->bwcfg->getUser($this->catalog_current_id),
61 $this->bwcfg->getPwd($this->catalog_current_id) );
62 $this->db_link->makeConnection();
63 }catch( PDOException $e ) {
64 CDBError::raiseError( $e );
68 if( $this->catalog_nb > 1 ) {
70 $this->tpl->assign( 'catalogs', $this->bwcfg->Get_Catalogs() );
72 $this->tpl->assign( 'catalog_nb', $this->catalog_nb );
76 // Initialize Smarty template classe
79 $this->tpl = new Smarty();
81 $this->tpl->compile_check = true;
82 $this->tpl->debugging = false;
83 $this->tpl->force_compile = true;
85 $this->tpl->template_dir = "./templates";
86 $this->tpl->compile_dir = "./templates_c";
89 function init_gettext()
91 global $smarty_gettext_path;
93 if ( function_exists("gettext") ) {
94 require_once( BW_SMARTY_GETTEXT . "smarty_gettext.php" );
95 $this->tpl->register_block('t','smarty_translate');
97 $language = $this->bwcfg->Get_Config_Param("lang");
99 putenv("LANG=$language");
100 setlocale(LC_ALL, $language);
101 bindtextdomain($domain,"./locale");
105 function smarty_translate($params, $text, &$smarty) {
108 $smarty->register_block('t','smarty_translate');
118 switch( $this->db_link->getDriver() )
121 $query = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
122 $query .= "FROM information_schema.TABLES ";
123 $query .= "WHERE table_schema = 'bacula' ";
124 $query .= "GROUP BY table_schema";
127 $query = "SELECT pg_database_size('bacula') AS dbsize";
130 // Not yet implemented
135 // Execute SQL statment
137 $result = $this->db_link->runQuery( $query );
138 $db_size = $result->fetch();
139 $db_size = CUtils::Get_Human_Size( $db_size['dbsize'] );
140 }catch( PDOException $e) {
141 CDBError::raiseError($e);
144 } // end function GetDbSize()
146 public function Get_Nb_Clients()
150 $query = "SELECT COUNT(*) AS nb_client FROM Client";
153 $clients = $this->db_link->runQuery( $query );
154 $clients_nb = $clients->fetch();
155 }catch( PDOException $e ) {
156 CDBError::raiseError( $e );
162 // Return an array of volumes ordered by poolid and volume name
163 function GetVolumeList()
167 $volumes_list = array();
171 // Get the list of pools id
172 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
175 foreach( $this->getPools() as $pool ) {
176 switch( $this->db_link->getDriver() )
180 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
181 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
182 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
185 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
186 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
187 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
191 $volumes = $this->db_link->runQuery($query);
193 if( !array_key_exists( $pool['name'], $volumes_list) )
194 $volumes_list[ $pool['name'] ] = array();
196 foreach( $volumes->fetchAll() as $volume ) {
197 if( $volume['lastwritten'] != "0000-00-00 00:00:00" ) {
199 // Calculate expiration date if the volume is Full
200 if( $volume['volstatus'] == 'Full' ) {
201 $expire_date = strtotime($volume['lastwritten']) + $volume['volretention'];
202 $volume['expire'] = strftime("%Y-%m-%d", $expire_date);
204 $volume['expire'] = 'N/A';
207 // Media used bytes in a human format
208 $volume['volbytes'] = CUtils::Get_Human_Size( $volume['volbytes'] );
210 $volume['lastwritten'] = "N/A";
211 $volume['expire'] = "N/A";
212 $volume['volbytes'] = "0 KB";
216 if( count( $volumes_list[ $pool['name'] ] ) % 2)
217 $volume['class'] = 'odd';
219 // Add the media in pool array
220 array_push( $volumes_list[ $pool['name']], $volume);
221 } // end foreach volumes
222 } // end foreach pools
224 }catch(PDOException $e) {
225 CDBError::raiseError($e);
228 return $volumes_list;
229 } // end function GetVolumeList()
231 public function countJobs( $start_timestamp, $end_timestamp, $status = 'ALL', $level = 'ALL', $jobname = 'ALL', $client = 'ALL' )
234 $where_interval = "";
235 $where_conditions = array();
238 // Calculate sql query interval
239 $start_date = date( "Y-m-d H:i:s", $start_timestamp);
240 $end_date = date( "Y-m-d H:i:s", $end_timestamp);
242 switch( $this->db_link->getDriver() )
246 $query .= "SELECT COUNT(*) AS job_nb FROM Job ";
247 $where_conditions[] = "(EndTime BETWEEN '$start_date' AND '$end_date')";
250 $query .= "SELECT COUNT(*) AS job_nb FROM job ";
251 $where_conditions[] = "(EndTime BETWEEN timestamp '$start_date' AND timestamp '$end_date')";
255 if( $status != 'ALL' ) {
256 switch( strtolower($status) )
259 array_pop( $where_conditions );
260 $where_conditions[] = "JobStatus = 'R' ";
263 $where_conditions[] = "JobStatus = 'T' ";
266 $where_conditions[] = "JobStatus IN ('f','E') ";
269 $where_conditions[] = "JobStatus = 'A' ";
272 array_pop( $where_conditions );
273 $where_conditions[] = "Job.JobStatus IN ('F','S','M','m','s','j','c','d','t','p','C') ";
279 if( $level != 'ALL' )
280 $where_conditions[] = "Level = '$level' ";
282 // Construct SQL query
283 foreach( $where_conditions as $k => $condition ) {
285 $query .= "WHERE $condition ";
287 $query .= "AND $condition ";
292 $jobs = $this->db_link->runQuery($query);
293 $result = $jobs->fetch();
294 }catch(PDOException $e) {
295 CDBError::raiseError($e);
298 return $result['job_nb'];
301 // Return the list of Pools in a array
302 public function getPools()
307 switch( $this->db_link->getDriver() )
311 $query = "SELECT name, poolid FROM Pool";
314 $query = "SELECT name, poolid FROM pool";
318 $result = $this->db_link->runQuery($query);
319 foreach( $result->fetchAll() as $pool )
321 }catch(PDOException $e) {
322 CDBError::raiseError($e);
328 public function Get_BackupJob_Names()
332 $backupjobs = array();
334 switch( $this->db_link->getDriver() )
338 $query = "SELECT name FROM Job GROUP BY name ORDER BY name";
341 $query = "SELECT name FROM Job GROUP BY name ORDER BY name";
345 $result = $this->db_link->runQuery($query);
346 foreach( $result->fetchAll() as $jobname )
347 $backupjobs[] = $jobname['name'];
348 }catch(PDOException $e) {
349 CDBError::raiseError($e);
355 public function countVolumes( $pool_id = 'ALL' )
361 switch( $this->db_link->getDriver() )
365 $query = 'SELECT COUNT(*) as vols_count ';
366 $query .= 'FROM Media ';
367 if( $pool_id != 'ALL' )
368 $query .= ' WHERE Media.poolid = ' . $pool_id;
371 $query = 'SELECT COUNT(*) as vols_count ';
372 $query .= 'FROM Media ';
373 if( $pool_id != 'ALL' )
374 $query .= ' WHERE media.poolid = ' . $pool_id;
380 $result = $this->db_link->runQuery($query);
381 $vols = $result->fetch();
382 }catch( PDOException $e) {
383 CDBError::raiseError($e);
386 return $vols['vols_count'];
389 public function getStoredFiles( $start_timestamp, $end_timestamp, $job_name = 'ALL' )
392 $start_date = date( "Y-m-d H:i:s", $start_timestamp);
393 $end_date = date( "Y-m-d H:i:s", $end_timestamp);
395 switch( $this->db_link->getDriver() )
399 $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
400 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' )";
403 $query = "SELECT SUM(JobFiles) AS stored_files FROM job ";
404 $query .= "WHERE ( endtime BETWEEN timestamp '$start_date' AND timestamp '$end_date' )";
408 if( $job_name != 'ALL' )
409 $query .= " AND name = '$job_name'";
413 $result = $this->db_link->runQuery( $query );
414 $result = $result->fetch();
415 }catch( PDOException $e) {
416 CDBError::raiseError($e);
419 if( isset($result['stored_files']) and !empty($result['stored_files']) )
420 return $result['stored_files'];
425 // Function: getStoredBytes
427 // $start_timestamp: start date in unix timestamp format
428 // $end_timestamp: end date in unix timestamp format
429 // $job_name: optional job name
431 public function getStoredBytes( $start_timestamp, $end_timestamp, $job_name = 'ALL' )
435 $start_date = date( "Y-m-d H:i:s", $start_timestamp);
436 $end_date = date( "Y-m-d H:i:s", $end_timestamp);
438 switch( $this->db_link->getDriver() )
442 $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
443 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' )";
446 $query = "SELECT SUM(jobbytes) as stored_bytes FROM job ";
447 $query .= "WHERE ( endtime BETWEEN timestamp '$start_date' AND timestamp '$end_date' )";
451 if( $job_name != 'ALL' )
452 $query .= " AND name = '$job_name'";
454 // Execute SQL statment
456 $result = $this->db_link->runQuery( $query );
457 $result = $result->fetch();
458 }catch(PDOException $e) {
459 CDBError::raiseError( $e );
462 if( isset($result['stored_bytes']) and !empty($result['stored_bytes']) )
463 return $result['stored_bytes'];