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