Refactorisé le socat afin de grapher dans Grafana les retry de connexions car j'ai des problèmes quand je perds le wifi

This commit is contained in:
Christian Zufferey
2020-06-06 13:21:08 +02:00
parent 5e71652c2c
commit a7093b2b49
7 changed files with 222 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
-- Petit script pour faire office de crontab pour les mesures
print("\n 0_cron.lua zf200604.1510 \n")
print("\n 0_cron.lua zf200604.1313 \n")
cron1=tmr.create()
cron1:alarm(10*1000, tmr.ALARM_AUTO, function()
@@ -13,7 +13,7 @@ cron1:alarm(10*1000, tmr.ALARM_AUTO, function()
f=nil
if verbose then print("End cron:\n"..node.heap()) end
if verbose then print("End cron:") end
collectgarbage()
if verbose then print(node.heap()) end
end)

View File

@@ -1,7 +1,7 @@
-- Petit script pour envoyer en // es valeurs sur un serveur WEB (InfluxDB)
-- via un http POST à travers un FIFO
if verbose then print("\n 0_http_post.lua zf200527.1836 \n") end
if verbose then print("\n 0_http_post.lua zf200606.1316 \n") end
t_zurl={} t_zarg={} f_zpost=false
@@ -27,7 +27,7 @@ function zpost()
else
zpost()
end
if verbose then print("End zpost:\n"..node.heap()) end
if verbose then print("End zpost:") end
collectgarbage()
if verbose then print(node.heap()) end
@@ -36,12 +36,11 @@ function zpost()
zurl=nil zarg=nil
end
function http_post(zurl,zarg)
table.insert(t_zurl, zurl) table.insert(t_zarg, zarg)
if verbose then print("t_zurl:") tprint(t_zurl) print("t_zarg:") tprint(t_zarg) end
if f_zpost==false then zpost() end
if verbose then print("End http_post:\n"..node.heap()) end
if verbose then print("End http_post:") end
collectgarbage()
if verbose then print(node.heap()) end
end

View File

@@ -0,0 +1,198 @@
--[[
tests connection reverse telnet commande à faire tourner sur le GATEWAY !
1ere console
pour une liaison directe:
socat TCP-LISTEN:23043,fork,reuseaddr STDIO
pour une console sur un port:
socat TCP-LISTEN:23043,reuseaddr,fork TCP-LISTEN:23000,reuseaddr,bind=127.0.0.1
2e console
telnet -r localhost 23000
]]
--[[
tests connection reverse telnet commande à faire tourner sur le GATEWAY ET sur sa MACHINE !
1ere console sur le GATEWAY
socat TCP-LISTEN:23043,reuseaddr,fork TCP-LISTEN:23000,reuseaddr,bind=127.0.0.1
2e console sur sa MACHINE
ssh -L 23000:localhost:23000 user@GATEWAY
3e console sur sa MACHINE (~.return pour sortir !)
telnet -r localhost 23000
ou sur MAC
telnet -rN localhost 23000
]]
print("\n 0_tst4_socat.lua zf200606.1256 \n")
local node, table, tmr, uwrite, tostring =
node, table, tmr, uart.write, tostring
local function telnet_listener(socket)
local insert, remove, concat, heap, gc =
table.insert, table.remove, table.concat, node.heap, collectgarbage
local fifo1, fifo1l, fifo2, fifo2l = {}, 0, {}, 0
local s -- s is a copy of the TCP socket if and only if sending is in progress
-- local wdclr, cnt = tmr.wdclr, 0
-- local function debug(fmt, ...)
-- if (...) then fmt = fmt:format(...) end
-- uwrite(0, "\r\nDBG: ",fmt,"\r\n" )
-- cnt = cnt + 1
-- if cnt % 10 then wdclr() end
-- end
local function flushGarbage()
if heap() < 13440 then gc() end
end
local function sendLine()
if not s then return end
if fifo2l + fifo1l == 0 then -- both FIFOs empty, so clear down s
s = nil
return
end
flushGarbage()
if #fifo2 < 4 then -- Flush FIFO1 into FIFO2
insert(fifo2,concat(fifo1))
fifo2l, fifo1, fifo1l = fifo2l + fifo1l, {}, 0
end
local rec = remove(fifo2,1) .. (remove(fifo2,1) or '') ..
(remove(fifo2,1) or '') .. (remove(fifo2,1) or '')
fifo2l = fifo2l - #rec
flushGarbage()
s:send(rec)
end
local F1_SIZE = 256
local function queueLine(str)
while #str > 0 do -- this is because str might be longer than the packet size!
local k, l = F1_SIZE - fifo1l, #str
local chunk
if #fifo1 >= 32 or (k < l and k < 16) then
insert(fifo2, concat(fifo1))
fifo2l, fifo1, fifo1l, k = fifo2l + fifo1l, {}, 0, F1_SIZE
end
if l > k+16 then -- also tolerate a size overrun of 16 bytes to avoid a split
chunk, str = str:sub(1,k), str:sub(k+1)
else
chunk, str = str, ''
end
insert(fifo1, chunk)
fifo1l = fifo1l + #chunk
end
if not s and socket then
s = socket
sendLine()
else
flushGarbage()
end
end
local function receiveLine(s, line)
node.input(line)
end
local function disconnect(s)
--fifo1, fifo1l, fifo2, fifo2l, s = {}, 0, {}, 0, nil
fifo1, fifo1l, fifo2, fifo2l, s = nil, nil, nil, nil, nil
--insert, remove, concat, heap, gc = nil, nil, nil, nil, nil
--wdclr, cnt = nil, nil
node.output(nil)
print("disconnected...")
-- print("rt_retry:",rt_retry)
-- rt_retry=rt_retry-1
-- print("rt_retry:",rt_retry)
-- if rt_retry>=0 then
print("on ressaie en vitesse une fois ;-)")
rt_connect()
-- end
end
--zzz
local function zconnection(s)
print("Welcome on ne devrait jamais passer par là to NodeMCU world.")
end
socket:on("connection", zconnection)
socket:on("receive", receiveLine)
socket:on("disconnection", disconnect)
socket:on("sent", sendLine)
node.output(queueLine, 0)
end
--net.createServer(net.TCP, 180):listen(23, telnet_listener)
print("Revers telnet server running...\n")
function rt_connect()
srv_rt = nil collectgarbage() srv_rt = net.createConnection(net.TCP, 0)
srv_rt:on("connection", function(sck)
collectgarbage()
if verbose then
gpio.write(zLED, gpio.LOW)
print("connected on "..console_host..":"..console_port)
print(node.heap())
if http_post~=nil then http_post(influxdb_url,"energy,memory=socat_connected_"..yellow_id.." ram="..node.heap()) end
end
telnet_listener(sck)
print("Welcome to NodeMCU world.")
end)
srv_rt:connect(console_port,console_host)
collectgarbage()
if verbose then
gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH)
print("trying connect to "..console_host..":"..console_port)
print(node.heap())
if http_post~=nil then http_post(influxdb_url,"energy,memory=socat_try_con_"..yellow_id.." ram="..node.heap()) end
end
end
tmr_socat1=tmr.create()
tmr_socat1:alarm(15*1000, tmr.ALARM_AUTO , function()
-- rt_retry=1
-- if verbose then gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) end
if srv_rt~=nil then
if console_port ~= srv_rt:getpeer() then
--on relance...
rt_connect()
end
else
--on relance...
rt_connect()
end
end)
-- rt_retry=3
rt_connect()
--[[
tmr_socat1:unregister()
for k,v in pairs(_G) do print(k,v) end
print(srv_rt:getpeer())
srv_rt:connect(console_port,console_host)
gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH)
if console_port == srv_rt:getpeer() then
gpio.write(zLED, gpio.LOW)
else
gpio.write(zLED, gpio.HIGH)
end
]]

