Location: PHPKode > projects > Monyet > classes/snmpmon.php
<?php

//this class will be notified if corresponding agent is deleted
//if ping is priorited. the priority sets to null
//

Final Class SNMPMon Extends EntityBase implements Observer {

	private $id;
	private $agent;
	private $snmpdef;
	private $green;
	//minimum value for red
	private $red;
	private $starttime;
	private $finishtime;
	
	public function __construct(){

		//PHYSIC TABLE DEFINITION for SNMP Monitoring
		//-----------------------------------------------------
		$tablename = 'snmpmon';

		$field_id = 'snmpmon_id';	
		$field_agent = 'agent';
		$field_snmpdef = "snmpdef";
		$field_user = 'user';
		$field_password = 'password';
		$field_green = 'green';
		$field_red = 'red';
		$field_starttime = 'starttime';
		$field_finishtime = 'finishtime';
		//-----------------------------------------------------

		$rawfields = array('agent'=>$field_agent,'snmpdef'=>$field_snmpdef,'username'=>$field_user,'password'=>$field_password,
				  'green'=>$field_green,'red'=>$field_red,'starttime'=>$field_starttime,'finishtime'=>$field_finishtime);
		$atr_finish = 'DATE_FORMAT(finishtime,\'%H:%i\') as '.$field_finishtime;
		$atr_start = 'DATE_FORMAT(starttime,\'%H:%i\') as '.$field_starttime;
	
		$agent = new Agent();
		$snmpdef = new SNMPDef();
		$attributes = array($agent->getAbsField('description'),$snmpdef->getAbsField('name'),$field_user,$field_password,$atr_start,$atr_finish,$field_green,$field_red);
		$condition = $agent->getAbsField('id').'='.$tablename.'.'.$field_agent.' AND '.$snmpdef->getAbsField('id').'='.$tablename.'.'.$field_snmpdef;
		parent::__construct(array($tablename,$agent->getPrimaryTable(),$snmpdef->getPrimaryTable()),$field_id,$attributes,$rawfields,$condition);
	}

	//this is only wrapper to provide single object
	//just copy paste for all children
	public function selectByID($id){
		if($id[0]=='m'){
			$tempstring = explode('_',$id);
			$condition = $this->getField('snmpdef').'='.$tempstring[1].' AND '.$this->getField('agent').'='.$tempstring[2];
			$sql = 'SELECT * FROM '.$this->getPrimaryTable().' WHERE '.$condition ;
			$result = $this->dba->query($sql);
		  	if(sizeof($result)!=0){
				$tempobj = $result[0];
			}
		}else{
			$tempobj = parent::selectByID($id);
		}
		if($tempobj){
			foreach($this->rawfields as $key => $value){
				$this->$key = $tempobj->$value;
			}
		}
	}
	//join table with agent
	//FIXME: still coupled with table field name
	public function selectCouple($attr1,$attr2){
		$sql = 'SELECT agents.'.$attr1.' as '.$attr1.',agents.'.$attr2.' as '.$attr2.' FROM '.$this->getTableQuery().' WHERE '.$this->condition;
		$temp = $this->dba->query($sql);
		$array = array();
		for($i=0;$i<sizeof($temp);$i++){
			$array[$temp[$i]->$attr1]=$temp[$i]->$attr2;
		}
		return $array;
	
	}
	public function getAgentID(){
		return $this->agent;
	}
	public function getSNMPDef(){
		return $this->snmpdef;
	}
	public function getUser(){
		return $this->user;
	}
	public function getPassword(){
		return $this->password;
	}
	public function getGreenLine(){
		return $this->green;
	}
	public function getRedLine(){
		return $this->red;
	}
	public function getStartTime(){
		return $this->starttime;
	}
	public function getFinishTime(){
		return $this->finishtime;
	}
	
	//special case notify remove for ping monitoring
	public function notifyRemove($obj,$id){
		if($obj instanceOf Agent){
			if($this->isExist($id)){
				$this->remove($id);
			}
		}
	}

	//drop table and set priority if there exists
	public function remove($id){
		$this->selectByID($id);
		$sql = 'DROP TABLE m_'.$this->getSNMPDef().'_'.$this->getAgentID();
		$this->dba->exec($sql);
		$sql = 'UPDATE agents SET priority=\'no\' WHERE priority = \'m_'.$this->getSNMPDef().'_'.$this->getAgentID().'\'';
		$this->dba->exec($sql);
		parent::remove($id);
	}
	//return array of agents that are not monitored for specified snmpdef $id yet
	public function selectRemaining($id){
		$agent = new Agent();
		$field_agent_id = $agent->getField('id');
		//replace hostname with description.. because that's more valuable
		$field_agent_hostname = $agent->getField('description');

		$sql = 'SELECT '.$field_agent_id.','.$field_agent_hostname.' FROM  '.$agent->getPrimaryTable().' WHERE '.
				  $field_agent_id.' NOT IN (SELECT '.$this->getField('agent').' FROM '.$this->getPrimaryTable().' WHERE '.$this->getField('snmpdef').'='.$id.')';
		$temp = $this->dba->query($sql);
		$result = array();
		for($i=0;$i<sizeof($temp);$i++){
			$result[$temp[$i]->$field_agent_id] = $temp[$i]->$field_agent_hostname;
		}
		return $result;
	}



	public function add($array){
		parent::add($array);
		$sql = 'CREATE TABLE m_'.$array[2].'_'.$array[1].' (posttime DATETIME NOT NULL,value DECIMAL( 10, 2 ) NOT NULL, PRIMARY KEY (posttime))';
	  	$this->dba->exec($sql);	
	}

	public function __destruct(){
		parent::__destruct();	
	}
}

?>
Return current item: Monyet