Location: PHPKode > projects > Monyet > classes/agent.php
<?php
Class Agent Extends EntitySubject {

	private $id;
	private $hostname;
	private $ip_address;
	private $description;
	private $is_alarm;
	private $type;
	private $priority;
	private $status;

	public function __construct(){
		//PHYSIC TABLE DEFINITION for Agent
		//-----------------------------------------------------
		$tablename = 'agents';
		
		$field_id = 'agent_id';
		$field_hostname = 'hostname';
		$field_ip_address = 'ip_address';
		$field_description = 'description';
		$field_is_alarm = 'is_alarm';
		$field_type = 'type';
		$field_priority = 'priority';
		$field_status = 'status';
		//-----------------------------------------------------
		
		$rawfields = array('id'=>$field_id,'hostname'=>$field_hostname,'ip_address'=>$field_ip_address,
				  'description'=>$field_description,'is_alarm'=>$field_is_alarm,'type'=>$field_type,'priority'=>$field_priority,'status'=>$field_status);

		$attributes = array($field_hostname,$field_ip_address,$field_description,$field_is_alarm,$field_type,$field_priority,$field_status);
		parent::__construct($tablename,$field_id,$attributes,$rawfields,1);
	}
	public function __call($method,$argument){
		if($method=='select'){
		   if(sizeof($argument)==2){
				return $this->selectOffset($argument[0],$argument[1]);	
			}
			else
			if(sizeof($argument)==0){
				return parent::selectAll();	
			}
			else
			if(sizeof($argument)==1){
				return parent::selectWithCondition($argument[0]);	
			}
			else{
				die('Invalid argument of select agent');
			}
		}
		else
		if($method=='selectCouple'){
		   if(sizeof($argument)==2){
				return $this->selectCoupleCondition($argument[0],$argument[1],1);	
			}
			else
			if(sizeof($argument)==3){
				return $this->selectCoupleCondition($argument[0],$argument[1],$argument[2]);	
			}
			else{
				die('invalid argument for selectCouple');
			}
		}
		else{
			die('method '.$method.' does\'nt exists.');
		}
	}


	//override the select coz i wanna translate priority
	public function selectOffset($offset,$limit){
		$priorityfield = $this->getField('priority');
		$temp = parent::selectOffset($offset,$limit);
		for($i=0;$i<sizeof($temp);$i++){
			$priority = $temp[$i]->$priorityfield;
			if($priority==''){
				$temp[$i]->$priorityfield = 'not defined';
			}
			else
		   if($priority=='no'){
				$temp[$i]->$priorityfield = 'no priority';
			}
			else
			if($priority[0]=='p'){
				$temp[$i]->$priorityfield = 'Ping Monitoring';
			}
			else
			if($priority[0]=='s'){
				$explode = explode('_',$priority);
				$service = new ServicesDef();
				$service->selectByID($explode[1]);
				$temp[$i]->$priorityfield = $service->getName();
			}
			else
			if($priority[0]=='m'){
				$explode = explode('_',$priority);
				$service = new SNMPDef();
				$service->selectByID($explode[1]);
				$temp[$i]->$priorityfield = $service->getName();
			}
		}
		return $temp;
	}

	//this is only wrapper to provide single object
	//just copy paste for all children
	public function selectByID($id){
		if($id[0]=='p'){
			//special for ping monitoring to simplify query table
			$tempstring = explode('_',$id);
			$id =  $tempstring[1];
		}else
		if(($id[0]=='s')||($id[0]=='m')){
			//special for service monitoring to simplify query table
			$tempstring = explode('_',$id);
			$id =  $tempstring[2];
		}
		$tempobj = parent::selectByID($id);
		if($tempobj){
			foreach($this->rawfields as $key => $value){
				$this->$key = $tempobj->$value;
			}
		}
	}
	
	public function getID(){
		return $this->id;
	}
	public function getHostname(){
		return $this->hostname;
	}
	public function getIPAddress(){
		return $this->ip_address;
	}
	public function getAlarm(){
		return $this->is_alarm;
	}
	public function getType(){
		return $this->type;
	}
	public function getDescription(){
		return $this->description;
	}
	public function getPriority(){
		return $this->priority;
	}
	public function getStatus(){
		return $this->status;
	}

	//get available monitoring definition for this agent id
	//it should be invoked after selectByID
	//if you want to set priority = nothing then add in controller
	public function getAvailableMonitoring(){
		$id = $this->id;	  
		$result = array();	  
		
		//check in ping monitoring definition
		$ping = new Ping();
		if($ping->isExist($id)){
			$result['p_'.$id] = 'Ping';
		}

		//check in service monitoring definition
		$servmon = new ServiceMon();
		$servmon_service = $servmon->getField('service');
		$sql = 'SELECT '.$servmon_service.' FROM '.$servmon->getPrimaryTable().' WHERE '.$servmon->getField('agent').'='.$id;
		$tmp = $this->dba->query($sql);
		for($i=0;$i<sizeof($tmp);$i++){
			$service = new ServicesDef();
			$service->selectByID($tmp[$i]->$servmon_service);
		  	$result['s_'.$tmp[$i]->$servmon_service.'_'.$id] = $service->getName();
		}
		//check in snmp definition
		$servmon = new SNMPMon();
		$servmon_service = $servmon->getField('snmpdef');
		$sql = 'SELECT '.$servmon_service.' FROM '.$servmon->getPrimaryTable().' WHERE '.$servmon->getField('agent').'='.$id;
		$tmp = $this->dba->query($sql);
		for($i=0;$i<sizeof($tmp);$i++){
			$service = new SNMPDef();
			$service->selectByID($tmp[$i]->$servmon_service);
		  	$result['m_'.$tmp[$i]->$servmon_service.'_'.$id] = $service->getName();
		}
		return $result;
	}

	//notify all observers that item is being deleted
	public function remove($id){
		parent::remove($id);
		$this->notifyObservers($id);
	}
	public function __destruct(){
		parent::__destruct();	
	}
	
}

?>
Return current item: Monyet