Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
suppfile_example.py
1 /*! \file suppfile_example.py
2  */
3 
4 #!/mnt/fastpath/usr/bin/python
5 
6 """suppfile_example.py: OpEN API support file generation example"""
7 
8 import OpEN_py as OpEN
9 from OpENUtil import *
10 import sys
11 
12 example_name = "suppfile_example"
13 
14 #
15 # Copyright 2016 Broadcom.
16 #
17 # Licensed under the Apache License, Version 2.0 (the "License");
18 # you may not use this file except in compliance with the License.
19 # You may obtain a copy of the License at
20 #
21 # http://www.apache.org/licenses/LICENSE-2.0
22 #
23 # Unless required by applicable law or agreed to in writing, software
24 # distributed under the License is distributed on an "AS IS" BASIS,
25 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 # See the License for the specific language governing permissions and
27 # limitations under the License.
28 #
29 
30 
31 #
32 # Python 2.6.6
33 #
34 
35 open_ = OpENUtil()
36 
37 #
38 # Define a reasonable number of lines to display by default
39 # (a typical support file can contain upwards of 20K lines!)
40 #
41 display_default = 100
42 
43 class SuppFileExample:
44  """Support File OpEN API Example."""
45 
46  def __init__(self, client):
47  self.m_client = client
48  self.support_filename = None
49 
50  def support_file_create(self):
51  """Execute 'show tech-support file' command internally to generate file."""
52 
53  # Get the maximum length of a file name string
54  max_filename_len_p = OpEN.new_uint32_tp()
55  OpEN.openapiSystemFileNameMaxLengthGet(self.m_client, max_filename_len_p)
56  max_filename_len = OpEN.uint32_tp_value(max_filename_len_p)
57  OpEN.delete_uint32_tp(max_filename_len_p)
58 
59  # Allocate a buffer and descriptor to receive output filename
60  # (add extra byte for end-of-string termination character)
61  bufsize = max_filename_len + 1
62  try:
63  outfile_buf = open_.getStringBuffer(bufsize)
64  except OpENBufferSizeError:
65  print("support_file_create: getStringBuffer raised OpENBufferSizeError")
66  return
67  except TypeError:
68  print("support_file_create: getStringBuffer raised TypeError")
69  return
70  outfile_desc = OpEN.open_buffdesc()
71  outfile_desc.pstart = outfile_buf
72  outfile_desc.size = bufsize
73 
74  filename = None
75  ret = OpEN.openapiSupportFileGenerate(self.m_client, outfile_desc)
76  if (ret == OpEN.OPEN_E_NONE) and (len(outfile_buf.cast()) > 0):
77  filename = outfile_buf.cast()
78  print "Tech support file created successfully.\n"
79  else:
80  if ret == OpEN.OPEN_E_UNAVAIL:
81  print "Feature not supported on this platform.\n"
82  elif ret == OpEN.OPEN_E_PARAM:
83  print "Error: Invalid parameter specified.\n"
84  else:
85  print "Error: Failed to create tech support file.\n"
86 
87  self.support_filename = filename
88  return filename
89 
90  def support_file_display(self, maxout=display_default):
91  """"Display file contents line-by-line. Optional maxout arg controls output:
92  0 : displays entire file
93  >0 : displays specified number of file lines
94  """
95  if self.support_filename is not None:
96  filename = self.support_filename
97  if maxout >= 0:
98  for line in open(filename):
99  # Do not add any extra newlines
100  print line,
101  if maxout > 1:
102  maxout -= 1
103  if maxout == 1:
104  break
105 
106 def main(argv):
107  """Demonstrate OpEN usage for generating Tech Support file."""
108 
109  # display_max parameter is optional
110  numargs = len(argv)
111  if (numargs > 1) or ((numargs == 1) and (argv[0] == '?')):
112  print "%s.py [display_max]\n" % (example_name,)
113  print " display_max: 0=all, >0=number of lines (default %d)\n" % (display_default,)
114  sys.exit()
115 
116  try:
117  display_max = int(argv[0])
118  if display_max < 0:
119  raise ValueError()
120  except ValueError:
121  print "The specified input parameter value must be 0 or greater.\n"
122  sys.exit()
123  except:
124  # Display default number of lines if optional parm not specified
125  display_max = display_default
126 
127  ret = open_.connect(example_name)
128  if ret == OpEN.OPEN_E_NONE:
129  print ""
130  open_.getNetworkOSVersion()
131  open_.getAPIVersion()
132  client = open_.get_client()
133  xmp = SuppFileExample(client)
134  print "Creating Tech Support file (this may take a while)..."
135  xmp.support_file_create()
136  if display_max >= 0:
137  if display_max == 0:
138  lines_msg = "all lines"
139  else:
140  lines_msg = "first line" if display_max == 1 else "first %d lines" % (display_max,)
141  print "Displaying %s of file %s:\n" % (lines_msg, xmp.support_filename)
142  xmp.support_file_display(display_max)
143  open_.terminate()
144  else :
145  print "Unable to connect.\n"
146 
147 if __name__ == '__main__':
148  main(sys.argv[1:])
149