]>
Commit | Line | Data |
---|---|---|
70b99f2f | 1 | #!/usr/bin/env python3 |
11c83aa4 | 2 | |
e60eb780 SMS |
3 | ######################################################## |
4 | ### Python Script to generate the FRR support bundle ### | |
5 | ######################################################## | |
6 | import os | |
7 | import subprocess | |
8 | import datetime | |
9 | ||
701a0192 | 10 | ETC_DIR = "/etc/frr/" |
11 | LOG_DIR = "/var/log/frr/" | |
e60eb780 SMS |
12 | SUCCESS = 1 |
13 | FAIL = 0 | |
14 | ||
15 | inputFile = ETC_DIR + "support_bundle_commands.conf" | |
16 | ||
e60eb780 SMS |
17 | # Create the output file name |
18 | def createOutputFile(procName): | |
701a0192 | 19 | fileName = procName + "_support_bundle.log" |
20 | oldFile = LOG_DIR + fileName | |
21 | cpFileCmd = "cp " + oldFile + " " + oldFile + ".prev" | |
22 | rmFileCmd = "rm -rf " + oldFile | |
23 | print("Making backup of " + oldFile) | |
24 | os.system(cpFileCmd) | |
25 | print("Removing " + oldFile) | |
26 | os.system(rmFileCmd) | |
27 | return fileName | |
28 | ||
e60eb780 SMS |
29 | |
30 | # Open the output file for this process | |
31 | def openOutputFile(fileName): | |
701a0192 | 32 | crt_file_cmd = LOG_DIR + fileName |
33 | print(crt_file_cmd) | |
34 | try: | |
35 | outputFile = open(crt_file_cmd, "w") | |
36 | return outputFile | |
37 | except IOError: | |
38 | return () | |
39 | ||
e60eb780 SMS |
40 | |
41 | # Close the output file for this process | |
70b99f2f | 42 | def closeOutputFile(f): |
701a0192 | 43 | try: |
70b99f2f | 44 | f.close() |
701a0192 | 45 | return SUCCESS |
46 | except IOError: | |
47 | return FAIL | |
48 | ||
e60eb780 SMS |
49 | |
50 | # Execute the command over vtysh and store in the | |
51 | # output file | |
52 | def executeCommand(cmd, outputFile): | |
701a0192 | 53 | cmd_exec_str = 'vtysh -c "' + cmd + '" ' |
e60eb780 | 54 | try: |
701a0192 | 55 | cmd_output = subprocess.check_output(cmd_exec_str, shell=True) |
56 | try: | |
57 | dateTime = datetime.datetime.now() | |
58 | outputFile.write(">>[" + str(dateTime) + "]" + cmd + "\n") | |
70b99f2f | 59 | outputFile.write(str(cmd_output)) |
701a0192 | 60 | outputFile.write( |
61 | "########################################################\n" | |
62 | ) | |
63 | outputFile.write("\n") | |
70b99f2f MS |
64 | except Exception as e: |
65 | print("Writing to output file Failed: ", e) | |
701a0192 | 66 | except subprocess.CalledProcessError as e: |
67 | dateTime = datetime.datetime.now() | |
68 | outputFile.write(">>[" + str(dateTime) + "]" + cmd + "\n") | |
69 | outputFile.write(e.output) | |
70 | outputFile.write("########################################################\n") | |
71 | outputFile.write("\n") | |
72 | print("Error:" + e.output) | |
e60eb780 SMS |
73 | |
74 | ||
75 | # Process the support bundle configuration file | |
76 | # and call appropriate functions | |
70b99f2f MS |
77 | def processConfFile(): |
78 | ||
79 | lines = list() | |
80 | outputFile = None | |
81 | ||
82 | try: | |
83 | with open(inputFile, "r") as supportBundleConfFile: | |
84 | for l in supportBundleConfFile: | |
85 | lines.append(l.rstrip()) | |
86 | except IOError: | |
87 | print("conf file {} not present".format(inputFile)) | |
88 | return | |
89 | ||
701a0192 | 90 | for line in lines: |
70b99f2f | 91 | if len(line) == 0 or line[0] == "#": |
701a0192 | 92 | continue |
70b99f2f | 93 | |
701a0192 | 94 | cmd_line = line.split(":") |
95 | if cmd_line[0] == "PROC_NAME": | |
96 | outputFileName = createOutputFile(cmd_line[1]) | |
97 | if outputFileName: | |
98 | print(outputFileName, "created for", cmd_line[1]) | |
99 | elif cmd_line[0] == "CMD_LIST_START": | |
100 | outputFile = openOutputFile(outputFileName) | |
101 | if outputFile: | |
102 | print(outputFileName, "opened") | |
103 | else: | |
104 | print(outputFileName, "open failed") | |
105 | return FAIL | |
106 | elif cmd_line[0] == "CMD_LIST_END": | |
107 | if closeOutputFile(outputFile): | |
108 | print(outputFileName, "closed") | |
109 | else: | |
110 | print(outputFileName, "close failed") | |
111 | else: | |
112 | print("Execute:", cmd_line[0]) | |
113 | executeCommand(cmd_line[0], outputFile) | |
114 | ||
115 | ||
e60eb780 | 116 | # Main Function |
70b99f2f | 117 | processConfFile() |