Location: PHPKode > projects > Monyet > controllers/systemstat.php
<?php

/*
 * view system statistic
 */

Class SystemStat extends ControllerBase {

	private $ping;
	private $agent;
	private $servmondef;
	private $snmpmon;
	private $snmpdef;
	private $servicesdef;
	private $netmap;

	public function __construct(){
		parent::__construct();
		$this->pingdef = new Ping();
		$this->agent = new Agent();
		$this->servmondef = new ServiceMon();
		$this->servicesdef = new ServicesDef();
		$this->snmpmon = new SNMPMon();
		$this->snmpdef = new SNMPDef();
		$this->netmap = new NetMapAgent();
	}
	//front page for displaying network map status
	public function netmapStatus(){
		$this->xml->addText(SITENAME.' -netmap','title',0);
		$this->xml->addLoop('systemstat/realnetworkstatus',null,REFRESH);
		$this->getMenu();
		$this->xml->flush();
	}
	public function realNetworkStatus(){
		$plotter = $this->getMap();	
		$this->xml->addGraph($plotter->attach(),'rightcontent',0);	
		$this->xml->addText($plotter->getImageMaps(),'rightcontent',1);		
		$this->xml->flush();
	}

	// this one called once and looping every 5 minutes defined in javascript
	// from all priorited monitoring
	public function buildMarquee(){
	   //only for active agent
		$array_agent = $this->agent->select('status="Active"');
		$marquee='';
		//wrap the field name for agents definition
		$field_id = $this->agent->getField('id');
		$field_description = $this->agent->getField('description');
		$field_priority = $this->agent->getField('priority');
		
		for($i=0;$i<sizeof($array_agent);$i++){
			$priority = $array_agent[$i]->$field_priority;

			$st='';
			$ft='';
			if($priority[0]=='p'){
				$this->pingdef->selectByID($priority);
				$st = $this->pingdef->getStartTime();
				$ft = $this->pingdef->getFinishTime();
			}else
			if($priority[0]=='s'){
				$this->servmondef->selectByID($priority);
				$st = $this->servmondef->getStartTime();
				$ft = $this->servmondef->getFinishTime();
			
			}
			else
			if($priority[0]=='m'){
				$this->snmpmon->selectByID($priority);
				$st = $this->snmpmon->getStartTime();
				$ft = $this->snmpmon->getFinishTime();
			}

			$tmpdate = date('G:i:s');
			if($st!=$ft){
				//agent with defined window time goes here
				//only for in window time
				if((strtotime($tmpdate)>strtotime($st))&&(strtotime($tmpdate)<strtotime($ft))){
					//only for agent with priority
					if($priority!='no'){
						$holder = HolderFactory::create($priority);
						$flag = $holder->getStatus();
						$url = $this->translatePriority($priority,'lasturl');
						$marquee.= ' <a href="'.$url.'">'.$array_agent[$i]->$field_description.'</a> '.$this->translateFlagStatus($flag,'smallimage').' &nbsp;';
					}
				}
			}else{
			//agent with 24hour window time goes here
			//only for defined priority
				if($priority!='no'){
					$holder = HolderFactory::create($priority);
					$flag = $holder->getStatus();
					$url = $this->translatePriority($priority,'lasturl');
					$marquee.= ' <a href="'.$url.'">'.$array_agent[$i]->$field_description.'</a> '.$this->translateFlagStatus($flag,'smallimage').' &nbsp;';
				}
			}
		}
		$this->xml->addText($marquee,'marquee',1);
		$this->xml->flush();	
	}

	public function lastStat(){
		//id get from post method is field priority = table ping monitoring
		$id=$this->getID();
		$this->xml->addText(SITENAME.' -last statistic','title',0);
		$this->xml->addLoop('systemstat/reallaststat','id='.$id,REFRESH);
		$this->xml->flush();
	}

	//last system stat last 
	public function realLastStat(){
	  	//id get from post method is field priority = table ping monitoring
		$id=$this->getID();
		//get current time
		//get past a hour time and plot available data
		$minute = date('i')-(date('i')%5);
		$last2hour = mktime(date('G')-1,$minute,0, date('m'),date('d'),date('Y'));
		$starttime = date('Y-m-d G:i:s',$last2hour);
		$finishtime = date('Y-m-d G:i:s');
		$this->getHeadStat($id,$this->getName().'/laststat',null);
		$this->xml->addBreak('rightcontent',2);
		//frame the detail by other function
		$this->monBox('Last Hour',$starttime,$finishtime,$id,1,false);
		$this->xml->buildSubMenu(array_merge($this->navigator->getSubMenu($this),$this->getLatencyMenu($id)));
		$this->xml->flush();	
	}

	public function statToday(){
		$id = $this->getID();
		$this->xml->addText(SITENAME.' -today statistic','title',0);
		$graphavail = '';
		if(isset($_POST['graphavail'])){
			$graphavail = '&graphavail='.$_POST['graphavail'];
		}
		$this->xml->addLoop('systemstat/realstattoday','id='.$id.$graphavail,REFRESH);
		$this->xml->flush();
	}
	public function realStatToday(){
		//id get from post method is field priority = table ping monitoring
		$id=$this->getID();
		$graphavail = 0;
		$post = 'test';
		if(isset($_POST['graphavail'])){
			if($_POST['graphavail']=='false'){
				$graphavail = false;
			}else{
				$graphavail = true;
			}

			$post = 'graphavail='.$_POST['graphavail'];
		}
		//	die($post);
		$starttime = date('Y-m-d').' 00:00:00';
		$finishtime = date('Y-m-d G:i:s');
		$this->getHeadStat($id,$this->getName().'/stattoday',$post);
		$this->xml->addBreak('rightcontent',2);
		$this->monBox('Today',$starttime,$finishtime,$id,12,$graphavail);
		$addedmenu = $this->getLatencyMenu($id);
		if($graphavail){
			$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/stattoday\',\'id='.$id.'&graphavail=false\')']='Toogle Availability';
		}else{
			$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/stattoday\',\'id='.$id.'&graphavail=true\')']='Toogle Availability';
		}
		$this->xml->buildSubMenu(array_merge($this->navigator->getSubMenu($this),$addedmenu));
		$this->xml->flush();	
	}

	public function history(){
	   $id = $_POST['id'];
		if(isset($_POST['starttime'])){
			$graphavail = false;
			if(isset($_POST['graphavail'])){
				if($_POST['graphavail']=='false'){
					$graphavail = false;
				}else{
					$graphavail = true;
				}
			}
			$id = $_POST['key'];
			$starttime = $_POST['starttime'];
			$finishtime = $_POST['finishtime'];
			$granularity = $_POST['granularity'];
			$interval=1;
			if($granularity=='hour'){
				$interval=12;
			}
			else
			if($granularity=='day'){
				$interval=288;
			}
			else
			if($granularity=='month'){
				$interval=(288*30);
			}

			//id get from post method is field priority = table ping monitoring
			$this->agent->selectByID($id);
			$this->xml->addText('<h3>Agent: '.$this->agent->getDescription().'</h3>','rightcontent',0);
	
			//redundant here.. but how to catch if user directly click the ping today from system summary
			//html and controller coupled here. sorry.. i just run out of time
			$table ='<table class="subbox"><tr><td rowspan="3" width="60">';
	
			//get traffic light.. translate to an image
			$pingmon = HolderFactory::create($id);	 
			$flag = $pingmon->getStatus();
			$table.=$this->translateFlagStatus($flag,'bigimage');
			$table.='</td><td>Status</td><td>:</td><td>';
			$table.=$this->translateFlagStatus($flag,'htmltext');
			$table.='</td></tr>';
			$table.='<tr><td>';
			if($flag=='gray'){
				$table.='Last Available is';
			}else{
				$table.='Available Since';
			}
			$table.='</td><td>:</td><td>'.$pingmon->getLastAvail().'</td></tr>';
			$table.='<tr><td>Current Round Trip Time (RTT)</td><td>:</td><td>';
			$table.=$pingmon->getRecent().' ms</td></tr>';
			$table.='</table>';
			$this->xml->addText($table,'rightcontent',1);
			$this->xml->addBreak('rightcontent',2);
			//each day contains ? 5 minutes
			$this->getHeadStat($id,$this->getName().'/history',null);
			$this->monBox('This Month',$starttime,$finishtime,$id,$interval,$graphavail);
			$addedmenu = $this->getLatencyMenu($id); 
			if($graphavail){
				$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/statthismonth\',\'id='.$id.'&graphavail=false\')']='Toogle Availability';
			}else{
				$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/statthismonth\',\'id='.$id.'&graphavail=true\')']='Toogle Availability';
			}
			$this->xml->buildSubMenu(array_merge($this->navigator->getSubMenu($this),$addedmenu));
			//rebuild the search panel
			$this->xml->addForm('javascript:sndReq(\'systemstat/history\')','divhistorypanel',0);
			$this->xml->addDatePicker('Start time',$starttime,'starttime');
			$this->xml->addDatePicker('Finish time',$finishtime,'finishtime');
			$this->xml->addHidden('id',$id);
			$this->xml->addSelect('Granularity',array('minutes'=>'5 minutes','hour'=>'hourly','day'=>'daily','month'=>'monthly'),$granularity,'granularity');
			$this->xml->addSubmitBtn('submitForm(\''.$this->getName().'/history\')','Submit');
		}
		else{
			//if usr change agent refresh the key
			$this->xml->replaceOnChange('agent','javascript:refreshSelectPOST(\'systemstat/pophist\',\'id='.$id.'\')');
			$this->xml->replaceOnChange('key',null);
			$this->xml->addForm('javascript:sndReqPOST(\'systemstat/history\',\'id='.$id.'\')','divhistorypanel',0);
			$this->xml->addDatePicker('Start time',date('Y-m-d').' 00:00:00','starttime');
			$this->xml->addDatePicker('Finish time',date('Y-m-d').' 23:59:00','finishtime');
			$this->xml->addHidden('id',$id);
			$this->xml->addSelect('Granularity',array('minutes'=>'5 minutes','hour'=>'hourly','day'=>'daily','month'=>'monthly'),'hour','granularity');
			$this->xml->addSubmitBtn('submitForm(\''.$this->getName().'/history\')','Submit');
		}
		$this->xml->flush();	
	}
	public function popHist(){
	  	$this->agent->selectByID($_POST['agent']);
		$this->xml->addSingleSelect($this->agent->getAvailableMonitoring(),$_POST['id'],'key','selectkey',null);	
		$this->xml->flush();
	} 

	public function statThisMonth(){
		$id = $this->getID();
		$this->xml->addText(SITENAME.' -today statistic','title',0);
		$graphavail = '';
		if(isset($_POST['graphavail'])){
			$graphavail = '&graphavail='.$_POST['graphavail'];
		}
		$this->xml->addLoop('systemstat/realstatthismonth','id='.$id.$graphavail,REFRESH);
		$this->xml->flush();
	}


	public function realStatThisMonth(){
		$id = $_POST['id'];
		$graphavail = false;
		if(isset($_POST['graphavail'])){
			if($_POST['graphavail']=='false'){
				$graphavail = false;
			}else{
				$graphavail = true;
			}
		}
		$starttime = date('Y-m-d G:i:s',mktime(0,0,0,date('m'),1,date('y')));
		$finishtime = date('Y-m-d G:i:s',mktime(0,0,0,date('m'),16,date('y')));
		if(date('d')>15){
			$finishtime = date('Y-m-d G:i:s',mktime(0,0,0,date('m')+1,1,date('y')));
		}

		//id get from post method is field priority = table ping monitoring
		$id = $_POST['id'];
		$this->agent->selectByID($id);
		$this->xml->addText('<h3>Agent: '.$this->agent->getDescription().'</h3>','rightcontent',0);

		//redundant here.. but how to catch if user directly click the ping today from system summary
		//html and controller coupled here. sorry.. i just run out of time
		$table ='<table class="subbox"><tr><td rowspan="3" width="60">';

		//get traffic light.. translate to an image
		$pingmon = HolderFactory::create($id);	 
		$flag = $pingmon->getStatus();
		$table.=$this->translateFlagStatus($flag,'bigimage');
		$table.='</td><td>Status</td><td>:</td><td>';
		$table.=$this->translateFlagStatus($flag,'htmltext');
		$table.='</td></tr>';
		$table.='<tr><td>';
		if($flag=='gray'){
			$table.='Last Available is';
		}else{
			$table.='Available Since';
		}
		$table.='</td><td>:</td><td>'.$pingmon->getLastAvail().'</td></tr>';
		$table.='<tr><td>Current Round Trip Time (RTT)</td><td>:</td><td>';
		$table.=$pingmon->getRecent().' ms</td></tr>';
		$table.='</table>';
		$this->xml->addText($table,'rightcontent',1);
		$this->xml->addBreak('rightcontent',2);
		//each day contains ? 5 minutes
		$this->getHeadStat($id,$this->getName().'/statthismonth',null);
		$this->monBox('This Month',$starttime,$finishtime,$id,288,$graphavail);
		$addedmenu = $this->getLatencyMenu($id); 
		if($graphavail){
			$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/statthismonth\',\'id='.$id.'&graphavail=false\')']='Toogle Availability';
		}else{
			$addedmenu['javascript:sndReqPOST(\''.$this->getName().'/statthismonth\',\'id='.$id.'&graphavail=true\')']='Toogle Availability';
		}
		$this->xml->buildSubMenu(array_merge($this->navigator->getSubMenu($this),$addedmenu));
		$this->xml->flush();	
	}

	public function index(){
		$this->xml->addText(SITENAME.' -systemstat','title',0);
		$this->xml->buildSubMenu($this->navigator->getSubMenu($this));
		$this->xml->flush();
	}

	//build general system status summary
	//contain current, today, yesterday, this month
	//FIXME: processText get confused.. please see the output from request.php?route=systemstat/summary
	//i prefer to build the container first and then populate the container 
	//using available method from xmlgenerator

	public function summary(){
		$this->xml->addText(SITENAME.' -system summary','title',0);
		$this->xml->addLoop('systemstat/realsummary',null,REFRESH);
		$this->xml->flush();
	}

	public function realsummary(){
			  
		//FIXME: currently network based on network latency. alternative: bandwith usage
		//wrap the table field with variable
		$array_agent = $this->agent->select('status="Active"');
		$field_type = $this->agent->getField('type');
		$field_desc = $this->agent->getField('description');
		$field_priority = $this->agent->getField('priority');

		//building the container (div)
		$maintable = '<table><tr><td valign="top"><div id="tablenetwork"></div></td><td>&nbsp;&nbsp;</td><td valign="top"><div id="tableserver"></div></td></tr></table>';
		$this->xml->addText($maintable,'rightcontent',0);		

		$tablenetwork = '<table class="sortable">';
		$tablenetwork.='<tr class="headtb"><th>NETWORK</th><th>LATENCY</th><th>AVAILABILITY</th></tr>';
		$tableserver = '<table class="sortable">';
		$tableserver.='<tr class="headtb"><th>SERVER</th><th>RESPONSE TIME</th><th>AVAILABILITY</th></tr>';
		$networkcount=0;
		$servercount=0;
		//only for agent with priority != no
		for($i=0;$i<sizeof($array_agent);$i++){
			if($array_agent[$i]->$field_priority!='no'){
				if($array_agent[$i]->$field_type=='Network'){
					if($networkcount%2==0){
						$trbg = ' bgcolor="#faf7f1"';
					}else{
						$trbg = '';
					}
					$tablenetwork.='<tr'.$trbg.'><td><div id="subtable_'.$i.'"></div></td><td><div id="subtable1_'.$i.'"></div></td>';
					$tablenetwork.='<td><div id="subtable2_'.$i.'"></div></td></tr>';
					$networkcount++;
				}
				if($array_agent[$i]->$field_type=='Server'){
					if($servercount%2==0){
						$trbg = ' bgcolor="#faf7f1"';
					}else{
						$trbg = '';
					}
					$tableserver.='<tr'.$trbg.'><td><div id="subtable_'.$i.'"></div></td><td><div id="subtable1_'.$i.'"></div></td>';
					$tableserver.='<td><div id="subtable2_'.$i.'"></div></td></tr>';
					$servercount++;
				}
			}
		}
		$tablenetwork.='</table>';
		$tableserver.='</table>';
		$this->xml->addText($tablenetwork,'tablenetwork',0);		
		$this->xml->addText($tableserver,'tableserver',0);	

		$yesterday = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-1,date('y'))); 
		$today = date('Y-m-d'); 
		$thismonth = date('n');

		//actual detail summary data	
		for($i=0;$i<sizeof($array_agent);$i++){
			if($array_agent[$i]->$field_priority!='no'){
				//create holder object	  
				$holder = HolderFactory::create($array_agent[$i]->$field_priority);
				
				$subtable = '<table class="noclass" width="100%">';
				$flag = $holder->getStatus();
				$subtable.='<tr><td><div id="agentdesc'.$i.'"></div></td><td width="5">'.$this->translateFlagStatus($flag,'smallimage').'</td></tr>';
				$subtable.='</tr></table>';
				
				$subtable1 = '<table class="noclass">';
				$subtable1.='<tr><td><div id="divcurrent_'.$i.'"></div></td><td>'.$holder->getRecent().'</td></tr>';
				$subtable1.='<tr><td><div id="divtoday_'.$i.'"></div></td><td>'.$holder->getDay($today,'stat').'</td></tr>';
				$subtable1.='<tr><td><div id="divyesterday_'.$i.'"></div></td><td>'.$holder->getDay($yesterday,'stat').'</td></tr>';
				$subtable1.='<tr><td>This Month</td><td>'.$holder->getMonth($thismonth,'avail').'</td>';
				$subtable1.='</tr></table>';
				
				$subtable2 = '<table class="noclass">';
				$subtable2.='<tr><td><div id="divtodayavail_'.$i.'"></div></td><td>'.$holder->getDay($today,'avail').' %</td></tr>';
				$subtable2.='<tr><td>Yesterday</td><td>'.$holder->getDay($yesterday,'avail').' %</td></tr>';
				$subtable2.='<tr><td>This Month</td><td>'.$holder->getMonth($thismonth,'avail').' %</td>';
				$subtable2.='</tr></table>';
				
				$this->xml->addText($subtable,'subtable_'.$i,0);		
				$this->xml->addText($subtable1,'subtable1_'.$i,0);		
				$this->xml->addText($subtable2,'subtable2_'.$i,0);
				$url = $this->translatePriority($array_agent[$i]->$field_priority,'lasturl');
				$this->xml->addURL($array_agent[$i]->$field_desc,$url,'agentdesc'.$i,0);	
				$url = $this->translatePriority($array_agent[$i]->$field_priority,'lasturl');
				$this->xml->addURL('Current',$url,'divcurrent_'.$i,0);	
				$url = $this->translatePriority($array_agent[$i]->$field_priority,'todayurl');
				$this->xml->addURL('Today',$url,'divtoday_'.$i,0);	
				$url = $this->translatePriority($array_agent[$i]->$field_priority,'todayavailurl');
				$this->xml->addURL('Today',$url,'divtodayavail_'.$i,0);	
				$this->xml->addURL('Yesterday',$url,'divyesterday_'.$i,0);	
			}
		}
		$this->getMenu();
		$this->xml->flush();	
	}
	public function detailAgent(){
		$id = $_POST['id'];
		$place = $_POST['place'];
		$this->xml->addURL('hide detail agent','javascript:sndReqPOST(\'systemstat/closedetailagent\',\'id='.$id.'&place='.$place.'\')',$place,0);
		$this->agent->selectByID($id);
		$this->xml->addBreak($place,2);
		$table = '<table>';
		$table.='<tr><td>Hostname</td><td>:</td><td>'.$this->agent->getHostname().'</td></tr>';
		$table.='<tr><td>IP Address</td><td>:</td><td>'.$this->agent->getIPAddress().'</td></tr>';
		$table.='</table>';
		$this->xml->addText($table,$place,1);
		$this->xml->flush();
	}
	public function closeDetailAgent(){
		$id = $_POST['id'];
		$place = $_POST['place'];
		$this->xml->addURL('show detail agent','javascript:sndReqPOST(\'systemstat/detailagent\',\'id='.$id.'&place='.$place.'\')',$place,0);
		$this->xml->flush();
	}


	//----------------------------------------------------------------------------------------------
	//umbrella function that not directly invoked by user goes here
	
	public function monBox($message,$starttime,$finishtime,$id,$interval,$graphavail){
		$plotter = new Plotter();
		$array_column='';
		$title='';
		if($graphavail){	
			if($id[0]=='p'){		
				$array_column = array("Checkpoint","Availability (%)");	  
				$title='Table Network Availability';
				$plotter->setTitle('Availability Network '.$this->agent->getDescription());
				$headtitle='Network Availability';
			}
			else
			if($id[0]=='s'){		
				$this->servmondef->selectByID($id);	
				$this->servicesdef->selectByID($this->servmondef->getService());
				$array_column = array("Checkpoint","Availability (%)");	  
				$title='Table Service Availability';
				$servicename = $this->servicesdef->getName();
				$plotter->setTitle($servicename.' Availability '.$this->agent->getDescription());
				$headtitle=$servicename.' Availability';
			}
		}
		else{
			if($id[0]=='p'){		
				$array_column = array("Checkpoint","RTT (ms)");	  
				$title='Table Network Latency';
				$plotter->setTitle('Network Latency '.$this->agent->getDescription());
				$headtitle='Network Latency';
			}
			else
			if($id[0]=='s'){		
				$this->servmondef->selectByID($id);	
				$this->servicesdef->selectByID($this->servmondef->getService());
				$array_column = array("Checkpoint","Response (ms)");	  
				$title='Table Service Response Time';
				$servicename = $this->servicesdef->getName();
				$plotter->setTitle($servicename.' Response Time '.$this->agent->getDescription());
				$headtitle=$servicename.' Response Time';
			}
			else
			if($id[0]=='m'){		
				$this->snmpmon->selectByID($id);	
				$this->snmpdef->selectByID($this->snmpmon->getSNMPDef());
				$array_column = array("Checkpoint","Value");	  
				$title='Table SNMP Return Value';
				$servicename = $this->snmpdef->getName();
				$plotter->setTitle($servicename.' Return Value '.$this->agent->getDescription());
				$headtitle=$servicename.' Return Value';
			}
		}
		$this->xml->addText('<h3>'.$message.' '.$headtitle.'</h3>','rightcontent',1);
		$plotter->setInterval($interval);
		$plotter->setGraphAvail($graphavail);
		$plotter->setGraph($id,$starttime,$finishtime);	
		$subtable = '<table>';
		$subtable.='<tr></tr><td>Peak Value</td><td>:</td><td>'.$plotter->getMaxStat().' ms</td></tr>';
		$subtable.='<tr></tr><td>Minimum Value</td><td>:</td><td>'.$plotter->getMinStat().' ms</td></tr>';
		$subtable.='<tr></tr><td>Average</td><td>:</td><td>'.$plotter->getAverageStat().' ms</td></tr>';
		$subtable.='<tr></tr><td>Availability ('.$message.')</td><td>:</td><td>';
		$subtable.=$plotter->getAvailability().' %</td></tr>';
		$subtable.='</table>';
		
		$table = '<table class="subbox">';
		$table.='<tr><td><div id="detaildata"></td><td width="20">&nbsp;</td><td><div id="detailgraph"></div></td></tr>';
		$table.='<tr><td colspan="2"></td><td><a name="avail"></a><h3>'.$message.' Summary</h3><div id="availpie"></div>'.$subtable.'</td></tr></table>';
		$this->xml->addText($table,'rightcontent',1);

		//attach data from graph plotter
		$this->xml->addGraph($plotter->attachLine(),'detailgraph',1);		
		$this->xml->addGraph($plotter->attachPie(),'availpie',1);	
		$this->xml->addText($plotter->getImageMaps(),'rightcontent',1);	
		$this->xml->addTable($plotter->getData(),$array_column,$title,null,0,'detaildata',1);
	
	}
	
	public function monBoxGranDay($message,$starttime,$finishtime,$id,$interval,$graphavail){
		$plotter = new GranDayPlotter();
		$array_column='';
		$plotter->setGraph($id,$starttime,$finishtime);	
		$table = '<table class="subbox">';
		$table.='<tr><td><div id="detaildata"></td><td width="20">&nbsp;</td><td><div id="detailgraph"></div></td></tr>';
		$table.='<tr><td colspan="2"></td><td><a name="avail"></a><h3>Summary</h3><div id="availpie"></div></td></tr></table>';
		$this->xml->addText($table,'rightcontent',1);

		//attach data from graph plotter
		$this->xml->addGraph($plotter->attachLine(),'detailgraph',1);		
	
	}
	//fungsi untuk menggambar network map
	public function getMap(){
		//very rough here FIXME: should be defined somewhere else
		$arrayobj = array();
		$netmapnet = new NetMapNet();
		//for set net device
		$result = $this->netmap->select();
		for($i=0;$i<sizeof($result);$i++){
			$obj = new StdClass();

			//i get the agent. find the priority. get status from the priority

			$this->agent->selectByID($result[$i]->agent_id);
			$obj->name = $this->agent->getDescription();
			$obj->x = $result[$i]->x;
			$obj->y = $result[$i]->y;
			$priority = $this->agent->getPriority();
			$status = $this->agent->getStatus();
			//agent with no priority doenst have clickable image maps
			if(($priority!='no')&&($status=='Active')){
				$holder = HolderFactory::create($priority);	  
				$flag = $holder->getStatus();
				$obj->color = $this->translateFlagStatus($flag,'htmlcolor');
				$recent = $holder->getRecent();
				if($recent!=0){
					$obj->message = $holder->getRecent().' ms';
				}
				else{
					$obj->message = 'down';
				}
				$obj->url = $this->translatePriority($priority,'lasturl');
			}
			else
			if($status=='Inactive'){
				$obj->color = 'white';
				$obj->message = 'disabled';
				$obj->url = '';
			}
			//if thereis no priority in agent definition
			else{
				$obj->color = 'white';
				$obj->message = 'no priority';
				$obj->url = '';
			}
			$arrayobj[] = $obj;
		}
		$plotter = new MapPlotter();
		$plotter->setAgent($arrayobj);

		//reuse the variable for net relation 
		$arrayobj = array();
		$arrayobj2 = array();
		$colorarray = array();
		$netrelation = new NetRelation();
		$result = $netrelation->select();
		for($i=0;$i<sizeof($result);$i++){
			//get net pole	  
			$netmapnet->selectByID($result[$i]->net_id);	  
			$obj = new StdClass();
			$obj->x = $netmapnet->getX();
			$obj->y = $netmapnet->getY();
			$arrayobj[] = $obj;
			
			$obj = new StdClass();
			$holder = HolderFactory::create('p_'.$result[$i]->ping_id);	  
			//check agent disabled or not here
			$this->agent->selectByID('p_'.$result[$i]->ping_id);
			$status = $this->agent->getStatus();
			if($status=='Inactive'){
				$obj->color = 'brown';
			}else{	
				$flag = $holder->getStatus();
				$obj->color = $flag;
			}
			$obj->monitor_id = $result[$i]->ping_id;
			$colorarray[] = $obj;
			
			//get agent pole and determine the color from ping monitoring
			$this->netmap->selectByID($result[$i]->agent_id);
			$obj2 = new StdClass();
			$obj2->x = $this->netmap->getX();
			$obj2->y = $this->netmap->getY();
			$arrayobj2[] = $obj2;
		}
		$plotter->setRelation($arrayobj,$arrayobj2,$colorarray);
	
		//reuse the variable	
		$arrayobj = array();
		$result = $netmapnet->select();
		//for set net device
		for($i=0;$i<sizeof($result);$i++){
			$obj = new StdClass();
			$obj->name = $result[$i]->name;
			$obj->x = $result[$i]->x;
			$obj->y = $result[$i]->y;
			$arrayobj[] = $obj;
		}
		$plotter->setNet($arrayobj);
		return $plotter;
	}

	//translate priority -> monitoring
	//current typeoutput: url, objectholder, objectdefinition
	public function translatePriority($priority,$typeoutput){
		if($typeoutput=='lasturl'){
			$url = 'laststat';	  
		}
		else
		if($typeoutput=='todayurl'){
			$url = 'stattoday';	  
		}
		else
		if($typeoutput=='todayavailurl'){
			$url = 'stattoday';	
			$priority.='&graphavail=true';  
		}
		return 'javascript:sndReqPOST(\''.$this->getName().'/'.$url.'\',\'id='.$priority.'\')';
	}

	//translate flag status
	//type output: image, text, htmlcolor
	public function translateFlagStatus($flag,$typeoutput){
		if($typeoutput=='bigimage'){
			$array = array('green'=>'<img src="gen_picts/trafficUp.gif">','red'=>'<img src="gen_picts/trafficDown.gif">',
				  'gray'=>'<img src="gen_picts/trafficDown.gif">','yellow'=>'<img src="gen_picts/trafficYellow.gif">');	
			return $array[$flag];
		}else
		if($typeoutput=='smallimage'){
			$array = array('green'=>'<img src=\'gen_picts/green.jpg\'></img>','red'=>'<img src=\'gen_picts/red.gif\'></img>',
				  'gray'=>'<img src=\'gen_picts/red.gif\'></img>','yellow'=>'<img src=\'gen_picts/yellow.jpg\'></img>');	
			return $array[$flag];
		}
		else
			if($typeoutput=='htmltext'){
			$array = array('green'=>'Available','red'=>'<text class="red">Critical</text>',
				  'gray'=>'<text class="red">Down</text>','yellow'=>'Warning');	
			return $array[$flag];
		}
		else
		if($typeoutput=='plaintext'){
			$array = array('green'=>'OK','red'=>'Critical','gray'=>'Down','yellow'=>'Warning');	
			return $array[$flag];
		}
		else
		if($typeoutput=='htmlcolor'){
			$array = array('green'=>'#befff5','yellow'=>'#ffff00','gray'=>'#f2f2fc','red'=>'#ff8c8f');
			return $array[$flag];
		}
	}

	//return menu for system stat
	//get from class navigation
	protected function getMenu(){
		$this->xml->buildSubMenu($this->navigator->getSubMenu($this));
	}

	protected function getLatencyMenu($id){
		$menu = array(
				  		 'javascript:sndReqPOST(\''.$this->getName().'/laststat\',\'id='.$id.'\')'=>'Last Hour',
						 'javascript:sndReqPOST(\''.$this->getName().'/stattoday\',\'id='.$id.'\')'=>'Today',
						 'javascript:sndReqPOST(\''.$this->getName().'/statthismonth\',\'id='.$id.'\')'=>'This Month',
						 'break'
			  );
		return $menu;
	}

	//return ID from post method or jump selection
	protected function getID(){
		$id='';
		if(isset($_POST['id'])){
			$id = $_POST['id'];
		}
		else{
			//id get from jump menu selection
			if(isset($_POST['agent'])){
				$agent = $_POST['agent'];
			}
			if(isset($_POST['key'])){
				$key = $_POST['key'];
			}
			//check wether key exists as available monitoring
			$this->agent->selectByID($agent);
			$monitoring = $this->agent->getAvailableMonitoring();
			if(array_key_exists($key,$monitoring)){
	 			$id = $key;
			}
			else{
				$id = $this->agent->getPriority();
			}	  
		}
		return $id;	
	}

	//return head layout 
	protected function getHeadStat($id,$jumpselect,$postdata){
		$this->agent->selectByID($id);
		$table = '<table width="100%"><tr><td valign="top"><div id="currentsummary"></div></td><td valign="top"><div id="panelcontrol"></div></td></tr></table>';
		$this->xml->addText($table,'rightcontent',0);

		//add panel control here
		$table = '<table width="250">';
		$table.='<tr><td>Agent</td><td>:</td><td><div id="selectagent"></div></td></tr>';
		$table.='<tr><td>Monitoring</td><td>:</td><td><div id="selectkey"></div></td></tr>';
		$table.='<tr><td colspan="2"></td><td><div id="divhistory"></td></tr>';
		$table.='<tr><td colspan="3"><div id="divhistorypanel"></td></tr>';
		$table.='</table>';
		$this->xml->addText($table,'panelcontrol',0);
		$this->xml->addURL('Custom History','javascript:sndReqPOST(\'systemstat/history\',\'id='.$id.'\')','divhistory',1);

		//jump menu action
		$this->xml->addSingleSelect($this->agent->selectCouple('id','description','status="Active"'),$this->agent->getID(),'agent','selectagent',$jumpselect,$postdata);	
		$this->xml->addSingleSelect($this->agent->getAvailableMonitoring(),$id,'key','selectkey',$jumpselect,$postdata);	
		
		$table ='<table class="subbox"><tr><td rowspan="3" width="60">';
		//get traffic light.. translate to an image
		$mon = HolderFactory::create($id);	 
		$flag = $mon->getStatus();
		$table.=$this->translateFlagStatus($flag,'bigimage');
		$table.='</td><td>Status</td><td>:</td><td>';
		$table.=$this->translateFlagStatus($flag,'htmltext');
		$table.='</td></tr>';
		$table.='<tr><td>';
		if($flag=='gray'){
			$table.='Last Available is';
		}else{
			$table.='Available Since';
		}
		$table.='</td><td>:</td><td>'.$mon->getLastAvail().'</td></tr>';
		$table.='<tr><td>';
		if($id[0]=='p'){
			$table.='Current Round Trip Time (RTT)';
		}else
		if($id[0]=='s'){
			$table.='Current Response Time';
		}
		$table.='</td><td>:</td><td>';
		$table.=$mon->getRecent().' ms</td></tr>';
		$table.='</table><br /><div id="clickmenudetail"></div>';
		$this->xml->addText('<h3>Agent: '.$this->agent->getDescription().'</h3>','currentsummary',0);
		$this->xml->addText($table,'currentsummary',1);
		$this->xml->addURL('show detail agent','javascript:sndReqPOST(\'systemstat/detailagent\',\'id='.$id.'&place=clickmenudetail\')','clickmenudetail',0);
	}
}

Return current item: Monyet