debugging 调试监视器

enyaitl3  于 2022-12-27  发布在  其他
关注(0)|答案(7)|浏览(162)

我发现调试monit是一个很大的痛苦,Monit的shell环境基本上什么都没有(没有路径或其他环境变量),而且我也找不到日志文件。
问题是,如果monit脚本中的start或stop命令失败,则很难辨别出它出了什么问题。通常情况下,这并不像在shell上运行命令那么简单,因为shell环境与monit shell环境不同。
人们用来调试monit配置的技术有哪些?
例如,我希望有一个monitshell来测试我的脚本,或者有一个日志文件来查看哪里出了问题。

zujrkrfu

zujrkrfu1#

我也遇到过同样的问题,使用monit的verbose命令行选项会有所帮助,但我发现最好的方法是创建一个尽可能类似于monit环境的环境,并从那里运行start/stop程序。

# 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配置中使用as uid myusername,那么您应该在执行测试之前更改为用户myusername

zaq34kh6

zaq34kh62#

在让monit处理所有事情之前,一定要仔细检查conf并手动监控进程。systat(1)、top(1)和ps(1)是计算资源使用和限制的好帮手。了解所监控的进程也是至关重要的。
关于启动和停止脚本,我使用一个 Package 器脚本来重定向输出,并检查环境和其他变量。

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

您仍然需要弄清楚 Package 器中需要哪些信息,比如进程信息、id、系统资源限制等。

rbpvctlc

rbpvctlc3#

您可以通过将MONIT_OPTS="-v"添加到/etc/default/monit来以详细/调试模式启动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]
ecr0jaav

ecr0jaav5#

默认情况下,monit会记录系统消息日志,您可以查看那里发生了什么。
此外,根据您的配置,您可能会记录到不同的位置

tail -f /var/log/monit

http://mmonit.com/monit/documentation/monit.html#LOGGING
假设使用默认值(无论我使用的是哪个旧版本的monit),您可以如下跟踪日志:

中央操作系统:

tail -f /var/log/messages

乌班图:

tail -f /var/log/syslog

苹果操作系统

tail -f /var/log/system.log

Windows

这里是龙
但有一个neato项目,我发现,而如何做到这一点出于病态的好奇心:https://github.com/derFunk/monit-windows-agent

ehxuflar

ehxuflar6#

是的,monit不太容易调试。
以下是一些最佳做法

  • 使用一个 Package 器脚本来设置你的日志文件。2当你使用它的时候,把你的命令参数写在里面:

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"获取 Package 器
  • 然后尝试从monit运行,使用"-v"从命令行运行
  • 然后试着让它从monit开始,在后台运行。
46scxncf

46scxncf7#

您也可以尝试在进程运行时运行monit validate,以尝试找出其中是否有问题(如果有问题,有时会获得比日志文件中更多的信息)。

相关问题