obsolete /var/www/html/generated removed - add placeholder for the plkit package
[myplc.git] / mtail.py
1 #!/usr/bin/env python
2
3 '''
4 Does tail -f on log files in a given directory. 
5 The display is in chronological order of the logged lines, 
6 given that the first column of log files is timestamp.
7 It can be altered to fit other formats too
8 '''
9
10 import os, sys, time
11 from optparse import OptionParser
12
13 class mtail:
14
15     subversion_id = "$Id$"
16
17     default_time_format = "%H:%M:%S"
18     
19     def __init__ (self, args ):
20         
21         # internal structure for tracking changes
22         self.files = {}
23         # parse command-line args : will set options and args
24         self.parse_args(args)
25         # initialize 
26         self.scan_files()
27
28     def parse_args (self, args):
29         usage = """usage: %prog [options] file-or-dir ...
30 example: 
31 # %prog -e '*access*' /var/log"""
32         parser=OptionParser(usage=usage,version=self.subversion_id)
33         # tail_period
34         parser.add_option("-p","--period", type="int", dest="tail_period", default=1,
35                           help="Files check period in seconds")
36         # rescan_period
37         parser.add_option("-d","--dir-period", type="int", dest="rescan_period", default=20,
38                           help="Directories rescan period in seconds")
39         # time format
40         parser.add_option("-f","--format", dest="time_format", default=mtail.default_time_format,
41                           help="Time format, defaults to " + mtail.default_time_format)
42         # show time
43         parser.add_option("-r","--raw", action="store_true", dest="show_time", default=True,
44                           help="Suppresses time display")
45
46         # note for exclusion patterns 
47         parser.add_option("-e","--exclude", action="append", dest="excludes", default=[],
48                           help="Exclusion pattern  -- can be specified multiple times applies on files not explicitly mentioned on the command-line")
49
50         parser.add_option("-u","--usual",action="store_true",dest="plc_mode",default=False,
51                           help="Shortcut for watching /var/log with default settings")
52
53         # verbosity
54         parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
55                           help="Run in verbose mode")
56
57         (self.options, self.args) = parser.parse_args(args)
58         self.optparse = parser
59
60         ### plc shortcuts
61         if self.options.plc_mode:
62             # monitor all files in /var/log with some exceptions
63             self.options.excludes.append('*access_log')
64             self.options.excludes.append('*request_log')
65             self.options.excludes.append('*.swp')
66             self.args.append('/var/log')
67             # watch the postgresql logs as well
68             self.args.append('/var/lib/pgsql/data/pg_log')
69
70         if self.options.verbose:
71             print 'Version:',self.subversion_id
72             print 'Options:',self.options
73             print 'Arguments:',self.args
74
75     def file_size (self,filename):
76         return os.stat(filename)[6]
77                 
78     def number_files (self):
79         return len(self.files)
80
81     # scans given arguments, and updates files accordingly
82     # can be run several times
83     def scan_files (self) :
84
85         if self.options.verbose:
86             print 'entering scan_files, files=',self.files
87
88         # mark entries in files as pre-existing
89         for key in self.files:
90             self.files[key]['old-file']=True
91
92         # refreshes the proper set of filenames
93         filenames = []
94         for arg in self.args:
95             if self.options.verbose:
96                 print 'scan_files -- Considering arg',arg
97             if os.path.isfile (arg):
98                 filenames += [ arg ]
99             elif os.path.isdir (arg) :
100                 filenames += self.walk (arg)
101             else:
102                 print "mtail : no such file or directory %s -- ignored"%arg
103
104         # updates files
105         for filename in filenames :
106             # known file
107             if self.files.has_key(filename):
108                 size = self.file_size(filename)
109                 offset = self.files[filename]['size']
110                 if size > offset:
111                     self.show_file_end(filename,offset,size)
112                     self.files[filename]['size']=size
113                 elif size < offset:
114                     self.show_file_when_size_decreased(filename,offset,size)
115                 try:
116                     del self.files[filename]['old-file']
117                 except:
118                     pass
119             else:
120                 # enter file with current size
121                 # if we didn't set format yet, it's because we are initializing
122                 try:
123                     self.format
124                     self.show_now()
125                     print self.format%filename,"new file"
126                     self.show_file_end(filename,0,self.file_size(filename))
127                 except:
128                     pass
129                 self.files[filename]={'size':self.file_size(filename)}
130         
131         # cleanup 
132         # avoid side-effects on the current loop basis
133         read_filenames = self.files.keys()
134         for filename in read_filenames:
135             if self.files[filename].has_key('old-file'):
136                 self.show_now()
137                 print self.format%filename,"file has gone"
138                 del self.files[filename]
139
140         # compute margin and format
141         if not filenames:
142             print sys.argv[0],": WARNING : no file in scope"
143             self.format="%s"
144         else:
145             if len(filenames)==1:
146                 self.margin=len(filenames[0])
147             else:
148                 # this stupidly fails when there's only 1 file
149                 self.margin=max(*[len(f) for f in filenames])
150             self.format="%%%ds"%self.margin
151             if self.options.verbose:
152                 print 'Current set of files:',filenames
153
154     def tail_files (self):
155
156         if self.options.verbose:
157             print 'tail_files'
158         for filename in self.files:
159             size = self.file_size(filename)
160             offset = self.files[filename]['size']
161             if size != offset:
162                 self.show_file_end(filename,offset,size)
163                 self.files[filename]['size']=size
164
165     def show_now (self):
166         if self.options.show_time:
167             label=time.strftime(self.options.time_format,time.localtime())
168             print label,
169
170     def show_file_end (self, filename, offset, size):
171         file = open(filename,"r")
172         file.seek(offset)
173         line=file.read(size-offset)
174         self.show_now()
175         print self.format%filename,'----------------------------------------'
176         print line
177         file.close()
178
179     def show_file_when_size_decreased (self, filename, offset, size):
180         print self.format%filename,'---------- file size decreased ---------', 
181         if self.options.verbose:
182             print 'size during last check',offset,'current size',size
183         else:
184             print ''
185
186     # get all files under a directory
187     def walk ( self, root ):
188         import fnmatch, os, string
189         
190         # initialize
191         result = []
192
193         # must have at least root folder
194         try:
195             names = os.listdir(root)
196         except os.error:
197             return result
198
199         # check each file
200         for name in names:
201             fullname = os.path.normpath(os.path.join(root, name))
202
203             # a file : check for excluded, otherwise append
204             if os.path.isfile(fullname):
205                 try:
206                     for exclude in self.options.excludes:
207                         if fnmatch.fnmatch(name, exclude):
208                             raise Exception('excluded')
209                     result.append(fullname)
210                 except:
211                     pass
212             # a dir : let's recurse - avoid symlinks for anti-loop
213             elif os.path.isdir(fullname) and not os.path.islink(fullname):
214                 result = result + self.walk( fullname )
215
216         return result
217
218     def run (self):
219
220         if self.number_files() == 0:
221             self.optparse.print_help()
222             sys.exit(1)
223         counter = 0
224     
225         while 1:
226             ## hit the period ?
227             # dont do this twice at startup
228             if (counter !=0 and counter % self.options.rescan_period == 0):
229                 self.scan_files()
230
231             if (counter % self.options.tail_period == 0):
232                 self.tail_files()
233
234             time.sleep(1)
235             counter += 1
236
237 ###
238 if __name__ == '__main__':
239     mtail (sys.argv[1:]).run()