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