rrd_fetch 不包含上次更新的值。为什么?
我将温度传感器的信息存储在 rrd 中。我想在 php.ini 中检索给定时间跨度的值。
对于过去 2 小时的数据,我使用这个:
rrd_fetch($file, array("AVERAGE", "-s -2h"));
This is running。但是,我可以通过rrd_lastupdate($file);获取的最后一个值在rrd_fetch的数据中找不到。如何调整我的代码,以便 rrd_fetch 调用也包含上次更新的数据?
rrd 在 Python 中的设置如下:
rrdtool.create(
RRD_FILE,
"--start", "now",
"--step", "300",
"RRA:AVERAGE:0.5:1:288",
"RRA:AVERAGE:0.5:12:336",
"RRA:AVERAGE:0.5:288:365",
"DS:temp:GAUGE:600:0:50")
并在 while 循环中更新如下:
while True:
temp = get_value()
data_rrd = "N:{0}".format(temp)
try:
rrdtool.update(file, data_rrd)
except:
print("Failed to write to RRD.")
time.sleep(300)
I am storing information from temperature sensors in a rrd. I want to retrieve the values from a given timespan in php.
For data from the last 2 hours, I use this:
rrd_fetch($file, array("AVERAGE", "-s -2h"));
This is working. However, the last value that I can get via rrd_lastupdate($file);
is not found in the data from rrd_fetch. How can I adapt my code so that the rrd_fetch call also contains data from the last update?
The rrd is setup like this in Python:
rrdtool.create(
RRD_FILE,
"--start", "now",
"--step", "300",
"RRA:AVERAGE:0.5:1:288",
"RRA:AVERAGE:0.5:12:336",
"RRA:AVERAGE:0.5:288:365",
"DS:temp:GAUGE:600:0:50")
And updated like this in a while loop:
while True:
temp = get_value()
data_rrd = "N:{0}".format(temp)
try:
rrdtool.update(file, data_rrd)
except:
print("Failed to write to RRD.")
time.sleep(300)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
lastupdate()
和fetch()
执行不同的操作。当您将数据添加到 RRD 时,后台会进行一个多阶段过程。首先,数据转化为费率;然后归一化;然后合并。
提交数据
update()
数据将转换为费率。如果您的 DS 是
gauge
类型,那么这是一个 noop,但其他类型将根据上次更新的时间进行转换。现在我们得到
lastupdate()
返回的值,也就是这个DP(数据点)数据已标准化。这意味着我们假设从最后一个已知数据点开始呈线性进展,并计算最后一步间隔的平均值(在上面的示例中为 300 秒)。这给出了时间窗口边界上的新 PDP(主数据点)(即 PDP 时间是间隔的倍数)
PDP 现在已合并。收集到足够的数据后,新的 CDP(合并数据点)将根据 CF 和 XFF 写入 RRA。如果您的 RRA 有 1cdp=1pdp(如您的情况),那么 CDP 应该与 PDP 相同。
现在我们在 RRA 中获取 CDP,这就是
fetch()
返回的内容。由此可以看出,您存储的值与
lastupdate()
不一样(由于归一化和速率);这与 fetch() 不同(由于合并)。此外,在时间窗口完成之前,您不会看到
lastupdate()
中出现的值;在 RRA CDP 完成之前,您不会看到它出现在fetch()
中。由于您的
fetch()
不需要比 1cdp=1pdp RRA 中更多的数据,因此您将避免因合并而发生的更改,但您可能会遇到标准化。如果您希望显示的值相同,则需要确保所有 3 个操作都是无操作。
使用类型
gauge
停止汇率转换确保所有更新都在时间边界上准确完成,以避免标准化。不要在更新中使用“N”(现在),将其修复为间隔(300 秒)倍数的时间戳。
拥有足够大小的 1cdp=1pdp RRA 以避免合并。
有关此内容的更多详细信息,请参阅 Alex 的著名页面:http://rrdtool.vandenbogaerdt.nl/process。 php
This is because
lastupdate()
andfetch()
do different things.When you add data to an RRD, there is a multi-stage process going on in the background. First, the data are turned to Rates; then Normalised; then Consolodated.
Data submitted
update()
Data are converted to a Rate. If your DS is of type
gauge
then this is a noop, but other types will be converted based on the time of the previous update.Now we get the value returned by
lastupdate()
, which is this DP (datapoint)Data are Normalised. This means that we assume a linear progression from the last known datapoint, and calculate the average over the last step Interval (300s in your example above). This gives a new PDP (primary datapoint) on a time window boundary (IE with the PDP time being a multiple of the Interval)
The PDP are now Consolodated. When sufficient have been collected,a new CDP (Consolodated Datapoint) is written to the RRA, depending on the CF and XFF. If your RRA has 1cdp=1pdp (as in your case) then the CDP should be the same as the PDP.
Now we get the CDP in the RRA, which is what
fetch()
returns.From this, you can see that the value you store is not the same as the
lastupdate()
(due to Normalisation and Rate); and this is not the same as thefetch()
(due to Consolodation).In addition, you won't see the value appearing in the
lastupdate()
until the time window has completed; and you wont see it appearing infetch()
until the RRA CDP is completed.Since your
fetch()
is not asking for more data than in your 1cdp=1pdp RRA, you'll avoid changes due to consolodation, but you're likely hitting Normalisation.If you want the shown values to be the same, then you need to ensure all 3 operations are no-ops.
Use type
gauge
to stop Rate conversionEnsure all updates are done exactly on a time boundary to avoid Normalisation. Don't use 'N' (now) in the update, fix it to a timestamp that is a multiple of the Interval (300s).
Have a 1cdp=1pdp RRA of sufficient size to avoid Consolodation.
For more detail on this, see Alex's famous page here: http://rrdtool.vandenbogaerdt.nl/process.php