黑帽联盟

标题: monit编译安装及使用说明 (全网最详细) [打印本页]

作者: 定位    时间: 2018-5-31 19:21
标题: monit编译安装及使用说明 (全网最详细)
1. 下载,官网:http://mmonit.com/
wget http://mmonit.com/monit/dist/monit-5.2.5.tar.gz -P /usr/local/src

2. 安装
tar zxvf /usr/local/src/monit-5.2.5.tar.gz -C /usr/local/src
cd /usr/local/src/monit-5.2.5
首先确保openssl和openssl-devel已经安装过,没有安装,按照下面的操作来
yum install openssl openssl-devel -y

如果已经安装openssl,执行下面的步骤,安装monit
./configure --with-ssl-incl-dir=/usr/include --with-ssl-lib-dir=/usr/lib64
失败:error: PAM enabled but headers or library not found
安装:yum install pam-devel
make
make install

cp monitrc /etc/
chmod 0700 /etc/monitrc

4. 设置选项

vi /etc/monitrc
set daemon 120
set logfile syslog facility log_daemon
set idfile /usr/local/monit/monit.id
set statefile /usr/local/monit/monit.state
set mailserver localhost
set alert emailaddress
set httpd port 2812 and use address 192.168.1.10
allow localhost
allow 192.168.1.0/24
allow admin:"password"
include /etc/monit.d/*

选项说明:

set daemon 120

设置monit作为守护进程运行,每2分钟监视一次

set logfile

设置日志文件

set idfile

设置id文件

set statefile

设置状态文件

set mailserver

设置邮件服务器

set alert

设置发送警告的邮箱

set httpd port portnumber and use address ipaddress

设置页面监控页面的监听IP和端口号

allow

允许连接的主机ip,或网段

allow username:password

设置页面监控访问的用户名和密码

include /etc/monit.d/*

可以将具体监控服务的文件放在这个目录中,monit会自动包含这些文件


5. 监控具体服务

    monit内置了一些协议,具体的在源代码目录的protocols中有,支持的有:aphe_status clamav default dns dwp ftp gener gps http imap ldap2 ldap3 lmtp mehe mysql nntp NOTES ntp3 pgsql radius rdate rsy sip smtp ssh tns

如果有协议支持,就可以直接使用protocol 协议进行测试

如果没有协议支持,也可以使用send和expect使用不支持的协议进行测试,见尾部的pdf文档中的协议说明那一节


a. 监控nginx

vi /etc/monit.d/nginx.monit
check process nginx with pidfile /usr/local/nginx/nginx.pid
start program = "/usr/local/nginx/sbin/nginx"
stop program = "/usr/local/nginx/sbin/nginx -s stop"
if failed host 192.168.1.11 port 8011 protocol http then restart

b. 监控memcached

vi /etc/monit.d/memcached.monit
check process memcached with pidfile /tmp/linuxjcq_memcached.pid
start program = "/etc/rc.d/init.d/memcached start"
stop program = "/etc/rc.d/init.d/memcached stop"
if failed host 192.168.1.10 port 11211 protocol memcache then restart

c. 监控php-fpm

vi /etc/monit.d/php-fpm.monit
check process php-fpm with pidfile /usr/local/php/logs/php-fpm.pid
start program = "/usr/local/php/sbin/php-fpm start"
stop program = "/usr/local/php/sbin/php-fpm stop"
if cpu > 50% for 2 cycles then alert
if cpu > 70% for 5 cycles then restart
if failed host 192.168.1.10 port 9000 then restart
if 5 restarts within 5 cycles then timeout

d. 监控mysql

vi /etc/monit.d/mysqld.monit
check process mysqld with pidfile /data/mysql/mysql.pid
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 192.168.1.10 port 3306 then restart
if 5 restarts within 5 cycles then timeout

6. 测试配置文件

/usr/local/monit/bin/monit -tc /etc/monitrc

7. 添加为init进程,并启动
vi /etc/init/monit.conf  
# monit  
start on stopped rc RUNLEVEL=[2345]
respawn  
exec /usr/local/bin/monit -Ic /etc/monitrc


8、邮件报警功能
主要关注的告警功能

set mailserver smtp.163.com port 25    ##设置邮件服务器及端口,这里使用的163邮件服务器
      username 发件人有限 password “发件人邮箱密码”

set mail-format {
    from: root@localhost.com
    subject: monit alert -- $EVENT $SERVICE
    message: $EVENT Service $SERVICE
    Date: $DATE
    Action: $ACTION
    Host: $HOST
    Description: $DESCRIPTION
    Your faithful employee,
    Monit
}

#设置邮件发送内容

set alert XXXX@163.com  #设置收件人邮箱,多个收件人写多行


9. monit一些常用选项

-c file
指定配置文件
-t
测试配置文件

对服务的操作

start all
启动所有的监控的服务
stop all
停止所有的监控的服务
restart all
重启所有的监控的服务
monitor all
监控所有的服务
unmonitor all
取消对所有服务的监控
start name
启动指定的服务
stop name
停止指定的服务
restart name
重启指定的服务
monitor name
监控指定的服务
unmonitor name
取消对指定服务的监控

monit自身相关的命令

reload
重新初始化monit
status
打印所有监控的服务的完整状态报告
summary
打印所有监控的服务概要
quit
退出monit,配置成init后,可以使用monit quit进行重启

10. 相关文档

pdf文档和官网手册

    http://mmonit.com/monit/documentation/monit.pdf
    http://mmonit.com/monit/documentation/monit.html

    因为我只需要用到对服务重启,monit还有对文件目录设备的监控,和对不包含的协议的支持,手册中有详细的说明。










欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5