<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/htdocs/devclass.info.php,v 1.13 2009/04/19 03:53:40 wurley Exp $

# Required Libraries
require './common.php';
# @todo: Graph storage by storage pool

# Defaults
$devclasses = objectCache('devclasses');
$volumes = objectCache('volumes');
$drives = objectCache('drives');
$stgps = objectCache('stgps');
$occupancy = objectCache('occupancy');
$nodes = objectCache('nodes');

# Start
echo 'Show device class';
echo '<form action="cmd.php">';
printf('<input type="hidden" name="cmd" value="%s" />',get_request('cmd','REQUEST'));
printf('<input type="hidden" name="index" value="%s" />',$app['server']->getIndex());

echo '<select name="devclass" onchange="javascript:submit()">';
echo '<option value="">&nbsp;</option>';

foreach ($devclasses->getDevClasses() as $devc => $devclass) {
	printf('<option value="%s" %s>%s</option>',
		$devclass->getName(),
		(isset($_REQUEST['devclass']) && ($_REQUEST['devclass'] == $devclass->getName())) ? 'selected' : '',
		$devclass->getName());
}
echo '</select>';
echo '</form>';

if (isset($_REQUEST['devclass']) && trim($_REQUEST['devclass']))
	foreach ($devclasses->getDevClasses() as $devc => $devclass) {
		if ($_REQUEST['devclass'] != $devclass->getName())
			continue;

		echo '<table width="100%" class="page_detail" border=0>';
		printf(_('<tr><td>Device Class</td><td><b>%s</b></td><td>&nbsp;</td></tr>'),$devc);

		printf('<tr><td>Type</td><td>'.
			classValue(_('%s %s media is used for this device class.'),'value').
			'</td><td>&nbsp;</td></tr>',
			$devclass->access,
			$devclass->type);

		if (trim($devclass->library)) {
			printf('<tr><td>Library</td><td>'.
				classValue(_('%s library is used to store this media.'),'value').
				'</td><td>&nbsp;</td></tr>',
				$devclass->library);

			printf('<tr><td>&nbsp;</td><td>'.
				classValue(_('%s drives defined in this library.'),'value').
				'</td><td>&nbsp;</td></tr>',
				count($drives->getDrives($devclass->library)));
		}

		if (trim($devclass->dir))
			printf('<tr><td>Local Directory</td><td>'.
				classValue(_('%s directory will contain %s volumes for this device class.'),'value').
				'</td><td>&nbsp;</td></tr>',
				$devclass->dir,$devclass->capacity);

		echo '</table>';
		echo '<br />';
		printf(_('Storage Pools using device class %s.').'<br />',$devc);

		$stgcount = 0;
		foreach ($stgps->getStoragePools() as $stgp => $stgpool) {
			# Ignore storage pools not of this device class.
			if ($stgpool->devclass != $devc) continue;

			echo '<table width="100%" class="result" border=0>';
			# Title
			echo "\n\n";
			printf('<tr><td colspan=2 class="titlel">%s - %s (%s)</td><td class="titlec">&nbsp;</td></tr>',
				$stgpool->getName(),$stgpool->type,$stgpool->access);

			# Utlisation and Migration details
			echo "\n\n";
			switch ($stgpool->type) {
				case 'PRIMARY' :
					switch ($devclass->access) {
						case 'Sequential' :
							printf('<tr><td>&nbsp;</td><td>'.
								classValue(_('%s%% storage capacity utilised with %s%% volumes used.'),
									'value').
								'</td><td>&nbsp;</td></tr>',
								$stgpool->utilisation,$stgpool->migratable);
						break;

						case 'Random' :
							printf('<tr><td>&nbsp;</td><td>'.
								classValue(_('%s%% storage capacity utilised with %s%% migratable data.'),
									'value').
								'</td><td>&nbsp;</td></tr>',
								$stgpool->utilisation,$stgpool->migratable);
						break;

						default :
							printf('<tr><td colspan=2>Unknown access strategy %s.</td><td>',$devclass->access);
					}
				break;

				case 'COPY' :
					printf('<tr><td>&nbsp;</td><td>'.
						classValue(_('%s%% storage capacity utilised.'),'value').
						'</td><td>&nbsp;</td></tr>',
						$stgpool->utilisation);
				break;

				default :
					printf('<tr><td colspan=2><b>%s</b> %s.</td><td>',
						_('Unknown pooltype'),$stgpool->type);
			}

			# Max object size
			echo "\n\n";
			if ($stgpool->maxsize)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('%3.2fMB Maximum Object Size.'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->maxsize/1024/1024);

			if ($stgpool->reclaim)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('%s%% wasted space is required before reclaimation. Reclamation will use %s'),
						'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->reclaim,($stgpool->reclaimstgp ? $stgpool->reclaimstgp : $stgp));

			if ($stgpool->reclaimrunning)
				printf('<tr><td>&nbsp;</td><td>'.
					_('Reclamation IS currently running.').
					'</td><td>&nbsp;</td></tr>');

			if ($stgpool->reuse)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('%s days must pass before volumes will be reused.'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->reuse);

			# Migration details
			if ($stgpool->nextstgp)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('Migration to %s will occur at %s%% (stopping at %s%%)'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->nextstgp,$stgpool->migr['hi'],$stgpool->migr['low']);

			if ($stgpool->migr['running'])
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('Migration IS currently running. %sMB Migrated %ss'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->migr['mb'],$stgpool->migr['sec']);

			# Collocation Information
			if ($stgpool->collocate)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('%s Collocation.'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->collocate);

			# Disk Caching.
			if ($stgpool->cache)
				printf('<tr><td>&nbsp;</td><td>'.
					classValue(_('%s Migration caching.'),'value').
					'</td><td>&nbsp;</td></tr>',
					$stgpool->cache);

			$stgcount++;
			echo '<tr><td>&nbsp;</td></tr>';

			# Volume information.
			echo "\n\n";
			switch ($devclass->type) {
				case 'DISK' :
					if ($volumes->getStoragePoolVolumes($stgpool->getName()))
						foreach ($volumes->getStoragePoolVolumes($stgpool->getName()) as $volume) {

							printf('<tr><td>&nbsp;</td><td>'.
								classValue(_('Vol %s (%s%% %s) - %s.'),'value').
								'</td><td>&nbsp;</td></tr>',
								$volume->getName(),$volume->utilisation,
								$volume->status['volume'],
								$volume->access);
					}
					break;

				default :

					# Scratch Media
					$temp = '';
					if (count($volumes->getStoragePoolScratchVolumes($stgpool->getName())))
						$temp .= sprintf('<tr><td>&nbsp;</td><td>'.
							classValue(_('%s scratch media in use (of %s).'),'value').
							'</td><td>&nbsp;</td></tr>',
							count($volumes->getStoragePoolScratchVolumes($stgpool->getName())),$stgpool->maxscratch);

					if (count($volumes->getStoragePoolDefinedVolumes($stgpool->getName())))
						$temp .= sprintf('<tr><td>&nbsp;</td><td>'.
							classValue(_('%s defined media in use.'),'value').
							'</td><td>&nbsp;</td></tr>',
							count($volumes->getStoragePoolDefinedVolumes($stgpool->getName())));

					if ($temp) {
						printf('<tr><td colspan=3>%s</td></tr>',
							_('Media SCRATCH Status'));
						echo $temp;
						echo '<tr><td>&nbsp;</td></tr>';
						$temp = '';
					}

					# Media Access
					foreach (array('FULL','FILLING','PENDING','EMPTY') as $status) {
						if (count($volumes->getStoragePoolVolumesStatus($stgpool->getName(),$status)))
							$temp .= sprintf('<tr><td>&nbsp;</td><td>'.
								classValue(_('%s media %s.'),'value').
								'</td><td>&nbsp;</td></tr>',
								count($volumes->getStoragePoolVolumesStatus($stgpool->getName(),$status)),$status);
					}
					if ($temp) {
						printf('<tr><td colspan=3>%s</td></tr>',
							_('Media FILLING Status'));
						echo $temp;
						echo '<tr><td>&nbsp;</td></tr>';
						$temp = '';
					}

					foreach (array('READWRITE','READONLY','UNAVAILABLE','OFFSITE','DESTROYED') as $status) {
						if (count($volumes->getStoragePoolVolumesAccess($stgpool->getName(),$status)))
							$temp .= sprintf('<tr><td>&nbsp;</td><td>'.
								classValue(_('%s media %s.'),'value').
								'</td><td>&nbsp;</td></tr>',
								count($volumes->getStoragePoolVolumesAccess($stgpool->getName(),$status)),$status);
					}
					if ($temp) {
						printf('<tr><td colspan=3>%s</td></tr>',
							_('Media ACCESS Status'));
						echo $temp;
						echo '<tr><td>&nbsp;</td></tr>';
						$temp = '';
					}

					# Utilisation
					$stat['count'] = 0;
					$stat['util'] = 0;
					$stat['reclaim'] = 0;
					$stat['cap'] = 0;
					foreach ($volumes->getStoragePoolVolumes($stgpool->getName()) as $volume) {
						$stat['util'] += $volume->utilisation;
						$stat['reclaim'] += $volume->reclaim;
						$stat['cap'] += $volume->estcap;
						$stat['count']++;
					}
					if ($stat['count'] > 0) {
						printf('<tr><td colspan=3>%s</td></tr>',
							_('Media Stats'));
						printf('<tr><td>&nbsp;</td><td>'.
							classValue(_('%3.2f%% average utilisation.'),'value').
							'</td><td>&nbsp;</td></tr>',
							$stat['util']/$stat['count']);
						printf('<tr><td>&nbsp;</td><td>'.
							classValue(_('%3.2f%% average wasted space.'),'value').
							'</td><td>&nbsp;</td></tr>',
							$stat['reclaim']/$stat['count']);
						printf('<tr><td>&nbsp;</td><td>'.
							classValue(_('%3.0f MB average capacity.'),'value').
							'</td><td>&nbsp;</td></tr>',
							$stat['cap']/$stat['count']);
					}
				break;

			}
			echo '<tr><td>&nbsp;</td></tr>';

			# Nodes with data in this pool
			echo "\n\n";
			$total = 0;
			$nodedata = '';
			$counter = 0;
			foreach ($occupancy->getStoragePoolTotals($stgpool->getName()) as $node => $typedetails) {
				$node = $nodes->getNode($node);

				foreach ($typedetails as $type => $details) {
					$nodedata .= sprintf('<tr class="%s"><td>%s</td><td>%s</td><td align="right">%s %s</td><td align="right">%s MB</td></tr>',
						($counter++%2==0?'even':'odd'),
							$node->getName(),$type,
							(isset($detail['files']) ? number_format($detail['files']) : '&nbsp;'),
							(count($node->getTapeVolumes($stgpool->getName(),$type)) ? count($node->getTapeVolumes($stgpool->getName(),$type)) : '&nbsp;'),
							isset($details['physical']) ? number_format($details['physical']) : '&nbsp;');

						$total += $details['physical'];
				}
			}

			if ($nodedata) {
				echo '<tr><td colspan=2>'."\n\n";
				echo '<table class="result_table" border=0>';
				printf('<tr><td colspan=4 class="heading">%s</td></tr>',
					_('Nodes with data in this storage pool.'));
				printf('<tr class="title"><td class="titlel">%s</td><td class="titler">%s</td><td class="titler">%s</td><td class="titler">%s</td></tr>',
					_('Node'),_('Type'),_('Volumes'),_('MB'));
				echo $nodedata;
				printf('<tr class="highlight"><td colspan=3 class="titlel">%s</td><td class="titler">%s MB</td></tr>',
					_('Total'),number_format($total));
				echo '</table>'."\n\n";
				echo '</td><td>&nbsp;</td></tr>';
				echo '<tr><td>&nbsp;</td></tr>';
			}
			echo '</table><br />'."\n\n";
		}

		if (! $stgcount)
			printf('<b>%s</b><br />',_('There are no storage pools using this DEVCLASS.'));

	}
?>
