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)

转载于:https://www.cnblogs.com/macula7/archive/2010/01/24/1960535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值