<div id="primary_header">
	<h1><?php echo $this->translate('Server Statistics') ?></h1>
</div>

<div id="primary_content">
    <div id="primary_content_inner">

        <?php if(isset($this->expiryMsg)): ?>
        <div class="warning"><?php echo $this->expiryMsg ?></div>
        <?php endif; ?>

		<form id="settingsForm" method="post" action="<?php echo $this->appBaseUrl; ?>/admin/appliance/serversmtpsettings">

<?php

// Find current IP's
$df = `df`;
$var = array();
$var['disktotal'] = 0;
$var['diskused'] = 0;
$var['loadavg'] = 0;
$var["rxeth0"] = 0;
$var["txeth0"] = 0;
$var["rxeth1"] = 0;
$var["txeth1"] = 0;

// Disk space
preg_match_all('/\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+(\/.*)/', $df, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
    if ($m[5] == '/' || $m[5] == '/usr' || $m[5] == '/messages') {
        $var['disktotal'] += $m[1];
        $var['diskused']  += $m[2];
    }
}

$var['diskfree'] = $var['disktotal'] - $var['diskused'];
$var['diskpercent'] = ($var['diskused'] / $var['disktotal']) * 100;
$var['diskpercent'] = sprintf( "%2.0f", $var['diskpercent'] );


$estimated = false;
if( $this->avgSizeBytes == 0 ) { 
	$this->avgSizeBytes = 100000; //set to estimated default size until messages in the system to do acurate calculation against.
	$estimated = true;
}

if( $this->avgMessagesPerDay == 0 ) {
	$this->avgMessagesPerDay = 2000; 
	$estimated = true;
}
	

// days of storage remaining
$messagesLeft = ($var['disktotal'] - $var['diskused'])/($this->avgSizeBytes/1024);
$var['daysRemaining'] = round($messagesLeft/$this->avgMessagesPerDay,0);
//echo '$this->avgSizeBytes:' . $this->avgSizeBytes . ' $this->avgMessagesPerDay:' . $this->avgMessagesPerDay . '<br />$var:<pre>' . print_r($var, true) . '</pre>';

foreach(array('disktotal', 'diskused', 'diskfree') as $type) {
	// In GB please
    $var[$type] = $var[$type] / 1048576;
	$var[$type] = sprintf( "%2.1fGB", $var[$type] );
}
//echo '$this->avgSizeBytes:' . $this->avgSizeBytes . ' $this->avgMessagesPerDay:' . $this->avgMessagesPerDay . ' ';
//echo '$var<pre>' . print_r($var, true) . '</pre>';
 
// Memory
$mem = `free`;
preg_match('/Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/', $mem, $m);
list (,$var['memtotal'], $var['memused'], $var['memfree'], $var['memshared'], $var['membuffer'], $var['memcache']) = $m;
$var['memtotal']   = sprintf( "%2.1f", $var['memtotal'] / 1024 );
$var['memused']    = sprintf( "%2.1f", $var['memused']  / 1024);
$var['memfree']    = sprintf( "%2.1f", $var['memfree']  / 1024);
	
$var['memcache'] = sprintf( "%2.1f",  ($var['membuffer'] + $var['memcache']) / 1024);

$var['mempercent'] = sprintf( "%2.1f", ( $var['memused'] - $var['memcache']) / $var['memtotal'] * 100);
$var['memcachepercent'] = sprintf( "%2.1f", $var['memcache']  / $var['memtotal'] * 100);
$var['mempercentfree'] = sprintf( "%2.1f", ( $var['memtotal'] - $var['memused']) / $var['memtotal'] * 100);

// CPU details
if ($fh = fopen('/proc/cpuinfo', 'r')) {
	while (false !== $line = fgets($fh)) {

    	if (preg_match('/(model name|cpu MHz|cache size|bogomips)\s+:\s+(.*)/i', $line, $m)) {
    		$name = strtolower($m[1]);
    		$value = $m[2];
    		$name = str_replace(' ', '', $name);
    		$var[$name] = $value;
    	}
	}
	fclose($fh);
}

$loadavg = '';

// load averages
if ($fh = fopen('/proc/loadavg', 'r')) {
	while (false !== $line = fgets($fh)) {
	    $loadavg .= $line;
	}

	fclose($fh);
	$loadavgs = preg_split('/\s+/', $loadavg);
	$var['loadavg'] = "$loadavgs[0], $loadavgs[1], $loadavgs[2]";
}

// Network usage
$uptime = `uptime`;
$uptime = trim($uptime);
preg_match('/up (.*?),/', $uptime, $m);
$var['uptime'] = $m[1];

// Ethernet usage
foreach(array('eth0', 'eth1') as $dev) {
	$usage = `ifconfig $dev`;
	
	if(preg_match('/RX bytes:(\d+).*?TX bytes:(\d+)/', $usage, $m))	{
		$var["rx$dev"] = sprintf( "%2.1f MB", $m[1] / 1048576);
		$var["tx$dev"] = sprintf( "%2.1f MB", $m[2] / 1048576);		
	}
}


