2
0

More than 3 years have passed since last update.

Telegraf の入力データフォーマットを JSON にする

Last updated at Posted at 2021-08-29

こちらのプログラムの Telegraf の入力フォーマットを JSON に変更しました。プロトコルは udp のままです。
Telegraf で InfluxDB 2.0 にデータを入れる

Telegraf の設定ファイル

/etc/telegraf/telegraf.conf
(省略)
[[outputs.influxdb_v2]]
urls = ["http://127.0.0.1:8086"]
token = "1zIYs1taCm3pWQOBUNk0ssYccOF7YgJcdGyJDbiFYTBf7hcPvyl5nGyt71rNVkYtg2rB
ceUwQ1r5Xzm7Mabcde=="
organization = "ekzemplaro"
bucket = "tochigi"
(省略)
[[inputs.socket_listener]]
service_address = "udp://:8092"
json_name_key = "name"
data_format = "json"
(省略)

テストスクリプト

#
echo '{"name":"aaa","temperature":23.5,"humidity":42.3}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":24.6,"humidity":53.5}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":25.9,"humidity":64.3}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":26.7,"humidity":76.3}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":25.5,"humidity":55.3}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":25.1,"humidity":48.3}' | ncat -4 -u -w 1 localhost 8092
sleep 10
echo '{"name":"aaa","temperature":24.1,"humidity":43.7}' | ncat -4 -u -w 1 localhost 8092

Python3 のテストスクリプト

udp_send.py
#! /usr/bin/python
#
#   udp_send.py
#
#                       Aug/29/2021
# ------------------------------------------------------------------
import  sys
import  json
import  time
from socket import socket, AF_INET, SOCK_DGRAM

HOST = ''
PORT = 8092
ADDRESS = "localhost"

# ------------------------------------------------------------------
def udp_send_proc(temp,humi):
    unit_aa = {}
    unit_aa["name"] = "aaa"
    unit_aa["temperature"] = temp
    unit_aa["humidity"] = humi
    msg = json.dumps(unit_aa)
#
    ss = socket(AF_INET, SOCK_DGRAM)
    ss.sendto(msg.encode(), (ADDRESS, PORT))
    ss.close()
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
udp_send_proc(25.4,55.2)
time.sleep(10)
udp_send_proc(26.4,56.2)
time.sleep(10)
udp_send_proc(26.2,56.9)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

Node.js のサンプルです。

udpclient.js
#! /usr/bin/node
// ---------------------------------------------------------------
//
//  udpclient.js
//
//                  Aug/29/2021 
// ---------------------------------------------------------------
// UDP Sample Client

// UDP 接続先
const host = "localhost"
const c_port = 8092
var dgram = require("dgram")
var client = dgram.createSocket("udp4")

// ---------------------------------------------------------------
// サーバからメッセージ受信したときの処理
client.on("message", function(msg, rinfo) {
    console.log("recieved: " + msg.toString("hex"))
    client.close()
})

// ---------------------------------------------------------------
// メッセージ送信でエラーが起きた時の処理
client.on("err", function(err) {
    console.log("client error: \n" + err.stack);
    console.close()
})

// ---------------------------------------------------------------
// Socket をクローズした時の処理
client.on("close", function() {
    console.log("closed.")
})

// ---------------------------------------------------------------
// メッセージ送信

function send(message, host, port) {
    client.send(message, 0, message.length, port, host, function(err, bytes) {
    console.log("*** sent ***")
    client.close()
    console.error ("*** 終了 ***")
    })
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

var unit_aa = new Object ()
unit_aa["name"] = "aaa"
unit_aa["temperature"] = 27.0
unit_aa["humidity"] = 56.3
const json_str = JSON.stringify(unit_aa)
const message = new Buffer.from(json_str)

send(message, host, c_port)

// ---------------------------------------------------------------

こちらのプログラムを参考にしました。
PythonによるUDP通信

Node.js で繰り返し送信するサンプルはこちら
Node.js: 一定時間毎に udp 送信を行う

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0