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 human_file_size( $size, $decimal = 2 )
188 $units = array('B','KB','MB','GB','TB');
192 if ( $hsize >= 1024 ) {
193 $hsize = $hsize / 1024;
201 $hsize = sprintf("%." . $decimal . "f", $hsize);
202 return $hsize . ' ' . $units[$unit_id];
209 if ( $this->driver == "mysql") {
210 $dbsize = $this->db_link->query("show table status") or die ("classes.inc: Error query: 3");
212 if ( $dbsize->numRows() ) {
213 while ( $res = $dbsize->fetchRow(DB_FETCHMODE_ASSOC) )
214 $database_size += $res["Data_length"];
219 else if ( $this->driver == "pgsql") {
220 $dbsize = $this->db_link->query("select pg_database_size('$this->dbs_name')") or die ("classes.inc: Error query: 4");
222 if (PEAR::isError($dbsize))
223 die($dbsize->getMessage());
225 if ( $dbsize->numRows() ) {
226 while ( $res = $dbsize->fetchRow() )
227 $database_size += $res[0];
235 return $this->human_file_size( $database_size );
236 } // end function GetDbSize()
238 public function Get_Nb_Clients()
240 $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
241 if( PEAR::isError($clients) )
242 die( "Unable to get client number" );
244 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
247 // Return an array of volumes ordered by poolid and volume name
248 function GetVolumeList() {
254 // Get the list of pools id
255 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
257 $this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
258 $pools = $this->db_link->query( $query );
260 if( PEAR::isError( $pools ) )
261 die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
263 while( $pool = $pools->fetchRow() ) {
264 switch( $this->driver )
268 $query = "SELECT Media.VolumeName, Media.VolBytes, Media.VolStatus, Pool.Name, Media.MediaType,Media.LastWritten, FROM_UNIXTIME(UNIX_TIMESTAMP(Media.LastWritten)+Media.VolRetention ) AS expire
269 FROM Pool LEFT JOIN Media ON Media.PoolId=Pool.PoolId WHERE poolid='$pool[0]'
270 ORDER BY Media.VolumeName";
272 $query = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
273 FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
274 WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
277 $query = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
278 FROM media LEFT JOIN pool ON media.poolid = pool.poolid
279 WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
281 $query = "SELECT Media.VolumeName, Media.VolBytes,Media.VolStatus,Pool.Name,Media.MediaType,Media.LastWritten, Media.LastWritten + Media.VolRetention * interval '1 second' AS expire
282 FROM Pool LEFT JOIN Media ON media.poolid=pool.poolid WHERE poolid='$pool[0]'
283 ORDER BY Media.VolumeName";
287 $query = ""; // not yet implemented
293 $this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
294 $medias = $this->db_link->query( $query );
296 if( PEAR::isError( $medias ) ) {
297 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
299 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
301 // Create array key for each pool
302 if( !array_key_exists( $pool['name'], $volumes) )
304 $volumes[ $pool['name'] ] = array();
306 while( $media = $medias->fetchRow() ) {
310 // If the pool is empty (no volumes in this pool)
311 if( $medias->numRows() == 0 ) {
312 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
314 if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
315 // Calculate expiration date if the volume is Full
316 if( $media['volstatus'] == 'Full' ) {
317 $expire_date = strtotime($media['lastwritten']) + $media['volretention'];
318 $media['expire'] = strftime("%Y-%m-%d", $expire_date);
320 $media['expire'] = 'N/A';
322 // Media used bytes in a human format
323 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
324 //echo "volume " . $media['volumename'] . 'vol bytes' .$media['volbytes'] . '<br />';
326 $media['lastwritten'] = "N/A";
327 $media['expire'] = "N/A";
328 $media['volbytes'] = "0 KB";
330 // Add the media in pool array
331 array_push( $volumes[ $pool['name']], $media);
337 } // end function GetVolumeList()
339 public function GetLastJobs( $delay = LAST_DAY )
345 // Interval calculation
346 $end_date = mktime();
347 $start_date = $end_date - $delay;
349 $start_date = date( "Y-m-d H:m:s", $start_date );
350 $end_date = date( "Y-m-d H:m:s", $end_date );
352 switch( $this->driver )
355 $query = 'SELECT COUNT(JobId) AS completed_jobs ';
356 $query .= 'FROM Job ';
357 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
358 $query .= "AND JobStatus = 'T'";
362 $jobs = $this->db_link->query( $query );
364 if (PEAR::isError( $jobs ) ) {
365 die( "Unable to get last completed jobs status from catalog<br />" . $status->getMessage() );
367 return $jobs->fetchRow();
369 } // end function GetLastJobStatus()
371 public function GetLastErrorJobs( $delay = LAST_DAY )
377 // Interval calculation
378 $end_date = mktime();
379 $start_date = $end_date - $delay;
381 $start_date = date( "Y-m-d H:m:s", $start_date );
382 $end_date = date( "Y-m-d H:m:s", $end_date );
384 //echo "start date: $start_date <br />";
385 //echo "end date: $end_date <br />";
387 switch( $this->driver )
390 $query = 'SELECT COUNT(JobId) AS failed_jobs ';
391 $query .= 'FROM Job ';
392 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
393 $query .= "AND JobStatus = 'f'";
396 $result = $this->db_link->query( $query );
398 if (PEAR::isError( $result ) ) {
399 die( "Unable to get last failed jobs status from catalog<br />query = $query <br />" . $result->getMessage() );
401 return $result->fetchRow( DB_FETCHMODE_ASSOC );
403 } // end function GetLastErrorJobs
405 public function Get_BackupJob_Names()
407 $query = "SELECT Name FROM Job GROUP BY Name";
408 $backupjobs = array();
410 $result = $this->db_link->query( $query );
412 if (PEAR::isError( $result ) ) {
413 die("Unable to get BackupJobs list from catalog" );
415 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
416 array_push( $backupjobs, $backupjob["Name"] );
422 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
427 // Interval calculation
428 $end_date = mktime();
429 $start_date = $end_date - $delay;
431 $start_date = date( "Y-m-d H:m:s", $start_date );
432 $end_date = date( "Y-m-d H:m:s", $end_date );
434 switch( $this->driver )
437 $query = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
438 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
441 $result = $this->db_link->query( $query );
443 if( PEAR::isError($result) ){
444 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
446 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
447 //echo 'elapsed = ' . $time['elapsed'] . '<br />';
448 $total_elapsed += $time['elapsed'];
450 // Verify if elapsed time is more than 1 day
451 if ( $total_elapsed > 86400 ) {
452 return gmstrftime("%d days %H:%M:%S", $total_elapsed );
454 return gmstrftime("%H:%M:%S", $total_elapsed );