1

我有一个我正在尝试创建的 RRD 数据库,并且我编写了几个循环来构建命令,它工作正常,并产生预期的输出。当我实际尝试在 Python 中运行 rrdtool.create 命令时,问题就出现了。这个循环产生一个字符串,我们称之为它rrdStr,包含以下内容:

'"0x04.rrd", "--step","5",
"DS:TEMP:GAUGE:10:0:100",
"DS:HUMIDITY:GAUGE:10:0:100",
"DS:LIGHT:GAUGE:10:0:1024",
"DS:POT:GAUGE:10:0:1024",
"RRA:AVERAGE:0.5:1:17142",
"RRA:MIN:0.5:1:17142",
"RRA:MAX:0.5:1:17142",
"RRA:AVERAGE:0.5:12:7200",
"RRA:MIN:0.5:12:7200",
"RRA:MAX:0.5:12:7200",
"RRA:AVERAGE:0.5:60:8640",
"RRA:MIN:0.5:60:8640",
"RRA:MAX:0.5:60:8640",
"RRA:AVERAGE:0.5:360:17532",
"RRA:MIN:0.5:360:17532",
"RRA:MAX:0.5:360:17532"'

据我所知,所有引号和换行符都已正确转义。

如果我像这样传递rrdtool.create字符串变量:rrdtool.create(rrdStr),我得到输出

rrdtool.error: you must define at least one Round Robin Archive

但是,如果我将上面的输出直接复制并粘贴到rrdtool.create()函数中,它会像预期的那样工作,并创建数据库。

知道是什么原因造成的以及如何解决吗?

4

2 回答 2

1

所以我终于想通了。您必须创建一个列表,而不是创建一个包含正确格式的所有命令的长字符串,在生成命令时将 rrd 命令的每一行添加到列表中。

上面的命令变成:

rrdStr = ["0x04.rrd", "--step","5"]
rrdStr += ["DS:TEMP:GAUGE:10:0:100"]
rrdStr += ["DS:HUMIDITY:GAUGE:10:0:100"]
rrdStr += ["DS:LIGHT:GAUGE:10:0:1024"]
rrdStr += ["DS:POT:GAUGE:10:0:1024"]
rrdStr += ["RRA:AVERAGE:0.5:1:17142"]
rrdStr += ["RRA:MIN:0.5:1:17142"]
rrdStr += ["RRA:MAX:0.5:1:17142"]
rrdStr += ["RRA:AVERAGE:0.5:12:7200"]
rrdStr += ["RRA:MIN:0.5:12:7200"]
rrdStr += ["RRA:MAX:0.5:12:7200"]
rrdStr += ["RRA:AVERAGE:0.5:60:8640"]
rrdStr += ["RRA:MIN:0.5:60:8640"]
rrdStr += ["RRA:MAX:0.5:60:8640"]
rrdStr += ["RRA:AVERAGE:0.5:360:17532"]
rrdStr += ["RRA:MIN:0.5:360:17532"]
rrdStr += ["RRA:MAX:0.5:360:17532"]

然后您可以将列表作为唯一命令传递rrdtool.create(rrdStr)之前失败的相同命令将成功运行

于 2013-08-22T02:20:50.627 回答
-1

在 python2.6 rrdtool-python-1.3.8-6.el6.x86_64 上使用 rrdtool.create(*rrdStr)

于 2014-09-22T08:17:30.480 回答