黑帽联盟

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

[运维监控] 用monit监控linux服务和进程

[复制链接]

852

主题

38

听众

3175

积分

白金VIP

Rank: 8Rank: 8

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

    [LV.Master]伴坛终老

    monit是一款功能强大的系统状态、进程、文件、目录和设备的监控软件,还可以在服务down掉的时候自动重启服务或者当某个进程占用过多的资源的时候自动停掉进程,并且支持Email报警功能、远程服务器监测、web 管理界面等.
      系统:centos 5.9
      需要的软件包:epel-release-5-4.noarch.rpm

    1.安装monit
    rpm -ivh epel-release-5-4.noarch.rpm
    yum -y install monit

    2.配置monit
    cp /etc/monit.conf /etc/monit.conf.bak
    vi /etc/monit.conf
    # 检查周期,默认为2分钟,对于网站来说有点长,可以根据需要自行调节,这改成30秒.
    set daemon  30

    # 日志文件
    set logfile /var/log/monit.log

    # 邮件通知服务器
    #
    #set mailserver mail.example.com
    set mailserver localhost

    # 简单的,这只改了一下发送人,有需要可以自己修改其它内容.
    set mail-format { from: monit@cnblackhat.com }

    # 设置邮件通知接收者.建议发到gmail,方便邮件过滤.
    set alert admin@cnblackhat.com

    set httpd port 2812 and            # 设置http监控页面的端口
         use address www.example.com   # http监控页面的IP或域名
         allow localhost               # 允许本地访问
         allow 58.68.78.0/24           # 允许此IP段访问
         ##allow 0.0.0.0/0.0.0.0       # 允许任何IP段,不建议这样干
         allow userxxx:passwordxxx     # 访问用户名密码

    # 系统整体运行状况监控,默认的就可以,可以自己去微调
    #
    # 系统名称,可以是IP或域名
    check system www.example.com
        if loadavg (1min) > 4 then alert
        if loadavg (5min) > 2 then alert
        if memory usage > 75% then alert
        if cpu usage (user) > 70% then alert
        if cpu usage (system) > 30% then alert
        if cpu usage (wait) > 20% then alert

    # 监控nginx
    #
    # 需要提供进程pid文件信息
    check process nginx with pidfile /var/run/nginx.pid
        # 进程启动命令行,注:必须是命令全路径
        start program = "/etc/init.d/nginx start"
        # 进程关闭命令行
        stop program  = "/etc/init.d/nginx stop"
        # nginx进程状态测试,监测到nginx连不上了,则自动重启
        if failed host blog.slogra.com port 80 protocol http then restart
        # 多次重启失败将不再尝试重启,这种就是系统出现严重错误的情况
        if 3 restarts within 5 cycles then timeout
        # 可选,设置分组信息
        group server

    #   可选的ssl端口的监控,如果有的话
    #    if failed port 443 type tcpssl protocol http
    #       with timeout 15 seconds
    #       then restart

    # 监控apache
    #
    check process apache with pidfile /var/run/httpd.pid
        start program = "/etc/init.d/httpd start"
        stop program  = "/etc/init.d/httpd stop"
        # apache吃cpu和内存比较厉害,额外添加一些关于这方面的监控设置
        if cpu > 50% for 2 cycles then alert
        if cpu > 70% for 5 cycles then restart
        if totalmem > 1500 MB for 10 cycles then restart
        if children > 250 then restart
        if loadavg(5min) greater than 10 for 20 cycles then stop
        if failed host blog.slogra.com port 8080 protocol http then restart
        if 3 restarts within 5 cycles then timeout
        group server
        # 可选,依赖于nginx
        depends on nginx

    # 监控php-fpm进程
    #
    check process php-fpm with pidfile /var/run/php-fpm.pid
        start program = "/etc/init.d/php-fpm start"
        stop program  = "/etc/init.d/php-fpm stop"
        if failed port 9000 type TCP then restart
        ## if failed unixsocket /var/run/php-fpm.sock then restart
        if 3 restarts within 5 cycles then timeout
        group server

    # 监控mysql进程
    #
    #mysql的pid有可能根据个人服务器名来命名的,所以我建议大家先看看自己机器上mysqld.pid到底是什么名字.
    check process mysqld with pidfile /var/lib/mysql/mysqld.pid
        start program = "/etc/init.d/mysql start"
        stop program = "/etc/init.d/mysql stop"
        if failed host 127.0.0.1 port 3306 then restart
        if 5 restarts within 5 cycles then timeout


    虽然在注释里有详细说明,但是我还是要再强调说明几点:

    1.start和stop的program参数里的命令必须是全路径,否则monit不能正常启动,比如killall应该是/usr/bin/killall.
    2.进程多次重启失败monit将不再尝试重启,收到这样的通知邮件表明系统出现了严重的问题,要引起足够的重视,需要赶紧人工处理.

    当然monit除了管理进程之外,还可以监控文件、目录、设备等,本文不做讨论,具体配置方式可以去参考monit的官方文档.

    3.启动monit
    chkconfig --add monit
    chkconfig monit on
    service monit start

    看到正确的提示信息即可,若遇到问题可以去查看配置里指定的日志文件,如/var/log/monit.log.

    帖子永久地址: 

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

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

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