调试Monit

82
我发现调试 Monit 很费劲。Monit 的 shell 环境基本上没有任何东西(没有路径或其他环境变量)。而且,我找不到任何日志文件。
问题在于,如果 Monit 脚本中的启动或停止命令失败,很难确定出错原因。通常情况下,这并不像在 shell 上运行命令那么简单,因为与 Monit shell 环境不同。
人们用什么技术来调试 Monit 配置呢?
例如,我会很高兴拥有一个 Monit shell,以测试我的脚本,或者有一个日志文件来查看出了什么问题。

我发现monit有记录设施。http://mmonit.com/monit/documentation/monit.html 不幸的是,它没有我想要的那么详细。 - Brian Takita
7个回答

94

我遇到了同样的问题。使用monit的verbose命令行选项可以有些帮助,但我发现最好的方法是创建一个尽可能类似于monit环境的环境,并从那里运行启动/停止程序。

# monit runs as superuser
$ sudo su

# the -i option ignores the inherited environment
# this PATH is what monit supplies by default
$ env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/sh

# try running start/stop program here
$

我发现最常见的问题与环境变量相关(尤其是 PATH)或权限相关。请记住,monit 通常以 root 用户身份运行。

此外,如果你在 monit 配置中使用了 as uid myusername,那么在进行测试之前,你应该切换到用户 myusername


谢谢,这很有帮助。但是如何在不拉取他们的环境的情况下更改为我的用户名? - Nils
@Chocohound $ sudo myusername; $ env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/sh - s01ipsist
2
@s01ipsist 这应该是 su myusername - Michał Szajbe
1
这是一个很好的技巧,总的来说。 - Robert Riedl
这对我仍然没有用,当从该环境执行时,脚本仍然可以正常运行,但是从monit调用时却不能。@thodg的解决方案帮了我很多。Monit真的需要在这方面添加文档。 - Breno Salgado

39

在让monit处理一切之前,请务必仔细检查您的配置并手动监视进程。systat(1)、top(1)和ps(1)是帮助您确定资源使用情况和限制的好工具。了解您要监视的进程也至关重要。

关于启动和停止脚本,我使用包装脚本来重定向输出并检查环境和其他变量。类似这样:

$ cat monit-wrapper.sh

#!/bin/sh
{
  echo "MONIT-WRAPPER date"
  date
  echo "MONIT-WRAPPER env"
  env
  echo "MONIT-WRAPPER $@"
  $@
  R=$?
  echo "MONIT-WRAPPER exit code $R"
} >/tmp/monit.log 2>&1

然后在 monit 中:

start program = "/home/billitch/bin/monit-wrapper.sh my-real-start-script and args"
stop program = "/home/billitch/bin/monit-wrapper.sh my-real-stop-script and args"

你仍需要确定你想在包装器中包含哪些信息,例如进程信息、ID、系统资源限制等。


3
非常感谢您提供的调试建议! - Dr Nic
2
@billitch的monit-wrapper非常优秀的一点是,生成的日志文件实际上包括了导致问题的错误信息(例如,无法找到可执行文件),而这是monit所吞噬的。这是一个非常好的建议,让我省了很多麻烦。 - ChrisW
我不得不使用 start program=/bin/bash -c "..." - Mirko
这个真的帮了我很大忙。之前尝试的所有方法都没有显示出问题所在,脚本在通过monit运行时失败,但是在终端中运行时却总是正常的(就像其他建议中提到的那样)。通过你的脚本运行时,脚本可以正常工作,似乎是因为我之前尝试的管道输出方式有问题。脚本会立即执行并退出,我启动的程序也会随之消失,而将我的脚本更改为像这个包装器中一样的管道输出方式后,它就可以正常工作了(?) - Breno Salgado

15

您可以在/etc/default/monit中添加MONIT_OPTS="-v"来启动Monit的详细/调试模式(不要忘记重新启动:/etc/init.d/monit restart)。

