rrdtool php 绘图,PHP封装RRDTool的操作

2022-04-26 00:00:00 专区 订阅 付费 默认 地址

class RRD

{undefined

//rrdtool 执行权限

private $rrdBinPath = '/usr/bin/rrdtool';

//rrd文件地址

private $rrdFile;

//默认开始时间从2000年的1月1号

private $rrdStart = 946656000;

//默认时间间隔是300秒(5分钟)

private $rrdStep = 300;

private $rrdDsname = array('dsname');

//rrdtool生成的图片地址

private $graphFile;

private $graphWidth = 800;

private $graphHeight = 400;

private $graphLineColor = array('#473C8B', '#66CDAA',

'#A0522D', '#EE3A8C','#CD0000');

public function __construct($rrdFile, $graphFile,

$rrdBinPath='')

{undefined

$this->rrdFile = $rrdFile;

$this->graphFile = $graphFile;

if($rrdBinPath)

{undefined

$this->rrdBinPath = $rrdBinPath;

}

}

public function rrdCreate($rrdStart=0, $rrdStep=0,

$rrdDsname=array(), $debug=0)

{undefined

if($rrdStart > 0)

{undefined

$this->rrdStart = $rrdStart;

}

if($rrdStep > 0)

{undefined

$this->rrdStep = $rrdStep;

}

if(count($rrdDsname) > 0)

{undefined

$this->rrdDsname = $rrdDsname;

}

$dsCommand = '';

foreach ($this->rrdDsname as $dsname)

{undefined

$dsCommand .= "DS:{$dsname}:GAUGE:600:0:U ";

}

$command = "{$this->rrdBinPath} create {$this->rrdFile}

--start {$this->rrdStart} --step {$this->rrdStep}

{$dsCommand} RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:4:600

RRA:AVERAGE:0.5:24:600 RRA:AVERAGE:0.5:288:730";

return $this->execCommand($command, $debug);

}

public function rrdUpdate($timestamp, $data=array(),

$debug=0)

{undefined

if($data && $timestamp > 0)

{undefined

$dsCommand = "";

$valueCommand = "";

foreach($data as $dsname=>$value)

{undefined

if(is_int($dsname))

{undefined

$dsname = $this->rrdDsname[$dsname];

}

$dsCommand .= $dsname.":";

$valueCommand .= $value.":";

}

$dsCommand = substr($dsCommand, 0, -1);

$valueCommand = substr($valueCommand, 0, -1);

$command = "{$this->rrdBinPath} update {$this->rrdFile}

-t {$dsCommand} {$timestamp}:{$valueCommand}";

return $this->execCommand($command, $debug);

}

else

{undefined

return 'ERROR_01';

}

}

public function rrdGraph($s, $e, $rrdDsname=array(), $option =

array(), $debug=0)

{undefined

if(isset($option['with']) && intval($option['with'])

> 0)

{undefined

$this->graphWidth = $option['with'];

}

if(isset($option['height']) &&

intval($option['height']) > 0)

{undefined

$this->graphHeight = $option['height'];

}

if(!$rrdDsname)

{undefined

$rrdDsname = $this->rrdDsname;

}

$defCommand = '';

$lineCommand = '';

foreach ($rrdDsname as $dsname)

{undefined

if(strpos($dsname, ':') !== false)

{undefined

$tempArr = explode(':', $dsname);

$tempDsname = $tempArr[0];

$tempLineColor = $tempArr[1];

$defCommand .=

"DEF:my{$tempDsname}='{$this->rrdFile}':{$tempDsname}:AVERAGE

VDEF:myaverage=my{$tempDsname},AVERAGE

VDEF:mymax=my{$tempDsname},MAXIMUM ";

$lineCommand .=

"LINE:my{$tempDsname}".$tempLineColor.":'".$tempDsname."'

".'GPRINT:myaverage'.$tempDsname.':"AVERAGE\:%6.0lf"

GPRINT:mymax'.$tempDsname.':"MAXIMUM\:%6.0lf\n" ';

}

else

{undefined

$defCommand .=

"DEF:my{$dsname}='{$this->rrdFile}':{$dsname}:AVERAGE

VDEF:myaverage{$dsname}=my{$dsname},AVERAGE

VDEF:mymax{$dsname}=my{$dsname},MAXIMUM ";

$lineCommand .=

"LINE:my{$dsname}".array_pop($this->graphLineColor).":'".$dsname."'

".'GPRINT:myaverage'.$dsname.':"AVERAGE\:%6.0lf"

GPRINT:mymax'.$dsname.':"MAXIMUM\:%6.0lf\n" ';

}

}

$command = "{$this->rrdBinPath} graph {$this->graphFile}

--start {$s} --end {$e} {$defCommand} {$lineCommand} -w

{$this->graphWidth} -h {$this->graphHeight}";

return $this->execCommand($command, $debug);

}

private function execCommand($command, $debug)

{undefined

if($debug !== 0)

{undefined

echo $command;

}

system($command, $result);

if($result === 0)

{undefined

return true;

}

else

{undefined

return 'ERROR_02';

}

}

}

相关文章