monit eth0 在网络接口因任何原因关闭时重新启动

发布于 2025-01-15 05:43:51 字数 1701 浏览 2 评论 0原文

只是尝试使用“monit”工具监视网络接口 eth0。我想重新启动网络接口,以防它因某种原因发生故障。

根据文档,该工具可以检查网络接口:

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

我只是想通过一个简单的工作示例将其付诸实践。这是我的 monitrc 配置文件:

# 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/*

我的计划是在网络接口因任何原因关闭时执行 shell 脚本来恢复网络接口。只是为了这个简单的例子,我创建了这个配置文件:

# 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"

这个“/usr/local/bin/test”脚本只是将“hello”写入文件。

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

但它从来没有被调用过。所以我认为它没有按预期工作。

当我执行“monit validate”时,这是与LAN接口相关的输出,当网络接口为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

当网络接口为DOWN时:

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

这个monit工具可以用来实现这个功能吗?或者有什么东西无法用该工具实现?非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2025-01-22 05:43:51

我找到了解决方案,在我的 net-iface.mon 配置文件中添加了“如果链接失败则重新启动”:

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

现在我可以强制关闭网络接口:

# ifconfig eth0 down

并且 monit 会自动执行 /usr/local/bin/test 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文