Page 1 of 1
Retrieve result of a ping
Posted: Saturday 18 April 2020 15:16
by Number8
Hello,
I'm running this code
Code: Select all
pingRet = os.execute('ping -c 1 myIpAddress')
dz.log('ping = ' .. pingRet)
In get a nil value. Is the ping command wrong?
Running 2020.1 (build 11946) on Debian NUC
Thank you
Re: Retrieve result of a ping
Posted: Saturday 18 April 2020 18:10
by waaren
Number8 wrote: Saturday 18 April 2020 15:16
I'm running os.execute('ping -c 1 myIpAddress') . Is the ping command wrong?
there are some issues with this approach:
- If myIpAddress does not respond you will block the eventSystem for the default wait time of ping which is 4 seconds.
- if myIpAddress is a variable the line should be pingRet = os.execute('ping -c 1 ' .. myIpAddress)
- os.execute does not return anything. You need io.popen to get a return from an OS command
if you want to capture output then you could use this
Code: Select all
local function osExecute(cmd)
local fileHandle = assert(io.popen(cmd, 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
return returnTable[3], commandOutput -- returnTable[3] contains returnCode
end
local myIpAddress = '192.168.192.109'
local pingReturnCode , pingCommandOutput = osExecute('ping -w1 -c1 ' .. myIpAddress) -- w1 = wait time max 1 second
print('pingReturnCode: ' .. pingReturnCode)
print('pingCommandOutput: ' .. pingCommandOutput)
Re: Retrieve result of a ping
Posted: Monday 20 April 2020 18:13
by Number8
Thanks waaren this is a much more efficient approach.
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 9:43
by hestia
Hello waaren
you could use this
I thing I did it in in the following script
Code: Select all
-- Test Internet status w www.google.fr and give status
-- from https://www.domoticz.com/forum/viewtopic.php?f=59&t=32250&p=244194&hilit=pingReturnCode+%2C+pingCommandOutput#p244194
local DEV_STATUS = 1118 -- alert device to log the status of the ping
local PING_ADDRESS = 'www.google.fr'
return {
logging = {
level =
domoticz.LOG_ERROR, --select one to override system log level normal = LOG_ERROR
--domoticz.LOG_DEBUG,
--domoticz.LOG_INFO,
--domoticz.LOG_ERROR,
--domoticz.LOG_FORCE
},
on = {
timer = {"every minute"},
},
data = {
ERROR_NUMBER = {initial={0}},
},
execute = function(dz, device)
_G.logMarker = _G.moduleLabel -- set logmarker to scriptname
local LOG_LEVEL = dz.LOG_INFO -- normal
--local LOG_LEVEL = dz.LOG_DEBUG
--local LOG_LEVEL = dz.LOG_ERROR
--local LOG_LEVEL = dz.LOG_FORCE
local function osExecute(cmd)
local fileHandle = assert(io.popen(cmd, 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
return returnTable[3], commandOutput -- returnTable[3] contains returnCode
end
local pingReturnCode , pingCommandOutput = osExecute('ping -w1 -c1 ' .. PING_ADDRESS) -- w1 = wait time max 1 second
local pingErrorLevel
local pingStatus
dz.log('pingReturnCode: ' .. pingReturnCode, LOG_LEVEL)
dz.log('pingCommandOutput: ' .. pingCommandOutput, LOG_LEVEL)
if pingReturnCode == 0 then
dz.data.ERROR_NUMBER=0
pingStatus='OK'
else
dz.data.ERROR_NUMBER=dz.data.ERROR_NUMBER+1
pingStatus='KO ' .. dz.data.ERROR_NUMBER .. " min."
end
dz.log('ERROR_NUMBER: ' .. dz.data.ERROR_NUMBER, LOG_LEVEL)
if dz.data.ERROR_NUMBER == 0 then
pingErrorLevel=dz.ALERTLEVEL_GREEN
elseif dz.data.ERROR_NUMBER == 1 then -- every min => 1 min
pingErrorLevel=dz.ALERTLEVEL_YELLOW
elseif dz.data.ERROR_NUMBER > 2 and dz.data.ERROR_NUMBER < 10 then -- every min, 10
pingErrorLevel=dz.ALERTLEVEL_ORANGE
else pingErrorLevel=dz.ALERTLEVEL_RED
end
dz.devices(DEV_STATUS).updateAlertSensor(pingErrorLevel, pingStatus)
end
}
And... when my internet is down, I get this "dzVents.lua has been running for more than 10 seconds"
- Spoiler: show
- 2020-11-04 09:24:00.692 Status: dzVents: Info: ------ Start internal script: Internet:, trigger: "every minute"
2020-11-04 09:24:10.543 Error: EventSystem: Warning!, lua script /home/pi/PROD_domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
2020-11-04 09:24:20.808 Status: dzVents: Debug: pingReturnCode: 2
2020-11-04 09:24:20.813 Status: dzVents: Debug: pingCommandOutput:
2020-11-04 09:24:20.818 Status: dzVents: Debug: ERROR_NUMBER: 2
2020-11-04 09:24:20.888 Status: dzVents: Debug: Processing device-adapter for Internet: Alert sensor adapter
2020-11-04 09:24:20.902 Status: EventSystem: Script event triggered: /home/pi/PROD_domoticz/dzVents/runtime/dzVents.lua
2020-11-04 09:24:20.577 Error: EventSystem: Warning!, lua script /home/pi/PROD_domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
2020-11-04 09:24:20.893 Error: dzVents: Error: (3.0.14) ------ Finished Internet after >20 seconds. (using 0.606 seconds CPU time !)
I thought that this template was to avoid blocking the eventSystem.
Did I miss something in my script?
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 15:53
by waaren
hestia wrote: Wednesday 04 November 2020 9:43
And... when my internet is down, I get this "dzVents.lua has been running for more than 10 seconds"
I thought that this template was to avoid blocking the eventSystem.
Did I miss something in my script?
The -w1 should cause a return after max 1 second. So either something else is causing the system to be extremely busy or ping itself does behave different then expected. If your return code is 2 it might help to catch the return message.
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 17:53
by hestia
waaren wrote: Wednesday 04 November 2020 15:53
ping itself does behave different then expected
My platform is quite standard: Pi 3 B+ / Linux 5.4.51-v7+ ; so the ping should be as anybody's one
I've done the ping for linux like this
- Spoiler: show
- date ; ping -w1 -c1 9.9.9.9 ; date
Wed 4 Nov 17:57:43 CET 2020
PING 9.9.9.9 (9.9.9.9) 56(84) bytes of data.
From 192.168.10.1 icmp_seq=1 Destination Net Unreachable
--- 9.9.9.9 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
Wed 4 Nov 17:57:43 CET 2020
So either something else is causing the system to be extremely busy
The system doesn't seem busy at all, via Monit I've got this during a test I've just done again:
System Load : [0.32] [0.33] [0.31] / CPU : 1.6%us, 2.4%sy, 0.1%wa
domoticz CPU Total: 2.1%
If your return code is 2 it might help to catch the return message.
Sorry, but I don't know what to do with this, my script was with LOG_DEBUG
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 18:31
by waaren
hestia wrote: Wednesday 04 November 2020 17:53
Sorry, but I don't know what to do with this, my script was with LOG_DEBUG
Don't know what could cause this but
Code: Select all
data = {
ERROR_NUMBER = {initial={0}}, -- {0} is a table
},
Should be
Code: Select all
data =
{
ERROR_NUMBER =
{
initial = 0,
},
},
Another thing you can try is not using the command ping but
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 21:49
by hestia
waaren wrote: Wednesday 04 November 2020 18:31
Another thing you can try is not using the command ping but
I did this one! and it is ok, no blockage
- Spoiler: show
- 2020-11-04 21:26:00.159 Status: dzVents: Info: ------ Start internal script: Internet:, trigger: "every minute"
2020-11-04 21:26:01.265 Status: dzVents: Debug: pingReturnCode: 1
2020-11-04 21:26:01.265 Status: dzVents: Debug: pingCommandOutput: PING 9.9.9.9 (9.9.9.9) 56(84) bytes of data.
2020-11-04 21:26:01.265 --- 9.9.9.9 ping statistics ---
2020-11-04 21:26:01.265 1 packets transmitted, 0 received, 100% packet loss, time 0ms
2020-11-04 21:26:01.265 Status: dzVents: Debug: ERROR_NUMBER: 1
2020-11-04 21:26:01.282 Status: dzVents: Debug: Processing device-adapter for Internet: Alert sensor adapter
2020-11-04 21:26:01.283 Status: dzVents: Info: ------ Finished Interne
BTW, for those who want to check internet, I had experienced that it is better to ping 9.9.9.9. or 8.8.8.8 than
www.google.com to check if internet is ok, because I had a real outage and my script with
www.google.com ping was ok ! I had another monitoring with 8.8.8.8 that was red, as real world!
I didn't do anything on the ERROR_NUMBER table... because it is ok like this!
Thanks again waaren
Re: Retrieve result of a ping
Posted: Wednesday 04 November 2020 22:26
by waaren
hestia wrote: Wednesday 04 November 2020 21:49
I didn't do anything on the ERROR_NUMBER table... because it is ok like this!
Good that it is working now!
The initial setting of ERROR_NUMBER is wrong and it will give the error
attempt to perform arithmetic on a table value (field 'ERROR_NUMBER')
if the initial run encounters a ping error. This because the line
Code: Select all
dz.data.ERROR_NUMBER=dz.data.ERROR_NUMBER+1
fails if dz.data.ERROR_NUMBER is a table
You wont see the error again after the first time the ping succeeds because of the line
where the script replaces the table by a number.
Re: Retrieve result of a ping [Solved]
Posted: Wednesday 04 November 2020 23:02
by hestia
waaren wrote: Wednesday 04 November 2020 22:26
The initial setting of ERROR_NUMBER is wrong and it will give the error
Understood, I changed it!
Thanks again
