Community discussions

MikroTik App
 
icepicknz
just joined
Topic Author
Posts: 18
Joined: Thu Jul 29, 2010 1:39 am

Notify of device bound or not

Fri Sep 10, 2021 1:52 am

Hi,

I'm trying to monitor a device (Xbox) in a short term rental, I want to know if its been removed from the network and be notified.
Unfortunately I cannot use net watch as the device stays offline and comes online briefly to re-bind to DHCP then goes offline again.

I'm trying to use the binder IP address using the following
/ip dhcp-server lease get [find comment="XBOX - 30c Snowmass" status=bound]
Now I need to add an IF statement and send a notification via LOG message if its up or down; but only report a single time per instance.
i.e. dont message me every time the script has run if the device is up, up, up, etc

I've gotten this far but it isn't working...
:local sentfound
:local sentmissing

:local macfound [/ip dhcp-server lease get [find comment="XBOX - 30c Snowmass" status=bound]]
:if ($macfound != "") do={
:if ($sentfound = 1) do {
:log info "XBOX found"
}
:set sentfound 0
:set sentmissing 0
}
} else= {
:if ($sentmissing = 1) do {
:log info "XBOX missing"
}
:set sentfound 0
:set sentmissing 0
}
}
I just cant figure out how to put it all together and like I said, only send a single notification per state

Any help would be greatly appreciated
Thanks in advance
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2865
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Notify of device bound or not

Fri Sep 10, 2021 9:19 am

Maybe this could help.
:local zone "home.sweet.home"
:local dnsrecord [/ip dns static find where address=$leaseActIP ]
:if ( $leaseBound = 0 ) do={
  :if ( [ :len $dnsrecord ] > 0 ) do={
    :log warning ("DNS release record for $[/ip dns static get $dnsrecord name] ( $leaseActIP )")
    /ip dns static remove $dnsrecord
  }
} else={
  :local hostname [/ip dhcp-server lease get [/ip dhcp-server lease find where address=$leaseActIP server=$leaseServerName ] host-name]	
  :local fqdn
  :local dhcplease
  :foreach dhcplease in=[/ip dhcp-server lease find where ( server="$leaseServerName" && address=$leaseActIP ) ] do={
    :set hostname [/ip dhcp-server lease get $dhcplease host-name ]
    :set fqdn ( $hostname . "." . $zone )
    :local ttl [/ip dhcp-server lease get $dhcplease expires-after ]
### do magic like this   
    :if ( [ :len $dnsrecord ] > 0 ) do={
      /ip dns static remove $dnsrecord
      :log warning ("Remove DNS record for $fqdn ( $leaseActIP )")
    }
    :log warning ("Renew DNS record for $fqdn ( $leaseActIP )")
    /ip dns static add name=$fqdn address=$leaseActIP ttl=$ttl comment=$leaseActMAC
###
  }
}

Who is online

Users browsing this forum: No registered users and 35 guests