黑帽联盟

 找回密码
 会员注册
查看: 1446|回复: 0
打印 上一主题 下一主题

[运维监控] monit编译安装及使用说明 (全网最详细)

[复制链接]

852

主题

38

听众

3174

积分

白金VIP

Rank: 8Rank: 8

  • TA的每日心情
    开心
    2024-3-7 12:52
  • 签到天数: 1538 天

    [LV.Master]伴坛终老

    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还有对文件目录设备的监控,和对不包含的协议的支持,手册中有详细的说明。





    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部