Package Products :: Package ZenReports :: Module ReportRunner |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenReports.ReportRunner

      1  #!/usr/bin/env python
      2  ##############################################################################
      3  #
      4  # Copyright (C) Zenoss, Inc. 2007, 2011, all rights reserved.
      5  #
      6  # This content is made available according to terms specified in
      7  # License.zenoss under the directory where your Zenoss product is installed.
      8  #
      9  ##############################################################################
     10
     11
     12  __doc__ = """ReportRunner
     13  Run a report plugin and display the output
     14  """
     15
     16  from pprint import pprint
     17  import csv
     18  from sys import stdout
     19
     20  import Globals
     21  from Products.ZenUtils.ZCmdBase import ZCmdBase
     22
     23 -class ReportRunner(ZCmdBase):
     24
     25 -    def main(self):
     26          if self.options.list:
     27              plugins = self.dmd.ReportServer.listPlugins()
     28              pprint(sorted(plugins))
     29              return
     30
     31          plugin =  self.args[0]
     32          args = {}
     33          for a in self.args[1:]:
     34              if a.find('=') > -1:
     35                  key, value = a.split('=', 1)
     36                  args[key] = value
     37
     38          self.log.debug("Running '%s' with %r", plugin, args)
     39          result = self.dmd.ReportServer.plugin(plugin, args)
     40          if not result:
     41              self.log.warn("No results returned from plugin.")
     42              return
     43
     44          if self.options.export == 'csv':
     45              self.writeCsv(result)
     46          else:
     47              if self.options.export_file:
     48                  fh = open(self.options.export_file, 'w')
     49              else:
     50                  fh = stdout
     51              pprint(result, stream=fh)
     52              if fh is not stdout:
     53                  fh.close()
     54
     55 -    def writeCsv(self, results):
     56          """
     57          Write the CSV output to standard output.
     58          """
     59          if self.options.export_file:
     60              fh = open(self.options.export_file, 'w')
     61          else:
     62              fh = stdout
     63
     64          sampleRow = results[0]
     65          fieldnames = sorted(sampleRow.values.keys())
     66
     67          # Write a header line that DictReader can import
     68          fh.write(','.join(fieldnames) + '\n')
     69          writer = csv.DictWriter(fh, fieldnames,
     70                                  quoting=csv.QUOTE_NONNUMERIC,
     71                                  lineterminator='\n')
     72          for line in results:
     73              writer.writerow(line.values)
     74
     75          if fh is not stdout:
     76              fh.close()
     77
     78 -    def buildOptions(self):
     79          ZCmdBase.buildOptions(self)
     80          self.parser.usage = "%prog [options] report_plugin [report_paramater=value]*"
     81          self.parser.remove_option('--daemon')
     82          self.parser.remove_option('--cycle')
     83          self.parser.remove_option('--watchdog')
     84          self.parser.remove_option('--watchdogPath')
     85          self.parser.remove_option('--socketOption')
     86          self.parser.add_option("--list",
     87                                 action="store_true",
     88                                 default=False,
     89                                 help="Show full names of all plugins to run. If the plugin" \
     90                                      " name is unique, just the name may be used." )
     91          self.parser.add_option("--export",
     92                                 default='python',
     93                                 help="Export the values as 'python' (default) or 'csv'")
     94          self.parser.add_option("--export_file",
     95                                 default='',
     96                                 help="Optional filename to store the output")
     97
     98
     99  if __name__ == '__main__':
    100      rr = ReportRunner()
    101      rr.main()
    102