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 +-------------------------------------------------------------------------+
18 class Bweb extends DB {
26 public $db_link; // Database link
28 private $config_file; // Config filename
29 private $config; // Loaded config from bacula.conf
30 private $catalogs = array(); // Catalog array
34 function __construct()
36 $this->bwcfg = new BW_Config();
39 // Loading configuration from config file
40 $this->bwcfg->Load_Config();
41 $this->catalog_nb = $this->bwcfg->Count_Catalogs();
43 // Select which catalog to connect to
44 if( isset( $_POST['catalog_id'] ) )
45 $dsn = $this->bwcfg->Get_Dsn( $_POST['catalog_id'] );
47 $dsn = $this->bwcfg->Get_Dsn( 0 );
49 // Connect to the database
50 $this->db_link = $this->connect( $dsn );
52 if (DB::isError($this->db_link)) {
53 die( 'Unable to connect to catalog <br />' . $this->db_link->getMessage());
55 $this->driver = $dsn['phptype'];
56 register_shutdown_function(array(&$this,'close') );
59 // Initialize smarty template classe
61 // Initialize smarty gettext function
62 $this->init_gettext();
65 if( $this->catalog_nb > 1 ) {
66 // Set current catalog in header template
67 if(isset( $_POST['catalog_id'] ) )
68 $this->tpl->assign( 'catalog_current', $_POST['catalog_id'] );
70 $this->tpl->assign( 'catalogs', $this->bwcfg->Get_Catalogs() );
74 // Initialize Smarty template classe
77 $this->tpl = new Smarty();
79 $this->tpl->compile_check = true;
80 $this->tpl->debugging = false;
81 $this->tpl->force_compile = true;
83 $this->tpl->template_dir = "./templates";
84 $this->tpl->compile_dir = "./templates_c";
85 $this->tpl->config_dir = "./configs";
88 function init_gettext()
90 global $smarty_gettext_path;
92 if ( function_exists("gettext") ) {
93 require_once( BW_SMARTY_GETTEXT . "smarty_gettext.php" );
94 $this->tpl->register_block('t','smarty_translate');
96 $language = $this->bwcfg->Get_Config_Param("lang");
98 putenv("LANG=$language");
99 setlocale(LC_ALL, $language);
100 bindtextdomain($domain,"./locale");
104 function smarty_translate($params, $text, &$smarty) {
107 $smarty->register_block('t','smarty_translate');
113 $this->db_link->disconnect();
116 // Return humanized size with default unit of GB
117 // if auto provide for unit argument, automaticaly decide which unit
118 function human_file_size( $size, $decimal = 2, $unit = 'auto' )
122 $units = array('B','KB','MB','GB','TB');
129 if ( $hsize >= 1024 ) {
130 $hsize = $hsize / 1024;
139 $p = array_search( $unit, $units);
140 $hsize = $hsize / pow(1024,$p);
144 $hsize = sprintf("%." . $decimal . "f", $hsize);
145 $hsize = $hsize . ' ' . $units[$unit_id];
155 switch( $this->driver )
158 $query = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
159 $query .= "FROM information_schema.TABLES ";
160 $query .= "WHERE table_schema = 'bacula' ";
161 $query .= "GROUP BY table_schema";
164 $query = "SELECT pg_database_size('bacula') AS dbsize";
167 // Not yet implemented
172 $result = $this->db_link->query( $query );
174 if(! PEAR::isError( $result ) )
176 $db = $result->fetchRow( DB_FETCHMODE_ASSOC );
177 $database_size = $db['dbsize'];
179 die( "Unable to get database size<br />" . $result->getMessage() );
181 return Utils::Get_Human_Size( $database_size );
182 } // end function GetDbSize()
184 public function Get_Nb_Clients()
186 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
187 if( PEAR::isError($clients) )
188 die( "Unable to get client number" );
190 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
193 // Return an array of volumes ordered by poolid and volume name
194 function GetVolumeList() {
200 // Get the list of pools id
201 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
203 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
204 $pools = $this->db_link->query( $query );
206 if( PEAR::isError( $pools ) )
207 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
209 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
210 switch( $this->driver )
213 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
214 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
215 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
218 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
219 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
220 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
223 $query = ""; // not yet implemented
229 $medias = $this->db_link->query( $query );
231 if( PEAR::isError( $medias ) ) {
232 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
234 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
236 // Create array key for each pool
237 if( !array_key_exists( $pool['name'], $volumes) )
239 $volumes[ $pool['name'] ] = array();
241 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
245 // If the pool is empty (no volumes in this pool)
246 if( $medias->numRows() == 0 ) {
247 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
249 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
250 // Calculate expiration date if the volume is Full
251 if( $media['volstatus'] == 'Full' ) {
252 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
253 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
255 $media['expire'] = 'N/A';
257 // Media used bytes in a human format
258 $media['volbytes'] = Utils::Get_Human_Size( $media['volbytes'] );
260 $media['lastwritten'] = "N/A";
261 $media['expire'] = "N/A";
262 $media['volbytes'] = "0 KB";
266 if( count( $volumes[ $pool['name'] ] ) % 2)
267 $media['class'] = 'odd';
269 // Add the media in pool array
270 array_push( $volumes[ $pool['name']], $media);
276 } // end function GetVolumeList()
278 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
280 $end_date = mktime();
281 $start_date = $end_date - $delay;
283 $start_date = date( "Y-m-d H:i:s", $start_date );
284 $end_date = date( "Y-m-d H:i:s", $end_date );
286 $query = "SELECT COUNT(JobId) as jobs FROM Job ";
287 $query .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
288 $query .= "Level = '$level' ";
290 $result = $this->db_link->query( $query );
292 if (PEAR::isError( $result ) ) {
293 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
295 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC );
296 return $jobs['jobs'];
301 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
303 $query = "SELECT COUNT(JobId) AS job_nb FROM Job ";
307 // Interval condition for SQL query
308 if( $delay != ALL ) {
309 $end_date = mktime();
310 $start_date = $end_date - $delay;
312 $start_date = date( "Y-m-d H:i:s", $start_date );
313 $end_date = date( "Y-m-d H:i:s", $end_date );
315 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
318 if( $status != 'any' ) {
322 $where_status = "JobStatus = 'T' ";
325 $where_status = "JobStatus IN ('f','E') ";
328 $where_status = "JobStatus = 'A' ";
331 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
336 if( !empty($where_delay) )
337 $query = $query . $where_delay . 'AND ' . $where_status;
339 if( !empty($where_status) )
340 $query = $query . 'WHERE ' . $where_status;
343 $jobs = $this->db_link->query( $query );
345 if (PEAR::isError( $jobs ) ) {
346 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
348 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC );
349 return $jobs['job_nb'];
353 // Return the list of Pools in a array
354 public function Get_Pools_List()
356 $pool_list = array();
359 $query = "SELECT Name, PoolId FROM Pool";
361 $result = $this->db_link->query ( $query );
363 if( PEAR::isError( $result ) ) {
364 die( "Unable to get the pool list from catalog" );
366 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
367 array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
373 public function Get_BackupJob_Names()
375 $query = "SELECT Name FROM Job GROUP BY Name";
376 $backupjobs = array();
378 $result = $this->db_link->query( $query );
380 if (PEAR::isError( $result ) ) {
381 die("Unable to get BackupJobs list from catalog" );
383 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
384 array_push( $backupjobs, $backupjob["Name"] );
390 // Return elasped time string for a job
391 function Get_ElapsedTime( $start_time, $end_time )
393 $diff = $end_time - $start_time;
395 $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
396 $diff -= $daysDiff*60*60*24;
398 $hrsDiff = sprintf("%02d", floor($diff/60/60) );
399 $diff -= $hrsDiff*60*60;
401 $minsDiff = sprintf("%02d", floor($diff/60) );
402 $diff -= $minsDiff*60;
403 $secsDiff = sprintf("%02d", $diff );
406 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
408 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
411 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
416 // Interval calculation
417 $end_date = mktime();
418 $start_date = $end_date - $delay;
420 $start_date = date( "Y-m-d H:i:s", $start_date );
421 $end_date = date( "Y-m-d H:i:s", $end_date );
423 switch( $this->driver )
426 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
427 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
430 $result = $this->db_link->query( $query );
432 if( PEAR::isError($result) ){
433 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
435 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
436 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
437 $total_elapsed += $time['elapsed'];
439 // Verify if elapsed time is more than 1 day
440 if ( $total_elapsed > LAST_DAY ) {
441 return date("%d days H:i:s", $total_elapsed );
443 return date("H:i:s", $total_elapsed );
448 // Return Jobs statistics for a specific interval such as
449 // - Completed jobs number
450 // - Failed jobs number
451 // - Waiting jobs number
452 // The returned values will be used by a Bgraph classe
453 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
461 // Interval calculation
462 $end_date = mktime();
463 $start_date = $end_date - $delay;
465 $start_date = date( "Y-m-d H:i:s", $start_date );
466 $end_date = date( "Y-m-d H:i:s", $end_date );
468 $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
474 $where = $interval_where . "JobStatus = 'T' ";
475 $label = "Completed";
477 case 'terminated_errors':
478 $where = $interval_where . "JobStatus = 'E' ";
479 $label = "Terminated with errors";
482 $where = $interval_where . "JobStatus = 'f' ";
486 $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
490 $where = "JobStatus = 'C' ";
491 $label = "Created but not running";
494 $where = "JobStatus = 'R' ";
498 $where = $interval_where . "JobStatus IN ('e','f') ";
503 $query = 'SELECT COUNT(JobId) AS ' . $type . ' ';
504 $query .= 'FROM Job ';
505 $query .= "WHERE $where ";
507 //echo 'query = ' . $query . '<br />';
509 $jobs = $this->db_link->query( $query );
511 if (PEAR::isError( $jobs ) ) {
512 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
514 $res = $jobs->fetchRow();
515 return array( $label , current($res) );
517 } // end function GetJobsStatistics()
519 public function CountVolumesByPool( $pools )
521 foreach( $pools as $pool_name => $pool ) {
523 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
524 //echo $query . '<br />';
525 //echo 'Pool name ' . $pool_name . '<br />';
526 $result = $this->db_link->query( $query );
528 if( PEAR::isError( $result ) ) {
529 die("Unable to get volume number from catalog");
531 $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
532 return array( $pool_name, $nb_vol['nb_vol'] );
537 public function GetStoredFiles( $delay = LAST_DAY )
541 $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
543 // Interval calculation
544 $end_date = mktime();
545 $start_date = $end_date - $delay;
547 $start_date = date( "Y-m-d H:i:s", $start_date );
548 $end_date = date( "Y-m-d H:i:s", $end_date );
551 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
553 $result = $this->db_link->query( $query );
555 if( !PEAR::isError($result) ) {
556 $nbfiles = $result->fetchRow(DB_FETCHMODE_ASSOC);
557 $totalfiles = $totalfiles + $nbfiles['stored_files'];
559 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
565 public function GetStoredBytes( $delay = LAST_DAY )
567 $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
569 // Interval calculation
570 $end_date = mktime();
571 $start_date = $end_date - $delay;
573 $start_date = date( "Y-m-d H:i:s", $start_date );
574 $end_date = date( "Y-m-d H:i:s", $end_date );
577 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
579 $result = $this->db_link->query( $query );
581 if( PEAR::isError( $result ) ) {
582 die( "Unable to get Job Bytes from catalog" );
584 return $result->fetchRow( DB_FETCHMODE_ASSOC );
588 public function GetStoredBytesByInterval( $start_date, $end_date )
590 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
592 $result = $this->db_link->query( $query );
594 if( PEAR::isError( $result ) ) {
595 die( "Unable to get Job Bytes from catalog" );
598 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
600 $day = date( "D d", strtotime($end_date) );
602 if( isset( $tmp['stored_bytes'] ) ) {
603 $hbytes = Utils::Get_Human_Size( $tmp['stored_bytes'], 3, 'GB' );
604 $hbytes = explode( " ", $hbytes );
605 $stored_bytes = $hbytes[0];
608 return array( $day, $stored_bytes );
612 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
614 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
615 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
616 $query .= "Name = '$jobname'";
618 $result = $this->db_link->query( $query );
620 if( PEAR::isError( $result ) ) {
621 die( "Unable to get Job Bytes from catalog" );
624 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
626 $day = date( "D d", strtotime($end_date) );
628 if( isset( $tmp['stored_bytes'] ) ) {
629 $hbytes = Utils::Get_Human_Size( $tmp['stored_bytes'], 3, 'GB' );
630 $hbytes = explode( " ", $hbytes );
631 $stored_bytes = $hbytes[0];
634 return array( $day, $stored_bytes );
638 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
640 $query = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
641 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
642 $query .= "Name = '$jobname'";
644 $result = $this->db_link->query( $query );
646 if( PEAR::isError( $result ) ) {
647 die( "Unable to get Job Files from catalog" );
650 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
652 $day = date( "D d", strtotime($end_date) );
653 $stored_files = $tmp['stored_files'];
655 return array( $day, $stored_files );