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

class RRD

{

//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='')

{

$this->rrdFile = $rrdFile;

$this->graphFile = $graphFile;

if($rrdBinPath)

{

$this->rrdBinPath = $rrdBinPath;

}

}

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

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

{

if($rrdStart > 0)

{

$this->rrdStart = $rrdStart;

}

if($rrdStep > 0)

{

$this->rrdStep = $rrdStep;

}

if(count($rrdDsname) > 0)

{

$this->rrdDsname = $rrdDsname;

}

$dsCommand = '';

foreach ($this->rrdDsname as $dsname)

{

$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)

{

if($data && $timestamp > 0)

{

$dsCommand = "";

$valueCommand = "";

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

{

if(is_int($dsname))

{

$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

{

return 'ERROR_01';

}

}

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

array(), $debug=0)

{

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

> 0)

{

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

}

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

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

{

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

}

if(!$rrdDsname)

{

$rrdDsname = $this->rrdDsname;

}

$defCommand = '';

$lineCommand = '';

foreach ($rrdDsname as $dsname)

{

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

{

$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

{

$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)

{

if($debug !== 0)

{

echo $command;

}

system($command, $result);

if($result === 0)

{

return true;

}

else

{

return 'ERROR_02';

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值