def main(self): if self.options.showsources: svcdef = self.appresponse.find_service('npm.reports') dr = svcdef.bind('source_names') source_names = dr.execute('get').data print('\n'.join(source_names)) return source = SourceProxy(name=self.options.sourcename) columns = [] headers = [] if self.options.keycolumns: for col in self.options.keycolumns.split(','): columns.append(Key(col)) headers.append(col) for col in self.options.valuecolumns.split(','): columns.append(Value(col)) headers.append(col) topbycolumns = [] if self.options.topbycolumns: for col in self.options.topbycolumns.split(','): topbycolumns.append(Key(col)) data_def = DataDef(source=source, columns=columns, granularity=self.options.granularity, resolution=self.options.resolution, time_range=self.options.timerange, limit=self.options.limit, topbycolumns=topbycolumns) if self.options.filterexpr: data_def.add_filter( TrafficFilter(type_='steelfilter', value=self.options.filterexpr)) report = Report(self.appresponse) report.add(data_def) report.run() data = report.get_data() headers = report.get_legend() report.delete() if self.options.csvfile: with open(self.options.csvfile, 'w') as f: for line in Formatter.get_csv(data, headers): f.write(line) f.write('\n') else: Formatter.print_csv(data, headers)
def main(self): if self.options.sourcetype == 'file': source = self.appresponse.fs.get_file_by_id(self.options.sourceid) elif self.options.sourcetype == 'job': source = self.appresponse.capture.\ get_job_by_name(self.options.sourceid) else: source = self.appresponse.clips.\ get_clip_by_id(self.options.sourceid) data_source = SourceProxy(source) columns = [] headers = [] if self.options.keycolumns: for col in self.options.keycolumns.split(','): columns.append(Key(col)) headers.append(col) for col in self.options.valuecolumns.split(','): columns.append(Value(col)) headers.append(col) data_def = DataDef(source=data_source, columns=columns, granularity=self.options.granularity, resolution=self.options.resolution, time_range=self.options.timerange) if self.options.filterexpr: data_def.add_filter( TrafficFilter(type_=self.options.filtertype, value=self.options.filterexpr)) report = Report(self.appresponse) report.add(data_def) report.run() data = report.get_data() headers = report.get_legend() report.delete() if self.options.csvfile: with open(self.options.csvfile, 'w') as f: for line in Formatter.get_csv(data, headers): f.write(line) f.write('\n') else: Formatter.print_csv(data, headers)
def handle(self, *args, **options): """ Main command handler. """ self.options = options if options['table_list']: # print out the id's instead of processing anything output = [] for t in Table.objects.all(): output.append([t.id, t.namespace, t.queryclassname, t.name, t]) Formatter.print_table(output, ['ID', 'Namespace', 'QueryClass', 'Name', 'Table']) elif options['table_list_by_report']: # or print them out organized by report/widget/table output = [] reports = Report.objects.all() for report in reports: for table in report.tables(): for widget in table.widget_set.all(): line = [table.id, report.title, widget.title, table] output.append(line) Formatter.print_table(output, ['ID', 'Report', 'Widget', 'Table']) elif options['criteria_list']: if 'table_id' in options and options['table_id'] is not None: table = Table.objects.get(id=options['table_id']) elif 'table_name' in options and options['table_name'] is not None: table = Table.objects.get(name=options['table_name']) else: raise ValueError("Must specify either --table-id or " "--table-name to run a table") form = self.get_form(table) # Only show criteria options that were included in report # and given a label, other ones are for internal table use. # criteria like ignore_cache can still be passed in, they # just won't be shown in this list output = [(k, v.label) for k, v in form.fields.iteritems() if v.label] Formatter.print_table(output, ['Keyword', 'Label']) else: if 'table_id' in options and options['table_id'] is not None: table = Table.objects.get(id=options['table_id']) elif 'table_name' in options and options['table_name'] is not None: table = Table.objects.get(name=options['table_name']) else: raise ValueError("Must specify either --table-id or " "--table-name to run a table") # Django gives us a nice error if we can't find the table self.console('Table %s found.' % table) # Parse criteria options criteria_options = {} if 'criteria' in options and options['criteria'] is not None: for s in options['criteria']: (k, v) = s.split(':', 1) criteria_options[k] = v form = self.get_form(table, data=criteria_options) if not form.is_valid(check_unknown=True): self.console('Invalid criteria:') logger.error('Invalid criteria: %s' % ','.join('%s:%s' % (k, v) for k, v in form.errors.iteritems())) for k, v in form.errors.iteritems(): self.console(' %s: %s' % (k, ','.join(v))) sys.exit(1) criteria = form.criteria() columns = [c.name for c in table.get_columns()] if options['only_columns']: print columns return job = Job.create(table=table, criteria=criteria, update_progress=False) job.save() self.console('Job created: %s' % job) self.console('Criteria: %s' % criteria.print_details()) start_time = datetime.datetime.now() job.start() self.console('Job running . . ', ending='') # wait for results while not job.done(): # self.console('. ', ending='') # self.stdout.flush() time.sleep(1) end_time = datetime.datetime.now() delta = end_time - start_time seconds = float(delta.microseconds + (delta.seconds + delta.days*24*3600)*10**6)/10**6 self.console('Done!! (elapsed time: %.2f seconds)' % seconds) self.console('') # Need to refresh the column list in case the job changed them # (ephemeral cols) columns = [c.name for c in table.get_columns()] if job.status == job.COMPLETE: if options['as_csv']: if options['output_file']: with open(options['output_file'], 'w') as f: for line in Formatter.get_csv(job.values(), columns): f.write(line) f.write('\n') else: Formatter.print_csv(job.values(), columns) else: Formatter.print_table(job.values(), columns) else: self.console("Job completed with an error:") self.console(job.message) sys.exit(1)
def main(self): source = SourceProxy(name='alert_list') if self.options.alert_detail: # detail view column_names = [ 'alert.id', 'alert.policy_id', 'alert.policy_name', 'alert.policy_eval_period', 'alert.policy_type_name', 'alert.policy_last_N', 'alert.policy_last_M', 'alert.severity_level', 'alert.severity', 'alert.start_time', 'alert.end_time', 'alert.duration', 'alert.ongoing', 'alert.low_violations_count', 'alert.medium_violations_count', 'alert.high_violations_count' ] else: # alert event listing view column_names = [ 'alert.id', 'alert.policy_id', 'alert.policy_name', 'alert.policy_type_name', 'alert.policy_type', 'alert.severity_level', 'alert.severity', 'alert.start_time', 'alert.end_time', 'alert.duration', 'alert.ongoing' ] columns = self.appresponse.get_column_objects('alert_list', column_names) data_def = DataDef(source=source, columns=columns, granularity=60, resolution=0, time_range=self.options.timerange) if self.options.alert_detail: fltr = {'value': 'alert.id="{}"'.format(self.options.alert_detail)} data_def._filters.append(fltr) report = Report(self.appresponse) report.add(data_def) report.run() data = report.get_data() headers = [x.lstrip('alert.') for x in report.get_legend()] if not data: print('\nNo data found.\n') else: if self.options.csvfile: with open(self.options.csvfile, 'w') as f: for line in Formatter.get_csv(data, headers): f.write(line) f.write('\n') else: Formatter.print_table(data, headers)
def main(self): if self.options.sourcename == 'packets': if self.options.sourceid is None: source = self.appresponse.capture.get_vifgs()[0] else: source = SourceProxy(name='packets', path=self.options.sourceid) else: source = SourceProxy(name='aggregates') columns = [] headers = [] for col in self.options.keycolumns.split(','): columns.append(Key(col)) headers.append(col) for col in self.options.valuecolumns.split(','): columns.append(Value(col)) headers.append(col) data_def = DataDef(source=source, columns=columns, granularity=self.options.granularity, resolution=self.options.resolution, live=True) if self.options.filterexpr: data_def.add_filter(TrafficFilter(type_='steelfilter', value=self.options.filterexpr)) print('Running report, press Ctrl-C to exit.') print('') report = self.appresponse.create_report(data_def) time.sleep(1) try: while 1: banner = '{} {}'.format(datetime.datetime.now(), '--' * 20) print(banner) try: data = report.get_data()['data'] headers = report.get_legend() if self.options.sortby: index = headers.index(self.options.sortby) data.sort(key=lambda x: x[index], reverse=True) if self.limit: total_rows = len(data) limit_string = ('Showing {} out of {} rows.' .format(self.limit, total_rows)) data = data[:self.limit] else: limit_string = None except KeyError: # something went wrong, print error and exit print('Error accessing data:') print(report.get_data()) raise KeyboardInterrupt if self.options.csvfile: with open(self.options.csvfile, 'a') as f: f.write(banner) f.write('\n') for line in Formatter.get_csv(data, headers): f.write(line) f.write('\n') if limit_string: f.write(limit_string) f.write('\n') else: Formatter.print_table(data, headers) if limit_string: print(limit_string) time.sleep(self.delay) except KeyboardInterrupt: print('Exiting ...') report.delete()
def handle(self, *args, **options): """ Main command handler. """ self.options = options if options['table_list']: # print out the id's instead of processing anything output = [] for t in Table.objects.all(): output.append([t.id, t.namespace, t.queryclassname, t.name, t]) Formatter.print_table( output, ['ID', 'Namespace', 'QueryClass', 'Name', 'Table']) elif options['table_list_by_report']: # or print them out organized by report/widget/table output = [] reports = Report.objects.all() for report in reports: for table in report.tables(): for widget in table.widget_set.all(): line = [table.id, report.title, widget.title, table] output.append(line) Formatter.print_table(output, ['ID', 'Report', 'Widget', 'Table']) elif options['criteria_list']: if 'table_id' in options and options['table_id'] is not None: table = Table.objects.get(id=options['table_id']) elif 'table_name' in options and options['table_name'] is not None: table = Table.objects.get(name=options['table_name']) else: raise ValueError("Must specify either --table-id or " "--table-name to run a table") form = self.get_form(table) # Only show criteria options that were included in report # and given a label, other ones are for internal table use. # criteria like ignore_cache can still be passed in, they # just won't be shown in this list output = [(k, v.label) for k, v in form.fields.iteritems() if v.label] Formatter.print_table(output, ['Keyword', 'Label']) else: if 'table_id' in options and options['table_id'] is not None: table = Table.objects.get(id=options['table_id']) elif 'table_name' in options and options['table_name'] is not None: table = Table.objects.get(name=options['table_name']) else: raise ValueError("Must specify either --table-id or " "--table-name to run a table") # Django gives us a nice error if we can't find the table self.console('Table %s found.' % table) # Parse criteria options criteria_options = {} if 'criteria' in options and options['criteria'] is not None: for s in options['criteria']: (k, v) = s.split(':', 1) criteria_options[k] = v form = self.get_form(table, data=criteria_options) if not form.is_valid(check_unknown=True): self.console('Invalid criteria:') logger.error('Invalid criteria: %s' % ','.join('%s:%s' % (k, v) for k, v in form.errors.iteritems())) for k, v in form.errors.iteritems(): self.console(' %s: %s' % (k, ','.join(v))) sys.exit(1) criteria = form.criteria() columns = [c.name for c in table.get_columns()] if options['only_columns']: print columns return job = Job.create(table=table, criteria=criteria, update_progress=False) job.save() self.console('Job created: %s' % job) self.console('Criteria: %s' % criteria.print_details()) start_time = datetime.datetime.now() job.start() self.console('Job running . . ', ending='') # wait for results while not job.done(): # self.console('. ', ending='') # self.stdout.flush() time.sleep(1) end_time = datetime.datetime.now() delta = end_time - start_time seconds = float(delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10**6) / 10**6 self.console('Done!! (elapsed time: %.2f seconds)' % seconds) self.console('') # Need to refresh the column list in case the job changed them # (ephemeral cols) columns = [c.name for c in table.get_columns()] if job.status == job.COMPLETE: if options['as_csv']: if options['output_file']: with open(options['output_file'], 'w') as f: for line in Formatter.get_csv( job.values(), columns): f.write(line) f.write('\n') else: Formatter.print_csv(job.values(), columns) else: Formatter.print_table(job.values(), columns) else: self.console("Job completed with an error:") self.console(job.message) sys.exit(1)