This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

influxdb library
#1
I am sharing a way to send easily object values to influxdb.

Update server and database name before using it.
influx_clim send object with tag "influx_clim"
add "influx_clim()" to a schedule for automatic export


Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
require 'ltn12' require 'socket.http' local https = require("ssl.https") local influxdb_debug = true local influxdb_server = "https://user:pass@host:8086" local influxdb_database = "db_name" local influxdb_buffer --function used to convert object name (logicmachine) to valid influxdb field key function influxdb_normalize(name)  return string.gsub(name,  " ", "_") end --add an entry to be send function influxdb_add(line_protocol)  if (influxdb_debug) then    alert("influxdb_add start")  end    if influxdb_buffer then    influxdb_buffer = influxdb_buffer .. "\n" .. tostring(line_protocol)  else    influxdb_buffer =  tostring(line_protocol)  end    if (influxdb_debug) then    alert("influxdb_add done")  end   end function influxdb_current()  return influxdb_buffer end -- send the buffer to influxdb function influxdb_send()    if (influxdb_debug) then    alert("influxdb_send start")  end    local request_insert_url = influxdb_server .. "/write?db=" .. influxdb_database   --  log(influxdb_buffer)    local response_body = { }  local body, code, hdrs, stat = https.request  {    url = request_insert_url;    verify = "none";    method = "POST";    headers =    {      ["Content-Length"] = #influxdb_buffer;    };    source = ltn12.source.string(influxdb_buffer);    sink = ltn12.sink.table(response_body);  }  influxdb_buffer = nil  if (code ~= 204) then    alert("influxdb error code:" .. tostring(code))    alert("influxdb_send http post error")  end  if (influxdb_debug) then       alert("influxdb_send done")  end   end --export all objects with tag "influx_clim" function influx_clim()    local values=grp.tag("influx_clim")  if (influxdb_debug) then    alert("influx_cilm start")  end  local txtvalues = ""  for index, id in ipairs(values) do    -- add separator    if (#txtvalues>0) then      txtvalues = txtvalues .. ","    end      -- calculate value to be sent    if (type(id["data"])=="boolean") then      value=id["data"] and 1 or 0    else      value=id["data"]    end    -- add key value pair    txtvalues = txtvalues .. influxdb_normalize(id["name"]) .. "=" .. value  end  influxdb_add("clim " .. txtvalues)  influxdb_send()  if (debug) then    alert("influx_cilm end")  end end
Reply
#2
(20.10.2018, 21:13)Matt Wrote: I am sharing a way to send easily object values to influxdb.

Update server and database name before using it.
influx_clim send object with tag "influx_clim"
add "influx_clim()" to a schedule for automatic export


Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
require 'ltn12' require 'socket.http' local https = require("ssl.https") local influxdb_debug = true local influxdb_server = "https://user:pass@host:8086" local influxdb_database = "db_name" local influxdb_buffer --function used to convert object name (logicmachine) to valid influxdb field key function influxdb_normalize(name)  return string.gsub(name,  " ", "_") end --add an entry to be send function influxdb_add(line_protocol)  if (influxdb_debug) then    alert("influxdb_add start")  end    if influxdb_buffer then    influxdb_buffer = influxdb_buffer .. "\n" .. tostring(line_protocol)  else    influxdb_buffer =  tostring(line_protocol)  end    if (influxdb_debug) then    alert("influxdb_add done")  end   end function influxdb_current()  return influxdb_buffer end -- send the buffer to influxdb function influxdb_send()    if (influxdb_debug) then    alert("influxdb_send start")  end    local request_insert_url = influxdb_server .. "/write?db=" .. influxdb_database   --  log(influxdb_buffer)    local response_body = { }  local body, code, hdrs, stat = https.request  {    url = request_insert_url;    verify = "none";    method = "POST";    headers =    {      ["Content-Length"] = #influxdb_buffer;    };    source = ltn12.source.string(influxdb_buffer);    sink = ltn12.sink.table(response_body);  }  influxdb_buffer = nil  if (code ~= 204) then    alert("influxdb error code:" .. tostring(code))    alert("influxdb_send http post error")  end  if (influxdb_debug) then       alert("influxdb_send done")  end   end --export all objects with tag "influx_clim" function influx_clim()    local values=grp.tag("influx_clim")  if (influxdb_debug) then    alert("influx_cilm start")  end  local txtvalues = ""  for index, id in ipairs(values) do    -- add separator    if (#txtvalues>0) then      txtvalues = txtvalues .. ","    end      -- calculate value to be sent    if (type(id["data"])=="boolean") then      value=id["data"] and 1 or 0    else      value=id["data"]    end    -- add key value pair    txtvalues = txtvalues .. influxdb_normalize(id["name"]) .. "=" .. value  end  influxdb_add("clim " .. txtvalues)  influxdb_send()  if (debug) then    alert("influx_cilm end")  end end

Hi, I am working on this script but It seems it does not work.

I mean that I use the script above in a library and in a scheduled script which runs everyminute I wrote as followed:

objs = grp.tag('influx_clim')
require ('user.influx')

The problem is that I expect some alert message (correct or incorrect) but nothing happened. How can sort it out?

Thanks.
Reply
#3
In the schedule, you only need to have:

Code:
1
influx_clim()
Reply
#4
(04.11.2020, 12:42)Matt Wrote: In the schedule, you only need to have:

Code:
1
influx_clim()

Sorry but I forgot to leave the feedback.

Everything works fine.

Thanks.

BR
Reply


Forum Jump: