db_archive_monitor.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import db_conn
  4. from db_log import db_archive_monitor_log
  5. # 获取日志
  6. logger = db_archive_monitor_log.logger
  7. # 获取数据库连接
  8. db = db_conn.db
  9. # 使用cursor()方法获取操作游标
  10. cursor = db.cursor()
  11. # SQL 查询语句
  12. # 需要归档的任务数量
  13. sql_archive_summary = "select count(*) qty from db_archive_info"
  14. # 归档失败的任务数量
  15. sql_archive_results = "select count(*) qty from vw_db_archive_fail"
  16. try:
  17. # 执行SQL语句
  18. cursor.execute(sql_archive_summary)
  19. # 获取所有记录列表
  20. results_archive_summary = cursor.fetchall()
  21. # 获取需要归档的任务数量
  22. for row in results_archive_summary:
  23. qty_archive_summary = row[0]
  24. #print qty_archive_summary
  25. # 执行SQL语句
  26. cursor.execute(sql_archive_results)
  27. # 获取所有记录列表
  28. results_archive_results = cursor.fetchall()
  29. # 获取归档失败的任务数量
  30. for row in results_archive_results:
  31. qty_archive_results = row[0]
  32. #print qty_archive_results
  33. # 判断是否归档成功
  34. if qty_archive_results == 0:
  35. logger.info("ok\n")
  36. logger.info("=========== DB归档汇总 ===========\n")
  37. logger.info("归档任务:" + str(qty_archive_summary))
  38. logger.info("归档失败:" + str(qty_archive_results))
  39. logger.info("\n=========== DB归档汇总 ===========")
  40. else:
  41. logger.info("false\n")
  42. logger.info("=========== DB归档汇总 ===========\n")
  43. logger.info("归档任务:" + str(qty_archive_summary))
  44. logger.info("归档失败:" + str(qty_archive_results))
  45. logger.info("归档失败详细信息")
  46. # 归档失败信息
  47. sql_select = "select id, substr(server_source,1,20) server_source, db_source, table_source from vw_db_archive_fail"
  48. # 执行SQL语句
  49. cursor.execute(sql_select)
  50. # 获取所有记录列表
  51. results = cursor.fetchall()
  52. for row in results:
  53. id = row[0]
  54. server_source = row[1]
  55. db_source = row[2]
  56. table_source = row[3]
  57. # 打印归档失败详细信息
  58. logger.info("[" + str(id) + "] " + server_source + " " + db_source + " " + table_source )
  59. logger.info("\n=========== DB归档汇总 ===========")
  60. except Exception as e:
  61. print( str(Exception))
  62. print( str(e))
  63. # 关闭游标
  64. cursor.close()
  65. # 关闭数据库连接
  66. db.close()