利用python 监控告警 nginx,mysql 进程

这个是进程nginx的,大家可以修改下里面的 ps -C 进程名

  
  
  
  
  1. #!/usr/bin/env python  

  2. import os, sys, time  

  3. while True:  

  4.    time.sleep(3)  

  5.    try:  

  6. ret = os.popen('ps -C nginx -o pid,cmd').readlines()  

  7.        if len(ret) <2:

  8.            print "nginx process killed, restarting service in 3 seconds."  

  9.            time.sleep(3)  

  10.            os.system("service nginx restart")  

  11.    except:  

  12.        print "Error", sys.exc_info()[1]  

邮件的可以用话smtplib发送

  
  
  
  
  1. #!/usr/bin/python

  2. # -*- coding: utf-8 -*-

  3. import smtplib

  4. from email.MIMEText import MIMEText

  5. from email.MIMEMultipart import MIMEMultipart

  6. def send_mail(to, sub, content, from_email, mail_pass, filelist = []):

  7.    '''

  8.    to:发给谁

  9.    sub:主题

  10.    content:内容

  11.    from_email:登录邮箱

  12.    mail_pass:登录密码

  13.    filelist:附件列表,文件路径

  14.    send_mail("[email protected]","the5fire","welcome to the5fire.net","[email protected]","xxxxxx")

  15.    '''

  16. mail_postfix = from_email.split('@')[1]

  17. mail_host="smtp.%s" % (mail_postfix,)

  18. mail_user= from_email.split('@')[0]

  19. me=mail_user+"<"+mail_user+"@"+mail_postfix+">"

  20. msgRoot = MIMEMultipart('related')

  21.    msgRoot['Subject'] = sub.encode('gbk')

  22.    msgRoot['Form'] = me

  23.    msgRoot['To'] = to

  24. msgRoot.preamble = 'this is a multi-part message IN MIME format'

  25. msgAlternative = MIMEMultipart('alternative')

  26.    msgRoot.attach(msgAlternative)

  27. msgText = MIMEText(content, 'html','gbk')

  28.    msgAlternative.attach(msgText)

  29.    for onefile in filelist:

  30. att = MIMEText(open(onefile,'rb').read(),'base64','gb2312')

  31.        att["Content-Type"] = 'application/octet-stream'

  32.        att["Content-Disposition"] = 'attachment;filename=%s' % onefile

  33.        msgAlternative.attach(att)

  34. message = msgRoot.as_string()

  35.    try:

  36. s = smtplib.SMTP()

  37.        try:

  38.            s.connect(mail_host)

  39.        except Exception,e:

  40.            print str(e)

  41.        s.starttls()

  42.        s.login(mail_user,mail_pass)

  43.        s.sendmail(me, to, message)

  44.        s.close()

  45.        return True

  46.    except Exception, e:

  47.        print str(e)

  48.        return False


你可能感兴趣的:(python,python,监控,nginx监控,python进程)