Jump to content
newbeen

[Python] Log size control

Recommended Posts

newbeen

Outro dia aqui na firma comecei a ter problemas com programas que criam log selvaticamente então fiz este programa que partilho aqui com vocês e só criar um ficheiro chamado loglist.data com o caminho para os log's que desejam "controlar" :D

import os, bz2, sys

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Log Size Control 100% cross platform (windows/linux);   #
#               Take the files for array                  #
#               Check the size if its bigger them         #
#               max_size compress to bz2 file             #
#                                                         #
#   Made by Rui Gomes - 29-07-2008                        #
#                                                         #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

max_size=20000  #define the max size of the log in kb

try:
   logs=open("loglist.data", "r").readlines() #catch the data in the data file
except:
   print "Its not possible read the config file loglist.data" #if the file its not present or dont have rights to read him
   sys.exit(1)

for log in logs:
   if os.path.isfile(log): #if the line in the loglist.data its a file     

       log=log.strip("\n") #Take out the change line character
       log=os.path.normpath(log) #make it easy you can put / or \ gonna always work in both systems (windows/linux)
       size=os.path.getsize(log)#get the size of the file
       sizekb=size/1024 #for more precision use sizekb=float(size)/1024

       if sizekb >= max_size: #test if the file its big or not...

           try:  #test if the file its free

               os.rename(log, "%s.bck"%log) #rename the file log
               uncompress_data=open("%s.bck"%log, "rb").read()
               compress_output=bz2.compress(uncompress_data) #compress the renamed file

               file_output=open(log+".bz", 'wb') #
               file_output.write(compress_output)# create the compress file of the log
               file_output.close()               #

               os.unlink("%s.bck"%log) #remove the backup file

           except IOError:

               print "Error file lock"
               pass #if the file its not free skip the compression to the next time, and move to the next file

           except:
               print "Unknown Error" #some vodoo happen!!!
               pass #go to the next file

   else:#if the line in the loglist.data its not a file 
       print "%s -> This file dont exist\n" %log

Código disponível na wiki em http://wiki.portugal-a-programar.pt/dev_geral:python:snippet:logs_size_control


RHCE - 120-062-534

Share this post


Link to post
Share on other sites
newbeen

O problema são os log não genericos criados por scripts já feitos por outras pessoas que trabalharam aqui e de outras tralhas como o nagios que tem meia dúzia de scripts feitos a mão em perl que criam log também, um deles tinha 800 e tal mgs :S

EDIT: nice nao conhecia o logrotate apenas o syslog


RHCE - 120-062-534

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.