66 lines
2.3 KiB
PHP
66 lines
2.3 KiB
PHP
<?php
|
|
class RRD {
|
|
const CREATE = 'create %s --step %d --start %d %s';
|
|
const UPDATE = 'update %s %d:%s';
|
|
const FETCH = 'fetch %s %s -r %d -s %d -e %d';
|
|
|
|
protected static $oInstance;
|
|
protected static $rProcess;
|
|
protected static $aPipes = array();
|
|
|
|
protected function __construct() {
|
|
self::$rProcess= proc_open('rrdtool -', array(
|
|
0 => array('pipe', 'r'),
|
|
1 => array('pipe', 'w')), self::$aPipes);
|
|
stream_set_blocking(self::$aPipes[1], false);
|
|
}
|
|
|
|
static function command($sCommand) {
|
|
if (!isset(self::$rProcess)) {
|
|
self::$oInstance = new self();
|
|
}
|
|
var_dump($sCommand);
|
|
fwrite(self::$aPipes[0], $sCommand . PHP_EOL);
|
|
$nNull = null;
|
|
$aRead = array(self::$aPipes[1]);
|
|
stream_select($aRead, $nNull, $nNull, 10);
|
|
return trim(stream_get_contents(self::$aPipes[1]));
|
|
}
|
|
|
|
static function create($sFile, $iStep, $iStart, $sContents) {
|
|
$sCommand = sprintf(self::CREATE, $sFile, $iStep, $iStart, str_replace("\n", ' ', trim($sContents)));
|
|
return RRD::command($sCommand);
|
|
}
|
|
|
|
static function update($sFile, $iTime, $aValues) {
|
|
$sCommand = sprintf(self::UPDATE, $sFile, $iTime, implode(':', $aValues));
|
|
return RRD::command($sCommand);
|
|
}
|
|
|
|
static function fetch($sFile, $iResolution, $iStart, $iEnd, $sType = 'AVERAGE') {
|
|
$sCommand = sprintf(self::FETCH, $sFile, $sType, $iResolution, $iStart, $iEnd);
|
|
$sData = RRD::command($sCommand);
|
|
$aData = explode("\n", trim($sData));
|
|
$aFields = preg_split("~[\s]+~", array_shift($aData));
|
|
$aFields = array_flip($aFields);
|
|
array_shift($aData);
|
|
array_pop($aData);
|
|
foreach ($aData as $iKey => $sRow) {
|
|
$aRow = explode(':', $sRow);
|
|
$iTime = current($aRow);
|
|
$mValue = trim(next($aRow));
|
|
if ($mValue != '-nan') {
|
|
$aValues[$iTime] = floatval($mValue);
|
|
}
|
|
//$aValues[$iTime] = $fValue == 0 ? null : $fValue;
|
|
}
|
|
return array($aFields, $aValues);
|
|
}
|
|
|
|
function __destruct() {
|
|
fwrite(self::$aPipes[0], "quit\n");
|
|
fclose(self::$aPipes[0]);
|
|
fclose(self::$aPipes[1]);
|
|
proc_close(self::$rProcess);
|
|
}
|
|
} |