绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
rrdtool php 绘图,PHP封装RRDTool的操作
2022-04-26 10:58:03

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';

}

}

}

分享好友

分享这个小栈给你的朋友们,一起进步吧。

RRDtool
创建时间:2022-04-26 10:31:50
RRDtool
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • gaokeke123
    专家
戳我,来吐槽~