/** * Determine useful information about RRD file * @file Name of RRD file to analyse * @return Array describing the RRD file */ function rrd_info($file) { $info = array('filename' => $file); $rrd = popen(RRDTOOL . ' info ' . escapeshellarg($file), 'r'); if ($rrd) { while (($s = fgets($rrd)) !== false) { $p = strpos($s, '='); if ($p === false) { continue; } $key = trim(substr($s, 0, $p)); $value = trim(substr($s, $p + 1)); if (strncmp($key, 'ds[', 3) == 0) { /* DS definition */ $p = strpos($key, ']'); $ds = substr($key, 3, $p - 3); if (!isset($info['DS'])) { $info['DS'] = array(); } $ds_key = substr($key, $p + 2); if (strpos($ds_key, '[') === false) { if (!isset($info['DS']["{$ds}"])) { $info['DS']["{$ds}"] = array(); } $info['DS']["{$ds}"]["{$ds_key}"] = rrd_strip_quotes($value); } } else { if (strncmp($key, 'rra[', 4) == 0) { /* RRD definition */ $p = strpos($key, ']'); $rra = substr($key, 4, $p - 4); if (!isset($info['RRA'])) { $info['RRA'] = array(); } $rra_key = substr($key, $p + 2); if (strpos($rra_key, '[') === false) { if (!isset($info['RRA']["{$rra}"])) { $info['RRA']["{$rra}"] = array(); } $info['RRA']["{$rra}"]["{$rra_key}"] = rrd_strip_quotes($value); } } else { if (strpos($key, '[') === false) { $info[$key] = rrd_strip_quotes($value); } } } } pclose($rrd); } return $info; }
function rrdtool_file_info($file) { $info = array('filename' => $file); $rrd = array_filter(explode(PHP_EOL, shell_exec($GLOBALS['config']['rrdtool'] . " info " . $file)), 'strlen'); if ($rrd) { foreach ($rrd as $s) { $p = strpos($s, '='); if ($p === false) { continue; } $key = trim(substr($s, 0, $p)); $value = trim(substr($s, $p + 1)); if (strncmp($key, 'ds[', 3) == 0) { /* DS definition */ $p = strpos($key, ']'); $ds = substr($key, 3, $p - 3); if (!isset($info['DS'])) { $info['DS'] = array(); } $ds_key = substr($key, $p + 2); if (strpos($ds_key, '[') === false) { if (!isset($info['DS']["{$ds}"])) { $info['DS']["{$ds}"] = array(); } $info['DS']["{$ds}"]["{$ds_key}"] = rrd_strip_quotes($value); } } else { if (strncmp($key, 'rra[', 4) == 0) { /* RRD definition */ $p = strpos($key, ']'); $rra = substr($key, 4, $p - 4); if (!isset($info['RRA'])) { $info['RRA'] = array(); } $rra_key = substr($key, $p + 2); if (strpos($rra_key, '[') === false) { if (!isset($info['RRA']["{$rra}"])) { $info['RRA']["{$rra}"] = array(); } $info['RRA']["{$rra}"]["{$rra_key}"] = rrd_strip_quotes($value); } } else { if (strpos($key, '[') === false) { $info[$key] = rrd_strip_quotes($value); } } } } } return $info; }