telegraf和influxdb记录

telegraf

需要到官网下载并安装。解压到根目录即可

Configuration options | Telegraf Documentation (influxdata.com)

难点是config文件的配置。官方文档过于全面,初学无从下手,好在摸索后可以从plugins网页文档里面筛选

Plugin directory | Telegraf Documentation (influxdata.com)

暂存一份精简的config文件如下:

[agent]
  interval = "10ms"
  hostname="b32bd9e0-6cdc-11ee-b64f-e45f01f126a9"
  omit_hostname=false

# OUTPUTS
[[outputs.influxdb_v2]]
  urls = ["http://ip.ip.ip.ip:8086"] # required.
  organization = "USTC"
  token = ""
  bucket = "telegraf" # required.
  precision = "1ms"

[[inputs.file]]
  files = ["example.bin"]
  data_format = "binary"

  [[inputs.file.binary]]
    metric_name = "seismometer"
    entries = [
      {name = "E", type = "float64"},
      {name = "N", type = "float64"},
      {name = "Z", type = "float64"},
      {type = "unix_ms", assignment = "time"},
    ]

它的输出是:

pi@raspberrypi:~/playground/test_telegraf $ telegraf --config bin.conf --test
2023-10-17T16:01:21Z I! Loading config: bin.conf
2023-10-17T16:01:21Z I! Starting Telegraf 1.28.2 brought to you by InfluxData the makers of InfluxDB
2023-10-17T16:01:21Z I! Available plugins: 240 inputs, 9 aggregators, 29 processors, 24 parsers, 59 outputs, 5 secret-stores
2023-10-17T16:01:21Z I! Loaded inputs: file
2023-10-17T16:01:21Z I! Loaded aggregators:
2023-10-17T16:01:21Z I! Loaded processors:
2023-10-17T16:01:21Z I! Loaded secretstores:
2023-10-17T16:01:21Z W! Outputs are not used in testing mode!
2023-10-17T16:01:21Z I! Tags enabled: host=b32bd9e0-6cdc-11ee-b64f-e45f01f126a9
> seismometer,host=b32bd9e0-6cdc-11ee-b64f-e45f01f126a9 E=0.07647374308470278,N=0.33807867163447913,Z=0.18040984118055958 1697549700294000000

其中seismometer是measurement,host是仪器UID,ENZ是各分量读数,最后是时间(单位ms)

这个程序是用来将地震仪存到二进制文件example.bin里的读数提取并发送到influxdb的。

写入读数的例程如下:

import mmap
import datetime
import time
import struct
import random
from tqdm import *


def genSN():
    a = random.random()
    b = random.random()
    c = random.random()
    now = datetime.datetime.now()
    ts = int(now.timestamp()*1000)

    res = struct.pack("3dL",a,b,c,ts)
    return res

with open('example.bin', 'r+b') as f:
    f.write(genSN())
print("init over")

with open('example.bin', 'r+b') as f:
    with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE) as m:
        for i in tqdm(range(1000)):
            sn = genSN()
            m[0:0+len(sn)] = sn
            #print(struct.unpack("3dL", m.read()))
            time.sleep(0.01)
print("write over")

为了提高写入速度,采用了mmap内存映射的方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值