?>

		<table class="settings">

			<tr>
				<td class="label"><?php echo $this->translate('Server Uptime') ?></td>
				<td><?php echo $var['uptime']; ?></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Load ( 1, 5 and 15 minutes)') ?></td>
				<td><?php echo $var['loadavg']; ?></td>
			</tr>

		</table>

		<div class="divider"></div>

		<table class="settings">

			<!-- need a divider -->
		
			<!-- Disk usage heading -->
			<tr>
				<td class="label"><?php echo $this->translate('Disk Usage') ?></td>
				<td><small><?php echo $this->translate('See reports on the disk space used for the Atmail appliance and the storage remaining.') ?></small></td>
			</tr>


			<tr>
				<td class="label"><?php echo $this->translate('Disk-space Used') ?></td>
				<td><?php echo $var['diskused']; ?></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Disk-space Total') ?></td>
				<td><?php echo $var['disktotal']; ?></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Disk-space Free') ?></td>
				<td><?php echo $var['diskfree']; ?></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Percentage Used') ?></td>
				<td>
					
					<table class="serv-stats" width="90%" border="0" cellspacing="0" cellpadding="0" height='26'>
					<tr>
					<td class="used" width="<?php echo $var['diskpercent']; ?>%"><b><font class="option"><?php echo $var['diskused']; ?></font></b></td>
					<td class="avail">
					<div align="right"><b><font class="option"><font color="#000000"><?php echo $var['diskpercent'];?>%</font></font></b></div>
					</td>
					</tr>
					</table>	
				</td>
			</tr>
			
			</table>

			
			
			<!-- divider -->
			<div class="divider"></div>
			
			<table class="settings">
			<!-- mem usage header -->
			<tr>
				<td class="label"><?php echo $this->translate('Memory Usage') ?></td>
				<td><small><?php echo $this->translate('View statistics of RAM usage, system cache and shared-memory usage') ?></small></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Memory Used') ?></td>
				<td><?php echo $var['memused']; ?> MB</td>
			</tr>

			<tr>
				<td class="label">Memory Total</td>
				<td><?php echo $var['memtotal']; ?> MB</td>
			</tr>

			<tr>
				<td class="label">Memory Cache</td>
				<td><?php echo $var['memcache']; ?> MB</td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Percentage Used') ?></td>
				<td>
					<table class="serv-stats"  width="100%" border="0" cellspacing="0" cellpadding="0" height='26'>
					<tr>
					<td class="used" width="<?php echo $var['mempercent']; ?>%" nowrap><b><font class="option"><?php echo $var['mempercent']; ?>% <?php echo $this->translate('Active') ?></font></b></td>

					<td class="cache" width="<?php echo $var['memcachepercent']; ?>%" nowrap><b><font class="option"><?php echo $var['memcachepercent']; ?>% <?php echo $this->translate('Cache') ?></font></b></td>

					<td class="free" nowrap>
					<div align="right" ><b><font class="option"><?php echo $var['mempercentfree']; ?>% <?php echo $this->translate('Free') ?></font></b></div>
					</td>
					</tr>
					</table>	
				</td>
			</tr>
			
			</table>

			<div class="divider"></div>

			<table class="settings">

			<tr>
				<td class="label"><?php echo $this->translate('CPU Usage') ?></td>
				<td><small><?php echo $this->translate('Details on the processor and CPU speed') ?></small></td>
			</tr>


			<tr>
				<td class="label"><?php echo $this->translate('Processor') ?></td>
				<td><?php echo $var['modelname']; ?></td>
			</tr>

			<tr>
				<td class="label"><?php echo $this->translate('Cache Size') ?></td>
				<td><?php echo $var['cachesize']; ?></td>
			</tr>

			</table>


			<div class="divider"></div>

			<table class="settings">

			<tr>
				<td class="label"><?php echo $this->translate('Network Usage') ?></td>
				<td><small><?php echo $this->translate('Statistics of the network traffic from the server Ethernet cards since last reboot') ?></small></td>
			</tr>

			<tr>
				<td class="label">eth0 - <?php echo $this->translate('Traffic Sent') ?></td>
				<td><?php echo $var['rxeth0']; ?></td>
			</tr>

			<tr>
				<td class="label">eth0 - <?php echo $this->translate('Traffic Received') ?></td>
				<td><?php echo $var['txeth0']; ?></td>
			</tr>

			<tr>
				<td class="label">eth1 - <?php echo $this->translate('Traffic Sent') ?></td>
				<td><?php echo $var['rxeth1']; ?></td>
			</tr>

			<tr>
				<td class="label">eth1 - <?php echo $this->translate('Traffic Received') ?></td>
				<td><?php echo $var['txeth1']; ?></td>
			</tr>

		</table>

		</form>
		</div>


		</div>

<script type="text/javascript">
    // bind to the form's submit event and cancel legacy submit 
    $('#settingsForm').submit(function() {              
		if( $("#settingsForm").attr("action") == false ) {
			alert('Form submission aborted: Action must be set');
			return false;
		}
	 	$.php($("#settingsForm").attr("action"),$("#settingsForm").serializeArray());
        return false; 
    }); 
</script>