]> git.proxmox.com Git - mirror_frr.git/blob - tools/generate_support_bundle.py
Merge pull request #7444 from sudhanshukumar22/bgp-clean-dampening-issue
[mirror_frr.git] / tools / generate_support_bundle.py
1 #!/usr/bin/env python3
2
3 ########################################################
4 ### Python Script to generate the FRR support bundle ###
5 ########################################################
6 import os
7 import subprocess
8 import datetime
9
10 ETC_DIR = "/etc/frr/"
11 LOG_DIR = "/var/log/frr/"
12 SUCCESS = 1
13 FAIL = 0
14
15 inputFile = ETC_DIR + "support_bundle_commands.conf"
16
17 # Create the output file name
18 def createOutputFile(procName):
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
29
30 # Open the output file for this process
31 def openOutputFile(fileName):
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
40
41 # Close the output file for this process
42 def closeOutputFile(f):
43 try:
44 f.close()
45 return SUCCESS
46 except IOError:
47 return FAIL
48
49
50 # Execute the command over vtysh and store in the
51 # output file
52 def executeCommand(cmd, outputFile):
53 cmd_exec_str = 'vtysh -c "' + cmd + '" '
54 try:
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")
59 outputFile.write(str(cmd_output))
60 outputFile.write(
61 "########################################################\n"
62 )
63 outputFile.write("\n")
64 except Exception as e:
65 print("Writing to output file Failed: ", e)
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)
73
74
75 # Process the support bundle configuration file
76 # and call appropriate functions
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
90 for line in lines:
91 if len(line) == 0 or line[0] == "#":
92 continue
93
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
116 # Main Function
117 processConfFile()