3 class Bweb extends DB {
11 public $db_link; // Database link
13 private $config_file; // Config filename
14 private $config; // Loaded config from bacula.conf
15 private $catalogs = array(); // Catalog array
19 function __construct()
21 $this->bwcfg = new BW_Config();
24 // Checking if config file exist and is readable
25 if( !$this->bwcfg->Check_Config_File() )
26 die( "Unable to load configuration file" );
28 $this->bwcfg->Load_Config();
29 $this->catalog_nb = $this->bwcfg->Count_Catalogs();
32 // Select which catalog to connect to
33 if( isset( $_POST['catalog_id'] ) )
34 $dsn = $this->bwcfg->Get_Dsn( $_POST['catalog_id'] );
36 $dsn = $this->bwcfg->Get_Dsn( 0 );
38 // Connect to the database
39 $this->db_link = $this->connect( $dsn );
41 if (DB::isError($this->db_link)) {
42 die( 'Unable to connect to catalog <br />' . $this->db_link->getMessage());
44 $this->driver = $dsn['phptype'];
45 register_shutdown_function(array(&$this,'close') );
48 // Initialize smarty template classe
50 // Initialize smarty gettext function
51 $this->init_gettext();
54 if( $this->catalog_nb > 1 ) {
55 // Set current catalog in header template
56 if(isset( $_POST['catalog_id'] ) )
57 $this->tpl->assign( 'catalog_current', $_POST['catalog_id'] );
59 $this->tpl->assign( 'catalogs', $this->bwcfg->Get_Catalogs() );
63 // Initialize Smarty template classe
66 $this->tpl = new Smarty();
68 $this->tpl->compile_check = true;
69 $this->tpl->debugging = false;
70 $this->tpl->force_compile = true;
72 $this->tpl->template_dir = "./templates";
73 $this->tpl->compile_dir = "./templates_c";
74 $this->tpl->config_dir = "./configs";
77 function init_gettext()
79 global $smarty_gettext_path;
81 if ( function_exists("gettext") ) {
82 require_once( BW_SMARTY_GETTEXT . "smarty_gettext.php" );
83 $this->tpl->register_block('t','smarty_translate');
85 $language = $this->bwcfg->Get_Config_Param("lang");
87 putenv("LANG=$language");
88 setlocale(LC_ALL, $language);
89 bindtextdomain($domain,"./locale");
93 function smarty_translate($params, $text, &$smarty) {
96 $smarty->register_block('t','smarty_translate');
102 $this->db_link->disconnect();
105 // Return humanized size with default unit of GB
106 // if auto provide for unit argument, automaticaly decide which unit
107 function human_file_size( $size, $decimal = 2, $unit = 'auto' )
111 $units = array('B','KB','MB','GB','TB');
118 if ( $hsize >= 1024 ) {
119 $hsize = $hsize / 1024;
128 $p = array_search( $unit, $units);
129 $hsize = $hsize / pow(1024,$p);
133 $hsize = sprintf("%." . $decimal . "f", $hsize);
134 $hsize = $hsize . ' ' . $units[$unit_id];
144 switch( $this->driver )
147 $query = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
148 $query .= "FROM information_schema.TABLES ";
149 $query .= "WHERE table_schema = 'bacula' ";
150 $query .= "GROUP BY table_schema";
153 $query = "SELECT pg_database_size('bacula') AS dbsize";
156 // Not yet implemented
161 $result = $this->db_link->query( $query );
163 if(! PEAR::isError( $result ) )
165 $db = $result->fetchRow( DB_FETCHMODE_ASSOC );
166 $database_size = $db['dbsize'];
168 die( "Unable to get database size<br />" . $result->getMessage() );
170 return $this->human_file_size( $database_size );
171 } // end function GetDbSize()
173 public function Get_Nb_Clients()
175 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
176 if( PEAR::isError($clients) )
177 die( "Unable to get client number" );
179 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
182 // Return an array of volumes ordered by poolid and volume name
183 function GetVolumeList() {
189 // Get the list of pools id
190 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
192 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
193 $pools = $this->db_link->query( $query );
195 if( PEAR::isError( $pools ) )
196 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
198 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
199 switch( $this->driver )
202 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
203 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
204 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
207 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
208 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
209 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
212 $query = ""; // not yet implemented
218 $medias = $this->db_link->query( $query );
220 if( PEAR::isError( $medias ) ) {
221 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
223 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
225 // Create array key for each pool
226 if( !array_key_exists( $pool['name'], $volumes) )
228 $volumes[ $pool['name'] ] = array();
230 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
234 // If the pool is empty (no volumes in this pool)
235 if( $medias->numRows() == 0 ) {
236 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
238 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
239 // Calculate expiration date if the volume is Full
240 if( $media['volstatus'] == 'Full' ) {
241 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
242 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
244 $media['expire'] = 'N/A';
246 // Media used bytes in a human format
247 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
249 $media['lastwritten'] = "N/A";
250 $media['expire'] = "N/A";
251 $media['volbytes'] = "0 KB";
255 if( count( $volumes[ $pool['name'] ] ) % 2)
256 $media['class'] = 'odd';
258 // Add the media in pool array
259 array_push( $volumes[ $pool['name']], $media);
265 } // end function GetVolumeList()
267 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
269 $end_date = mktime();
270 $start_date = $end_date - $delay;
272 $start_date = date( "Y-m-d H:i:s", $start_date );
273 $end_date = date( "Y-m-d H:i:s", $end_date );
275 $query = "SELECT COUNT(JobId) as jobs FROM Job ";
276 $query .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
277 $query .= "Level = '$level' ";
279 $result = $this->db_link->query( $query );
281 if (PEAR::isError( $result ) ) {
282 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
284 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC );
285 return $jobs['jobs'];
290 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
292 $query = "SELECT COUNT(JobId) AS job_nb FROM Job ";
296 // Interval condition for SQL query
297 if( $delay != ALL ) {
298 $end_date = mktime();
299 $start_date = $end_date - $delay;
301 $start_date = date( "Y-m-d H:i:s", $start_date );
302 $end_date = date( "Y-m-d H:i:s", $end_date );
304 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
307 if( $status != 'any' ) {
311 $where_status = "JobStatus = 'T' ";
314 $where_status = "JobStatus IN ('f','E') ";
317 $where_status = "JobStatus = 'A' ";
320 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
325 if( !empty($where_delay) )
326 $query = $query . $where_delay . 'AND ' . $where_status;
328 if( !empty($where_status) )
329 $query = $query . 'WHERE ' . $where_status;
332 $jobs = $this->db_link->query( $query );
334 if (PEAR::isError( $jobs ) ) {
335 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
337 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC );
338 return $jobs['job_nb'];
342 // Return the list of Pools in a array
343 public function Get_Pools_List()
345 $pool_list = array();
348 $query = "SELECT Name, PoolId FROM Pool";
350 $result = $this->db_link->query ( $query );
352 if( PEAR::isError( $result ) ) {
353 die( "Unable to get the pool list from catalog" );
355 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
356 array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
362 public function Get_BackupJob_Names()
364 $query = "SELECT Name FROM Job GROUP BY Name";
365 $backupjobs = array();
367 $result = $this->db_link->query( $query );
369 if (PEAR::isError( $result ) ) {
370 die("Unable to get BackupJobs list from catalog" );
372 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
373 array_push( $backupjobs, $backupjob["Name"] );
379 // Return elasped time string for a job
380 function Get_ElapsedTime( $start_time, $end_time )
382 $diff = $end_time - $start_time;
384 $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
385 $diff -= $daysDiff*60*60*24;
387 $hrsDiff = sprintf("%02d", floor($diff/60/60) );
388 $diff -= $hrsDiff*60*60;
390 $minsDiff = sprintf("%02d", floor($diff/60) );
391 $diff -= $minsDiff*60;
392 $secsDiff = sprintf("%02d", $diff );
395 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
397 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
400 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
405 // Interval calculation
406 $end_date = mktime();
407 $start_date = $end_date - $delay;
409 $start_date = date( "Y-m-d H:i:s", $start_date );
410 $end_date = date( "Y-m-d H:i:s", $end_date );
412 switch( $this->driver )
415 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
416 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
419 $result = $this->db_link->query( $query );
421 if( PEAR::isError($result) ){
422 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
424 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
425 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
426 $total_elapsed += $time['elapsed'];
428 // Verify if elapsed time is more than 1 day
429 if ( $total_elapsed > LAST_DAY ) {
430 return date("%d days H:i:s", $total_elapsed );
432 return date("H:i:s", $total_elapsed );
437 // Return Jobs statistics for a specific interval such as
438 // - Completed jobs number
439 // - Failed jobs number
440 // - Waiting jobs number
441 // The returned values will be used by a Bgraph classe
442 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
450 // Interval calculation
451 $end_date = mktime();
452 $start_date = $end_date - $delay;
454 $start_date = date( "Y-m-d H:i:s", $start_date );
455 $end_date = date( "Y-m-d H:i:s", $end_date );
457 $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
463 $where = $interval_where . "JobStatus = 'T' ";
464 $label = "Completed";
466 case 'terminated_errors':
467 $where = $interval_where . "JobStatus = 'E' ";
468 $label = "Terminated with errors";
471 $where = $interval_where . "JobStatus = 'f' ";
475 $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
479 $where = "JobStatus = 'C' ";
480 $label = "Created but not running";
483 $where = "JobStatus = 'R' ";
487 $where = $interval_where . "JobStatus IN ('e','f') ";
492 $query = 'SELECT COUNT(JobId) AS ' . $type . ' ';
493 $query .= 'FROM Job ';
494 $query .= "WHERE $where ";
496 //echo 'query = ' . $query . '<br />';
498 $jobs = $this->db_link->query( $query );
500 if (PEAR::isError( $jobs ) ) {
501 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
503 $res = $jobs->fetchRow();
504 return array( $label , current($res) );
506 } // end function GetJobsStatistics()
508 public function CountVolumesByPool( $pools )
510 foreach( $pools as $pool_name => $pool ) {
512 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
513 //echo $query . '<br />';
514 //echo 'Pool name ' . $pool_name . '<br />';
515 $result = $this->db_link->query( $query );
517 if( PEAR::isError( $result ) ) {
518 die("Unable to get volume number from catalog");
520 $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
521 return array( $pool_name, $nb_vol['nb_vol'] );
526 public function GetStoredFiles( $delay = LAST_DAY )
530 $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
532 // Interval calculation
533 $end_date = mktime();
534 $start_date = $end_date - $delay;
536 $start_date = date( "Y-m-d H:i:s", $start_date );
537 $end_date = date( "Y-m-d H:i:s", $end_date );
540 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
542 $result = $this->db_link->query( $query );
544 if( !PEAR::isError($result) ) {
545 $nbfiles = $result->fetchRow(DB_FETCHMODE_ASSOC);
546 $totalfiles = $totalfiles + $nbfiles['stored_files'];
548 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
554 public function GetStoredBytes( $delay = LAST_DAY )
556 $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
558 // Interval calculation
559 $end_date = mktime();
560 $start_date = $end_date - $delay;
562 $start_date = date( "Y-m-d H:i:s", $start_date );
563 $end_date = date( "Y-m-d H:i:s", $end_date );
566 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
568 $result = $this->db_link->query( $query );
570 if( PEAR::isError( $result ) ) {
571 die( "Unable to get Job Bytes from catalog" );
573 return $result->fetchRow( DB_FETCHMODE_ASSOC );
577 public function GetStoredBytesByInterval( $start_date, $end_date )
579 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
581 $result = $this->db_link->query( $query );
583 if( PEAR::isError( $result ) ) {
584 die( "Unable to get Job Bytes from catalog" );
587 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
589 $day = date( "D d", strtotime($end_date) );
591 if( isset( $tmp['stored_bytes'] ) ) {
592 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
593 $hbytes = explode( " ", $hbytes );
594 $stored_bytes = $hbytes[0];
597 return array( $day, $stored_bytes );
601 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
603 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
604 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
605 $query .= "Name = '$jobname'";
607 $result = $this->db_link->query( $query );
609 if( PEAR::isError( $result ) ) {
610 die( "Unable to get Job Bytes from catalog" );
613 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
615 $day = date( "D d", strtotime($end_date) );
617 if( isset( $tmp['stored_bytes'] ) ) {
618 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
619 $hbytes = explode( " ", $hbytes );
620 $stored_bytes = $hbytes[0];
623 return array( $day, $stored_bytes );
627 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
629 $query = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
630 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
631 $query .= "Name = '$jobname'";
633 $result = $this->db_link->query( $query );
635 if( PEAR::isError( $result ) ) {
636 die( "Unable to get Job Files from catalog" );
639 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
641 $day = date( "D d", strtotime($end_date) );
642 $stored_files = $tmp['stored_files'];
644 return array( $day, $stored_files );