db_archive_report_weekly.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import db_conn
  4. import os
  5. import time
  6. import smtp_config
  7. from email.mime.text import MIMEText
  8. from email.header import Header
  9. def send_mail(mail_msg):
  10. # 调用send_mail函数
  11. mail_body = """
  12. <style type="text/css">
  13. table.gridtable {
  14. font-family: verdana,arial,sans-serif;
  15. font-size:11px;
  16. color:#333333;
  17. border-width: 1px;
  18. border-color: #666666;
  19. border-collapse: collapse;
  20. }
  21. table.gridtable th {
  22. border-width: 1px;
  23. padding: 8px;
  24. border-style: solid;
  25. border-color: #666666;
  26. background-color: #dedede;
  27. }
  28. table.gridtable td {
  29. border-width: 1px;
  30. padding: 8px;
  31. border-style: solid;
  32. border-color: #666666;
  33. background-color: #ffffff;
  34. }
  35. </style>
  36. <!-- Table goes in the document BODY -->
  37. <table class="gridtable">
  38. <tr>
  39. <th>序号</th><th>归档周次</th><th>归档日期-起</th><th>归档日期-止</th>
  40. <th>服务器</th><th>数据库</th><th>表名</th><th>归档成功</th><th>归档失败</th>
  41. <th >总归档数据量(行)</th><th>总归档时长(分钟)</th><th>平均归档数据量(行)</th><th>平均归档时长(分钟)</th>
  42. </tr>
  43. """
  44. mail_body = mail_body + mail_msg + "</table>"
  45. message = MIMEText(mail_body, 'html', 'utf-8')
  46. subject = 'DB归档周报'
  47. message['Subject'] = Header(subject, 'utf-8')
  48. smtp_config.send_mail(message)
  49. return
  50. #定义邮件体变量
  51. mail_msg = ""
  52. # 获取数据库连接
  53. db = db_conn.db
  54. # 使用cursor()方法获取操作游标
  55. cursor = db.cursor()
  56. # SQL 查询语句
  57. # 备份日报
  58. sql_archive_report = "select cast((@i:= @i+1) as char) as id, archive_week, archive_week_start, archive_week_end, " \
  59. "substr(server_source,1,15) server_source, db_source, table_source, archive_success_qty, archive_fail_qty, " \
  60. "archive_sum_data, archive_sum_minute, archive_avg_data, archive_avg_minute " \
  61. "from vw_db_archive_report_weekly t, (select @i:=0) as a"
  62. try:
  63. # 执行SQL语句
  64. cursor.execute(sql_archive_report)
  65. # 获取所有记录列表
  66. results = cursor.fetchall()
  67. for row in results:
  68. id = str(row[0])
  69. archive_week = str(row[1])
  70. archive_week_start = str(row[2])
  71. archive_week_end = str(row[3])
  72. server_source = str(row[4])
  73. db_source = str(row[5])
  74. table_source = str(row[6])
  75. archive_success_qty = str(row[7])
  76. archive_fail_qty = str(row[8])
  77. archive_sum_data = str(row[9])
  78. archive_sum_minute = str(row[10])
  79. archive_avg_data = str(row[11])
  80. archive_avg_minute = str(row[12])
  81. # 生成邮件内容
  82. mail_msg_single = """
  83. <tr>
  84. <td align="center">%s</td><td>%s</td><td>%s</td><td>%s</td>
  85. <td>%s</td><td>%s</td><td>%s</td><td align="right">%s</td><td align="right">%s</td>
  86. <td align="right">%s</td><td align="right">%s</td><td align="right">%s</td><td align="right">%s</td>
  87. </tr> """ % \
  88. (id, archive_week, archive_week_start, archive_week_end, server_source, db_source, table_source,
  89. archive_success_qty, archive_fail_qty, archive_sum_data, archive_sum_minute, archive_avg_data, archive_avg_minute)
  90. mail_msg = mail_msg + mail_msg_single
  91. # SQL 插入语句
  92. sql_insert = "insert into db_archive_report_weekly(archive_week, archive_week_start, archive_week_end, server_source, db_source, " \
  93. "table_source, archive_success_qty, archive_fail_qty, archive_sum_data, archive_sum_minute, archive_avg_data, archive_avg_minute) " \
  94. "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % \
  95. (archive_week, archive_week_start, archive_week_end, server_source, db_source,table_source, archive_success_qty, archive_fail_qty, archive_sum_data,
  96. archive_sum_minute, archive_avg_data, archive_avg_minute)
  97. # 执行sql语句
  98. cursor.execute(sql_insert)
  99. # 提交到数据库执行
  100. db.commit()
  101. # 发送邮件
  102. send_mail(mail_msg)
  103. except Exception as e:
  104. print (str(Exception))
  105. print (str(e))
  106. # 关闭游标
  107. cursor.close()
  108. # 关闭数据库连接
  109. db.close()