forked from samuelclay/NewsBlur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtail.py
executable file
·57 lines (50 loc) · 1.73 KB
/
rtail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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()
op.add_option("-i", "--identity", dest="identity")
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)
if options.identity:
s = subprocess.Popen(["ssh", "-i", options.identity, hostname, "tail -f " + path], stdout=subprocess.PIPE)
else:
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 = "[%-5s] %s" % (host.group(1), data)
sys.stdout.write(combination_message)
break
except KeyboardInterrupt:
print " --- End of Logging ---"
if __name__ == "__main__":
main()