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 +-------------------------------------------------------------------------+
18 define('CONFIG_DIR', "configs");
19 define('CONFIG_FILE', "bacula.conf");
20 define('BACULA_TYPE_BYTES_FILES', 1);
21 define('BACULA_TYPE_FILES_JOBID', 2);
22 define('BACULA_TYPE_BYTES_ENDTIME_ALLJOBS', 69);
24 require_once "paths.php";
25 require_once "DB.php"; // Pear DB
26 require_once "config.inc.php";
27 require_once "bgraph.inc.php";
28 require_once($smarty_path."Config_File.class.php");
30 if (!function_exists('array_fill')) { // For PHP < 4.2.0 users
31 require_once('array_fill.func.php');
34 class Bweb extends DB {
42 public $db_link; // Database link
43 private $db_dsn; // Data Source Name
45 private $config_file; // Config filename
46 private $config; // Loaded config from bacula.conf
47 private $catalogs = array(); // Catalog array
49 function __construct()
51 $this->catalogs = array();
53 // Loading configuration
54 $this->config_file = getcwd() . '/configs/bacula.conf';
55 if( !$this->load_config() )
56 die( "Unable to load configuration");
58 //echo "Number of catalog defined " . count($this->catalogs) . "<br />";
61 $conf = new Config_File (CONFIG_DIR);
65 $sections = $conf->get(CONFIG_FILE,"DATABASE","host");
66 array_push($this->dbs, "DATABASE");
68 while ( !empty($sections) ) {
69 $sections = $conf->get(CONFIG_FILE,"DATABASE".$i,"host");
70 if ( !empty($sections) )
71 array_push($this->dbs,"DATABASE".$i);
78 if ( !empty($_POST['sel_database']) ) {
79 $_SESSION['DATABASE'] = $_POST['sel_database'];
80 $sec = $_POST['sel_database'];
82 if (isset($_SESSION['DATABASE']) )
83 $sec = $_SESSION['DATABASE'];
89 $this->dsn['hostspec'] = $conf->get(CONFIG_FILE,$sec,"host");
90 $this->dsn['username'] = $conf->get(CONFIG_FILE,$sec,"login");
91 $this->dsn['password'] = $conf->get(CONFIG_FILE,$sec,"pass");
92 $this->dsn['database'] = $conf->get(CONFIG_FILE,$sec,"db_name");
93 $this->dsn['phptype'] = $conf->get(CONFIG_FILE,$sec,"db_type"); // mysql, pgsql
95 if ( $conf->get(CONFIG_FILE,$sec,"db_port") )
96 $this->dsn[port] = $conf->get(CONFIG_FILE,$sec,"db_port");
99 // Construct a valid dsn
100 $this->db_dsn['hostspec'] = $this->catalogs[0]["host"];
101 $this->db_dsn['username'] = $this->catalogs[0]["login"];
102 $this->db_dsn['password'] = $this->catalogs[0]["pass"];
103 $this->db_dsn['database'] = $this->catalogs[0]["db_name"];
104 $this->db_dsn['phptype'] = $this->catalogs[0]["db_type"];
107 $this->db_link = $this->connect($this->db_dsn);
109 if (DB::isError($this->db_link)) {
110 die($this->db_link->getMessage());
112 $this->driver = $this->db_dsn['phptype'];
113 register_shutdown_function(array(&$this,'close'));
114 $this->dbs_name = $this->db_dsn['database'];
118 function load_config()
120 $this->config = parse_ini_file( $this->config_file, true );
122 if( !$this->config == false ) {
123 // Loading database connection information
124 foreach( $this->config as $parameter => $value )
126 //echo "Param $parameter = $value <br />";
127 if( is_array($value) ){ // Parsing database section
128 array_push( $this->catalogs, $value );
136 public function get_config_param( $param )
138 if( isset( $this->config[$param] ) )
139 return $this->config[$param];
144 public function Get_Nb_Catalogs()
146 return count( $this->catalogs );
152 $this->db_link->disconnect();
157 function CalculateBytesPeriod($server,$StartDate,$EndPeriod) { // Bytes transferred in a period.
159 $result =& $this->db_link->query("select SUM(JobBytes) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
160 or die("classes.inc: Error query: 1");
161 $return =& $result->fetchRow();
167 function CalculateFilesPeriod($server,$StartDate,$EndPeriod) { // Number of files transferred in a period.
169 $result =& $this->db_link->query("select SUM(JobFiles) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
170 or die("classes.inc: Error query: 2");
171 $return =& $result->fetchRow();
177 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear) { // Convert date for Smarty. Check if only works with Mysql.
179 $this->StartDate=$StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
180 $this->EndDate=$EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:59"; // last day full
184 function GetDataVolumes() {
187 $res = $this->db_link->query("SELECT Name FROM Pool");
188 while ( $tmp =& $res->fetchRow() ) {
189 if ($this->driver == "mysql" )
190 $result = $this->db_link->query("select Media.VolumeName, Media.VolBytes,Media.VolStatus,Pool.Name,Media.MediaType,Media.LastWritten,FROM_UNIXTIME(UNIX_TIMESTAMP(Media.LastWritten)+Media.VolRetention ) as expire from Pool LEFT JOIN Media ON Media.PoolId=Pool.PoolId where Name='$tmp[0]' order by Media.VolumeName");
191 else if ($this->driver == "pgsql")
192 $result = $this->db_link->db_query("select Media.VolumeName, Media.VolBytes,Media.VolStatus,Pool.Name,Media.MediaType,Media.LastWritten, Media.LastWritten + Media.VolRetention * interval '1 second' as expire from Pool LEFT JOIN Media ON Media.PoolId=Pool.PoolId where Name='$tmp[0]' order by Media.VolumeName");
193 while ( $tmp1 = $result->fetchRow() ) {
194 $pos = array_key_exists($tmp[0],$volume);
196 array_push($volume["$tmp[0]"],$tmp1);
198 $volume += array($tmp[0]=>array($tmp1));
207 function human_file_size( $size, $decimal = 2 )
211 $units = array('B','KB','MB','GB','TB');
215 if ( $hsize >= 1024 ) {
216 $hsize = $hsize / 1024;
224 $hsize = sprintf("%." . $decimal . "f", $hsize);
225 return $hsize . ' ' . $units[$unit_id];
232 if ( $this->driver == "mysql") {
233 $dbsize = $this->db_link->query("show table status") or die ("classes.inc: Error query: 3");
235 if ( $dbsize->numRows() ) {
236 while ( $res = $dbsize->fetchRow(DB_FETCHMODE_ASSOC) )
237 $database_size += $res["Data_length"];
242 else if ( $this->driver == "pgsql") {
243 $dbsize = $this->db_link->query("select pg_database_size('$this->dbs_name')") or die ("classes.inc: Error query: 4");
245 if (PEAR::isError($dbsize))
246 die($dbsize->getMessage());
248 if ( $dbsize->numRows() ) {
249 while ( $res = $dbsize->fetchRow() )
250 $database_size += $res[0];
258 return $this->human_file_size( $database_size );
259 } // end function GetDbSize()
261 public function Get_Nb_Clients()
263 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
264 if( PEAR::isError($clients) )
265 die( "Unable to get client number" );
267 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
270 // Return an array of volumes ordered by poolid and volume name
271 function GetVolumeList() {
277 // Get the list of pools id
278 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
280 $this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
281 $pools = $this->db_link->query( $query );
283 if( PEAR::isError( $pools ) )
284 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
286 while( $pool = $pools->fetchRow() ) {
287 switch( $this->driver )
291 $query = "SELECT Media.VolumeName, Media.VolBytes, Media.VolStatus, Pool.Name, Media.MediaType,Media.LastWritten, FROM_UNIXTIME(UNIX_TIMESTAMP(Media.LastWritten)+Media.VolRetention ) AS expire
292 FROM Pool LEFT JOIN Media ON Media.PoolId=Pool.PoolId WHERE poolid='$pool[0]'
293 ORDER BY Media.VolumeName";
295 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
296 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
297 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
300 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
301 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
302 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
304 $query = "SELECT Media.VolumeName, Media.VolBytes,Media.VolStatus,Pool.Name,Media.MediaType,Media.LastWritten, Media.LastWritten + Media.VolRetention * interval '1 second' AS expire
305 FROM Pool LEFT JOIN Media ON media.poolid=pool.poolid WHERE poolid='$pool[0]'
306 ORDER BY Media.VolumeName";
310 $query = ""; // not yet implemented
316 $this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
317 $medias = $this->db_link->query( $query );
319 if( PEAR::isError( $medias ) ) {
320 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
322 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
324 // Create array key for each pool
325 if( !array_key_exists( $pool['name'], $volumes) )
327 $volumes[ $pool['name'] ] = array();
329 while( $media = $medias->fetchRow() ) {
333 // If the pool is empty (no volumes in this pool)
334 if( $medias->numRows() == 0 ) {
335 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
337 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
338 // Calculate expiration date if the volume is Full
339 if( $media['volstatus'] == 'Full' ) {
340 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
341 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
343 $media['expire'] = 'N/A';
345 // Media used bytes in a human format
346 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
347 //echo "volume " . $media['volumename'] . 'vol bytes' .$media['volbytes'] . '<br />';
349 $media['lastwritten'] = "N/A";
350 $media['expire'] = "N/A";
351 $media['volbytes'] = "0 KB";
353 // Add the media in pool array
354 array_push( $volumes[ $pool['name']], $media);
360 } // end function GetVolumeList()
362 public function GetLastJobs( $delay = LAST_DAY )
368 // Interval calculation
369 $end_date = mktime();
370 $start_date = $end_date - $delay;
372 $start_date = date( "Y-m-d H:m:s", $start_date );
373 $end_date = date( "Y-m-d H:m:s", $end_date );
375 switch( $this->driver )
378 $query = 'SELECT COUNT(JobId) AS completed_jobs ';
379 $query .= 'FROM Job ';
380 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
381 $query .= "AND JobStatus = 'T'";
385 $jobs = $this->db_link->query( $query );
387 if (PEAR::isError( $jobs ) ) {
388 die( "Unable to get last completed jobs status from catalog<br />" . $status->getMessage() );
390 return $jobs->fetchRow();
392 } // end function GetLastJobStatus()
394 public function GetLastErrorJobs( $delay = LAST_DAY )
400 // Interval calculation
401 $end_date = mktime();
402 $start_date = $end_date - $delay;
404 $start_date = date( "Y-m-d H:m:s", $start_date );
405 $end_date = date( "Y-m-d H:m:s", $end_date );
407 //echo "start date: $start_date <br />";
408 //echo "end date: $end_date <br />";
410 switch( $this->driver )
413 $query = 'SELECT COUNT(JobId) AS failed_jobs ';
414 $query .= 'FROM Job ';
415 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
416 $query .= "AND JobStatus = 'f'";
419 $result = $this->db_link->query( $query );
421 if (PEAR::isError( $result ) ) {
422 die( "Unable to get last failed jobs status from catalog<br />query = $query <br />" . $result->getMessage() );
424 return $result->fetchRow( DB_FETCHMODE_ASSOC );
426 } // end function GetLastErrorJobs
428 public function Get_BackupJob_Names()
430 $query = "SELECT Name FROM Job GROUP BY Name";
431 $backupjobs = array();
433 $result = $this->db_link->query( $query );
435 if (PEAR::isError( $result ) ) {
436 die("Unable to get BackupJobs list from catalog" );
438 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
439 array_push( $backupjobs, $backupjob["Name"] );
445 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
450 // Interval calculation
451 $end_date = mktime();
452 $start_date = $end_date - $delay;
454 $start_date = date( "Y-m-d H:m:s", $start_date );
455 $end_date = date( "Y-m-d H:m:s", $end_date );
457 switch( $this->driver )
460 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
461 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
464 $result = $this->db_link->query( $query );
466 if( PEAR::isError($result) ){
467 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
469 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
470 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
471 $total_elapsed += $time['elapsed'];
473 // Verify if elapsed time is more than 1 day
474 if ( $total_elapsed > 86400 ) {
475 return gmstrftime("%d days %H:%M:%S", $total_elapsed );
477 return gmstrftime("%H:%M:%S", $total_elapsed );