*nix tail -f multiple log files with Thread

D

David

Hi everyone,

I copied a program from C to track multiple log files. I would like to
be able to print a label when a log file is updated. Here is the program;

<code>
#!/usr/bin/python
from threading import Thread
import subprocess
from Queue import Queue

num_threads = 3
queue = Queue()
logfiles = ["/var/log/messages",
"/var/log/apache2/access_log",
"/var/log/apache2/error_log"]

def logtailer(i, q,):
while True:
lfile = q.get()
sudo = "sudo"
tail = "tail"
arg = "-f"
ret = subprocess.call([sudo, tail, arg, lfile])
q.task_done()

for i in range(num_threads):
worker = Thread(target=logtailer, args=(i, queue))
worker.setDaemon(True)
worker.start()

for lfile in logfiles:
queue.put(lfile)

queue.join()
</code>

And here is a sample of the output;

[Fri Feb 13 08:58:48 2009] [error] [client 127.0.0.1] File does not
exist: /var/www/localhost/htdocs/moodle/favicon.ico
[Fri Feb 13 08:58:51 2009] [error] [client 127.0.0.1] File does not
exist: /var/www/localhost/htdocs/moodle/favicon.ico
Feb 13 09:02:26 opteron sudo: david : TTY=pts/4 ; PWD=/home/david ;
USER=root ; COMMAND=/bin/tail -f /var/log/apache2/error_log
Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session opened for
user root by david(uid=0)
Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session closed for
user root
Feb 13 09:10:01 opteron cron[10633]: (root) CMD (test -x
/usr/sbin/run-crons && /usr/sbin/run-crons )
Feb 13 09:18:33 opteron su[10678]: Successful su for root by david
Feb 13 09:18:33 opteron su[10678]: + pts/6 david:root
Feb 13 09:18:33 opteron su[10678]: pam_unix(su:session): session opened
for user root by david(uid=1000)
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/theme/custom_corners/favicon.ico HTTP/1.1" 200 894
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/lib/speller/spellChecker.js HTTP/1.1" 200 15980
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET / HTTP/1.1" 200 13718
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/theme/custom_corners/styles.php HTTP/1.1" 200 30709

I would like to be able to add a label like;

<error_log>
[Fri Feb 13 08:58:48 2009] [error] [client 127.0.0.1] File does not
exist: /var/www/localhost/htdocs/moodle/favicon.ico
[Fri Feb 13 08:58:51 2009] [error] [client 127.0.0.1] File does not
exist: /var/www/localhost/htdocs/moodle/favicon.ico
<syslog>
Feb 13 09:02:26 opteron sudo: david : TTY=pts/4 ; PWD=/home/david ;
USER=root ; COMMAND=/bin/tail -f /var/log/apache2/error_log
Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session opened for
user root by david(uid=0)
Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session closed for
user root
Feb 13 09:10:01 opteron cron[10633]: (root) CMD (test -x
/usr/sbin/run-crons && /usr/sbin/run-crons )
Feb 13 09:18:33 opteron su[10678]: Successful su for root by david
Feb 13 09:18:33 opteron su[10678]: + pts/6 david:root
Feb 13 09:18:33 opteron su[10678]: pam_unix(su:session): session opened
for user root by david(uid=1000)
<access_log>
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/theme/custom_corners/favicon.ico HTTP/1.1" 200 894
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/lib/speller/spellChecker.js HTTP/1.1" 200 15980
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET / HTTP/1.1" 200 13718
127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET
/theme/custom_corners/styles.php HTTP/1.1" 200 30709

I can create the label like this;
def label()
print "<%s\n>" % lfile

But I have no idea how to get it to work when a thread is updated.
thanks
-david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,026
Latest member
TishaMadri

Latest Threads

Top