【Web】Monit监控程序使用

33 篇文章 139 订阅

简介

Monit是一款Unix/Linux上使用的系统监控程序,对应的官网是https://mmonit.com/monit/。它能够监控的内容包括进程、文件和目录、系统状态等等,也能对错误进行修复。

安装和使用

本文测试的环境是Ubuntu18.04系统,可以直接安装monit:

这里使用的monit版本如下(因为不同的版本,对应的目录和操作等可能有差异,所以这里特别说明):

安装monit之后它就启动了,可以通过命令查看其状态:

​这里需要使用root权限查看(后文直接切换到root下操作),可以看到monit的一般信息,比如对应的配置文件、日志文件,当前的状态等等。monit还支持通过网页查看monit状态,不过需要先打开monit配置文件中的http选项:

#
## Monit has an embedded HTTP interface which can be used to view status of
## services monitored and manage services from a web interface. The HTTP
## interface is also required if you want to issue Monit commands from the
## command line, such as 'monit status' or 'monit restart service' The reason
## for this is that the Monit client uses the HTTP interface to send these
## commands to a running Monit daemon. See the Monit Wiki if you want to
## enable SSL for the HTTP interface.
#
 set httpd port 2812 and
     use address localhost  # only accept connection from localhost
     allow localhost        # allow localhost to connect to the server and
     allow admin:monit      # require user 'admin' with password 'monit'

然后重启monit:

monit reload /etc/monit/monitrc

之后使用Web访问本地接口就可以查看monit:

使用方法说明

monit的帮助说明如下,这里可以查看到monit的基本使用方法:

Usage: monit [options]+ [command]
Options are as follows:
 -c file                             Use this control file
 -d n                               Run as a daemon once per n seconds
 -g name                        Set group name for monit commands
 -l logfile                         Print log information to this file
 -p pidfile                        Use this lock file in daemon mode
 -s statefile                     Set the file monit should write state information to
 -I                                   Do not run in background (needed when run from init)
 --id                                Print Monit's unique ID
 --resetid                        Reset Monit's unique ID. Use with caution
 -B                                  Batch command line mode (do not output tables or colors)
 -t                                   Run syntax check for the control file
 -v                                  Verbose mode, work noisy (diagnostic output)
 -vv                                Very verbose mode, same as -v plus log stacktrace on error
 -H [filename]                Print SHA1 and MD5 hashes of the file or of stdin if the
                                      filename is omited; monit will exit afterwards
 -V                                 Print version number and patchlevel
 -h                                 Print this text
Optional commands are as follows:
 start all                                    - Start all services
 start <name>                          - Only start the named service
 stop all                                    - Stop all services
 stop <name>                          - Stop the named service
 restart all                                - Stop and start all services
 restart <name>                       - Only restart the named service
 monitor all                               - Enable monitoring of all services
 monitor <name>                     - Only enable monitoring of the named service
 unmonitor all                          - Disable monitoring of all services
 unmonitor <name>                 - Only disable monitoring of the named service
 reload                                     - Reinitialize monit
 status [name]                          - Print full status information for service(s)
 summary [name]                     - Print short status information for service(s)
 report [up|down|..]                  - Report state of services. See manual for options
 quit                                         - Kill the monit daemon process
 validate                                  - Check all services and start if not running
 procmatch <pattern>             - Test process matching pattern

也可以在https://mmonit.com/monit/documentation/monit.html查看monit的文档。

首先需要关注的就是monit启动时的配置脚本,对应的就是/etc/monit/monit.rc,该文件内容挺多,但是大部分都是注释掉的,并没有真正生效。这里只介绍几个最基本的配置,如下所示:

set daemon 120 
set log /var/log/monit.log
include /etc/monit/conf.d/*
include /etc/monit/conf-enabled/*

第一行是设置monit的时间间隔,单位是秒,即2分钟启动监控一次;第二行是设置monit的日志;第三行和第四行是设置额外的配置文件。通常monit.rc只是设置monit的基本的全局配置,而真正监控的配置是通过后面两行包含的,即目录conf.d和conf-enabled下的配置文件。不过初始的时候conf.d和conf-enabled目录下是没有配置文件的,所以实际上目前打开的monit也没有特别得监控什么东西,相应的还有一个conf-available目录,这里面有不少配置文件,包括了对一些系统内容的监控:

上图可以看到对网络服务器、数据库、日志等的监控。如果要对上述内容进行监控,只需要将这些文件移动到conf.d或conf-enabled目录下即可,至于到底是要放到哪个目录似乎没有特别的规定,也许是守护进程放到conf.d其它放到conf-enabled?以上的配置文件是对常用的进程的监控,如果我们需要定制化的配置,那么就需要自己写配置文件了,对于配置文件,monit有自己的格式,下面将简单的介绍。

配置文件解析

我们可以从conf-available目录下的示例来了解monit的配置文件,下面是一个例子:

 check process acpid with pidfile "/var/run/acpid.pid"
   group system
   group acpid
   start program = "/etc/init.d/acpid start"
   stop program  = "/etc/init.d/acpid stop"
   if 5 restarts within 5 cycles then timeout
   depends on acpid_bin
   depends on acpid_rc

 check file acpid_bin with path "/usr/sbin/acpid"
   group acpid
   include /etc/monit/templates/rootbin

 check file acpid_rc with path "/etc/init.d/acpid"
   group acpid
   include /etc/monit/templates/rootbin

从这个配置文件里面可以看出来一些简单的语法:

1. monit将每个check语句当做一个监控的动作,监控的内容这里有process和file,这在前文也已经提到过,完整的可以监控内容包括:Process、File、Fifo、File System、Directory、Remote Host、System、Program、Network。对应的语句格式如下:

CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>
CHECK FILE <unique name> PATH <path>
CHECK FIFO <unique name> PATH <path>
CHECK FILESYSTEM <unique name> PATH <string>
CHECK DIRECTORY <unique name> PATH <path>
CHECK HOST <unique name> ADDRESS <host>
CHECK SYSTEM <unique name>
CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>

2. check语句之后还包含具体的操作,涉及到不同的关键词,包括group、start、stop、if、depends、include等等;这里的内容涉及到的太多了,具体还是参考https://mmonit.com/monit/documentation/monit.html

3. 可以使用include语句,将具体操作放到一个文件中,方便在不同操作中调用,就像c语言中的头文件一样。

下面是自己写的一个monit配置的最简单例子,并放到/etc/monit/conf-enabled目录中,对应的文件名是test.conf:

check file test.txt with path "/tmp/test.txt"
        if size > 1 KB then alert

这个配置的作用很简单,就是监视文件/tmp/test.txt的大小,如果大于1KB就报警。之后再重启monit,查看Web可以看到新的配置:

之后往/tmp/test.txt中增加内容,直到超过1KB:

root@X1C:/tmp# dd if=/dev/zero of=/tmp/test.txt count=1 bs=2KB
记录了1+0 的读入
记录了1+0 的写出
2000 bytes (2.0 kB, 2.0 KiB) copied, 0.000465062 s, 4.3 MB/s

再查看Web,就已经报错了:

以上是配置文件的一个简单示例。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值