Monitor进程Linux

Section One: mmonitor.com

What Monit can do



You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particular useful for monitoring daemon processes, such as those started at system boot time from /etc/init.d/. For instance sendmail, sshd, apache and mysql.
In difference to many monitoring systems, Monit can act if an error situation should occur, e.g.; if sendmail is not running, Monit can start sendmail again automatically or if apache is using too much resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as; how much memory or cpu cycles a process is using.
You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons - you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.
Monit can monitor network connections to various servers, either on localhost or on remote hosts. TCP, UDP and Unix Domain Sockets are supported. Network test can be performed on a protocol level; Monit has built-in tests for the main Internet protocols, such as HTTP, SMTP etc. Even if a protocol is not supported you can still test the server as you can configure Monit to send any data and test the response from the server.
Monit can be used to test programs or scripts at certain times, much like cron, but in addition, you can test the exit value of a program and perform an action or send an alert if the exit value indicates an error. This means that you can use Monit to perform any type of check you can write a script for.

Finally, Monit can be used to monitor general system resources on localhost such as overall CPU usage, Memory and Load Average.


在debian或ubuntu上安装monit非常方便,通过下面的命令

sudo apt-get install monit

即可,其它*nix上也很简单,下载源码走一遍安装三步就OK了。

./configure
make
make install

安装后,默认的配置文件为/etc/monit/monitrc。

要再强调说明几点:

  1. start和stop的program参数里的命令必须是全路径,否则monit不能正常启动,比如killall应该是/usr/bin/killall。
  2. 对于spawn-fcgi,很多人会用它来管理PHP的fast-cgi进程,但spawn-fcgi本身也是有可能挂掉的,所以还是需要用monit来监控spawn-fcgi。spawn-fcgi必须带-P参数才会有pid文件,而且fast-cgi走的不是http协议,monit的protocol参数也没有cgi对应的设置,一定要去掉protocol http这项设置才管用。
  3. 进程多次重启失败monit将不再尝试重启,收到这样的通知邮件表明系统出现了严重的问题,要引起足够的重视,需要赶紧人工处理。
Section Two:

一、Logwatch介绍

Logwatch是一款专门监测Linux日志的软件。它可以将日志统计分析并通过邮件发送给指定收件人[1][2]。

二、背景

一个基于CentOS6.1的实验用Linux集群。由于其架设在内网,没有公网IPv4地址。而所采用的DNS服务商又不支持AAAA记录。因此打算使用其他SMTP将日志发送到我的邮箱。

三、过程

1.首先配置mailx所使用的SMTP。修改/etc/mail.rc并添加smtp身份认证信息[3]。例如

set from=monitor@yourdomain.com
set smtp=smtp.yourdomain.com
set smtp-auth=login
set smtp-auth-user=monitor
set smtp-auth-password=PassWord

2.我发现/etc/cron.daily/0logwatch中,任务使用了默认配置文件/usr/share/logwatch/default.conf/logwatch.conf ,因此修改该配置文件

第35行 MailTo = 接收日志用的邮箱,多个邮箱用英文逗号分隔

第44行 MailFrom = monitor@yourdomain.com #由于部分SMTP不允许邮件别名,建议此地址与mailx配置的发件人相同。

第109行 mailer = "mailx -t" #将邮件客户端更换为mailx。其中-t参数表示从内容中直接读取邮件头。参考 man mailx

3.手动执行logwatch。等待一会儿,就会收到邮件。如果失败,可以查看系统日志或者在系统中执行mail,查看用户的本地邮件。

 

OT. 在配置Rocks Cluster时若需要让全部节点将日志都使用外部smtp发送邮件,需要手动修改所有节点的上述文件。

[1] http://sourceforge.net/projects/logwatch/

[2] http://dbanotes.net/opensource/logwatch_linux_log.html


python邮件+cron

1,应用范围:在linux系统下的监控程序,根据进程名称监控一些进程或程序,如果某个程序停止运行,则发送一个提醒邮件或者飞信通知。

通过crontab机制,在设定的时间间隔内反复执行监控程序,达到每隔一个时间间隔监控一下进程的运行情况,若有异常则报警。

2,注意事项:

(1)进程列表在一个configurationfile.txt文件中,进程名的格式又/n分隔,如下:

chrome

firefox

.....

(2)通过飞信发送通知短信的前提是发送的对象必须是自己的飞信好友,发送的邮箱和手机号可以再程序中自行修改。

(3)配置crontab,进入etc/crontab 文件,在crontab文件下加一行,注意程序和配置文件的路径都要写绝对路径,我的crontab格式如下

* /10 * * * root python /home/smartbrandnew/Desktop/MonitorProc.py /home/smartbrandnew/Desktop/configurationfile.txt

每10分钟执行一次

3,使用方法:

(1)编辑好configurationfile.txt,进程名的配置文件,在代码中修改发送接收的邮箱号及密码,或者发送接收的飞信号及密码,以及选择发送邮件还是飞信的方式,或者二者皆要。

(2)将MonitorProc.py和configurationfile.txt发在同一个目录下。

(3)配置crontab,重写系统即会在你设定的时间间隔反复执行MonitorProc.py监控程序,监控配置文件里的进程。

===code

#!/usr/bin/env python
# coding: utf-8 


import os
import sys
import time
import re
import psutil
import smtplib 
import cookielib
import urllib
import urllib2


from email.mime.text import MIMEText


def sendMsg(pname):

    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)


    url1 = 'http://quanapi.sinaapp.com/fetion.php?'
    user = 'u=15257117182&' 
    password = 'p=USw5BDdk&'
    to = 'to=15257117182&' #接收人
    message = 'm=' + pname + 'terminated!'


    url = url1 + user + password + to + message


    #url = 'http://quanapi.sinaapp.com/fetion.php?u=15257117182&p=USw5BDdk&to=15257136759&m=terminated'
    res=urllib2.urlopen(url)


def send_mail(pname):


sender = 'smartbrandnew@163.com' #根据需求改变收发件人
receiver = 'smartbrandnew@126.com'
subject = 'monitor warning'
smtpserver = 'smtp.163.com'
username = 'smartbrandnew'#发件人的登录名和密码
password = 'scbj5566'


msg = MIMEText(pname + '<html>  terminated</html>','html','utf-8') 


msg['Subject'] = subject


smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()


def getprocinfo():


proclist = psutil.get_pid_list() #获取系统中所有的进程id


print 'total process num:' + str(len(proclist))#一共的进程数


procnamelist = [] #定义存放所有进程名的list


for i in range(0,len(proclist)-1):


p = psutil.Process(proclist[i])
procnamelist.append(p.name)


return procnamelist


def monitor():


list = getprocinfo()#系统的进程列表


#inputfile = open('configurationfile.txt')#读取配置文件
inputfile = open(sys.argv[1])
pname = [] #存放需要监控的进程名
judge = []#存放为每一个进程判断是否在进程列表中的状态


for line in inputfile.readlines():
line=line.strip('\n') #去掉换行符
pname.append(line)


inputfile.close()

print pname
for j in range(0,len(pname)):


judge.append(pname[j] in list)


if (judge[j] != 0): #如果pname还在pnamelist中

print pname[j] + ' still working!'

else:
print pname[j] + ' terminated!'
send_mail(pname[j]) #可以选择发送邮件还是飞信
#sendMsg(pname[j])  
print judge




def main():


monitor()


if __name__ == "__main__":


main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值