然后,您可以使用tail -f /var/log/monit.log捕获输出。

[CEST Jun  4 21:10:42] info     : Starting Monit 5.17.1 daemon with http interface at [*]:2812
[CEST Jun  4 21:10:42] info     : Starting Monit HTTP server at [*]:2812
[CEST Jun  4 21:10:42] info     : Monit HTTP server started
[CEST Jun  4 21:10:42] info     : 'ocean' Monit 5.17.1 started
[CEST Jun  4 21:10:42] debug    : Sending Monit instance changed notification to monit@example.io
[CEST Jun  4 21:10:42] debug    : Trying to send mail via smtp.sendgrid.net:587
[CEST Jun  4 21:10:43] debug    : Processing postponed events queue
[CEST Jun  4 21:10:43] debug    : 'rootfs' succeeded getting filesystem statistics for '/'
[CEST Jun  4 21:10:43] debug    : 'rootfs' filesytem flags has not changed
[CEST Jun  4 21:10:43] debug    : 'rootfs' inode usage test succeeded [current inode usage=8.5%]
[CEST Jun  4 21:10:43] debug    : 'rootfs' space usage test succeeded [current space usage=59.6%]
[CEST Jun  4 21:10:43] debug    : 'ws.example.com' succeeded testing protocol [WEBSOCKET] at [ws.example.com]:80/faye [TCP/IP] [response time 114.070 ms]
[CEST Jun  4 21:10:43] debug    : 'ws.example.com' connection succeeded to [ws.example.com]:80/faye [TCP/IP]

10

monit -c /path/to/your/config -v


5

默认情况下,monit会将日志记录到您的系统消息日志中,您可以在那里查看发生了什么。

此外,根据您的配置,您可能会将日志记录到不同的位置。

tail -f /var/log/monit

http://mmonit.com/monit/documentation/monit.html#LOGGING

假设使用旧版本的monit默认设置,您可以按如下方式跟踪日志:
CentOS:
tail -f /var/log/messages

Ubuntu:

tail -f /var/log/syslog

Mac OSX

tail -f /var/log/system.log

Windows

这里有一些需要注意的问题。

但是我在搜索如何做到这一点的时候,发现了一个很棒的项目,可以满足您的好奇心:https://github.com/derFunk/monit-windows-agent


我在我的监控设置中没有看到这个文件。 - weisjohn
你在使用UNIX机器吗?/var/log/messages是许多UNIX机器上系统日志记录的标准位置。 - WattsInABox
我使用的是Ubuntu 12.04 LTS。我已经解决了我的monit问题,但是奇怪的是我没有它... - weisjohn
4
不完全如此。每个UNIX发行版都可以记录开发人员选择的标准消息。显然,Ubuntu记录到“/var/log/syslog”。“var / log / messages在哪里?” - WattsInABox
RHL和CentOS是tail -f /var/log/monit。 - JJ_Coder4Hire

2

嗯,监控工具 Monit 不太容易调试。

以下是几个最佳实践:

  • 使用包装脚本来设置日志文件。在此过程中,编写您的命令参数:

shell:

#!/usr/bin/env bash

logfile=/var/log/myjob.log
touch ${logfile} 
echo $$ ": ################# Starting " $(date) "########### pid " $$ >> ${logfile}

echo "Command: the-command $@" >> ${logfile} # log your command arguments
{
  exec the-command $@
} >> ${logfile} 2>&1

那真的很有帮助。

我发现另一件有用的事情是使用“-v”运行monit,它会给你更多的信息。所以流程如下:

  • 通过shell运行"sudo my-wrapper"来使包装器正常工作
  • 然后尝试在monit中使用“-v”参数从命令行启动它
  • 最后尝试在monit中以后台模式运行它

0

如果进程正在运行,您也可以尝试运行monit validate,以查找它们是否存在问题(如果有任何问题,则有时会获得比日志文件中更多的信息)。除此之外,您没有太多可以做的了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接