14.12.2021, 10:00
Modified send_metric function for the previous example. Replace IP/BUCKET/ORG/TOKEN placeholders with your parameters.
Code:
function send_metric(table, name, addr, value)
if name == nil or name == '' then
return
end
name = string.gsub(name, ' ', '\\ ')
local url = 'http://IP:8086/api/v2/write?bucket=BUCKET&org=ORG'
local body
if type(value) == 'boolean' then
body = string.format('%s,name=%s,addr=%s state=%s', table, name, addr, value)
else
-- most likely number
body = string.format('%s,name=%s,addr=%s value=%s', table, name, addr, value)
end
local res, code = http.request({
url = url,
method = 'POST',
body = body,
headers = {
Authorization = 'Token TOKEN'
}
})
if code ~= 204 then
log('error sending to influx', res, code, body)
end
end