【问题标题】:Import sensor data to RRDtool DB将传感器数据导入 RRDtool DB
【发布时间】:2017-03-11 14:25:38
【问题描述】:

尝试将从 RFXtrx433e USB 控制器收集的几个温度传感器的数据导入 RRDtool DB。输出到 .txt 文件

我的数据库是这样创建的:

[代码]

# Script to create rrd-file

# 24h with 2,5 min resolution
# 7d with 5 min resolution
# 1y with 10 min resolution
# 20y with 1h resolution

directory="/home/pi/temp/rrddata/"
filename="domoticz_temp.rrd"
# Check i file already exists
if [ ! -f "$directory$filename" ]
then
        # File doesn't exist, create new rrd-file
        echo "Creating RRDtool DB for outside temp sensor"
        rrdtool create $directory$filename \
                 --step 120 \
                 DS:probe:GAUGE:120:-50:60 \
                 DS:xxxx1:GAUGE:120:-50:60 \
                 DS:vardagsrum:GAUGE:120:-50:60 \
                 RRA:AVERAGE:0.5:1:576 \
                 RRA:AVERAGE:0.5:2:2016 \
                 RRA:AVERAGE:0.5:4:52560 \
                 RRA:AVERAGE:0.5:24:175200 \
                 RRA:MAX:0.5:1:5760 \
                 RRA:MAX:0.5:2:2016 \
                 RRA:MAX:0.5:4:52560 \
                 RRA:MAX:0.5:24:175200 \
                 RRA:MIN:0.5:1:5760 \
                 RRA:MIN:0.5:2:2016 \
                 RRA:MIN:0.5:4:52560 \
                 RRA:MIN:0.5:24:175200
        echo "Done!"
else
        echo $directory$filename" already exists, delete it first."
fi

传感器数据的导入

rrdtool update /home/pi/temp/rrddata/domoticz_temp.rrd --template probe N:`head -n 1 </home/pi/temp/output/temp_probe.txt`

导入的文本文件只包含一行数字(通过 LUA 脚本从传感器收集的温度)

创建图表的代码

rrdtool graph /home/pi/temp/output/img/test/hour.png \
-w 697 -h 287 -a PNG \
--slope-mode \
--start -6h --end now \
--vertical-label "Last 6 hour temperature" \
DEF:probe=/home/pi/temp/rrddata/domoticz_temp.rrd:probe:AVERAGE \
DEF:xxxx1=/home/pi/temp/rrddata/domoticz_temp.rrd:xxxx1:AVERAGE \
DEF:vardagsrum=/home/pi/temp/rrddata/domoticz_temp.rrd:vardagsrum:AVERAGE \
COMMENT:"  Location       Min        Max       Senaste\l" \
LINE1:probe#ff0000:"Utetemp" \
LINE1:0#ff0000: \
GPRINT:probe:MIN:"    %5.1lf"  \
GPRINT:probe:MAX:"     %5.1lf"  \
GPRINT:probe:LAST:"     %5.1lf\n"  \
LINE1:xxxx1#00ff00:"Xxxx1" \
LINE1:0#00ff00: \
GPRINT:probe:MIN:"      %5.1lf"  \
GPRINT:probe:MAX:"     %5.1lf"  \
GPRINT:probe:LAST:"     %5.1lf\n"  \
LINE1:vardagsrum#0000ff:"vardagsrum" \
LINE1:0#0000ff: \
GPRINT:probe:MIN:" %5.1lf"  \
GPRINT:probe:MAX:"     %5.1lf"  \
GPRINT:probe:LAST:"     %5.1lf\n"  \

给我这张图http://i.imgur.com/lnFxTik.png

现在回答我的问题:

  • 我是否以正确的方式创建了数据库和脚本的其余部分?我认为应该对不在数据库中的值获取 NAN?

  • 如何导入其余传感器?它们位于多个模拟 TXT 文件中。

  • 我应该/我可以通过另一种更好的方式从传感器收集数据以将它们输入 RRDtool 数据库吗?

