def run(self): criteria = self.job.criteria netshark = DeviceManager.get_device(criteria.netshark_device) self.export_name = str( path_to_class(netshark, criteria.netshark_source_name)) source = netshark.get_capture_job_by_name(self.export_name) timefilter = TimeFilter(criteria.starttime, criteria.endtime) handle = Job._compute_handle(self.table, criteria) # check if pcaps directory exists, if not make the directory if not os.path.exists(PCAP_DIR): os.mkdir(PCAP_DIR) while self.all_pcap_size > settings.PCAP_SIZE_LIMIT: self.delete_oldest_pcap() self.filename = add_pcap_dir('%s.pcap' % handle) filters = ([BpfFilter(filt) for filt in self.table.options.filters] or None) with netshark.create_export( source, timefilter, filters=filters, wait_for_data=self.table.options.wait_for_data, wait_duration=self.table.options.wait_duration) as e: self.download(e) return QueryComplete(pandas.DataFrame([dict(filename=self.filename)]))
def run(self): criteria = self.job.criteria netshark = DeviceManager.get_device(criteria.netshark_device) self.export_name = str(path_to_class(netshark, criteria.netshark_source_name)) source = netshark.get_capture_job_by_name(self.export_name) timefilter = TimeFilter(criteria.starttime, criteria.endtime) handle = Job._compute_handle(self.table, criteria) # check if pcaps directory exists, if not make the directory if not os.path.exists(PCAP_DIR): os.mkdir(PCAP_DIR) while self.all_pcap_size > settings.PCAP_SIZE_LIMIT: self.delete_oldest_pcap() self.filename = add_pcap_dir('%s.pcap' % handle) filters = ([BpfFilter(filt) for filt in self.table.options.filters] or None) with netshark.create_export( source, timefilter, filters=filters, wait_for_data=self.table.options.wait_for_data, wait_duration=self.table.options.wait_duration) as e: self.download(e) return QueryComplete(pandas.DataFrame([dict(filename=self.filename)]))
def create_report_history(request, report, widgets): """Create a report history object. :param request: request object :param report: Report object :param widgets: List of widget definitions """ # create the form to derive criteria for bookmark only # the form in the calling context can not be used # because it does not include hidden fields fields_by_section = report.collect_fields_by_section() all_fields = SortedDict() [all_fields.update(c) for c in fields_by_section.values()] form = TableFieldForm(all_fields, hidden_fields=report.hidden_fields, include_hidden=True, data=request.POST, files=request.FILES) # parse time and localize to user profile timezone timezone = get_timezone(request) form.apply_timezone(timezone) form_data = form.cleaned_data url = request._request.path + '?' # form_data contains fields that don't belong to url # e.g. ignore_cache, debug # thus use compute_field_precedence method to filter those table_fields = {k: form_data[k] for k in form.compute_field_precedence()} # Convert field values into strings suitable for bookmark def _get_url_fields(flds): for k, v in flds.iteritems(): if k in ['starttime', 'endtime']: yield (k, str(datetime_to_seconds(v))) elif k in ['duration', 'resolution']: try: yield (k, str(int(timedelta_total_seconds(v)))) except AttributeError: # v is of special value, not a string of some duration yield (k, v.replace(' ', '+')) else: # use + as encoded white space yield (k, str(v).replace(' ', '+')) yield ('auto_run', 'true') url_fields = ['='.join([k, v]) for k, v in _get_url_fields(table_fields)] # Form the bookmark link url += '&'.join(url_fields) last_run = datetime.datetime.now(timezone) # iterate over the passed in widget definitions and use those # to calculate the actual job handles # since these will mimic what gets used to create the actual jobs, # the criteria will match more closely than using the report-level # criteria data handles = [] for widget in widgets: wobj = Widget.objects.get( slug=widget['widgetslug'], section__in=Section.objects.filter(report=report) ) fields = wobj.collect_fields() form = TableFieldForm(fields, use_widgets=False, hidden_fields=report.hidden_fields, include_hidden=True, data=widget['criteria'], files=request.FILES) if form.is_valid(): # parse time and localize to user profile timezone timezone = get_timezone(request) form.apply_timezone(timezone) form_criteria = form.criteria() widget_table = wobj.table() form_criteria = form_criteria.build_for_table(widget_table) try: form_criteria.compute_times() except ValueError: pass handle = Job._compute_handle(widget_table, form_criteria) logger.debug('ReportHistory: adding handle %s for widget_table %s' % (handle, widget_table)) handles.append(handle) else: # log error, but don't worry about it for adding to RH logger.warning("Error while calculating job handle for Widget %s, " "internal criteria form is invalid: %s" % (wobj, form.errors.as_text())) job_handles = ','.join(handles) if request.user.is_authenticated(): user = request.user.username else: user = settings.GUEST_USER_NAME logger.debug('Creating ReportHistory for user %s at URL %s' % (user, url)) ReportHistory.create(namespace=report.namespace, slug=report.slug, bookmark=url, first_run=last_run, last_run=last_run, job_handles=job_handles, user=user, criteria=table_fields, run_count=1)
def create_report_history(request, report, widgets): """Create a report history object. :param request: request object :param report: Report object :param widgets: List of widget definitions """ # create the form to derive criteria for bookmark only # the form in the calling context can not be used # because it does not include hidden fields fields_by_section = report.collect_fields_by_section() all_fields = OrderedDict() [all_fields.update(c) for c in fields_by_section.values()] form = TableFieldForm(all_fields, hidden_fields=report.hidden_fields, include_hidden=True, data=request.POST, files=request.FILES) # parse time and localize to user profile timezone timezone = get_timezone(request) form.apply_timezone(timezone) form_data = form.cleaned_data url = request._request.path + '?' # form_data contains fields that don't belong to url # e.g. ignore_cache, debug # thus use compute_field_precedence method to filter those table_fields = {k: form_data[k] for k in form.compute_field_precedence()} # Convert field values into strings suitable for bookmark def _get_url_fields(flds): for k, v in flds.iteritems(): if k in ['starttime', 'endtime']: yield (k, str(datetime_to_seconds(v))) elif k in ['duration', 'resolution']: try: yield (k, str(int(timedelta_total_seconds(v)))) except AttributeError: # v is of special value, not a string of some duration yield (k, v.replace(' ', '+')) else: # use + as encoded white space yield (k, str(v).replace(' ', '+')) yield ('auto_run', 'true') url_fields = ['='.join([k, v]) for k, v in _get_url_fields(table_fields)] # Form the bookmark link url += '&'.join(url_fields) last_run = datetime.datetime.now(timezone) # iterate over the passed in widget definitions and use those # to calculate the actual job handles # since these will mimic what gets used to create the actual jobs, # the criteria will match more closely than using the report-level # criteria data handles = [] for widget in widgets: wobj = Widget.objects.get( slug=widget['widgetslug'], section__in=Section.objects.filter(report=report) ) fields = wobj.collect_fields() form = TableFieldForm(fields, use_widgets=False, hidden_fields=report.hidden_fields, include_hidden=True, data=widget['criteria'], files=request.FILES) if form.is_valid(): # parse time and localize to user profile timezone timezone = get_timezone(request) form.apply_timezone(timezone) form_criteria = form.criteria() widget_table = wobj.table() form_criteria = form_criteria.build_for_table(widget_table) try: form_criteria.compute_times() except ValueError: pass handle = Job._compute_handle(widget_table, form_criteria) logger.debug('ReportHistory: adding handle %s for widget_table %s' % (handle, widget_table)) handles.append(handle) else: # log error, but don't worry about it for adding to RH logger.warning("Error while calculating job handle for Widget %s, " "internal criteria form is invalid: %s" % (wobj, form.errors.as_text())) job_handles = ','.join(handles) if request.user.is_authenticated(): user = request.user.username else: user = settings.GUEST_USER_NAME logger.debug('Creating ReportHistory for user %s at URL %s' % (user, url)) ReportHistory.create(namespace=report.namespace, slug=report.slug, bookmark=url, first_run=last_run, last_run=last_run, job_handles=job_handles, user=user, criteria=table_fields, run_count=1)