]> git.proxmox.com Git - mirror_frr.git/blob - tools/generate_support_bundle.py
Merge pull request #5722 from donaldsharp/kernel_routes
[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 # Create the output file name
28 def createOutputFile(procName):
29 fileName = procName + "_support_bundle.log"
30 oldFile = LOG_DIR + fileName
31 cpFileCmd = "cp " + oldFile + " " + oldFile + ".prev"
32 rmFileCmd = "rm -rf " + oldFile
33 print("Making backup of " + oldFile)
34 os.system(cpFileCmd)
35 print("Removing " + oldFile)
36 os.system(rmFileCmd)
37 return fileName
38
39 # Open the output file for this process
40 def openOutputFile(fileName):
41 crt_file_cmd = LOG_DIR + fileName
42 print(crt_file_cmd)
43 try:
44 outputFile = open(crt_file_cmd, "w")
45 return outputFile
46 except IOError:
47 return ()
48
49 # Close the output file for this process
50 def closeOutputFile(file):
51 try:
52 file.close()
53 return SUCCESS
54 except IOError:
55 return FAIL
56
57 # Execute the command over vtysh and store in the
58 # output file
59 def executeCommand(cmd, outputFile):
60 cmd_exec_str = "vtysh -c \"" + cmd + "\" "
61 try:
62 cmd_output = subprocess.check_output(cmd_exec_str, shell=True)
63 try:
64 dateTime = datetime.datetime.now()
65 outputFile.write(">>[" + str(dateTime) + "]" + cmd + "\n")
66 outputFile.write(cmd_output)
67 outputFile.write("########################################################\n")
68 outputFile.write('\n')
69 except:
70 print("Writing to ouptut file Failed")
71 except subprocess.CalledProcessError as e:
72 dateTime = datetime.datetime.now()
73 outputFile.write(">>[" + str(dateTime) + "]" + cmd + "\n")
74 outputFile.write(e.output)
75 outputFile.write("########################################################\n")
76 outputFile.write('\n')
77 print("Error:" + e.output)
78
79
80 # Process the support bundle configuration file
81 # and call appropriate functions
82 def processConfFile(lines):
83 for line in lines:
84 if line[0][0] == '#':
85 continue
86 cmd_line = line.split(':')
87 if cmd_line[0] == "PROC_NAME":
88 outputFileName = createOutputFile(cmd_line[1])
89 if outputFileName:
90 print(outputFileName, "created for", cmd_line[1])
91 elif cmd_line[0] == "CMD_LIST_START":
92 outputFile = openOutputFile(outputFileName)
93 if outputFile:
94 print(outputFileName, "opened")
95 else:
96 print(outputFileName, "open failed")
97 return FAIL
98 elif cmd_line[0] == "CMD_LIST_END":
99 if closeOutputFile(outputFile):
100 print(outputFileName, "closed")
101 else:
102 print(outputFileName, "close failed")
103 else:
104 print("Execute:" , cmd_line[0])
105 executeCommand(cmd_line[0], outputFile)
106
107 # Main Function
108 lines = openConfFile(inputFile)
109 if not lines:
110 print("File support_bundle_commands.conf not present in /etc/frr/ directory")
111 else:
112 processConfFile(lines)