<?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
 */
?>
<?php

class EstatisticasController extends Controller
{	
	/**
	 * @var string the default layout for the views. Defaults to 'column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='column1';
	
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
		
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('index','estatisticas','UpdateAjaxProcessador','UpdateAjaxMemoria','UpdateAjaxTrafegoGeral','UpdateAjaxTrafegoEstatico','UpdateAjaxTrafegoDinamico','UpdateAjaxIODisco','UpdateAjaxDiscoCache'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$model=new Extension();
		$data = array();
		$data["dataProcessador"] = $this->getProcessador();
		$data["dataMemoria"] = $this->getMemoria();
		$data["data"] = $model->findBySql("SELECT SUM(traffic_static) AS traffic_static, SUM(traffic_dynamic) AS traffic_dynamic, SUM(cache_static) AS cache_static, SUM(cache_dynamic) AS cache_dynamic FROM thundersnarf_data;");
		
		$this->render('estatisticas',$data);
	}
	
	private function getProcessador(){
		$xml_thunder = Yii::app()->user->getState('xml_thunder',array("vazio"));
		$processador = array(
			"Modelo"=>trim(exec("sysctl -n hw.model")),
			"CPUs"=>exec("sysctl -n hw.ncpu"),
			"THREADSTotal"=>$xml_thunder["THUNDER_BASE"]["MAXSERVERS"],
			"THREADSAtivos"=>exec('ps auxwwwwwww | grep "sbin/thunder\|thunder: \|thunder6: " | grep -v grep | wc -l'),
			"LOAD_AVARAGE"=>exec("uptime")
		);
		return $processador;
	}
	
	public function actionUpdateAjaxProcessador()
	{ 
        $this->renderPartial('_ajaxProcessadorContent', $this->getProcessador());
	}
	
	private function getMemoria(){
		$memoria = array(
			"Total"=>exec('echo "`sysctl -n hw.physmem`/1024/1024"|bc'),
			"NaoAlocada"=>exec('echo "`sysctl -n vm.stats.vm.v_free_count`*`sysctl -n hw.pagesize`/1024/1024" | bc'),
			"Ativa"=>exec('echo "`sysctl -n vm.stats.vm.v_active_count`*`sysctl -n hw.pagesize`/1024/1024" | bc'),
			"Inativa"=>exec('echo "`sysctl -n vm.stats.vm.v_inactive_count`*`sysctl -n hw.pagesize`/1024/1024" | bc'),
			"Cached"=>exec('echo "`sysctl -n vm.stats.vm.v_cache_count` * `sysctl -n hw.pagesize`/1024/1024" | bc'),
			"Buffer"=>exec('echo "`sysctl -n vfs.bufspace`/1024/1024" | bc'),
			"SwapTotal"=>exec("swapinfo -m | awk '{getline 2;print $2}'"),
			"SwapUsada"=>exec("swapinfo -m | awk '{getline 2;print $3}'"),
		);
		return $memoria;
	}
	
	public function actionUpdateAjaxMemoria()
	{ 
        $this->renderPartial('_ajaxMemoriaContent', $this->getMemoria());
	}
	
	public function actionUpdateAjaxIODisco()
	{
		$data = array();
 
        $this->renderPartial('_ajaxIODiscoContent', $data, false, true);
	}
	
	public function actionUpdateAjaxDiscoCache()
	{
		$data = array();    
		
        $this->renderPartial('_ajaxDiscoCacheContent', $data, false, true);
	}
	
	public function actionUpdateAjaxTrafegoGeral()
	{
		$model=new Extension();
		$data = $model->findBySql("SELECT SUM(traffic_static) AS traffic_static, SUM(traffic_dynamic) AS traffic_dynamic, SUM(cache_static) AS cache_static, SUM(cache_dynamic) AS cache_dynamic FROM thundersnarf_data;");
		
        $this->renderPartial('_ajaxTrafegoGeralContent', $data, false, true);
	}
	
	public function actionUpdateAjaxTrafegoDinamico()
	{
		$model=new Extension();
		$data = $model->findBySql("SELECT SUM(traffic_static) AS traffic_static, SUM(traffic_dynamic) AS traffic_dynamic, SUM(cache_static) AS cache_static, SUM(cache_dynamic) AS cache_dynamic FROM thundersnarf_data;");
		
        $this->renderPartial('_ajaxTrafegoDinamicoContent', $data, false, true);
	}
	
	public function actionUpdateAjaxTrafegoEstatico()
	{
		$model=new Extension();
		$data = $model->findBySql("SELECT SUM(traffic_static) AS traffic_static, SUM(traffic_dynamic) AS traffic_dynamic, SUM(cache_static) AS cache_static, SUM(cache_dynamic) AS cache_dynamic FROM thundersnarf_data;");
		
        $this->renderPartial('_ajaxTrafegoEstaticoContent', $data, false, true);
	}
	
	public function sizeFormat($size){
	    if($size<1024)
	    {
	        return $size." bytes";
	    }
	    else if($size<(1024*1024))
	    {
	        $size=round($size/1024,1);
	        return $size." KB";
	    }
	    else if($size<(1024*1024*1024))
	    {
	        $size=round($size/(1024*1024),1);
	        return $size." MB";
	    }
	    else
	    {
	        $size=round($size/(1024*1024*1024),1);
	        return $size." GB";
	    }

	}
}