-
Notifications
You must be signed in to change notification settings - Fork 0
/
nettail.py
68 lines (53 loc) · 1.88 KB
/
nettail.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
58
59
60
61
62
63
64
65
66
67
68
import requests
from time import sleep
from datetime import datetime
import sys
import config
log = None
def inform(msg):
print msg
log.write(msg.encode('utf-8') + '\n')
def main(argv=None):
global log
# standard template for allowing testing of the main() function
# but note we don't actually use command line arguments right now
if not argv:
argv = sys.argv
log = open('logs/nettail_' + datetime.strftime(
datetime.now(), '%Y-%m-%d') + '.log',
'wb') # overwrite the nettail daily log
try:
process_log()
except BaseException:
# You shouldn't catch BaseException. Seriously, don't do it.
# But this quickly / dirtily writes down the current log when
# the script is interrupted, e.g. with Ctrl+C.
# Which is the main usecase...
log.close()
raise
log.close()
def process_log():
last_log = ''
while True:
r = requests.get(config.LOG_LOCATION, auth=config.BASIC_AUTH)
cur_log = r.text
# We only want to print the changed lines for this to be a true
# "tail" utility. We go over the last log, removing all of its
# lines from the current log - pretty inefficient and painful,
# but works.
if cur_log != last_log:
cur_log_lines = cur_log.splitlines()
last_log_lines = last_log.splitlines()
for line in last_log_lines:
try:
cur_log_lines.remove(line)
except ValueError: # the last log has lines which the
# current one does not, probably
# router returned an error HTML doc
pass
for line in cur_log_lines:
inform(line)
last_log = cur_log
sleep(config.WAIT)
if __name__ == '__main__':
main()