使用monit规范的扩展业务的进程监控和管理

前言:

看到这个标题的人一定会很疑惑,进程监控和报警,不都是用zabbix,nagios么?  对于管理的话,自己写crontab脚本不就行了。 当然这肯定是可以的。 标题说了,规范和扩展!   所谓的进程监控就是 在进程不可用,或者是被kill掉,也有外因,比如进程吃内存大,需要重启进程,让他初始化程序的实例,简单说,就是进程pid是在,只是进程是假死的,不可用的。

对于这些进程管理和监控的需求,说下圈子里面解决这类问题的办法:

大家一定知道supervisord这个东西,现在很多人都喜欢用他,我个人的nginx tornado的方案,就是用supervisord来管理进程的, 话说他也是支持进程监控的,但是比较粗糙。他的方法就是检测pid,pid死了,他会重启进程 。 他的这种方法,不太适用于我们上面说的这个场景。

百度在用这个supervisord做进程状态监控,但是百度已经对这个做了二次开发,他们扩展了不少东西,比如监控检测的规则,报警的接口化,对外的探测通知socket接口,别的进程也可以通过标准,拿到服务器进程的一些情况。 这已经实现supervisord扩展效果,我想大家也可以开发出来,只是耗时耗力。  supervisord的源码,我也顺了下源码,难度不太大 !  有时间我会出一个supervisord二次开发的文档。

还有一个叫god,是ruby写的。 现在小米在用,本人对于ruby有些不感冒,这里就简单讲讲他的有点。 其实god在一定程度上来说,要比supervisord健全的。 我最欣赏他的地方就是,他的通知相当强大,他只是webkook回调,email,jabber。他的进程监测也不错。 有兴趣的朋友可以试试。

下面是god的一个例子。

  w.name = "hello"
  w.start = "ruby xiaorui.rb"
  w.keepalive(:memory_max => 333.megabytes,
              :cpu_max => 50.percent)

还有一个蓝汛叫amr的东西,听了介绍,感觉功能很是饱满,很牛叉的样子。 只是没有开源,不明白他的实现细节。  现在各大公司都有一套类似进程监控的东西。

好了,这里说下我推荐的monit的用法。

大家可以先看看monit的介绍 http://mmonit.com/monit

Monit 是一款功能非常丰富的进程、文件、目录和设备的监测软件,用于Unix平台。它可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误。

Monit 对管理员来说可谓神器也。话说 Nagios 也就是在监控牛,而 Monit 不但本地监控牛,远程服务监控也牛。更牛的是,只要你花点功夫,你的服务就永远都能“死而复生”。

安装

yum -y install monit

monit主配置,30s检测一次

vi /etc/monit.conf
#检测的时间,默认是120s
set daemon  30
set mailserver localhost
set mail-format { from: [email protected] }
set alert [email protected]
#原文:http://rfyiamcool.blog.51cto.com/1030776/1437572

具体的配置

[[email protected] ~]# cat /etc/monit.d/nginx.monit.conf    
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed port 9000 type TCP then start

语法的检测:

[[email protected] ~]# monit -t
Control file syntax OK
[[email protected] ~]#
原文:http://rfyiamcool.blog.51cto.com/1030776/1437572

monit也是支持web数据展现的,虽然功能比较的简单,而且没有操作的功能:

他可以像supervisord那样,直接启动、关闭、重启某个时间。

[[email protected] ~]# monit -h
Usage: monit [options] {arguments}
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 start, stop, restart, monitor and unmonitor
 -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 for run from init)
 -t            Run syntax check for the control file
 -v            Verbose mode, work noisy (diagnostic output)
 -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 action arguments for non-daemon mode are as follows:
 start all      - Start all services
 start name     - Only start the named service
 stop all       - Stop all services
 stop name      - Only 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         - Print full status information for each service
 summary        - Print short status information for each service
 quit           - Kill monit daemon process
 validate       - Check all services and start if not running

(Action arguments operate on services defined in the control file)
#原文:http://rfyiamcool.blog.51cto.com/1030776/1437572

咱们经常用的saltstack也是有monit的一个模块,当然有些简单吧。没有添加的模块,当时这些咱们可以自己写模块实现添加。 有时间写个saltstack的monit添加删除模块,提交给官方 。 (没提交也别打我)

salt.modules.monit
Monit service module. This module will create a monit type service watcher.

salt.modules.monit.monitor(name)
monitor service via monit

CLI Example:

