]>
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 | ||
29 | def open_with_backup(path): | |
30 | if os.path.exists(path): | |
31 | print("Making backup of " + path) | |
32 | subprocess.check_call("mv {0} {0}.prev".format(path)) | |
33 | return open(path, "w") | |
34 | ||
35 | def main(): | |
36 | parser = argparse.ArgumentParser() | |
37 | parser.add_argument("-c", "--config", default="/etc/frr/support_bundle_commands.conf", help="input config") | |
38 | parser.add_argument("-l", "--log-dir", default="/var/log/frr", help="directory for logfiles") | |
39 | args = parser.parse_args() | |
40 | ||
41 | collecting = False # file format has sentinels (seem superfluous) | |
42 | proc_cmds = {} | |
43 | proc = None | |
44 | temp = None | |
45 | ||
46 | # Collect all the commands for each daemon | |
e60eb780 | 47 | try: |
5417cc2d CH |
48 | for line in open(args.config): |
49 | line = line.rstrip() | |
50 | if len(line) == 0 or line[0] == "#": | |
51 | continue | |
52 | ||
53 | cmd_line = line.split(":") | |
54 | if cmd_line[0] == "PROC_NAME": | |
55 | proc = cmd_line[1] | |
56 | temp = tempfile.NamedTemporaryFile("w+") | |
57 | collecting = False | |
58 | elif cmd_line[0] == "CMD_LIST_START": | |
59 | collecting = True | |
60 | elif cmd_line[0] == "CMD_LIST_END": | |
61 | collecting = False | |
62 | temp.flush() | |
63 | proc_cmds[proc] = open(temp.name) | |
64 | temp.close() | |
65 | elif collecting: | |
66 | temp.write(line + "\n") | |
701a0192 | 67 | else: |
5417cc2d CH |
68 | print("Ignoring unexpected input " + line.rstrip()) |
69 | except IOError as error: | |
70 | logging.fatal("Cannot read config file: %s: %s", args.config, str(error)) | |
71 | return | |
701a0192 | 72 | |
5417cc2d CH |
73 | # Spawn a vtysh to fetch each set of commands |
74 | procs = [] | |
75 | for proc in proc_cmds: | |
76 | ofn = os.path.join(args.log_dir, proc + "_support_bundle.log") | |
77 | p = subprocess.Popen( | |
78 | ["/usr/bin/env", "vtysh", "-t"], | |
79 | stdin=proc_cmds[proc], | |
80 | stdout=open_with_backup(ofn), | |
81 | stderr=subprocess.STDOUT, | |
82 | ) | |
83 | procs.append(p) | |
84 | ||
85 | for p in procs: | |
86 | p.wait() | |
87 | ||
88 | if __name__ == "__main__": | |
89 | main() |