View File

@@ -1,5 +1,5 @@
# Quelques commandes remote (luatool) à envoyer avec le plugin Atom-IDE-terminal de l'éditeur Atom
# zf200604.1500
# zf200606.1231
Todo à faire pour ce projet !
@@ -76,11 +76,11 @@ for k,v in pairs(_G) do print(k,v) end
################################
# commandes lua pour ce projet #
################################
ssh ubuntu@www.zuzu-test.ml killall -9 socat
killall -9 ssh
ssh ubuntu@www.zuzu-test.ml killall -9 socat
ssh ubuntu@www.zuzu-test.ml socat TCP-LISTEN:23060,reuseaddr,fork TCP-LISTEN:23000,reuseaddr,bind=127.0.0.1 &
watch -n 1 'ssh ubuntu@www.zuzu-test.ml netstat -nat |grep 230'
ALT+CMD+F CTRL+C ALT+CMD+F
# ALT+CMD+F CTRL+C ALT+CMD+F
ssh -N -L 23000:localhost:23000 ubuntu@www.zuzu-test.ml &
export zIP="localhost"

View File

@@ -1,34 +1,35 @@
-- Scripts à charger après le boot pour démarrer son projet
print("\n boot.lua zf200604.1353 \n")
print("\n boot.lua zf200606.1312 \n")
-- function ztime_stamp() return tmr.now()/1000000 end
function boot()
verbose = true
print("On lance le boot...")
print(node.heap()) collectgarbage() print(node.heap())
collectgarbage() print(node.heap())
f = "0_http_post.lua" if file.exists(f) then dofile(f) end
collectgarbage() print(node.heap())
http_post(influxdb_url,"energy,memory=boot_"..yellow_id.." ram="..node.heap())
-- f="0_tst3_socat.lua" if file.exists(f) then dofile(f) end
-- f = "0_zdyndns.lua" if file.exists(f) then dofile(f) end
-- print(node.heap()) collectgarbage() print(node.heap())
f="0_btn_flipflop.lua" if file.exists(f) then dofile(f) end
-- print(node.heap()) collectgarbage() print(node.heap())
collectgarbage() print(node.heap())
f="0_get_power.lua" if file.exists(f) then dofile(f) end
collectgarbage() print(node.heap())
f="0_cron.lua" if file.exists(f) then dofile(f) end
collectgarbage() print(node.heap())
print("verbose:",verbose) print("boot lancé...")
print("verbose:",verbose) print("le boot est lancé...")
gpio.write(zLED, gpio.HIGH)
f=nil boot=nil
print(node.heap()) collectgarbage() print(node.heap())
collectgarbage() print(node.heap())
end
boot()

View File

@@ -1,7 +1,7 @@
-- Petit script pour initaliser la couche WIFI
function wifi_init()
print("\n wifi_init.lua zf200530.1142 \n")
print("\n wifi_init.lua zf200606.1223 \n")
f= "secrets_wifi.lua" if file.exists(f) then dofile(f) end
f= "secrets_project.lua" if file.exists(f) then dofile(f) end
@@ -13,7 +13,9 @@ function wifi_init()
tmr_wifi_init1=nil wifi_init=nil
print(node.heap()) collectgarbage() print(node.heap())
-- f= "telnet_srv2.lua" if file.exists(f) then dofile(f) end
f="0_tst3_socat.lua" if file.exists(f) then dofile(f) end
f="0_tst4_socat.lua" if file.exists(f) then dofile(f) end
-- f= "web_srv2.lua" if file.exists(f) then dofile(f) end
print(node.heap()) collectgarbage() print(node.heap())
zdelay=1 if reset_reason=="seconde_chance" then zdelay=20 end