<?php
//flag with critical status should be excluded when calculting statistic, and also
//checkpoint that outside of window time
abstract class Holder extends EntityBase {
protected $value;
protected $posttime;
public function __construct($tablename){
//PHYSIC TABLE DEFINITION for generic holder
//-----------------------------------------------------
$field_value = 'value';
$field_posttime = 'posttime';
//-----------------------------------------------------
$rawfields = array('value'=>$field_value,'posttime'=>$field_posttime);
$attributes = $rawfields;
parent::__construct($tablename,$field_posttime,$attributes,$rawfields,1);
}
public function getStatus(){
$warningthreshold = $this->getWarningThreshold();
$criticalthreshold = $this->getCriticalThreshold();
$recent = $this->getRecent();
$flag='';
if(($warningthreshold==0)&&($criticalthreshold==0)){
$flag = 'green';
}
else
if($warningthreshold<=$recent){
if($criticalthreshold<=$recent){
$flag = 'red';
}else{
$flag = 'yellow';
}
}else{
$flag = 'green';
}
if($recent==0){
$flag = 'gray';
}
return $flag;
}
public function getRecent(){
$value = $this->getField('value');
$sql = 'SELECT '.$value.' FROM '.$this->getPrimaryTable().' ORDER BY '.$this->getField('posttime').' DESC LIMIT 0,1';
$result = $this->dba->query($sql);
if($result){
return $result[0]->$value;
}
}
public function getBeforeRecent(){
$value = $this->getField('value');
$sql = 'SELECT '.$value.' FROM '.$this->getPrimaryTable().' ORDER BY '.$this->getField('posttime').' DESC LIMIT 1,1';
$result = $this->dba->query($sql);
if($result){
return $result[0]->$value;
}
}
public function getStatusBeforeRecent(){
$warningthreshold = $this->getWarningThreshold();
$criticalthreshold = $this->getCriticalThreshold();
$recent = $this->getBeforeRecent();
$flag='';
if(($warningthreshold==0)&&($criticalthreshold==0)){
$flag = 'green';
}
else
if($warningthreshold<=$recent){
if($criticalthreshold<=$recent){
$flag = 'red';
}else{
$flag = 'yellow';
}
}else{
$flag = 'green';
}
if($recent==0){
$flag = 'gray';
}
return $flag;
}
//day format yyyy-mm-dd
//type: stat,avail
public function getDay($day,$type){
$start = $day.' 00:00';
$finish = $day.' 23:59';
if($type=='stat'){
return $this->getStat($start,$finish);
}else
if($type=='avail'){
return $this->getAvail($start,$finish);
}
}
//month format 1-12
public function getMonth($month,$type){
$start = date('Y-m-d G:i:s',mktime(0,0,0,$month,1,date('y')));
$finish = date('Y-m-d G:i:s',mktime(0,0,0,$month+1,0,date('y')));
if($type=='stat'){
return $this->getStat($start,$finish);
}else
if($type=='avail'){
return $this->getAvail($start,$finish);
}
}
//FIXME: coupled with table field name
//currently window time is ignored.. please fix this soon. fixed (17.09.07)
public function getStat($starttime,$finishtime){
$q_start_time = " AND posttime >= \"".$starttime."\"";
$q_finish_time = " AND posttime <= \"".$finishtime."\"";
//get value != 0 and within the time range
$sql = "SELECT value FROM ".$this->getPrimaryTable().
" WHERE value!=0 AND value!=-1 ".$q_start_time." ".$q_finish_time;
$result = $this->dba->query($sql);
$total = 0;
for($i=0;$i<sizeof($result);$i++){
$total+=$result[$i]->value;
}
if(sizeof($result)==0){
return 0;
}
return round(($total/sizeof($result))*100)/100;
}
public function getAvail($starttime,$finishtime){
$q_start_time = " AND posttime >= \"".$starttime."\"";
$q_finish_time = " AND posttime <= \"".$finishtime."\"";
$q_critical='';
$critical = $this->getCriticalThreshold();
if($critical!=0){
$q_critical = ' AND value<'.$critical;
}
//get value != 0 and value != -1 and value are critical and within the time range
$sql = "SELECT count(*) as totalok FROM ".$this->getPrimaryTable().
" WHERE value!=0 AND value!=-1 ".$q_critical." ".$q_start_time." ".$q_finish_time;
$result = $this->dba->query($sql);
$totalok = $result[0]->totalok;
//get all total checkpoint
$sql = "SELECT count(*) as total FROM ".$this->getPrimaryTable().
" WHERE 1 ".$q_critical." ".$q_start_time." ".$q_finish_time;
$result = $this->dba->query($sql);
$total = $result[0]->total;
if($total==0){
return 0;
}
return round(($totalok/$total)*10000)/100;
}
//this method get last since status change
//if agent is up, get last checkpoint since agent is up
//if agent is down, get last checkpoint since agent is down
public function getLastAvail(){
$t = $this->getField('value');
$posttime = $this->getField('posttime');
$recent = $this->getRecent();
if(($recent!=0)&&($recent!=-1)){
$condition = $t.' = 0';
}else{
$condition = $t.' != 0'.' AND '.$t.' != -1';
}
$sql = 'SELECT '.$posttime.' FROM '.$this->getPrimaryTable().' WHERE '.$condition.' ORDER BY '.$this->getField('posttime').' DESC LIMIT 0,1';
$result = $this->dba->query($sql);
if($result){
return $result[0]->$posttime;
}else{
//if it is new agent added in definition get the first row asc
$sql = 'SELECT '.$posttime.' FROM '.$this->getPrimaryTable().' ORDER BY '.$this->getField('posttime').' ASC LIMIT 0,1';
$result = $this->dba->query($sql);
if($result){
return $result[0]->$posttime;
}
}
}
abstract function getWarningThreshold();
abstract function getCriticalThreshold();
}
?>