希望任何人都可以帮助我。


新信息!

用于收集传感器数据的 LUA 脚本

commandArray = {}
if (devicechanged['Probe']) then
        local file = io.open("/home/pi/temp/output/temp_probe.txt", "w")
        file:write(tonumber(otherdevices_temperature['Probe']))
        file:close()
end

if (devicechanged['Xxxx1']) then
        local file = io.open("/home/pi/temp/output/temp_xxxx1.txt", "w")
        file:write(tonumber(otherdevices_temperature['Xxxx1']))
        file:close()
end

if (devicechanged['Vardagsrum']) then
        local file = io.open("/home/pi/temp/output/temp_vardagsrum.txt", "w")
        file:write(tonumber(otherdevices_temperature['Vardagsrum']))
        file:close()
end
return commandArray`

【问题讨论】:

    标签: bash graph raspberry-pi2 rrdtool


    【解决方案1】:
    1. 是的,如果缺少值,您将得到 NaN。您的 create 语句看起来不错...虽然 20y 分辨率为 1h...哇!

    2. 从多个文本文件导入会像这样工作

    .

    A=`perl -ne 'chomp;print;exit' xx1.txt`
    B=`perl -ne 'chomp;print;exit' xx2.txt`
    rrdtool update domoticz_temp.rrd --template xx1:xx2 N:$A:$B
    

    .

    1. 是的,我建议不要先将它们写入文件,而是直接更新 rrd 文件。

    【讨论】:

    • 1.是的,我知道的有点多。将制作一个最大 1y 的新 :)。 2. 请指导我如何将它们直接写入rrd。我已将我的 LUA 脚本添加到 Total 新手之上,因此我非常感谢您的所有帮助
    【解决方案2】:
    # 24h with 2,5 min resolution
    # 7d with 5 min resolution
    # 1y with 10 min resolution
    # 20y with 1h resolution
    ...
            rrdtool create $directory$filename \
                     --step 120 \
                     DS:probe:GAUGE:120:-50:60 \
                     DS:xxxx1:GAUGE:120:-50:60 \
                     DS:vardagsrum:GAUGE:120:-50:60 \
                     RRA:AVERAGE:0.5:1:576 \
                     RRA:AVERAGE:0.5:2:2016 \
                     RRA:AVERAGE:0.5:4:52560 \
                     RRA:AVERAGE:0.5:24:175200 \
    

    好的,您的步长似乎是 2 分钟,而您的 RRA 分为 1、2、4 和 24 步。这对应于 2 分钟、4 分钟、8 分钟和 48 分钟,而不是 2.5、5、10 和 1 小时。也许你的步数应该是 150?此外,您的 DS 上的心跳与您的步数相同,这可能会导致您丢失数据。一般来说,心跳应该是步长的 1.5 到 2 倍左右,以允许不规则的数据到达。

    但是,这与您的“未知”问题无关,Tobi 已经回答了很多问题。

    1. 您将在尚未加载的时间段上显示“未知”,是的。

    2 和 3。由于您有一个 RRD,因此您需要在同一操作中以同一时间戳更新所有样本。在这种情况下,您最好一次收集它们并将它们存储到同一个文件中,这样您就可以将它们一起加载并一起存储到 RRD 中。如果这是一个问题,并且传感器是独立探测的,那么我建议为每个传感器设置一个单独的 RRD,以便您可以独立更新它们。您仍然可以同时生成所有 3 个图形,因为您可以将图形 DEF 定义为指向不同的 RRD 文件没有问题。这可能是一个更好的方法。

    Tobi 关于 20 年 RRA 的说法是对的,这可能有点过分;)

    【讨论】:

    • 好点。我做了几个 RRD 并得到了我想要的结果。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2019-10-05
    • 2017-12-22
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多