📜 ⬆️ ⬇️

ifstated (network interface monitor)

spiteful copy-paste of my own text with www.efolution.org

Based on Calomel.org (translation and easy adaptation)

Program description
')
ifstated - monitors the state of the network / networks and executes various commands depending on changes in the states of network interfaces Tracking is performed both by direct checking of the “state” in the OS data (whether the interface is turned on, whether there is a physical connection), and by performing “tests” using external programs (for example, a banal ping).

due to its flexibility, ifstated can not only restart problematic network connections, but also perform more complex operations, monitor and dynamically configure carp (4) and pf (4).

In principle, ifstated is a set of if-then constructs, which makes it possible to lay down any logic of program behavior.


Using the program

Ifstated is configured using /etc/ifstated.conf. As a sample, you can take one of the typical examples below or write your own from scratch.

After preparing the configuration file, run / usr / ifstated.

To start automatically when the system boots, add the line ifstated_flags = "" to the /etc/rc.conf.local file.

Samples of application and settings.

Example 1. Monitoring the status of a “simple” connection with DHCP.

Personally, I use this example personally on my netbook, due to the problematic work of the access point with a wi-fi usb stick. The problem is that sooner or later, but always at the wrong time, the connection strangely “hangs up”, it saves IP getting through DHCP.

According to the following ifstated configuration file, it does the following:

1. Checks the on (up) interface.

2. Checks for IP.

3. Attempts to ping google three times with a delay of 2 seconds between packets.

If any of the “tests” fail, then the interface is marked as offline, after which the corresponding commands are run to bring it into working condition, namely ifconfig rum0 up and restarting dhclient.

After the interface starts, an email will be sent to root @ localhost with a mention of the problem.

Contents of the ifstated.conf file

#
## Calomel.org - /etc/ifstated.conf
## Efolution.org
#

init-state auto
net_check = '("ifconfig egress | grep \" status: active \ "&& \
ifconfig egress | grep \ "inet *. *. *. * netmask \" | grep -v \ "inet 192 \" && \
ping -q -c 3 -i 2 -w 1 www.google.com > / dev / null "every 120) '
state auto {
if $ net_check {
set-state if_online
}

if! $ net_check {
set-state if_offline
}
}

state if_online {
init {
run “ifconfig egress | mail -s 'External Interface ON-line' root @ localhost "
}
if! $ net_check {
set-state if_offline
}
}

state if_offline {
init {
run “ifconfig egress | mail -s 'External Interface OFF-line' root @ localhost "
run "pkill dhclient; sleep 2; ifconfig rum0 up && sleep 2 && \
ifconfig egress | grep \ "status: active \" && / sbin / dhclient rum0 "
}

if $ net_check {
set-state if_online
}
}

Example 2. Monitoring virtual CARP devices.

This setting can be used to track the master machine. ifstated keeps track of Carp1 and in case of a change of state sends an email to root @ localhost.

Contents of the ifstated.conf file

#
## Calomel.org - /etc/ifstated.conf
## Efolution.org
init-state auto
fw_carp_up = "carp1.link.up"
fw_carp_init = "carp1.link.unknown"
state auto {
if ($ fw_carp_init)
run "sleep 10"
if ($ fw_carp_up)
set-state fw_master
if (! $ fw_carp_up)
set-state fw_slave
}

state fw_master {
init {
run “echo MASTER firewall is now` hostname` | mail -s 'Firewall State Change: MASTER' root @ localhost "
}
if ($ fw_carp_init)
run "sleep 2"
if (! $ fw_carp_up)
set-state fw_slave
}
state fw_slave {
init {
run “echo SLAVE firewall is now` hostname` | mail -s 'Firewall State Change: SLAVE' root @ localhost "
}
if ($ fw_carp_init)
run "sleep 2"
if ($ fw_carp_up)
set-state fw_master
}

Checking the performance settings

Run the command ifstated -dvv

Thus, ifstated will start displaying all the information about its activity on the screen, which will allow you to see its behavior on your “requests”.

Afterword

I think the examples above allow you to assess the full potential of ifstated and encourage you to customize it if necessary for your needs.

Source: https://habr.com/ru/post/71802/


All Articles