3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004-2005 Juan Luis Frances Jiminez |
6 | This program is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU General Public License |
8 | as published by the Free Software Foundation; either version 2 |
9 | of the License, or (at your option) any later version. |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 +-------------------------------------------------------------------------+
17 require_once "paths.php";
18 require_once "DB.php"; // Pear DB
19 require_once "config.inc.php";
20 require_once "bgraph.inc.php";
22 class Bweb extends DB {
30 public $db_link; // Database link
31 private $db_dsn; // Data Source Name
33 private $config_file; // Config filename
34 private $config; // Loaded config from bacula.conf
35 private $catalogs = array(); // Catalog array
37 function __construct()
39 $this->catalogs = array();
41 // Loading configuration
42 $this->config_file = getcwd() . '/configs/bacula.conf';
44 if( !$this->load_config() )
45 die( "Unable to load configuration");
47 // Construct a valid dsn
48 $this->db_dsn['hostspec'] = $this->catalogs[0]["host"];
49 $this->db_dsn['username'] = $this->catalogs[0]["login"];
50 $this->db_dsn['password'] = $this->catalogs[0]["pass"];
51 $this->db_dsn['database'] = $this->catalogs[0]["db_name"];
52 $this->db_dsn['phptype'] = $this->catalogs[0]["db_type"];
55 $this->db_link = $this->connect($this->db_dsn);
57 if (DB::isError($this->db_link)) {
58 die($this->db_link->getMessage());
60 $this->driver = $this->db_dsn['phptype'];
61 register_shutdown_function(array(&$this,'close'));
62 $this->dbs_name = $this->db_dsn['database'];
66 function load_config()
68 $this->config = parse_ini_file( $this->config_file, true );
70 if( !$this->config == false ) {
71 // Loading database connection information
72 foreach( $this->config as $parameter => $value )
74 //echo "Param $parameter = $value <br />";
75 if( is_array($value) ){ // Parsing database section
76 array_push( $this->catalogs, $value );
84 public function get_config_param( $param )
86 if( isset( $this->config[$param] ) )
87 return $this->config[$param];
92 public function Get_Nb_Catalogs()
94 return count( $this->catalogs );
100 $this->db_link->disconnect();
105 function CalculateBytesPeriod($server,$StartDate,$EndPeriod) { // Bytes transferred in a period.
107 $result =& $this->db_link->query("select SUM(JobBytes) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
108 or die("classes.inc: Error query: 1");
109 $return =& $result->fetchRow();
115 function CalculateFilesPeriod($server,$StartDate,$EndPeriod) { // Number of files transferred in a period.
117 $result =& $this->db_link->query("select SUM(JobFiles) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
118 or die("classes.inc: Error query: 2");
119 $return =& $result->fetchRow();
125 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear) { // Convert date for Smarty. Check if only works with Mysql.
127 $this->StartDate=$StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
128 $this->EndDate=$EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:59"; // last day full
132 // Return humanized size with default unit of GB
133 // if auto provide for unit argument, automaticaly decide which unit
134 function human_file_size( $size, $decimal = 2, $unit = 'auto' )
138 $units = array('B','KB','MB','GB','TB');
145 if ( $hsize >= 1024 ) {
146 $hsize = $hsize / 1024;
156 $p = array_search( $unit, $units);
157 $hsize = $hsize / pow(1024,$p);
161 $hsize = sprintf("%." . $decimal . "f", $hsize);
162 $hsize = $hsize . ' ' . $units[$unit_id];
172 switch( $this->driver )
175 $query = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
176 $query .= "FROM information_schema.TABLES ";
177 $query .= "WHERE table_schema = 'bacula' ";
178 $query .= "GROUP BY table_schema";
181 $query = "SELECT pg_database_size('bacula') AS dbsize";
184 // Not yet implemented
189 $result = $this->db_link->query( $query );
191 if(! PEAR::isError( $result ) )
193 $db = $result->fetchRow( DB_FETCHMODE_ASSOC );
194 $database_size =+ $db['dbsize'];
196 die( "Unable to get database size<br />" . $jobs->getMessage() );
198 return $this->human_file_size( $database_size );
199 } // end function GetDbSize()
201 public function Get_Nb_Clients()
203 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
204 if( PEAR::isError($clients) )
205 die( "Unable to get client number" );
207 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
210 // Return an array of volumes ordered by poolid and volume name
211 function GetVolumeList() {
217 // Get the list of pools id
218 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
220 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
221 $pools = $this->db_link->query( $query );
223 if( PEAR::isError( $pools ) )
224 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
226 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
227 switch( $this->driver )
230 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
231 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
232 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
235 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
236 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
237 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
240 $query = ""; // not yet implemented
246 $medias = $this->db_link->query( $query );
248 if( PEAR::isError( $medias ) ) {
249 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
251 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
253 // Create array key for each pool
254 if( !array_key_exists( $pool['name'], $volumes) )
256 $volumes[ $pool['name'] ] = array();
258 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
262 // If the pool is empty (no volumes in this pool)
263 if( $medias->numRows() == 0 ) {
264 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
266 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
267 // Calculate expiration date if the volume is Full
268 if( $media['volstatus'] == 'Full' ) {
269 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
270 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
272 $media['expire'] = 'N/A';
274 // Media used bytes in a human format
275 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
277 $media['lastwritten'] = "N/A";
278 $media['expire'] = "N/A";
279 $media['volbytes'] = "0 KB";
283 if( count( $volumes[ $pool['name'] ] ) % 2)
284 $media['class'] = 'odd';
286 // Add the media in pool array
287 array_push( $volumes[ $pool['name']], $media);
293 } // end function GetVolumeList()
295 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
297 $end_date = mktime();
298 $start_date = $end_date - $delay;
300 $start_date = date( "Y-m-d H:i:s", $start_date );
301 $end_date = date( "Y-m-d H:i:s", $end_date );
303 $query = "SELECT COUNT(JobId) as jobs FROM Job ";
304 $query .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
305 $query .= "Level = '$level' ";
307 $result = $this->db_link->query( $query );
309 if (PEAR::isError( $result ) ) {
310 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
312 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC );
313 return $jobs['jobs'];
318 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
320 $query = "SELECT COUNT(JobId) AS job_nb FROM Job ";
324 // Interval condition for SQL query
325 if( $delay != ALL ) {
326 $end_date = mktime();
327 $start_date = $end_date - $delay;
329 $start_date = date( "Y-m-d H:i:s", $start_date );
330 $end_date = date( "Y-m-d H:i:s", $end_date );
332 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
335 if( $status != 'any' ) {
339 $where_status = "JobStatus = 'T' ";
342 $where_status = "JobStatus IN ('f','E') ";
345 $where_status = "JobStatus = 'A' ";
348 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
353 if( !empty($where_delay) )
354 $query = $query . $where_delay . 'AND ' . $where_status;
356 if( !empty($where_status) )
357 $query = $query . 'WHERE ' . $where_status;
360 $jobs = $this->db_link->query( $query );
362 if (PEAR::isError( $jobs ) ) {
363 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
365 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC );
366 return $jobs['job_nb'];
370 // Return the list of Pools in a array
371 public function Get_Pools_List()
373 $pool_list = array();
376 $query = "SELECT Name, PoolId FROM Pool";
378 $result = $this->db_link->query ( $query );
380 if( PEAR::isError( $result ) ) {
381 die( "Unable to get the pool list from catalog" );
383 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
384 array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
390 public function Get_BackupJob_Names()
392 $query = "SELECT Name FROM Job GROUP BY Name";
393 $backupjobs = array();
395 $result = $this->db_link->query( $query );
397 if (PEAR::isError( $result ) ) {
398 die("Unable to get BackupJobs list from catalog" );
400 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
401 array_push( $backupjobs, $backupjob["Name"] );
407 // Return elasped time string for a job
408 function Get_ElapsedTime( $start_time, $end_time )
410 $diff = $end_time - $start_time;
412 $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
413 $diff -= $daysDiff*60*60*24;
415 $hrsDiff = sprintf("%02d", floor($diff/60/60) );
416 $diff -= $hrsDiff*60*60;
418 $minsDiff = sprintf("%02d", floor($diff/60) );
419 $diff -= $minsDiff*60;
420 $secsDiff = sprintf("%02d", $diff );
423 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
425 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
428 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
433 // Interval calculation
434 $end_date = mktime();
435 $start_date = $end_date - $delay;
437 $start_date = date( "Y-m-d H:i:s", $start_date );
438 $end_date = date( "Y-m-d H:i:s", $end_date );
440 switch( $this->driver )
443 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
444 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
447 $result = $this->db_link->query( $query );
449 if( PEAR::isError($result) ){
450 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
452 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
453 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
454 $total_elapsed += $time['elapsed'];
456 // Verify if elapsed time is more than 1 day
457 if ( $total_elapsed > LAST_DAY ) {
458 return date("%d days H:i:s", $total_elapsed );
460 return date("H:i:s", $total_elapsed );
465 // Return Jobs statistics for a specific interval such as
466 // - Completed jobs number
467 // - Failed jobs number
468 // - Waiting jobs number
469 // The returned values will be used by a Bgraph classe
470 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
478 // Interval calculation
479 $end_date = mktime();
480 $start_date = $end_date - $delay;
482 $start_date = date( "Y-m-d H:i:s", $start_date );
483 $end_date = date( "Y-m-d H:i:s", $end_date );
485 $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
491 $where = $interval_where . "JobStatus = 'T' ";
492 $label = "Completed";
494 case 'terminated_errors':
495 $where = $interval_where . "JobStatus = 'E' ";
496 $label = "Terminated with errors";
499 $where = $interval_where . "JobStatus = 'f' ";
503 $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
507 $where = "JobStatus = 'C' ";
508 $label = "Created but not running";
511 $where = "JobStatus = 'R' ";
515 $where = $interval_where . "JobStatus IN ('e','f') ";
520 $query = 'SELECT COUNT(JobId) AS ' . $type . ' ';
521 $query .= 'FROM Job ';
522 $query .= "WHERE $where ";
524 //echo 'query = ' . $query . '<br />';
526 $jobs = $this->db_link->query( $query );
528 if (PEAR::isError( $jobs ) ) {
529 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
531 $res = $jobs->fetchRow();
532 return array( $label , current($res) );
534 } // end function GetJobsStatistics()
536 public function GetPoolsStatistics( $pools )
538 foreach( $pools as $pool_name => $pool ) {
540 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
541 //echo $query . '<br />';
542 //echo 'Pool name ' . $pool_name . '<br />';
543 $result = $this->db_link->query( $query );
545 if( PEAR::isError( $result ) ) {
546 die("Unable to get volume number from catalog");
548 $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
549 return array( $pool_name, $nb_vol['nb_vol'] );
554 public function GetStoredFiles( $delay = LAST_DAY )
558 $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
560 // Interval calculation
561 $end_date = mktime();
562 $start_date = $end_date - $delay;
564 $start_date = date( "Y-m-d H:i:s", $start_date );
565 $end_date = date( "Y-m-d H:i:s", $end_date );
568 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
570 $result = $this->db_link->query( $query );
572 if( !PEAR::isError($result) ) {
573 $nbfiles = $result->fetchRow(DB_FETCHMODE_ASSOC);
574 $totalfiles = $totalfiles + $nbfiles['stored_files'];
576 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
582 public function GetStoredBytes( $delay = LAST_DAY )
584 $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
586 // Interval calculation
587 $end_date = mktime();
588 $start_date = $end_date - $delay;
590 $start_date = date( "Y-m-d H:i:s", $start_date );
591 $end_date = date( "Y-m-d H:i:s", $end_date );
594 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
596 $result = $this->db_link->query( $query );
598 if( PEAR::isError( $result ) ) {
599 die( "Unable to get Job Bytes from catalog" );
601 return $result->fetchRow( DB_FETCHMODE_ASSOC );
605 public function GetStoredBytesByInterval( $start_date, $end_date )
607 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
609 $result = $this->db_link->query( $query );
611 if( PEAR::isError( $result ) ) {
612 die( "Unable to get Job Bytes from catalog" );
615 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
617 $day = date( "D d", strtotime($end_date) );
619 if( isset( $tmp['stored_bytes'] ) ) {
620 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
621 $hbytes = explode( " ", $hbytes );
622 $stored_bytes = $hbytes[0];
625 return array( $day, $stored_bytes );
629 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
631 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
632 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
633 $query .= "Name = '$jobname'";
635 $result = $this->db_link->query( $query );
637 if( PEAR::isError( $result ) ) {
638 die( "Unable to get Job Bytes from catalog" );
641 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
643 $day = date( "D d", strtotime($end_date) );
645 if( isset( $tmp['stored_bytes'] ) ) {
646 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
647 $hbytes = explode( " ", $hbytes );
648 $stored_bytes = $hbytes[0];
651 return array( $day, $stored_bytes );
655 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
657 $query = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
658 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
659 $query .= "Name = '$jobname'";
661 $result = $this->db_link->query( $query );
663 if( PEAR::isError( $result ) ) {
664 die( "Unable to get Job Files from catalog" );
667 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
669 $day = date( "D d", strtotime($end_date) );
670 $stored_files = $tmp['stored_files'];
672 return array( $day, $stored_files );