salt ‘*‘ monit.monitor <service name>
salt.modules.monit.restart(name)
Restart service via monit

CLI Example:

salt ‘*‘ monit.restart <service name>
salt.modules.monit.start(name)
CLI Example:

salt ‘*‘ monit.start <service name>
salt.modules.monit.stop(name)
Stops service via monit

CLI Example:

salt ‘*‘ monit.stop <service name>
salt.modules.monit.summary(svc_name=‘‘)
Display a summary from monit

CLI Example:

salt ‘*‘ monit.summary
salt ‘*‘ monit.summary <service name>
salt.modules.monit.unmonitor(name)
Unmonitor service via monit

CLI Example:

salt ‘*‘ monit.unmonitor <service name>

这一段配置的意思是说,当9000不通的时候,启动下进程  触发的是 start program !!!

他的检测语法:

IF <TEST> THEN ACTION [ELSE IF SUCCEEDED THEN ACTION]

action包括  altert,start,stop.restart,exec

alert 不用说了,就是报警的邮件

start、stop、restart 就是触发start、stop、restart program

exec 可以自定一定脚本

这里就写点例子,给大家看看。

 check process tomcat with pidfile /var/run/tomcat.pid
       start program = "/etc/init.d/tomcat start" 
             as uid nobody and gid nobody
       stop program  = "/etc/init.d/tomcat stop"
             # You can also use id numbers instead and write:
             as uid 99 and with gid 99
       if failed port 8080 then alert

如果8080端口不同的话,报警 !

check process named with pidfile /var/run/named.pid

start program = "/etc/init.d/named start"

stop program  = "/etc/init.d/named stop"

if failed port 53 use type udp protocol dns then restart

if 3 restarts within 5 cycles then timeout

如果53的udp端口不通,就重启。

check process apache with pidfile /var/run/httpd.pid

start "/etc/init.d/httpd start"

stop  "/etc/init.d/httpd stop"

if failed host www.sol.no port 80 then alert

if failed host shop.sol.no port 443 then alert

if failed host chat.sol.no port 80 then alert

if failed host www.tildeslash.com port 80 then alert

如果主机不通的话,alert。 host 填写域名和ip port 端口 !

 check process apache with pidfile /var/run/httpd.pid
       start "/etc/init.d/httpd start"
       stop  "/etc/init.d/httpd stop"
       if failed 
          host www.sol.no port 80 protocol http
       then alert

这里还可以指明是http 协议 !

 check process apache with pidfile /var/run/httpd.pid
       start "/etc/init.d/httpd start"
       stop  "/etc/init.d/httpd stop"
       if failed 
          host www.sol.no port 80 and
          send "GET / HTTP/1.1\r\nHost: www.sol.no\r\n\r\n"
          expect "HTTP/[0-9\.]{3} 200.*"
       then alert

还可以用expect做数据的推送。

 check host www.tildeslash.com with address www.tildeslash.com
       if failed 
          icmp type echo count 5 with timeout 15 seconds
       then alert

icmp检测,指定count的数目,不至于一直没完没了的ping。

 check host tildeslash with address www.tildeslash.com
       if failed 
          port 80 protocol http and 
          request "/monit/dist/monit-5.7.tar.gz"
          with checksum f9d26b8393736b5dfad837bb13780786
       then alert

这里还可以计算文件的md5,request支持get 和 post !

 if failed 
    port 80
    protocol http
    request "/data/show?a=b&c=d"
 then restart

monit check 状态检测的时间,是可以定义的。

 Name:        | Allowed values:            | Special characters:              
 ---------------------------------------------------------------
 Minutes      | 0-59                       | * - ,
 Hours        | 0-23                       | * - ,
 Day of month | 1-31                       | * - ,
 Month        | 1-12 (1=jan, 12=dec)       | * - ,
 Day of week  | 0-6 (0=sunday, 6=saturday) | * - ,

check process nginx with pidfile /var/run/nginx.pid
   every 2 cycles
Example 2: Check every workday 8AM-7PM
 check program checkOracleDatabase with
       path /var/monit/programs/checkoracle.pl
   every "* 8-19 * * 1-5"
Example 3: 在这个时间端,就不要检测  Sunday 0AM-3AM
 check process mysqld with pidfile /var/run/mysqld.pid
   not every "* 0-3 * * 0"

各种的判断,内存和cpu的判断。

