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内存映射的方法