Location: PHPKode > projects > Monyet > classes/servicemon.php

//this class will be notified by class agent and servicesdef if belonging item is being removed

Class ServiceMon Extends EntityBase implements Observer {

	private $id;
	private $service;
	private $agent;
	private $starttime;
	private $finishtime;
	private $green;
	private $red;

	public function __construct(){

		//PHYSIC TABLE DEFINITION for Service Monitoring
		$tablename = 'servmondef';
		$field_agent = 'agent_id';
		$field_id = 'servmon_id';
		$field_service = 'service_id';
		$field_starttime = 'starttime';
		$field_finishtime = 'finishtime';
		$field_green = 'green';
		$field_red = 'red';

		$rawfields = array('id'=>$field_id,'agent'=>$field_agent,'service'=>$field_service,'starttime'=>$field_starttime,

		$agent = new Agent();
		$servicesdef = new ServicesDef();

		$atr_finish = 'DATE_FORMAT(finishtime,\'%H:%i\') as '.$field_finishtime;
		$atr_start = 'DATE_FORMAT(starttime,\'%H:%i\') as '.$field_starttime;
		$attributes = array($agent->getAbsField('description'),$servicesdef->getAbsField('name'),$atr_start,$atr_finish,$field_green,$field_red);

		$condition = $agent->getAbsField('id').'='.$tablename.'.'.$field_agent.' AND '.$servicesdef->getAbsField('id').'='.$tablename.'.'.$field_service;


	//this is only wrapper to provide single object
	//just copy paste for all children
	public function selectByID($id){
		//special for service monitoring to simplify query table
			$tempstring = explode('_',$id);
			$condition = $this->getField('service').'='.$tempstring[1].' AND '.$this->getField('agent').'='.$tempstring[2];
			$sql = 'SELECT * FROM '.$this->getPrimaryTable().' WHERE '.$condition ;
			$result = $this->dba->query($sql);
				$tempobj = $result[0];
			$tempobj = parent::selectByID($id);
			foreach($this->rawfields as $key => $value){
				$this->$key = $tempobj->$value;

	public function getID(){
		return $this->id;
	public function getStartTime(){
		return $this->starttime;
	public function getFinishTime(){
		return $this->finishtime;
	public function getAgent(){
		return $this->agent;
	public function getService(){
		return $this->service;
	public function getGreenLine(){
		return $this->green;
	public function getRedLine(){
		return $this->red;

	public function notifyRemove($obj,$id){
		if($obj instanceOf Agent){
		if($obj instanceOf ServicesDef){

	//remove from this table with field = id
	public function removeWith($field,$id){
		//remove all data holder
		$servmonid = $this->getField('id');
		$sql = 'SELECT '.$servmonid.' FROM '.$this->getPrimaryTable().' WHERE '.$this->getField($field).'='.$id;
		$result = $this->dba->query($sql);

	//drop table and set priority if there exists
	public function remove($id){
		//remove the holder
		$sql = 'DROP TABLE s_'.$this->getService().'_'.$this->getAgent();
		$sql = 'UPDATE agents SET priority=\'no\' WHERE priority = \'s_'.$this->getService().'_'.$this->getAgent().'\'';
		//remove the table entry
	//better hard to remove and easy to add
	public function add($array){
		//service_id + agent_id
		$sql = 'CREATE TABLE s_'.$array[1].'_'.$array[2].' (posttime DATETIME NOT NULL,value DECIMAL( 5, 2 ) NOT NULL, PRIMARY KEY (posttime))';
	//return array of agents that are not monitored for specified service $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('service').'='.$id.')';
		$temp = $this->dba->query($sql);
		$result = array();
			$result[$temp[$i]->$field_agent_id] = $temp[$i]->$field_agent_hostname;
		return $result;

	public function __destruct(){

Return current item: Monyet