#xiaorui.cc
check process freeswitch 
    with pidfile /usr/local/freeswitch/log/freeswitch.pid
  start program = "/usr/local/freeswitch/bin/freeswitch -nc -hp"
  stop program = "/usr/local/freeswitch/bin/freeswitch -stop"
  if total memory > 1000.0 MB for 5 cycles then alert
  if total memory > 1500.0 MB for 5 cycles then alert
  if total memory > 2000.0 MB for 5 cycles then restart
  if cpu > 60% for 5 cycles then alert
  if failed 
     port 5060 type udp protocol SIP
     target [email protected] and maxforward 10 
  then restart
 check process asterisk 
   with pidfile /var/run/asterisk/asterisk.pid
   start program = "/usr/sbin/asterisk"
   stop program = "/usr/sbin/asterisk -r -x ‘shutdown now‘"
   if total memory > 1000.0 MB for 5 cycles then alert
   if total memory > 1500.0 MB for 5 cycles then alert
   if total memory > 2000.0 MB for 5 cycles then restart
   if cpu > 60% for 5 cycles then alert
   if failed 
      port 5060 type udp protocol SIP
      and target [email protected] maxforward 10
   then restart

邮件也是可以定制,如果你想发送短信和语音的话,大家可以自定义报警的脚本。这样控制性更好。如果监控用的是zabbix的话,可以配合monit一块搞起。 比如的时候,咱们就不用email报警了,直接用zabbix sender主动触发 trigger action。

 #xiaorui.cc
 check process apache with pidfile /var/run/httpd.pid
       start program = "/etc/init.d/httpd start"
       stop program  = "/etc/init.d/httpd stop"
       if cpu > 40% for 2 cycles then alert
       if total cpu > 60% for 2 cycles then alert
       if total cpu > 80% for 5 cycles then restart
       if mem > 100 MB for 5 cycles then stop
       if loadavg(5min) greater than 10.0 for 8 cycles then stop

 check process apache with pidfile /var/run/httpd.pid
      start = "/etc/init.d/httpd start"
      stop  = "/etc/init.d/httpd stop"
      alert [email protected] on {nonexist, timeout} 
        with mail-format { 
              from:     [email protected]$HOST
              subject:  apache $EVENT - $ACTION
              message:  This event occurred on $HOST at $DATE. 
              Your faithful employee,
              monit
      }
      if failed host www.tildeslash.com  port 80 then restart
      if 3 restarts within 5 cycles then timeout
      depend httpd_bin
      group apache
 check file httpd_bin with path /usr/local/apache/bin/httpd
       alert [email protected] on {checksum, timestamp, 
                  permission, uid, gid}
             with mail-format {subject: Alaaarrm! on $HOST}
       if failed checksum 
          and expect 8f7f419955cefa0b33a2ba316cba3659
              then unmonitor
       if failed permission 755 then unmonitor
       if failed uid root then unmonitor
       if failed gid root then unmonitor
       if changed timestamp then alert
       group apache

我这里有些服务flask开发的,利用uwsgi来***能。uwsgi有时因为程序的prefork的数目的限制和程序本身逻辑的bug,造成把uwsgi的进程堵塞掉。 这个时候需要monit重启下uwsgi的服务。

check process nginx with pidfile /var/run/nginx.pid
  start program = "/etc/init.d/nginx start"
  stop program = "/etc/init.d/nginx stop"
  if failed port 443 type tcpssl protocol http
      request "/lvs_vip/status_code" hostheader "xiaorui.cc"
      with timeout 5 seconds
  then alert
  if failed port 443 type tcpssl protocol http
      request "/lvs_vip/status_code" "xiaorui.cc"
      with timeout 10 seconds
      3 times within 4 cycles
  then restart
  depends on uwsgi

总结:

在某种程度上来说,这类的工具有些单机,咱们可以利用puppet,saltstack这类配置管理工具,使这类的配置中心统一化。  咱们可以在中心节点,直接下发monit的配置。monit这东西的扩展性很不错,大家可以好好的试试!

使用monit规范的扩展业务的进程监控和管理

时间: 2024-08-26 05:48:27

使用monit规范的扩展业务的进程监控和管理的相关文章

进程监控与管理

1.进程 狭义定义:进程是正在运行的程序的实例 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动.它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元. 进程是由进程控制块.程序段.数据段三部分组成   程序 程序是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念.而进程是程序在处理机上的一次执行过程,它是一个动态的概念. 进程和程序区别      1.程序是静态概念,本身作为一种软件资源长期保存:而进程是程序的执

