本文共 3556 字,大约阅读时间需要 11 分钟。
一.python日志模块
二.python数据库连接
三.时间处理模块
四.python传递参数给shell
五.发送邮件(py2.7)
六.python计划任务
七.supervisor
/etc/supervisor.conf[program:check]command = python /data/remote_back/checkbackup.pydirectory = /data/remote_backuser = rootroot@GS_TMN_Data:[/data/remote_back]supervisorctl check RUNNING pid 5771, uptime 19:15:25
八.综合实例
#coding:utf-8import threadingimport MySQLdbfrom datetime import datetimeimport time,osimport smtplibfrom email.mime.text import MIMEText#from log import loggerimport loggingdef get_log(): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='myapp.log', filemode='w') logger = logging.getLogger('root') return loggerdef get_con(): host = "120.138.75.88" port = 5849 logsdb = "serverlist" user = "xxxx" password = "xxxxx" con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8") return condef calculate_time(): now = time.mktime(datetime.now().timetuple())-60*2 result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)) return resultdef backup_time(): now = time.mktime(datetime.now().timetuple())-60*2 result = time.strftime('%Y%m%d', time.localtime(now)) backupresult = str(result) return backupresultdef get_data(agent): select_time = calculate_time() get_log().info("select time:"+select_time) sql = 'SELECT COUNT(DISTINCT gamedatadbname) FROM gameserverinfo WHERE agent="%s" and isdelete!=1 ORDER BY zone' %(agent) conn = get_con() cursor = conn.cursor() cursor.execute(sql) results = cursor.fetchall() num1 = results[0] num = num1[0] cursor.close() conn.close() return numdef get_backupdata(agent):# backupnum = os.popen('ls *`date +%Y%m%d04`* |wc -l').read().strip('\n') shijian = '*' + backup_time() + '04' +'*' guize = "%s" %(agent) + shijian os.environ['guize'] = str(guize) backupnum = os.popen('ls $guize |wc -l').read().strip('\n') return backupnum def check(): numdata = int(get_data('ynvng')) backupnum = int(get_backupdata('ynvng')) if numdata == backupnum: result = "备份成功" else: result = "备份失败" return resultdef send_email(content): sender = "lgl15984@163.com" receiver = ["992975991@qq.com","luoguoling@mokylin.com"] host = 'smtp.163.com' port = 465 msg = MIMEText(content) msg['From'] = "lgl15984@163.com" msg['To'] = "992975991@qq.com" msg['Subject'] = "vng backup check" try: smtp = smtplib.SMTP_SSL(host, port) smtp.login(sender, 'xxxx') smtp.sendmail(sender, receiver, msg.as_string()) get_log().info("send email success") except Exception, e: get_log().error(e)# print edef task():# get_data = get_data()# get_backupdata= get_backupdata() while True: data = get_data('ynvng') backupdata= get_backupdata('ynvng') shijian = backup_time() get_log().info("monitor running") results = check() print results get_log().info(results) if results == "备份成功": content = "越南备份成功" else: content = "越南备份失败" print content,data,backupdata send_email(content + " 备份时间:" + shijian + " 应该备份个数: " + str(data) + " 备份个数:" + str(backupdata)) time.sleep(720*60)def run_monitor(): monitor = threading.Thread(target=task) monitor.start() if __name__ == "__main__": run_monitor()
九.运行效果
程序运行效果图
本文转自 luoguo 51CTO博客,原文链接:http://blog.51cto.com/luoguoling/1616617
转载地址:http://lruyx.baihongyu.com/