如何使用Monit环境变量?

如何使用Monit环境变量?

根据监控链接

Monit 不使用任何环境变量。但是,当 Monit 执行启动/停止/重新启动程序或 exec 操作时,它将设置几个环境变量,可执行文件可以利用这些变量来获取有关触发该操作的事件的信息。

是否可以在自定义操作中使用这些变量?

例如,对于通知,我不使用邮件服务,而是使用自定义脚本,该脚本应该接收 ENV monit 变量并提供输出。这是测试环境变量的基本示例。

check process dhcp with pidfile "/var/run/dhcpd.pid"
        start = "/etc/init.d/isc-dhcp-server start"
        stop = "/etc/init.d/isc-dhcp-server stop"
        if does not exist program then exec "/bin/echo $MONIT_EVENT > /tmp/monittest"
        depends on lan

当我故意让程序失败时,就像 check process dhcp with pidfile "/var/run/unexisting.pid"

我在 中没有得到任何输出/tmp/monittest。难道我做错了什么?

答案1

是的,有错误。 monitexec似乎执行exec(3)给定字符串的样式执行,而不是调用system(3);这意味着不支持 shell 语法(重定向等),因为提供的数据不是通过 shell 运行的。相反,编写使用 monit 环境变量的合适代码(将导出到这样的代码中exec):

# cat /root/blah                                                               
#!/bin/sh
echo "$MONIT_EVENT" > /root/woot
# chmod +x /root/blah
#

然后从 monit 配置中调用该代码:

# tail -2 /etc/monitrc                                                         
check process itsdeadjim with pidfile "/nopenopenope"
    if does not exist then exec "/root/blah"
# 

/root/woot为我填充了文件:

# rm /root/woot
# rcctl restart monit && sleep 10
monit(ok)
monit(ok)
# cat /root/woot
Does not exist
# 

相关内容