Page 1 of 1

dzVents ping tool, yet another option

Posted: Wednesday 19 June 2019 22:41
by hoeby
I want to make my own dzVents ping tool.
Maybe there are some on the WWW, but i am having fun making things and think about it. And of course, trying to learn something out of it.

The scripts i started with, that works for 1 device.
But would like to have multi devices in the same script.

Tried it with this code. But somehow it doesn't take the IP address out of the list.
Is there somebody wo can help?

Code: Select all

return {
    on = { 
       devices = {'Samsung werk' 
       }
    },

    execute = function(domoticz, device)
--                                Device name        IP-adres
        local devices_ping =  {
	                            ['Gateway']        = 'xxx.xxx.xxx.xxx',  
	                            }
        
        for _, node in pairs(devices_ping) do
            local ip = devices_ping[node]
            print(ip)
            
        
            --local ip = 'xxx.xxx.xxx.xxx'
            ping_success=""
            ping_success=os.execute('ping -c1 -w1 ' .. ip)   
                if ping_success then
                    print("ping success ")
                else
                    print("ping failed ")
                end   
            end    
        end
}   

Re: dzVents ping tool, yet another option

Posted: Wednesday 19 June 2019 23:26
by waaren
hoeby wrote: Wednesday 19 June 2019 22:41 ... tried it, but somehow it doesn't take the IP address out of the list.
Is there somebody who can help?
you can practice this type of constructs also on the CLI if you installed Lua on your PI

Code: Select all

sudo apt install lua5.2
Have a look at this example of the 'in pairs' construct

Code: Select all

#lua
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
 devices_ping =
>> {
>>   ['Gateway'] = 'xxx.xxx.xxx.xxx',
>>   ['Gateway2'] = 'yyy.yyy.yyy.yyy',
>> }
>
>for  key, value in pairs(devices_ping) do
>>   print( key .. " ==>> " .. value)
>>end
Gateway ==>> xxx.xxx.xxx.xxx
Gateway2 ==>> yyy.yyy.yyy.yyy
>
Or use a Lua IDE on your desktop (ZeroBrane or another)
ZeroBrane IDE
ZeroBrane IDE
ZeroBrane.png (263.69 KiB) Viewed 4251 times

Re: dzVents ping tool, yet another option

Posted: Thursday 20 June 2019 20:13
by hoeby
Thanks waaren, i will take a look at zerobrane.

I think i finished the script for now.
I am always open for options of changes

Code: Select all

--[[Version: 19062001
Script works in dzVents --> timer.

With this script is it possible to to a ping to netwerk devices.
The script does 1 ping to each device, when the script is triggerd.
It doesn't do a continuous ping every second untill timeouts.

Setup the settings needed and/or wanted. 

There is a line which says: -- Don't change things undernead this line
If you have no idea how the scripts works, please lissen to this.
If you know how it works and have ideas, please post them on http://www.domoticz.com/forum/viewtopic.php?f=59&t=28420
]]--

