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 "config.inc.php";
19 class Bweb extends DB {
28 public $db_link; // Database link
29 private $db_dsn; // Data Source Name
31 private $config_file; // Config filename
32 private $config; // Loaded config from bacula.conf
33 private $catalogs = array(); // Catalog array
35 function __construct()
37 $this->catalogs = array();
39 // Loading configuration
40 $this->config_file = getcwd() . '/configs/bacula.conf';
42 if( !$this->load_config() )
43 die( "Unable to load configuration");
45 // Construct a valid dsn
46 $this->db_dsn['hostspec'] = $this->catalogs[0]["host"];
47 $this->db_dsn['username'] = $this->catalogs[0]["login"];
48 $this->db_dsn['password'] = $this->catalogs[0]["pass"];
49 $this->db_dsn['database'] = $this->catalogs[0]["db_name"];
50 $this->db_dsn['phptype'] = $this->catalogs[0]["db_type"];
53 $this->db_link = $this->connect($this->db_dsn);
55 if (DB::isError($this->db_link)) {
56 die($this->db_link->getMessage());
58 $this->driver = $this->db_dsn['phptype'];
59 register_shutdown_function(array(&$this,'close'));
60 $this->dbs_name = $this->db_dsn['database'];
63 // Initialize smarty template classe
65 // Initialize smarty gettext function
66 $this->init_gettext();
69 // Initialize Smarty template classe
72 $this->tpl = new Smarty();
74 $this->tpl->compile_check = true;
75 $this->tpl->debugging = false;
76 $this->tpl->force_compile = true;
78 $this->tpl->template_dir = "./templates";
79 $this->tpl->compile_dir = "./templates_c";
80 $this->tpl->config_dir = "./configs";
83 function init_gettext()
85 global $smarty_gettext_path;
87 if ( function_exists("gettext") ) {
88 require_once( BW_SMARTY_GETTEXT . "smarty_gettext.php" );
89 $this->tpl->register_block('t','smarty_translate');
91 $language = $this->get_config_param("lang");
93 putenv("LANG=$language");
94 setlocale(LC_ALL, $language);
95 bindtextdomain($domain,"./locale");
99 function smarty_translate($params, $text, &$smarty) {
102 $smarty->register_block('t','smarty_translate');
106 function load_config()
108 $this->config = parse_ini_file( $this->config_file, true );
110 if( !$this->config == false ) {
111 // Loading database connection information
112 foreach( $this->config as $parameter => $value )
114 //echo "Param $parameter = $value <br />";
115 if( is_array($value) ){ // Parsing database section
116 array_push( $this->catalogs, $value );
124 public function get_config_param( $param )
126 if( isset( $this->config[$param] ) )
127 return $this->config[$param];
132 public function Get_Nb_Catalogs()
134 return count( $this->catalogs );
140 $this->db_link->disconnect();
145 function CalculateBytesPeriod($server,$StartDate,$EndPeriod) { // Bytes transferred in a period.
147 $result =& $this->db_link->query("select SUM(JobBytes) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
148 or die("classes.inc: Error query: 1");
149 $return =& $result->fetchRow();
155 function CalculateFilesPeriod($server,$StartDate,$EndPeriod) { // Number of files transferred in a period.
157 $result =& $this->db_link->query("select SUM(JobFiles) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
158 or die("classes.inc: Error query: 2");
159 $return =& $result->fetchRow();
165 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear) { // Convert date for Smarty. Check if only works with Mysql.
167 $this->StartDate=$StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
168 $this->EndDate=$EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:59"; // last day full
172 // Return humanized size with default unit of GB
173 // if auto provide for unit argument, automaticaly decide which unit
174 function human_file_size( $size, $decimal = 2, $unit = 'auto' )
178 $units = array('B','KB','MB','GB','TB');
185 if ( $hsize >= 1024 ) {
186 $hsize = $hsize / 1024;
196 $p = array_search( $unit, $units);
197 $hsize = $hsize / pow(1024,$p);
201 $hsize = sprintf("%." . $decimal . "f", $hsize);
202 $hsize = $hsize . ' ' . $units[$unit_id];
212 switch( $this->driver )
215 $query = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
216 $query .= "FROM information_schema.TABLES ";
217 $query .= "WHERE table_schema = 'bacula' ";
218 $query .= "GROUP BY table_schema";
221 $query = "SELECT pg_database_size('bacula') AS dbsize";
224 // Not yet implemented
229 $result = $this->db_link->query( $query );
231 if(! PEAR::isError( $result ) )
233 $db = $result->fetchRow( DB_FETCHMODE_ASSOC );
234 $database_size =+ $db['dbsize'];
236 die( "Unable to get database size<br />" . $jobs->getMessage() );
238 return $this->human_file_size( $database_size );
239 } // end function GetDbSize()
241 public function Get_Nb_Clients()
243 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
244 if( PEAR::isError($clients) )
245 die( "Unable to get client number" );
247 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
250 // Return an array of volumes ordered by poolid and volume name
251 function GetVolumeList() {
257 // Get the list of pools id
258 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
260 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
261 $pools = $this->db_link->query( $query );
263 if( PEAR::isError( $pools ) )
264 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
266 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
267 switch( $this->driver )
270 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
271 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
272 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
275 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
276 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
277 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
280 $query = ""; // not yet implemented
286 $medias = $this->db_link->query( $query );
288 if( PEAR::isError( $medias ) ) {
289 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
291 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
293 // Create array key for each pool
294 if( !array_key_exists( $pool['name'], $volumes) )
296 $volumes[ $pool['name'] ] = array();
298 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
302 // If the pool is empty (no volumes in this pool)
303 if( $medias->numRows() == 0 ) {
304 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
306 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
307 // Calculate expiration date if the volume is Full
308 if( $media['volstatus'] == 'Full' ) {
309 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
310 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
312 $media['expire'] = 'N/A';
314 // Media used bytes in a human format
315 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
317 $media['lastwritten'] = "N/A";
318 $media['expire'] = "N/A";
319 $media['volbytes'] = "0 KB";
323 if( count( $volumes[ $pool['name'] ] ) % 2)
324 $media['class'] = 'odd';
326 // Add the media in pool array
327 array_push( $volumes[ $pool['name']], $media);
333 } // end function GetVolumeList()
335 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
337 $end_date = mktime();
338 $start_date = $end_date - $delay;
340 $start_date = date( "Y-m-d H:i:s", $start_date );
341 $end_date = date( "Y-m-d H:i:s", $end_date );
343 $query = "SELECT COUNT(JobId) as jobs FROM Job ";
344 $query .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
345 $query .= "Level = '$level' ";
347 $result = $this->db_link->query( $query );
349 if (PEAR::isError( $result ) ) {
350 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
352 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC );
353 return $jobs['jobs'];
358 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
360 $query = "SELECT COUNT(JobId) AS job_nb FROM Job ";
364 // Interval condition for SQL query
365 if( $delay != ALL ) {
366 $end_date = mktime();
367 $start_date = $end_date - $delay;
369 $start_date = date( "Y-m-d H:i:s", $start_date );
370 $end_date = date( "Y-m-d H:i:s", $end_date );
372 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
375 if( $status != 'any' ) {
379 $where_status = "JobStatus = 'T' ";
382 $where_status = "JobStatus IN ('f','E') ";
385 $where_status = "JobStatus = 'A' ";
388 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
393 if( !empty($where_delay) )
394 $query = $query . $where_delay . 'AND ' . $where_status;
396 if( !empty($where_status) )
397 $query = $query . 'WHERE ' . $where_status;
400 $jobs = $this->db_link->query( $query );
402 if (PEAR::isError( $jobs ) ) {
403 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
405 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC );
406 return $jobs['job_nb'];
410 // Return the list of Pools in a array
411 public function Get_Pools_List()
413 $pool_list = array();
416 $query = "SELECT Name, PoolId FROM Pool";
418 $result = $this->db_link->query ( $query );
420 if( PEAR::isError( $result ) ) {
421 die( "Unable to get the pool list from catalog" );
423 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
424 array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
430 public function Get_BackupJob_Names()
432 $query = "SELECT Name FROM Job GROUP BY Name";
433 $backupjobs = array();
435 $result = $this->db_link->query( $query );
437 if (PEAR::isError( $result ) ) {
438 die("Unable to get BackupJobs list from catalog" );
440 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
441 array_push( $backupjobs, $backupjob["Name"] );
447 // Return elasped time string for a job
448 function Get_ElapsedTime( $start_time, $end_time )
450 $diff = $end_time - $start_time;
452 $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
453 $diff -= $daysDiff*60*60*24;
455 $hrsDiff = sprintf("%02d", floor($diff/60/60) );
456 $diff -= $hrsDiff*60*60;
458 $minsDiff = sprintf("%02d", floor($diff/60) );
459 $diff -= $minsDiff*60;
460 $secsDiff = sprintf("%02d", $diff );
463 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
465 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
468 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
473 // Interval calculation
474 $end_date = mktime();
475 $start_date = $end_date - $delay;
477 $start_date = date( "Y-m-d H:i:s", $start_date );
478 $end_date = date( "Y-m-d H:i:s", $end_date );
480 switch( $this->driver )
483 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
484 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
487 $result = $this->db_link->query( $query );
489 if( PEAR::isError($result) ){
490 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
492 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
493 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
494 $total_elapsed += $time['elapsed'];
496 // Verify if elapsed time is more than 1 day
497 if ( $total_elapsed > LAST_DAY ) {
498 return date("%d days H:i:s", $total_elapsed );
500 return date("H:i:s", $total_elapsed );
505 // Return Jobs statistics for a specific interval such as
506 // - Completed jobs number
507 // - Failed jobs number
508 // - Waiting jobs number
509 // The returned values will be used by a Bgraph classe
510 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
518 // Interval calculation
519 $end_date = mktime();
520 $start_date = $end_date - $delay;
522 $start_date = date( "Y-m-d H:i:s", $start_date );
523 $end_date = date( "Y-m-d H:i:s", $end_date );
525 $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
531 $where = $interval_where . "JobStatus = 'T' ";
532 $label = "Completed";
534 case 'terminated_errors':
535 $where = $interval_where . "JobStatus = 'E' ";
536 $label = "Terminated with errors";
539 $where = $interval_where . "JobStatus = 'f' ";
543 $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
547 $where = "JobStatus = 'C' ";
548 $label = "Created but not running";
551 $where = "JobStatus = 'R' ";
555 $where = $interval_where . "JobStatus IN ('e','f') ";
560 $query = 'SELECT COUNT(JobId) AS ' . $type . ' ';
561 $query .= 'FROM Job ';
562 $query .= "WHERE $where ";
564 //echo 'query = ' . $query . '<br />';
566 $jobs = $this->db_link->query( $query );
568 if (PEAR::isError( $jobs ) ) {
569 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
571 $res = $jobs->fetchRow();
572 return array( $label , current($res) );
574 } // end function GetJobsStatistics()
576 public function GetPoolsStatistics( $pools )
578 foreach( $pools as $pool_name => $pool ) {
580 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
581 //echo $query . '<br />';
582 //echo 'Pool name ' . $pool_name . '<br />';
583 $result = $this->db_link->query( $query );
585 if( PEAR::isError( $result ) ) {
586 die("Unable to get volume number from catalog");
588 $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
589 return array( $pool_name, $nb_vol['nb_vol'] );
594 public function GetStoredFiles( $delay = LAST_DAY )
598 $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
600 // Interval calculation
601 $end_date = mktime();
602 $start_date = $end_date - $delay;
604 $start_date = date( "Y-m-d H:i:s", $start_date );
605 $end_date = date( "Y-m-d H:i:s", $end_date );
608 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
610 $result = $this->db_link->query( $query );
612 if( !PEAR::isError($result) ) {
613 $nbfiles = $result->fetchRow(DB_FETCHMODE_ASSOC);
614 $totalfiles = $totalfiles + $nbfiles['stored_files'];
616 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
622 public function GetStoredBytes( $delay = LAST_DAY )
624 $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
626 // Interval calculation
627 $end_date = mktime();
628 $start_date = $end_date - $delay;
630 $start_date = date( "Y-m-d H:i:s", $start_date );
631 $end_date = date( "Y-m-d H:i:s", $end_date );
634 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
636 $result = $this->db_link->query( $query );
638 if( PEAR::isError( $result ) ) {
639 die( "Unable to get Job Bytes from catalog" );
641 return $result->fetchRow( DB_FETCHMODE_ASSOC );
645 public function GetStoredBytesByInterval( $start_date, $end_date )
647 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
649 $result = $this->db_link->query( $query );
651 if( PEAR::isError( $result ) ) {
652 die( "Unable to get Job Bytes from catalog" );
655 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
657 $day = date( "D d", strtotime($end_date) );
659 if( isset( $tmp['stored_bytes'] ) ) {
660 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
661 $hbytes = explode( " ", $hbytes );
662 $stored_bytes = $hbytes[0];
665 return array( $day, $stored_bytes );
669 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
671 $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
672 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
673 $query .= "Name = '$jobname'";
675 $result = $this->db_link->query( $query );
677 if( PEAR::isError( $result ) ) {
678 die( "Unable to get Job Bytes from catalog" );
681 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
683 $day = date( "D d", strtotime($end_date) );
685 if( isset( $tmp['stored_bytes'] ) ) {
686 $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
687 $hbytes = explode( " ", $hbytes );
688 $stored_bytes = $hbytes[0];
691 return array( $day, $stored_bytes );
695 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
697 $query = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
698 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
699 $query .= "Name = '$jobname'";
701 $result = $this->db_link->query( $query );
703 if( PEAR::isError( $result ) ) {
704 die( "Unable to get Job Files from catalog" );
707 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
709 $day = date( "D d", strtotime($end_date) );
710 $stored_files = $tmp['stored_files'];
712 return array( $day, $stored_files );