<?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,
'finishtime'=>$field_finishtime,'green'=>$field_green,'red'=>$field_red);
$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;
parent::__construct(array($tablename,$agent->getPrimaryTable(),$servicesdef->getPrimaryTable()),
$field_id,$attributes,$rawfields,$condition);
}
//this is only wrapper to provide single object
//just copy paste for all children
public function selectByID($id){
$tempobj='';
//special for service monitoring to simplify query table
if($id[0]=='s'){
$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);
if(sizeof($result)!=0){
$tempobj = $result[0];
}
}
else{
$tempobj = parent::selectByID($id);
}
if($tempobj){
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){
$this->removeWith('agent',$id);
}
else
if($obj instanceOf ServicesDef){
$this->removeWith('service',$id);
}
}
//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);
for($i=0;$i<sizeof($result);$i++){
$this->remove($result[$i]->$servmonid);
}
}
//drop table and set priority if there exists
public function remove($id){
//remove the holder
$this->selectByID($id);
$sql = 'DROP TABLE s_'.$this->getService().'_'.$this->getAgent();
$this->dba->exec($sql);
$sql = 'UPDATE agents SET priority=\'no\' WHERE priority = \'s_'.$this->getService().'_'.$this->getAgent().'\'';
$this->dba->exec($sql);
//remove the table entry
parent::remove($id);
}
//better hard to remove and easy to add
public function add($array){
parent::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))';
$this->dba->exec($sql);
}
//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();
for($i=0;$i<sizeof($temp);$i++){
$result[$temp[$i]->$field_agent_id] = $temp[$i]->$field_agent_hostname;
}
return $result;
}
public function __destruct(){
parent::__destruct();
}
}
?>