zoukankan      html  css  js  c++  java
  • python数据库查询监控

    #!/usr/bin/python2.7
    # encoding: utf-8

    #import MySQLdb as MySQLdb
    import MySQLdb
    import traceback
    import time
    import smtplib
    from email.mime.text import MIMEText

    def getCurrentDateTime():
    now=int(time.time())
    timeArray=time.localtime(now)
    todayDate=time.strftime("%Y-%m-%d",timeArray)
    todayTime=time.strftime("%H:%M:%S",timeArray)
    todayDateTime=time.strftime("%Y-%m-%d %H:%M:%S",timeArray)
    return todayDateTime

    def sendMail(tomail,subject,body):
    HOST="aaa.123.com"
    SUBJECT=subject
    TO=tomail
    FROM="11@aa.com"
    text=body

    msg=MIMEText(text,"plain","utf-8")
    msg['Subject'] = SUBJECT
    msg['From'] = FROM
    msg['To'] = ','.join(TO)

    try:
    server=smtplib.SMTP()
    server.connect(HOST,"25")
    server.starttls()
    server.login("11@aa.com","bbbbbb")
    server.sendmail(FROM,TO,msg.as_string())
    server.quit()
    print "邮件发送成功"
    except Exception,e:
    print "失败"+str(e)

    def connRemoteMysql(*arg):

    ip = arg[0]
    user = arg[1]
    passwd = arg[2]
    port = arg[3]

    try:
    conn = MySQLdb.connect(host=ip,user=user,passwd=passwd,port=port,charset='utf8') #关键字参数
    #cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) #传递参数cursors.DictCursor返回字典结构
    cursor = conn.cursor() #获取操作游标
    conn.select_db('aaa') #得到当前指向数据库的指针
    #sql1 = 'select * from aaa_user ;'
    sql2 = 'select count(*) from bbb_user;'
    cursor.execute(sql2) #执行命令方法
    tuple1 = cursor.fetchall() #接收返回值方法
    tuple2 = tuple1[0]
    result = tuple2[0]
    #print "当前注册用户数为:%s" % r #内置%格式化字符串,
    #print "aaaa:{num}人。".format(num=result) #str.format()字符串处理函数
    #conn.commit()
    return result
    except:
    traceback.print_exc()
    conn.rollback()
    finally:
    cursor.close()
    conn.close()
    if __name__ == "__main__":

    ip = "192.168.11.1"
    user = "aaa"
    passwd = "bbbbb"
    port = 3306
    person = connRemoteMysql(ip,user,passwd,port)

    tomail = ['11@qq.com']
    body = "aaa:{}".format(person)
    subject = "aaa-%s" % getCurrentDateTime()
    sendMail(tomail, subject, body)

  • 相关阅读:
    【ybtoj】【kmp】公共子串
    【ybtoj】【Hash】最大分离度
    【哈希表模板(链前版本)】【ybtoj】【特殊序列】
    【ybtoj】【单调队列】出题方案
    【ybtoj】【单调队列】写博客
    【ybtoj】【线段树】魔法传输
    【ybtoj】【单调队列入门专题】
    OS:3-存储管理
    OS:2-处理器管理
    OS:1-操作系统概观
  • 原文地址:https://www.cnblogs.com/gaoyanbing/p/12581794.html
Copyright © 2011-2022 走看看