summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/exa-receive.py
blob: 2ea3a75a5f2751bf316fd5b79db613c01ad75117 (plain)
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
#!/usr/bin/env python3

"""
exa-receive.py: Save received routes form ExaBGP into file
"""

import argparse
import os
from sys import stdin
from datetime import datetime

parser = argparse.ArgumentParser()
parser.add_argument(
    "--no-timestamp", dest="timestamp", action="store_false", help="Disable timestamps"
)
parser.add_argument(
    "--logdir", default="/tmp/gearlogdir", help="The directory to store the peer log in"
)
parser.add_argument("peer", type=int, help="The peer number")
args = parser.parse_args()

savepath = os.path.join(args.logdir, "peer{}-received.log".format(args.peer))
routesavefile = open(savepath, "w")

while True:
    try:
        line = stdin.readline()
        if not line:
            break

        if not args.timestamp:
            routesavefile.write(line)
        else:
            timestamp = datetime.now().strftime("%Y%m%d_%H:%M:%S - ")
            routesavefile.write(timestamp + line)
        routesavefile.flush()
    except KeyboardInterrupt:
        pass
    except IOError:
        # most likely a signal during readline
        pass

routesavefile.close()