Monit:开源服务器监控工具

Monit:开源服务器监控工具

2017年11月14日 16:16:25 wguoyong 阅读数:354php

https://www.cnblogs.com/52fhy/p/6412547.htmlhtml

 

 

Monit是一个跨平台的用来监控Unix/linux系统(好比Linux、BSD、OSX、Solaris)的工具。Monit特别易于安装,并且很是轻量级(只有500KB大小),而且不依赖任何第三方程序、插件或者库。node

Monit能够监控服务器进程状态、HTTP/TCP状态码、服务器资源变化、文件系统变更等等,根据这些变化,能够设定邮件报警、重启进程或服务。易于安装、轻量级的实现以及强大的功能,让Monit成为一个理想的后备监控工具。mysql

官网:https://mmonit.com/monit
文档:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/monit-5.20.0.tar.gzlinux

注意:Monit是一个开源工具,但M/Monit是收费的。nginx

安装

yum install monit

固然也能够下载源码安装。web

当前使用版本:sql

 
  1. # monit -Vapache

  2. This is Monit version 5.17.1浏览器

  3. Built with ssl, with pam and with large files

  4. Copyright (C) 2001-2016 Tildeslash Ltd. All Rights Reserved.

经常使用命令

 
  1. monit -t # 配置文件检测

  2. monit # 启动monit daemon

  3. monit -c /var/monit/monitrc # 启动monit daemon时指定配置文件

  4. monit reload # 当更新了配置文件须要重载

  5. monit status # 查看全部服务状态

  6. monit status nginx # 查看nginx服务状态

  7. monit stop all # 中止全部服务

  8. monit stop nginx # 中止nginx服务

  9. monit start all # 启动全部服务

  10. monit start nginx # 启动nginx服务

  11. monit -V # 查看版本

配置文件

使用yum安装默认配置文件在:

 
  1. /etc/monitrc # 主配置文件

  2. /etc/monit.d/ # 单独配置各项服务

为了保护控制文件和密码的安全性,monitrc必须具备读写权限不超过0700(u=xrw,g=,o=)。