return {
    on = { 
       --devices = {'trigger device'}, 
       --timer      = { "every 10 minutes" },
       timer        =  {'every minute'}, 
         },

    data = { timeoutcount = { initial = {} } },

    execute = function(dz, device)
        
-- SETTINGS:
        
-- Setup the device and the IP-address which this device has.
-- When wanted to switch a (dummy) device with this script, be aware that the device name is exactly the same as your (dummy) device
--                                Device name        IP-adres
        local devices_ping =  {
	                            ['Dummy1']        = 'XXX.XXX.XXX.XXX', 
	                            ['Dummy2']        = 'XXX.XXX.XXX.XXX',
	                            ['Dummy3']        = 'XXX.XXX.XXX.XXX',
	                          }

-- How many timeouts, before a notification or switch off will be executed.
        local timeout = 10
        
-- This is the name which the notification starts with        
        local notifyHead = 'Ping-tool'
        
-- Switch dummy device
-- When this is set to true, than the script will switch the pinged devices. Besure that the device is present in domoticz.
-- When you don't want to switch a device, set it to false.
-- When true, than at ping success the switch will be ON, when failed the switch will be OFF.
        local dummydev    = false
        
-- Telegram settings
-- Set true for sending telegram, set false for NOT sending telegram.
        local telegram    = false                       
        
-- Email settings (domoticz email settings have to be filled in)
-- Set true for sending email, set false for NOT sending email.
        local email       = false                       
        local mailaddress = '[email protected]'




-- Don't change things undernead this line    
        local timeoutcount    = dz.data.timeoutcount -- short reference
        
        for deviceName, ipaddr in pairs(devices_ping) do
            local ip = devices_ping[node]
        
            ping_success=""
            ping_success=os.execute('ping -c1 -w1 ' .. ipaddr)   
                if timeoutcount[deviceName] == nil then
                    timeoutcount[deviceName] = 0
                end
                
                if ping_success then
                    if timeoutcount[deviceName] == 0 then
                        print("ping "..deviceName.." success ")
                    else
                        print("ping "..deviceName.." success, device is back.")
                        timeoutcount[deviceName] = 0 -- reset the counter
                        if telegram then
                            dz.notify(notifyHead, notifyHead ..", ".. deviceName .." is back.",dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                        end                    
                        if email then
                            dz.email('Domoticz ping tool', '' ..notifyHead.. ', '.. deviceName .." is back.", ''..mailaddress..'')
                        end
                    end    
                    if dummydev then
                        if dz.devices(deviceName).state == 'Off' then
                        dz.devices(deviceName).switchOn()
                        end
                    end    
                else
                    timeoutcount[deviceName] = timeoutcount[deviceName] + 1
                    print (deviceName .." ping ".. timeoutcount[deviceName] .." times failed.")
                end   
                
                if (timeoutcount[deviceName] == timeout) then
                    print (deviceName .." has " ..timeout.. " timeouts, this exceeds the timeout counter.")
                    if dummydev then
                        dz.devices(deviceName).switchOff()
                    end
                    if telegram then
                        dz.notify(notifyHead, notifyHead ..", ".. deviceName .." has " ..timeout.. " timeouts, this exceeds the timeout counter.",dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                    end                    
                    if email then
                        dz.email('Domoticz ping tool', '' ..notifyHead.. ', '.. deviceName .." has " ..timeout.. " timeouts, this exceeds the timeout counter.", ''..mailaddress..'')
                    end
                end    
            end    
        end
}   


Re: dzVents ping tool, yet another option

Posted: Thursday 20 June 2019 20:41
by waaren
hoeby wrote: Thursday 20 June 2019 20:13 I think i finished the script for now.
I am always open for options of changes
If you want to extent the script to use nmap (kind of ping to IP:PORT), you might want to have a look at this. I would implement it in a different way now but the basics are still usable

Re: dzVents ping tool, yet another option

Posted: Thursday 20 June 2019 21:18
by hoeby
Sorry, i didn't know you already made a fantastic script :oops: .
I searched for ping tool in dzvents, but yours i didn't found.

Mine looks like a very very very basic copy of yours.

Will take a look at yours, could be that i copy it and replace mine.

Re: dzVents ping tool, yet another option  [Solved]

Posted: Thursday 20 June 2019 22:05
by waaren
hoeby wrote: Thursday 20 June 2019 21:18 Sorry, i didn't know you already made a fantastic script :oops: .
Absolutely no need to be sorry. I completely agree with you that just the writing and learning how stuff works is a lot of fun. I only tried to encourage you !

Re: dzVents ping tool, yet another option

Posted: Thursday 27 June 2019 23:20
by hoeby
new version, 2 thinks changed/added

Version 19062001 only switch the device ON / OFF when timeout was exceeded, but did this only once. Now on every fail after timeout setting it checks the status of your device, and set it back in sync, if needed.

For windows the ping script needs to be different.
This is added. But a manual action to activate or deactivated what you need

Port function not added. Not yet for now, but when it will, i don't know

Code: Select all

--[[Version: 19062701
Script works in dzVents --> timer.

With this script it is possible to do a ping to multi netwerk devices.
The script does 1 ping to each device, when the script is triggerd.
It doesn't do a continuous ping every second untill timeouts.

Setup the settings needed and/or wanted. 

There is a line which says: -- Don't change things undernead this line
If you have no idea how the scripts works, please lissen to this.
If you know how it works and have ideas, please post them on http://www.domoticz.com/forum/viewtopic.php?f=59&t=28420
]]--

return {
    on = { 
       --devices = {'Trigger device'}, 
       --timer      = { "every 10 minutes" },
       timer        =  {'every minute'}, 
         },

    data = { timeoutcount = { initial = {} } },

    execute = function(dz, device)
        
-- SETTINGS:
        
-- Setup the device and the IP-address which this device has.
-- When wanted to switch a (dummy) device with this script, be aware that the device name is exactly the same as your (dummy) device
--                                Device name        IP-adres
        local devices_ping =  {
	                            ['Dummy1']         = 'XXX.XXX.XXX.XXX', 
	                            ['Dummy2']            = 'XXX.XXX.XXX.XXX',
--	                            ['Dummy3']              = 'XXX.XXX.XXX.XXX',
	                          }

-- How many timeouts, before a notification or switch off will be executed.
        local timeout = 10
        
-- This is the name which the notification starts with        
        local notifyHead = 'Ping-tool'
        
-- Switch dummy device
-- When this is set to true, than the script will switch the pinged devices. Besure that the device is pressent in domoticz.
-- When you don't want to switch a device, set it to false.
-- When true, than at ping success the switch will be ON, when failed the switch will be OFF.
        local dummydev    = true
        
-- Telegram settings
-- Set true for sending telegram, set false for NOT sending telegram.
        local telegram    = false                       
        
-- Email settings (domoticz email settings have to be filled in)
-- Set true for sending email, set false for NOT sending email.
        local email       = false                       
        local mailaddress = '[email protected]'




-- Don't change things undernead this line    
        local timeoutcount    = dz.data.timeoutcount -- short reference
        
        for deviceName, ipaddr in pairs(devices_ping) do
            
            ping_success=""
            ping_success=os.execute('ping -c1 -w1 ' .. ipaddr) -- For domoticz on Pi
--          ping_success=os.execute('ping ' ..ipaddr.. ' -w 100') -- For domoticz on Windows
            if timeoutcount[deviceName] == nil then
                    timeoutcount[deviceName] = 0
                end
                
                if ping_success then
                    if timeoutcount[deviceName] == 0 then
                        print("ping "..deviceName.." succes ")
                    else
                        print("ping "..deviceName.." succes, device is back.")
                        timeoutcount[deviceName] = 0 -- reset the counter
                        if telegram then
                            dz.notify(notifyHead, notifyHead ..", ".. deviceName .." is back.",dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                        end                    
                        if email then
                            dz.email('Domoticz ping tool', '' ..notifyHead.. ', '.. deviceName .." is back.", ''..mailaddress..'')
                        end
                    end    
                    if dummydev then
                        if dz.devices(deviceName).state == 'Off' then
                        dz.devices(deviceName).switchOn()
                        end
                    end    
                elseif timeoutcount[deviceName] < timeout then
                    timeoutcount[deviceName] = timeoutcount[deviceName] + 1
                    print ("ping ".. deviceName .." ".. timeoutcount[deviceName] .." times failed.")
                end   
                
                if timeoutcount[deviceName] >= timeout then
                    print ("ping ".. deviceName .." has " ..timeoutcount[deviceName].. " timeouts, this exceeds the timeout number setting.")
                    if dummydev then
                        if dz.devices(deviceName).state == 'On' then
                            dz.devices(deviceName).switchOff()
                        end
                    end
                    if telegram then
                        dz.notify(notifyHead, notifyHead ..", ".. deviceName .." has " ..timeout.. " timeouts, this exceeds the timeout counter.",dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                    end                    
                    if email then
                        dz.email('Domoticz ping tool', '' ..notifyHead.. ', '.. deviceName .." has " ..timeout.. " timeouts, this exceeds the timeout counter.", ''..mailaddress..'')
                    end
                end    
            end    
        end
}   

--[[ 
History
19062701 - Changed dummydev, so it will be in sync with status. 
         - Domoticz windows ping function added
19062001 - Start of scripts
]]--