]>
Commit | Line | Data |
---|---|---|
70b99f2f | 1 | #!/usr/bin/env python3 |
5417cc2d CH |
2 | # |
3 | # Copyright (c) 2021, LabN Consulting, L.L.C. | |
4 | # | |
5 | # This program is free software; you can redistribute it and/or | |
6 | # modify it under the terms of the GNU General Public License | |
7 | # as published by the Free Software Foundation; either version 2 | |
8 | # of the License, or (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License along | |
16 | # with this program; see the file COPYING; if not, write to the Free Software | |
17 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | # | |
11c83aa4 | 19 | |
e60eb780 SMS |
20 | ######################################################## |
21 | ### Python Script to generate the FRR support bundle ### | |
22 | ######################################################## | |
5417cc2d CH |
23 | import argparse |
24 | import logging | |
e60eb780 SMS |
25 | import os |
26 | import subprocess | |
5417cc2d CH |
27 | import tempfile |
28 | ||
667cfdc8 | 29 | |
5417cc2d CH |
30 | def open_with_backup(path): |
31 | if os.path.exists(path): | |
32 | print("Making backup of " + path) | |
b321edc5 | 33 | subprocess.check_call("mv {0} {0}.prev".format(path), shell=True) |
5417cc2d CH |
34 | return open(path, "w") |
35 | ||
667cfdc8 | 36 | |
5417cc2d CH |
37 | def main(): |
38 | parser = argparse.ArgumentParser() | |
667cfdc8 DS |
39 | parser.add_argument( |
40 | "-c", | |
41 | "--config", | |
42 | default="/etc/frr/support_bundle_commands.conf", | |
43 | help="input config", | |
44 | ) | |
45 | parser.add_argument( | |
46 | "-l", "--log-dir", default="/var/log/frr", help="directory for logfiles" | |
47 | ) | |
5417cc2d CH |
48 | args = parser.parse_args() |
49 | ||
667cfdc8 | 50 | collecting = False # file format has sentinels (seem superfluous) |
5417cc2d CH |
51 | proc_cmds = {} |
52 | proc = None | |
53 | temp = None | |
54 | ||
55 | # Collect all the commands for each daemon | |
e60eb780 | 56 | try: |
5417cc2d CH |
57 | for line in open(args.config): |
58 | line = line.rstrip() | |
59 | if len(line) == 0 or line[0] == "#": | |
60 | continue | |
61 | ||
62 | cmd_line = line.split(":") | |
63 | if cmd_line[0] == "PROC_NAME": | |
64 | proc = cmd_line[1] | |
65 | temp = tempfile.NamedTemporaryFile("w+") | |
66 | collecting = False | |
67 | elif cmd_line[0] == "CMD_LIST_START": | |
68 | collecting = True | |
69 | elif cmd_line[0] == "CMD_LIST_END": | |
70 | collecting = False | |
71 | temp.flush() | |
72 | proc_cmds[proc] = open(temp.name) | |
73 | temp.close() | |
74 | elif collecting: | |
75 | temp.write(line + "\n") | |
701a0192 | 76 | else: |
5417cc2d CH |
77 | print("Ignoring unexpected input " + line.rstrip()) |
78 | except IOError as error: | |
79 | logging.fatal("Cannot read config file: %s: %s", args.config, str(error)) | |
80 | return | |
701a0192 | 81 | |
5417cc2d CH |
82 | # Spawn a vtysh to fetch each set of commands |
83 | procs = [] | |
84 | for proc in proc_cmds: | |
85 | ofn = os.path.join(args.log_dir, proc + "_support_bundle.log") | |
86 | p = subprocess.Popen( | |
87 | ["/usr/bin/env", "vtysh", "-t"], | |
88 | stdin=proc_cmds[proc], | |
89 | stdout=open_with_backup(ofn), | |
90 | stderr=subprocess.STDOUT, | |
91 | ) | |
92 | procs.append(p) | |
93 | ||
94 | for p in procs: | |
95 | p.wait() | |
96 | ||
667cfdc8 | 97 | |
5417cc2d CH |
98 | if __name__ == "__main__": |
99 | main() |