主配置文件主要配置全局:
/etc/monitrc

 
  1. ## Global section

  2. set daemon 30

  3.  
  4. set logfile syslog

  5.  
  6. # 邮箱设置

  7. set mailserver xxx@xxx

  8. username "xxx" password "xxx"

  9. # using ssl

  10. set alert xxx@xxx

  11. set alert xxx@xxx #能够设置多个

  12.  
  13. set mail-format {

  14. from: xxx@xxx

  15. subject: [$SERVICE] $EVENT

  16. message:

  17. [$SERVICE] $EVENT

  18.  
  19. Date: $DATE

  20. Action: $ACTION

  21. Host: $HOST

  22. Description: $DESCRIPTION

  23.  
  24. Your faithful employee,

  25. Monit }

  26.  
  27. # 设置web服务认证

  28. set httpd port 2812 and

  29. # ssl enable

  30. # pemfile /etc/certs/monit.pem

  31. # use address all # only accept connection from localhost

  32. allow 127.0.0.1 # 容许localhost链接

  33. allow admin:monit # web登陆的用户名和密码

  34. ## Services

  35.  
  36. ## Includes

  37. include /etc/monit.d/*

配置文件关键字:
'if', 'and', 'with(in)', 'has', 'us(ing|e)', 'on(ly)', 'then', 'for', 'of' 。

如何监控

基本流程

1.修改主配置文件
2.在/etc/monit.d/增长指定服务的配置文件,例如/etc/monit.d/nginx。配置变写完毕,使用下列,命令检测是否正确:

monit -t

3.启动monit:

monit

4.启动全部服务或者单个服务:

monit start all

5.若修改了配置文件,重载配置:

monit reload

6.使用下面命令查看监控状态:

monit status

控制台输出:

 
  1. $ monit status

  2. The Monit daemon 5.17.1 uptime: 4d 15h 45m

  3.  
  4. Process 'nginx'

  5. status Running

  6. monitoring status Monitored

  7. pid 20563

  8. parent pid 1

  9. uid 0

  10. effective uid 0

  11. gid 0

  12. uptime 3d 22h 36m

  13. threads 1

  14. children 2

  15. memory 820 kB

  16. memory total 7.3 MB

  17. memory percent 0.0%

  18. memory percent total 0.2%

  19. cpu percent 0.0%

  20. cpu percent total 0.0%

  21. data collected Sat, 18 Feb 2017 10:09:56

  22.  
  23. System 'iZ28s4jxu17Z'

  24. status Running

  25. monitoring status Monitored

  26. load average [0.03] [0.06] [0.06]

  27. cpu 8.4%us 0.8%sy 0.2%wa

  28. memory usage 1.5 GB [41.2%]

  29. swap usage 0 B [0.0%]

  30. data collected Sat, 18 Feb 2017 10:09:56

或者浏览器输入http://localhost:2812登陆网页版查看实时状态。

设置错误提醒

Monit默认状况下若是一个服务失败只发送一个通知:

alert foo@bar

若是您但愿在服务保持处于失败状态时每十个周期通知一次,您可使用:

alert foo@bar with reminder on 10 cycles

一样,若是您想在每一个失败的周期得到通知,您可使用:

alert foo@bar with reminder on 1 cycle

要禁止某些用户和服务的警报,能够在服务检查的局部配置里添加语句:

noalert mail-address

服务类型

首先须要理解在monit里什么是服务(service)。看监控语法:

check <类型> <服务名> [PATH <path>] [ADDRESS <host address>]

其中类型是monit支持的监控类型,一共有:system、file、process、fifo、filesystem、directory、host、network、program。
服务名必需是英文且惟一,不能够出现重复!
后面的带[]是根据类型须要添加的。

服务类型语法

每一个服务条目由关键字组成check,后面是服务类型。每一个条目须要惟一的描述性名称,能够自由选择。此名称由Monit用于在内部和与用户的全部交互中引用该服务。

目前,支持九种类型的检查语句:

进程

CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>

<path>是程序的pid文件的绝对路径。pid文件是一个包含进程惟一ID的文件。若是pid文件不存在或不包含正在运行的进程的PID编号,则Monit将调用该条目的start方法(若是已定义)。

<regex>是使用PID文件的替代方法,并使用进程名称模式匹配来查找要监视的进程。选择具备最长正常运行时间的最顶部匹配的父级,所以若是进程名称是惟一的,则此检查形式是最有用的。应该尽量使用Pid文件,由于它定义了预期的PID。您能够测试一个进程是否匹配来自命令行使用的模式monit procmatch "regex-pattern"。这将列出匹配或不匹配的全部进程,regex模式。

文件

CHECK FILE <unique name> PATH <path>

<path>是文件的绝对路径。若是文件不存在,Monit将调用该条目的start方法(若是已定义),若是<path>不指向常规文件类型(例如目录),Monit将禁用此条目的监视。若是Monit在被动模式下运行或者没有定义start方法,Monit只会在错误时发送警报。

Fifo

CHECK FIFO <unique name> PATH <path>

<path>是fifo的绝对路径。若是fifo不存在,Monit将定义调用该条目的start方法,若是<path>没有指向fifo类型(例如目录),Monit将禁用对该条目的监视。若是Monit在被动模式下运行或者没有定义start方法,Monit只会在错误时发送警报。

文件系统

CHECK FILESYSTEM <unique name> PATH <path>

<path>是设备/磁盘,安装点,文件或做为文件系统一部分的目录的路径。建议直接使用块特殊文件(例如Linux上的/dev/hda1或Solaris上的/dev/dsk/c0t0d0s1等)若是使用挂载点(例如/data),请注意文件系统是卸载的测试仍然是真的,由于挂载点存在。

若是文件系统不可用,Monit将调用该条目的start方法(若是已定义)。若是不指向文件系统,Monit将禁用对此条目的监视。若是Monit在被动模式下运行或者没有定义start方法,Monit只会在错误时发送警报。

目录

CHECK DIRECTORY <unique name> PATH <path>

<path>是目录的绝对路径。若是目录不存在,Monit将调用该条目的start方法(若是已定义)。若是<path>不指向目录,monit将禁用对此条目的监视。若是Monit在被动模式下运行或者没有定义启动方法,Monit只会在错误时发送警报。

远程主机

CHECK HOST <unique name> ADDRESS <host address>

主机地址能够指定为主机名字符串或点分十进制格式的IP地址字符串。例如,tildeslash.com或“64.87.72.95”。

系统

CHECK SYSTEM <unique name>

的惟一的名称一般是本地主机名,而是可使用任何描述性名称。若是使用变量$ HOST做为名称,它将扩展为主机名。此检查容许监控通常系统资源,如CPU使用率,总内存使用或负载平均。该惟一名称在邮件警报中用做系统主机名,在M/Monit中用做主机条目的初始名称。

自定义

CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]

<path>是可执行程序或脚本的绝对路径。该状态测试容许一个检查程序的退出状态。若是程序没有在<number>秒内完成执行,Monit将终止它。默认程序超时为300秒(5分钟)。程序的输出被记录并在用户界面和警报中可用,默认状况下最大为512B。您可使用set limits语句自定义限制。

网络

CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>

<ipaddress>是受监视网络接口的IPv4或IPv6地址。也能够在Linux上使用接口名称,例如“eth0”。

服务检测时间

可使用every语句修改服务检查计划。

有三种变体:

1.轮询周期倍数

EVERY [number] CYCLES

2.Cron-style

 
  1. EVERY [cron]

  2.  
  3. # [cron]

  4. # * * * * *

  5. # 分 时 日 月 周

3.与Cron-style相反(do-not-check)

NOT EVERY [cron]

示例:
示例1:每两个周期检查一次

 
  1. check process nginx with pidfile /var/run/nginx.pid

  2. every 2 cycles

示例2:在上午8点到下午7点之间检查每一个工做日

 
  1. check program checkOracleDatabase

  2. with path /var/monit/programs/checkoracle.pl

  3. every "* 8-19 * * 1-5"

示例3:在星期日0AM到3AM之间不要在备份窗口中运行检查,不然运行具备常规轮询周期频率的检查。

 
  1. check process mysqld with pidfile /var/run/mysqld.pid

  2. not every "* 0-3 * * 0"

注意不要使用特定的分钟,由于Monit可能不会在那分钟运行。

服务重启限制

Monit提供了一种重启限制机制,用于服务在较长时间内拒绝启动或响应的状况。
超时语句的语法以下(关键字在大写):

IF <number> RESTART <number> CYCLE(S) THEN <action>

该行动值是常见的任何一个动做或超时(为向后兼容,等于取消监视行动)。

下面是一个示例,若是Monit将在3个周期内从新启动服务2次,将取消监视服务:

if 2 restarts within 3 cycles then unmonitor

要在禁用监视后使Monit再次检查服务,请从命令行运行monit monitor servicename。

超时设置自定义exec的示例:

if 5 restarts within 5 cycles then exec "/foo/bar"

中止服务的示例:

if 7 restarts within 10 cycles then stop

服务示例

一个完整的HOST监控服务语法:

 
  1. check host <service> address <address or ip>

  2. if failed

  3. xxx

  4. then alert

  5. alert xx@xxx

解释:
第一行是检查类型为host的服务,须要设定服务名及服务器地址;
第二行至第四行的意思是中间的预期代码xxx若是失败,则执行then alert
最后一行alert xx@xxx配置局部推送的邮箱,可选。能够多行,表示配置多个。

第二行至第四行也能够写成一行:

if failed xxx then alert

下面是示例:

/etc/monit.d/test

 
  1. ## system

  2. check system $HOST

  3. if loadavg (1min) > 4 then alert

  4. if loadavg (5min) > 2 then alert

  5. if cpu usage > 95% for 10 cycles then alert

  6. if memory usage > 75% then alert

  7. if swap usage > 25% then alert

  8.  
  9. ## file

  10. check file apache_bin with path /usr/local/apache/bin/httpd

  11. if failed checksum and

  12. expect the sum 8f7f419955cefa0b33a2ba316cba3659 then unmonitor

  13. if failed permission 755 then unmonitor

  14. if failed uid root then unmonitor

  15. if failed gid root then unmonitor

  16. alert security@foo.bar on {

  17. checksum, permission, uid, gid, unmonitor

  18. } with the mail-format { subject: Alarm! }

  19. group server

  20.  
  21. ## process

  22. check process apache with pidfile /usr/local/apache/logs/httpd.pid

  23. start program = "/etc/init.d/httpd start" with timeout 60 seconds

  24. stop program = "/etc/init.d/httpd stop"

  25. if cpu > 60% for 2 cycles then alert

  26. if cpu > 80% for 5 cycles then restart

  27. if totalmem > 200.0 MB for 5 cycles then restart

  28. if children > 250 then restart

  29. if loadavg(5min) greater than 10 for 8 cycles then stop

  30. if failed host www.tildeslash.com port 80 protocol http

  31. and request "/somefile.html"

  32. then restart

  33. if failed port 443 protocol https with timeout 15 seconds then restart

  34. if 3 restarts within 5 cycles then unmonitor

  35. depends on apache_bin

  36. group server

  37.  
  38. ## filesystem

  39. check filesystem datafs with path /dev/sdb1

  40. start program = "/bin/mount /data"

  41. stop program = "/bin/umount /data"

  42. if failed permission 660 then unmonitor

  43. if failed uid root then unmonitor

  44. if failed gid disk then unmonitor

  45. if space usage > 80% for 5 times within 15 cycles then alert

  46. if space usage > 99% then stop

  47. if inode usage > 30000 then alert

  48. if inode usage > 99% then stop

  49. group server

  50.  
  51. ## file's timestamp

  52. check file database with path /data/mydatabase.db

  53. if failed permission 700 then alert

  54. if failed uid data then alert

  55. if failed gid data then alert

  56. if timestamp > 15 minutes then alert

  57. if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba

  58. ## directory permission

  59. check directory bin with path /bin

  60. if failed permission 755 then unmonitor

  61. if failed uid 0 then unmonitor

  62. if failed gid 0 then unmonitor

  63. ## remote host

  64. check host myserver with address 192.168.1.1

  65. if failed ping then alert

  66. if failed port 3306 protocol mysql with timeout 15 seconds then alert

  67. if failed port 80 protocol http

  68. and request /some/path with content = "a string"

  69. then alert

  70. ## network link status

  71. check network public with interface eth0

  72. if failed link then alert

  73. if changed link then alert

  74. if saturation > 90% then alert

  75. if download > 10 MB/s then alert

  76. if total upload > 1 GB in last hour then alert

  77. ## custom program status output

  78. check program myscript with path /usr/local/bin/myscript.sh

  79. if status != 0 then alert

控制台访问验证

访问Monit Web界面主要经过ALLOW选项进行控制,ALLOW选项用于指定身份验证并仅受权特定客户端进行链接。

若是正在使用Monit命令行界面,至少须要一个明文密码(见下文),不然Monit命令行界面将没法链接到Monit Web界面。

尝试链接到Monit,但提交错误的用户名和/或密码的客户端将使用其IP地址记录。

主机和网络容许列表

Monit维护容许链接的主机和网络的访问控制列表。您能够添加任意数量的主机,但只容许具备有效域名或其IP地址的主机。

Monit将查询名称服务器以检查任未尝试链接的主机。若是主机(客户端)正在尝试链接,但没法在访问列表中找到或没法解决,Monit将当即关闭与客户端的链接。

配置文件示例:

 
  1. set httpd port 2812

  2. allow localhost

  3. allow my.other.work.machine.com

  4. allow 10.1.1.1

  5. allow 192.168.1.0/255.255.255.0

  6. allow 10.0.0.0/8

在容许列表中未说起的客户端,尝试链接到Monit将被拒绝访问,并使用其IP地址记录。

明文用户和密码

若是allow语句包含用单独的“:”字符分隔的用户名和密码,Monit将使用基本认证。

注意:可使用特殊字符,但对于非字母数字,必须引用密码。

句法:

ALLOW <username>:<password>

PAM

在提供PAM的平台(如Linux,Mac OS X,FreeBSD,NetBSD)上支持PAM。

句法:

ALLOW @<group>

其中group是容许访问Monit的Web界面的组名称

只读用户

最后,能够将一些用户定义为只读。只读用户能够读取Monit网页,但没法访问按钮,而且没法从Web界面更改服务。

 
  1. set httpd port 2812

  2. allow admin:password

  3. allow hauk:password read-only

  4. allow @admins

  5. allow @users read-only

经过在 username:password 后使用只读关键字将用户设置为只读。在上述示例中,用户hauk被定义为只读用户,而admin用户具备全部访问权限。

实践

监听Nginx、php-fpm及API接口

/etc/monit.d/http

 
  1. # check nginx process

  2. check process nginx with pidfile /run/nginx.pid

  3. start program = "/usr/local/nginx/sbin/nginx " with timeout 10 seconds

  4. stop program = "/usr/local/nginx/sbin/nginx -s stop"

  5. if changed pid then restart

  6.  
  7. # check php-fpm process

  8. check process php-fpm with MATCHING php-fpm

  9. start program = "/usr/local/php/sbin/php-fpm" with timeout 10 seconds

  10. stop program = "/usr/bin/killall php-fpm" with timeout 10 seconds

  11. if failed port 9000 for 3 cycles then restart

  12.  
  13. # check http status

  14. check host dev_xxx_http address xxx

  15. start program = "/usr/local/php/sbin/php-fpm ; /usr/local/nginx/sbin/nginx -s reload" with timeout 10 seconds

  16. stop program = "/usr/bin/killall php-fpm ; /usr/local/nginx/sbin/nginx -s stop" with timeout 10 seconds

  17. if failed

  18. port 80

  19. protocol http

  20. and status = 200

  21. for 3 cycles

  22. then restart

  23. #alert xxx@xxx #能够单独设置新的通知者

  24. #alert xxx@xxx

  25.  
  26. if failed

  27. port 80

  28. protocol http

  29. request "/Api/Login/Get_Userinfo/"

  30. and status = 200

  31. for 3 cycles

  32. then restart

监听TCP

/etc/monit.d/tcp

 
  1. check host dev_xxx_swoole_xxx address xxx

  2. start program = "/usr/local/php/bin/php Server.php" with timeout 10 seconds

  3. stop program = "/usr/bin/kill -9 $(ps -aux|grep -E 'Server|swoole_server'|grep -v grep|awk '{print $2}')" with timeout 10 seconds

  4. if failed port xxx type tcp for 3 cycles then restart

做者:飞鸿影~

出处:http://52fhy.cnblogs.com/

相关文章
相关标签/搜索