mirror of
https://github.com/viq/NewsBlur.git
synced 2025-08-05 16:49:45 +00:00
Now that I've moved to multiple app servers, gotta log multiple server logs. Rtail to the rescue.
This commit is contained in:
parent
a17ffd5fae
commit
02c4731fd8
1 changed files with 53 additions and 0 deletions
53
utils/rtail.py
Executable file
53
utils/rtail.py
Executable file
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
./rtail.py user@host:path/foo.log bar.log host2:/path/baz.log
|
||||||
|
"""
|
||||||
|
|
||||||
|
import optparse
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import select
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
op = optparse.OptionParser()
|
||||||
|
options, args = op.parse_args()
|
||||||
|
streams = list()
|
||||||
|
for arg in args:
|
||||||
|
if re.match(r"^(.+@)?[a-zA-Z0-9.-]+:.+", arg):
|
||||||
|
# this is a remote location
|
||||||
|
hostname, path = arg.split(":", 1)
|
||||||
|
s = subprocess.Popen(["ssh", hostname, "tail -f " + path], stdout=subprocess.PIPE)
|
||||||
|
s.name = arg
|
||||||
|
streams.append(s)
|
||||||
|
else:
|
||||||
|
s = subprocess.Popen(["tail", "-f", arg], stdout=subprocess.PIPE)
|
||||||
|
s.name = arg
|
||||||
|
streams.append(s)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
r, _, _ = select.select(
|
||||||
|
[stream.stdout.fileno() for stream in streams], [], [])
|
||||||
|
for fileno in r:
|
||||||
|
for stream in streams:
|
||||||
|
if stream.stdout.fileno() != fileno:
|
||||||
|
continue
|
||||||
|
data = os.read(fileno, 4096)
|
||||||
|
if not data:
|
||||||
|
streams.remove(stream)
|
||||||
|
break
|
||||||
|
host = re.match(r'^(.*?)\.', stream.name)
|
||||||
|
combination_message = "[%s] %s" % (host.group(1), data)
|
||||||
|
sys.stdout.write(combination_message)
|
||||||
|
break
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print " --- End of Logging ---"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Add table
Reference in a new issue