实现chrome扩展启动本地进程 - 补充

实现chrome扩展启动本地进程 - 补充 标签: chrome扩展启动本地程序访问本地磁盘 2014-10-17 11:42 6753人阅读 评论(17) 收藏 举报  分类: Chrome Plugin 版权声明:本文为博主原创文章,未经博主允许不得转载. 示例 主要包含如下部分 com.google.chrome.demo-win.json native_cmd.bat manifest.json popup.html popup.js 功能简介: 实现一个扩展,用户点击一个按钮后会启动本

Swift的源码目录结构 其中proxy是前端的业务接入进程

Swift的源码目录结构.其中proxy是前端的业务接入进程.account.container和object目录分别是账户.容器 和对象的业务处理逻辑进程.common目录是一些通用工具代码.common中比较重要的有:哈希环的处理逻辑.接下来会依次介绍各个进程的源码逻辑和 一些关键点机制. 各个业务进程或模块之间的逻辑关系可以参考文中的架构图. 二.Proxy进程的业务处理 首先需要掌握基于PasteDeploy的堆栈式WSGI架构.根据PasteDeploy定义的各个层,可以很快理清配置文

Java进程监控

目录 1.引言 2. 程序启停, 为进程自定义项目名称 3. 操作系统判断 4. 获取进程信息 5. 内存,CPU信息 6. 堆内存信息 7. 端口信息 8. 线程信息 9. MXBean使用样例 9.1 根据pid获取jvm对象 9.2 根据jvm对象获取jmx服务 9.3. 使用MXBean代理获取具体管理工具 9.4 访问MXBean demo 9.6 demo ?最近项目需要对Java进程,堆栈信息,内存,cpu等资源的消耗进行监控,借鉴了git已有的轮子JPOM java项目管理系统和

20150917 Linux进程查看与管理以及作业管理

第一.基础知识 MBR引导--内核--内核程序-->协调其它程序 一般内核运行在硬件之上,各应用也在硬件之前 1)OS的基本功能:文件系统.网络功能.进程管理.内存管理.驱动程序.安全功能 以上为通用目的设置的程序., 程序=指令+数据, 程序运行在内存当中.这个内存由物理内存映射逻辑空间 左则表示二个框分别表示指令和数据.物理内存划分固定大小的页框称为pageframe. 右则下面的方框表示物理内存.每个进程在右则上面的方框中,这部分为假的内存 空间称为线性地址空间. 2)CPU指令:  四个

C#通过“委托和事件”的方式实现进程监控并与“普通方式”对比

今天重新学习了一下观察者模式,对我的思路产生了启发.进程监控程序之前写过几个,这回换一种思路,改用委托和事件来实现.我已经用序号将关键的几步标注,方便大家理顺思路.代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq;

linux 进程监控

linux 进程监控 monit monit是一个小型的开放源码工具来管理和监控Unix系统.Monit可以自动维护进程,及时避免进程异常退出等产生的问题. 系统: monit可以监控问题的发生,包括进程状态.系统cpu负载.内存占用情况等,例如当apache服务的cpu负载以及内存闸弄情况过高时候,它会重启apache服务. 进程: monit可以监控守护进程,包括系统进程.例如当某个进行down掉,它会自动恢复重启该进程. 文件系统:Monit可以监控本地文件.目录.文件系统的变化,包括时间

9-17 linux进程查看和管理

Linux进程查看与管理 OS的基本功能:文件系统.网络功能.进程管理.内存管理.驱动程序.安全功能 程序=指令+数据 CPU:  特权指令  普通指令 程序执行环境:  内核模式:运行内核级指令  用户模式:运行普通指令 应用程序:   运行普通指令:直接运行于CPU   运行特权指令:通过system call 内存:8bits, 1byte  32bits, 2^32, 0-2^32-1, 2^10*2^10*2^10*2^2 bytes = 2^10*2^10*2^2 kilo byte

进程监控top free ps grep pgrep pkill kill &amp; bg fg jobs nohup ctrl+z pstree

一.top命令 https://blog.csdn.net/gxiaop/article/details/55096686 1是显示其他cpu内容 M按照内存排序 P按照cpu排序 K 杀掉某个进行 nice 表示优先级改动过的,优先级数字越低,优先度越大 top -p 1234 显示1234的情况 二.free命令 free 按照kb显示 free -m 按照m显示 free -h 按照G显示 三.ps命令 https://blog.csdn.net/u014636209/article/de