rrdtool可轻松解析输出

时间:2011-10-18 09:07:10

标签: rrdtool rrd

我正在使用大量的RRD文件,我必须对数据进行大量查询 - 主要是通过读取所有数据并传递它。

目前,我使用rrdtool fetch <filename> CF --start XXX --end YYY,但由于它一次只返回一个CF的数据,我首先要做一个单独的查询来查找CF(=运行和解析rrdtool info <filename>)和然后为每个找到的CF运行rrdtool fetch。但是,输出很容易解析。

或者,对于我想要的每件事,有rrdtool xport DEF:XX=<filename>:RRA:CF ... XPORT:XX:XX ...个后面命令的多个“集合”。从好的方面来说,这可以一次性给我所有的数据,但我仍然需要事先对我想要的数据有一个相当好的想法。此外,它只会吐出XML(总是很难解析)。

我有一种感觉我错过了一些非常明显的东西,因为从文件中获取时间戳→数字列表根本不是一件很麻烦的事情...任何线索?

3 个答案:

答案 0 :(得分:2)

虽然有are patches around用于添加JSON支持,但目前还没有解决方法:

  • 解析至少两种不同的输出格式(rrdtool info的ASCII,然后是rrdtool xport的XML或rrdtool fetch的表格数据。)
  • 通过rrdtool dump将文件的全部内容转储到XML,然后重新实现相当多的librrd内部。

答案 1 :(得分:1)

我编写了一个解析器,将rrdtool info /tmp/pb_1_amp.rrd的输出转换为嵌套数组。所以来自:

filename = "/tmp/pb_1_amp.rrd"
rrd_version = "0003"
step = 1800
last_update = 1372685403
header_size = 1208
ds[amp].index = 0
ds[amp].type = "GAUGE"
ds[amp].minimal_heartbeat = 3200
ds[amp].min = 0.0000000000e+00
ds[amp].max = 1.0000000000e+02
ds[amp].last_ds = "5.6"
ds[amp].value = 1.6800000000e+01
ds[amp].unknown_sec = 0
rra[0].cf = "AVERAGE"
rra[0].rows = 576
rra[0].cur_row = 385
rra[0].pdp_per_row = 1
rra[0].xff = 5.0000000000e-01
rra[0].cdp_prep[0].value = NaN
rra[0].cdp_prep[0].unknown_datapoints = 0
rra[1].cf = "AVERAGE"
rra[1].rows = 672
rra[1].cur_row = 159
rra[1].pdp_per_row = 6
rra[1].xff = 5.0000000000e-01
rra[1].cdp_prep[0].value = 1.6999833333e+01
rra[1].cdp_prep[0].unknown_datapoints = 0
rra[2].cf = "AVERAGE"
rra[2].rows = 732
rra[2].cur_row = 639
rra[2].pdp_per_row = 24
rra[2].xff = 5.0000000000e-01
rra[2].cdp_prep[0].value = 1.6999833333e+01
rra[2].cdp_prep[0].unknown_datapoints = 0
rra[3].cf = "AVERAGE"
rra[3].rows = 1460
rra[3].cur_row = 593
rra[3].pdp_per_row = 144
rra[3].xff = 5.0000000000e-01
rra[3].cdp_prep[0].value = 6.6083527778e+02
rra[3].cdp_prep[0].unknown_datapoints = 0

为:

Array
(
    [filename] => /tmp/pb_1_amp.rrd
    [rrd_version] => 0003
    [step] => 1800
    [last_update] => 1372685403
    [header_size] => 1208
    [ds] => Array
        (
            [amp] => Array
                (
                    [index] => 0
                    [type] => GAUGE
                    [minimal_heartbeat] => 3200
                    [min] => 0.0000000000e+00
                    [max] => 1.0000000000e+02
                    [last_ds] => 5.6
                    [value] => 1.6800000000e+01
                    [unknown_sec] => 0
                )

        )

    [rra] => Array
        (
            [0] => Array
                (
                    [cf] => AVERAGE
                    [rows] => 576
                    [cur_row] => 385
                    [pdp_per_row] => 1
                    [xff] => 5.0000000000e-01
                    [cdp_prep] => Array
                        (
                            [0] => Array
                                (
                                    [value] => NaN
                                    [unknown_datapoints] => 0
                                )

                        )

                )

            [1] => Array
                (
                    [cf] => AVERAGE
                    [rows] => 672
                    [cur_row] => 159
                    [pdp_per_row] => 6
                    [xff] => 5.0000000000e-01
                    [cdp_prep] => Array
                        (
                            [0] => Array
                                (
                                    [value] => 1.6999833333e+01
                                    [unknown_datapoints] => 0
                                )

                        )

                )

            [2] => Array
                (
                    [cf] => AVERAGE
                    [rows] => 732
                    [cur_row] => 639
                    [pdp_per_row] => 24
                    [xff] => 5.0000000000e-01
                    [cdp_prep] => Array
                        (
                            [0] => Array
                                (
                                    [value] => 1.6999833333e+01
                                    [unknown_datapoints] => 0
                                )

                        )

                )

            [3] => Array
                (
                    [cf] => AVERAGE
                    [rows] => 1460
                    [cur_row] => 593
                    [pdp_per_row] => 144
                    [xff] => 5.0000000000e-01
                    [cdp_prep] => Array
                        (
                            [0] => Array
                                (
                                    [value] => 6.6083527778e+02
                                    [unknown_datapoints] => 0
                                )

                        )

                )

        )

)

它在PHP中,但应该很容易移植到任何其他语言。这是代码:

$store = array();
foreach ($lines as $line) {
    list($raw_key, $raw_val) = explode(' = ', $line);

    $keys      = preg_split('/[\.\[\]]/', $raw_key, -1, PREG_SPLIT_NO_EMPTY);
    $key_count = count($keys);
    $pointer   = &$store;

    foreach ($keys as $key_num => $key) {
        if (!array_key_exists($key, $pointer)) {
            $pointer[$key] = array();
        }
        $pointer = &$pointer[$key];
        if ($key_num+1 === $key_count) {
            $pointer = trim($raw_val, '"');
        }
    }
}

它假定rrdtool info输出按换行(\n)拆分并在$lines中找到。希望这会有所帮助。

答案 2 :(得分:0)

如果您希望“目录”使用rrdtool info,如果您想要整个内容,请使用rrdtool dump

但是......你为什么要这样?

欢呼声 TOBI