Ejemplo n.º 1
0
 /**
  * Get the values of a metric
  *
  * @param int $metricId The metric id
  * @return array
  */
 public function getValues($metricId, $rows = 100)
 {
     $rrdFile = $this->rrdPath . '/' . $metricId . '.rrd';
     if (file_exists($rrdFile)) {
         $options = array('--start', $this->startTime, '--end', $this->endTime, '--maxrows', $rows, 'DEF:metric=' . $rrdFile . ':value:AVERAGE', 'XPORT:metric:"Values"');
         $values = rrd_xport($options);
         if (false === $values) {
             throw new \Exception("Error when getting metric values");
         }
         return $values['data'][0]['data'];
     } else {
         throw new \Exception("rrd file " . $rrdFile . " doesn't exist");
     }
 }
function do_xport($start, $end, $step, $filename, $testnet = false)
{
    if ($testnet) {
        $testnet = "testnet";
    } else {
        $testnet = '';
    }
    xecho("RRD DB xport ({$start} to {$end} step {$step} {$testnet}): ");
    $xport = rrd_xport(array("-s", $start, "-e", $end, "--step", $step, "DEF:a=mnvotes2{$testnet}.rrd:Yea:MAX", "DEF:b=mnvotes2{$testnet}.rrd:Nay:MAX", "DEF:c=mnvotes2{$testnet}.rrd:Abstain:MAX", 'XPORT:a:Yea', 'XPORT:b:Nay', 'XPORT:c:Abstain'));
    if ($xport === false) {
        echo "Failed!\n";
        return false;
    } else {
        echo "OK\n";
        foreach ($xport["data"] as $key1 => $data1) {
            foreach ($xport["data"][$key1]["data"] as $key2 => $data2) {
                if (is_nan($data2)) {
                    $xport["data"][$key1]["data"][$key2] = false;
                } else {
                    $xport["data"][$key1]["data"][$key2] = intval(round($data2));
                }
            }
        }
    }
    xecho("JSON Encoding output: ");
    $json = json_encode($xport);
    if ($json !== false) {
        echo "OK (" . strlen($json) . " bytes)\n";
        xecho("Writing to file: ");
        if (file_put_contents($filename, $json) !== false) {
            echo "OK\n";
            return true;
        } else {
            echo "Error\n";
            return false;
        }
    } else {
        echo "Error\n";
        return false;
    }
}