I need to monitor the local address(IP) of a network interface(i.e. whenever it's changed or the interface is unplugged do some stuff) and I'm wondering what's the best way to do that. Currently I'm using InterfaceByName to get and verify/match the interface details in an infinite loop. I monitor several interfaces so I'm wondering if it's a bad thing or if I should use some delays etc. It would be great if the OS would send a signal whenever the interface is updated but I'm not aware of a such thing.
This is going to be very system specific. On Linux I think the simplest way right now is reading the sysfs
files for each interface.
$ cat /sys/class/net/eth0/operstate
up
$ cat /sys/class/net/eth0/carrier
1
Polling them occasionally shouldn't consume too many resources, and give you what you need.
For more timely updates, you can setup a netlink socket to receive NETLINK_ROUTE
updates from the kernel. There's a few Go netlink packages around, but I'm not sure how general they are.