'monit eth0 to restart network interface in case it is down for whatever reason

Just trying to monitorize a network interface eth0 with the "monit" tool. I want to restart the network interface in case it fails down for whatever reason.

According to documentation, this tool can check for network interfaces:

https://mmonit.com/monit/documentation/monit.html#Network

I am just trying to put this into practice with a simple working example. This is my monitrc configuration file:

# cat /etc/monitrc
set logfile syslog

set pidfile /var/run/monit.pid
set idfile /tmp/.monit.id
set statefile /tmp/.monit.state

set httpd unixsocket /var/run/monit.sock
     allow 127.0.0.1

include /etc/monit.d/*

My plan is executing a shell script to recover the network interface when the network interface is down for any reason. Just for this simple example, I created this configuration file:

# cat /etc/monit.d/net-iface.mon 
CHECK network LAN with interface eth0
    start program = "/usr/local/bin/test"
    stop  program = "/usr/local/bin/test"
    restart  program = "/usr/local/bin/test"

This "/usr/local/bin/test" script just writes "hello" to a file.

#! /bin/bash
echo hello >> /tmp/monit.log

But it is never called. So I assume it is not working as expected.

When I execute "monit validate", this is the output related with LAN interface, when the network interface is UP:

Network 'LAN'
  status                            UP
  monitoring status                 Monitored
  link capacity                     100 Mb/s full-duplex
  download                          0 B/s [0 packets/s] [0 errors]
  upload                            6 B/s [0 packets/s] [0 errors] (0.0% link saturation)
  data collected                    Thu, 01 Jan 1970 17:09:08

And when the network interface is DOWN:

Network 'LAN'
  status                            Link down
  monitoring status                 Monitored
  data collected                    Thu, 01 Jan 1970 00:17:29

Can this monit tool be used to implement this functionality? Or is something it can't be implemented with that tool? Many thanks in advance.



Solution 1:[1]

I found the solution, adding the "if failed link then restart" in my net-iface.mon configuration file:

# cat /etc/monit.d/net-iface.mon 
check network LAN with interface eth0
        restart program = "/usr/local/bin/test"
        if failed link then restart

Now I can force the network interface down:

# ifconfig eth0 down

And monit executes the /usr/local/bin/test automatically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman