zoukankan      html  css  js  c++  java
  • Python监控Apache,MySQL

    基于上次的程序修改了,添加了检查Apache服务器的功能。

    import MySQLdb
    import sys
    import os
    import time
    import smtplib
    import socket
    #该程序用于检测MySQL数据库服务器、Apache服务器是否正常服务,服务器当机的话会往目的邮箱发送一封邮件
    #使用说明:
    #1:在服务器数据库上建立数据库test1(可以不含任何表)
    #2:使用“python monitor.py 用户名 密码 Apache服务器IP地址 端口”调用该程序,如果用户名为root,密码为sa,IP为211.87.xxx.xxx,,端口8080。则为python monitor.py root sa 211.87.xxx.xxx 8080
    #也可以使用python monitor.py 用户名 密码来调用,但是需要将程序末尾的默认IP地址和默认端口修改为要监听的服务器
    #3:可通过修改sleep_time来改变监测周期,单位为秒

    sleep_time=60
    ISOTIMEFORMAT='%Y-%m-%d %X'#时间格式
    def conn(user,pwd):
        try:
            conn=MySQLdb.connect(host='localhost',user=user,passwd=pwd,db='test1')
            return 1
        except:
            return 0
    def send(content):
        mail_server='smtp.gmail.com'    #邮件服务器,这里使用gmail
        try:
            s=smtplib.SMTP(mail_server)
            s.starttls()
            s.login('****','*****')#用户名和密码,如果test@gmail.com密码为111.则为s.login('test','111')
            s.sendmail('wangfabo1986@gmail.com','wangfabo1986@gmail.com',content)#参数为发送者,接受者,消息
            s.quit
            print "mail have sended!"
        except:
            print "mail send error!"

    def check_server(address,port,content):
        s=socket.socket()
        #print "attemping to connect to %s on port %s"%(address,port)
        try:
            s.connect((address,port))
    s.send("GET Tomcat HTTP/1.1\r\nHost:211.87.233.233\r\n\r\n")#使用Tomcat服务器测试的,可能需要修改里面内容!!!!!!!!!!!!!
            s.recv(200)
            print "Apache Server connect successed! "+ time.strftime( ISOTIMEFORMAT, time.localtime() )
            return content
        except socket.error,e:
            print "Apache Server connect failed! "+ time.strftime( ISOTIMEFORMAT, time.localtime() )
            return content+"Apache server crashes! "


    def check_DB(user,pwd,content):
        if conn(user,pwd)==1:
    print 'DB Server is OK! '+ time.strftime( ISOTIMEFORMAT, time.localtime() )
            return ''
        else:
    print 'DB Server crashes! '+ time.strftime( ISOTIMEFORMAT, time.localtime() )
    return content+'DB server crashes! '
       
    def monitor(user,pwd,address,port):
        havesend=0
        content=''
        while True:
    time.sleep(sleep_time)
    lastcontent=content
    content=''
            content=check_DB(user,pwd,content)
            content=check_server(address,port,content)
    contentChanged=(not content==lastcontent) and (not content=='')
            if not content=='':
                if havesend==0 or contentChanged:
                    send(content)
                    havesend=1
                else:
                    continue
            else:
                continue
           
        
    if __name__=="__main__":
        if (not len(sys.argv)==3)&(not len(sys.argv)==5):
            print '''usage:DBusername DBpassword Apache_address Apache_port or
            DBusername DBpassword(with default Apache address)
            '''
            sys.exit(1)
        user=sys.argv[1]
        pwd=sys.argv[2]
        if len(sys.argv)==5:
            address=sys.argv[3]
            port=sys.argv[4]
        else:
            address="211.87.***.***"#默认IP地址,可能需要修改
            port=8080               #默认端口地址,可能需要修改
        os.system('cls')
        print "开始监测.....!!!"
        monitor(user,pwd,address,port)

  • 相关阅读:
    二叉树的创建、递归,非递归遍历
    一文读懂PID控制算法(抛弃公式,从原理上真正理解PID控制)
    说走就走的旅行 ——一个人重游长安
    OpenCV笔记:pyrDown()函数和pryUp()函数的使用
    考研心得--一个差劲的ACMer
    HDU 3530 --- Subsequence 单调队列
    POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
    极角排序详解:
    POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
    给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
  • 原文地址:https://www.cnblogs.com/macula7/p/1960695.html
Copyright © 2011-2022 走看看