<?php
/**
 * ThunderSnarf
 *
 * @author Ricardo H.F. Sette P. <ricardohfsp@gmail.com>
 * @link http://www.freebsdbrasil.com.br/
 * @copyright Copyright &copy; 2010 - FreeBSD Brasil LTDA
 * @license licença GNU/GPL v2.0 http://www.gnu.org/licenses/gpl-2.0.html
 */

$saidaGstat = array();
if (!file_exists('/usr/sbin/gstat')) {
    echo "O arquivo /usr/sbin/gstat não existe ou está inacessível devido à restrições do safe mode, verifique!";
    exit();
}
$errGstat = 0;
if (substr(sprintf('%o', fileperms('/usr/sbin/gstat')), -4) == "4555")
	exec("gstat -b",$saidaGstat,$errGstat);
else
	echo "O comando gstat está desabilitado, para habilita-lo execute: 'chmod 4555 /usr/sbin/gstat'."; 
if($errGstat>0){
	echo "O comando 'gstat -b' é válido somente para o sistema operacionao FreeBSD com a versão superior ou igual à 8.";
}else{
	$mount = $swapinfo = array();
	exec('mount',$mount);
	exec('swapinfo',$swapinfo);
	$array_particoes = array();
	foreach ($mount as $num => $dev) {
		$explode_dev = explode(" ",$dev);
		$array_particoes[$explode_dev[0]] = $explode_dev[2];
	}
	foreach ($swapinfo as $num => $swap) {		
		$explode_swap = explode(" ",$swap);
		$array_particoes[$explode_swap[0]] = "swap";
	}
	?>
	<br />
	<br />
				<table style="border:1px solid #c1c1c1">
	<?php
	foreach($saidaGstat as $key=>$value){
		if (preg_match("/.*L.*ops.*kBps.*ms.*w.*kBps.*ms.*busy.*Name/", $value)){
			if(!isset($_GET["IOFull"]) OR $_GET["IOFull"]==0){
		?>
					<tr>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>L(q)</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>ops/s</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>%Carga</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>Nome</th>
					</tr>
		<?php
			}else{
		?>
					<tr>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>L(q)</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>ops/s</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>r/s</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>kBps</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>ms/r</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>w/s</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>kBps</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>ms/w</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>%Carga</th>
						<th style='border:1px solid #c1c1c1;text-align:center;background-color:#f1f1f1'>Nome</th>
					</tr>
		<?php
			}
		}else if(preg_match("/^\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+\.\d+)\s*(\d+)\s*(\d+)\s*(\d+\.\d+)\s*(\d+\.\d+)\s*(.*)$/",$value,$matches)){
			/*[0] =>     0      0      0      0    0.0      0      0    0.0    0.0  fd0
    		[1] => 0
		    [2] => 0
		    [3] => 0
		    [4] => 0
		    [5] => 0.0
		    [6] => 0
		    [7] => 0
		    [8] => 0.0
		    [9] => 0.0
		    [10] => fd0*/			
			if(!isset($_GET["IOFull"]) OR $_GET["IOFull"]==0){
			?>
					<tr>
						<td style="border:1px solid #c1c1c1;text-align:center;">
						<?php echo ($matches[1]);?>
						</td>
						<td style="border:1px solid #c1c1c1;text-align:center;">
						<?php echo ($matches[2]);?>
						</td>
						<td style="border:1px solid #c1c1c1;text-align:center;">
							<div style="position:relative;background-color:#FFFF99;border:1px solid #c1c1c1;text-align:left;">
						        <div style="width: <?php echo ($matches[9]<=100?$matches[9]:100);?>%;background-color:<?php echo ((int)($matches[9]))>79 ? "red" : (((int)($matches[9]))>49 ? "lightPink" : "LimeGreen");?>;height:30px;margin-top:0;position:static;text-align:right;" class="dl_widget_memoria">
						        </div>
								<span style="font-size:0.85em;height:22px;position:absolute;text-align:center;top:8px;"><?php echo ($matches[9]);?>%</span>
							</div>
						</td>
						<td style="border:1px solid #c1c1c1;text-align:left;">
						<?php
						foreach ($array_particoes as $key => $value) {
							if(preg_match('/.*'.str_replace("/","\\/",$matches[10]).'$/',$key)){
								$matches[10] = $matches[10]." - (".$value.")";
							}
						}
						echo $matches[10];
						?>
						</td>
					</tr>
			<?php
			}else{
			?>
					<tr>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[1];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[2];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[3];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[4];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[5];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[6];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[7];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;"><?php echo $matches[8];?></td>
						<td style="border:1px solid #c1c1c1;text-align:center;">
							<div style="position:relative;background-color:#FFFF99;border:1px solid #c1c1c1;text-align:left;">
						        <div style="width: <?php echo ($matches[9]<=100?$matches[9]:100);?>%;background-color:<?php echo ((int)($matches[9]))>79 ? "red" : (((int)($matches[9]))>49 ? "lightPink" : "LimeGreen");?>;height:30px;margin-top:0;position:static;text-align:right;" class="dl_widget_memoria">
						        </div>
								<span style="font-size:0.85em;height:22px;position:absolute;text-align:center;top:8px;"><?php echo ($matches[9]);?>%</span>
							</div>
						</td>
						<td style="border:1px solid #c1c1c1;text-align:left;">
						<?php
						foreach ($array_particoes as $key => $value) {
							if(preg_match('/.*'.str_replace("/","\\/",$matches[10]).'$/',$key)){
								$matches[10] = $matches[10]." - (".$value.")";
							}
						}
						echo $matches[10];
						?>
						</td>
					</tr>
			<?php
			}
		}
	}
	?>
				</table>
	<?php
}