rotate_mysql_logs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python
  2. #
  3. # Questo script sostituisce quello di CyberPax del 2005.
  4. # Lo script si interfaccia al database mySQL presente sull'host e
  5. # provvede alla rotazione mensile di alcune tabelle.
  6. # Lo script e' pensato per essere inserito in /etc/cron.monthly.
  7. # Essendo python praticamente non ha bisogno di commenti.
  8. #
  9. # blue, Marzo 2014
  10. ######## IMPORT E DEFINIZIONE VARIABILI GLOBALI ##############
  11. from datetime import date, timedelta
  12. #timestamp = '_'+str(date.today().year)+'_'+str(date.today().month-1)
  13. mydate = date.today() - timedelta(1);
  14. timestamp = mydate.strftime('_%Y_%m');
  15. import MySQLdb as mysql
  16. conn = mysql.connect(user='user',passwd='secretpassword',db='logs');
  17. db = conn.cursor()
  18. ######### CREA LA TABELLA table CON IL LAYOUT DESIDERATO #######
  19. def create_log_table(table):
  20. global db
  21. db.execute('CREATE TABLE '+table+' \
  22. ( ID int unsigned not null auto_increment primary key, \
  23. CustomerID bigint, \
  24. ReceivedAt datetime NULL, \
  25. DeviceReportedTime datetime NULL, \
  26. Facility smallint NULL, \
  27. Priority smallint NULL, \
  28. FromHost varchar(60) NULL, \
  29. Message text, \
  30. NTSeverity int NULL, \
  31. Importance int NULL, \
  32. EventSource varchar(60), \
  33. EventUser varchar(60) NULL, \
  34. EventCategory int NULL, \
  35. EventID int NULL, \
  36. EventBinaryData text NULL, \
  37. MaxAvailable int NULL, \
  38. CurrUsage int NULL, \
  39. MinUsage int NULL, \
  40. MaxUsage int NULL, \
  41. InfoUnitID int NULL , \
  42. SysLogTag varchar(60), \
  43. EventLogType varchar(60), \
  44. GenericFileName VarChar(60), \
  45. SystemID int NULL, \
  46. Checksum int NULL, \
  47. ProcessID varchar(60) \
  48. )')
  49. #### RUOTA LA TABELLA table ####
  50. def rotate_log_table(table):
  51. global db
  52. try:
  53. create_log_table('newtable')
  54. db.execute('rename table '+table+' to '+table+timestamp)
  55. db.execute('rename table newtable to '+table)
  56. except mysql.OperationalError,e:
  57. print('could not rotate tables: '+str(e))
  58. try:
  59. db.execute('drop table newtable');
  60. except mysql.OperationalError:
  61. pass
  62. exit(1)
  63. ####################### MAIN PROGRAM #####################
  64. for i in ('lcm1','lcm2','laur','server','switches','videoconf','cuda'):
  65. rotate_log_table(i)
  66. conn.commit()