Ejemplo n.º 1
0
 def _get_opserver_query(cls, job_execution_id, status):
     value = "%s:%s" % (job_execution_id, cls.JOB_STATUS.get(status.value))
     match = OpServerUtils.Match(name=cls.OBJECT_ID,
                                 value=value,
                                 op=OpServerUtils.MatchOp.EQUAL)
     return OpServerUtils.Query(cls.TABLE,
                                start_time=cls.START_TIME,
                                end_time=cls.END_TIME,
                                select_fields=cls.FIELDS,
                                where=[[match.__dict__]])
Ejemplo n.º 2
0
    def _get_overlay_flow_data(self):
        """Fetch the overlay flow data from the FlowRecord Table.

        Convert the where clause in the OverlayToUnderlay query according
        to the schema defined for the FlowRecord Table. Get the overlay
        flow data [source vrouter, destination vrouter, flowtuple hash,
        encapsulation] from the FlowRecord Table required to query the
        underlay data.
        """
        # process where clause
        try:
            where_or_list = self.query_json['where']
        except KeyError:
            where_or_list = []
        flow_record_where = []
        for where_and_list in where_or_list:
            flow_record_where_and_list = []
            for match_term in where_and_list:
                fname = self._overlay_to_flowrecord_name(match_term['name'])
                match = OpServerUtils.Match(name=fname,
                                            value=match_term['value'],
                                            op=match_term['op'],
                                            value2=match_term.get('value2'))
                flow_record_where_and_list.append(match.__dict__)
                if match_term.get('suffix') is not None:
                    fname = self._overlay_to_flowrecord_name(
                        match_term['suffix']['name'])
                    match = OpServerUtils.Match(
                        name=fname,
                        value=match_term['suffix']['value'],
                        op=match_term['suffix']['op'],
                        value2=match_term['suffix'].get('value2'))
                    flow_record_where_and_list.append(match.__dict__)
            flow_record_where.append(flow_record_where_and_list)

        # populate the select list
        flow_record_select = [
            FlowRecordNames[FlowRecordFields.FLOWREC_VROUTER_IP],
            FlowRecordNames[FlowRecordFields.FLOWREC_OTHER_VROUTER_IP],
            FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_SPORT],
            FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_PROTO]
        ]

        flow_record_query = OpServerUtils.Query(
            table=FLOW_TABLE,
            start_time=self._start_time,
            end_time=self._end_time,
            select_fields=flow_record_select,
            where=flow_record_where,
            dir=1)
        return self._send_query(json.dumps(flow_record_query.__dict__))
Ejemplo n.º 3
0
    def _get_underlay_flow_data(self, flow_record_data):
        """Fetch the underlay data from the UFlowData table.

        Construct the Where clause for the UFlowData query from the
        FlowRecord query response. Convert the select clause, sort_fields,
        filter clause in the OverlayToUnderlay query according to the schema
        defined for the UFlowData table.
        """
        if not len(flow_record_data):
            return []

        # populate where clause for Underlay Flow query
        uflow_data_where = []
        for row in flow_record_data:
            # if any of the column value is None, then skip the row
            if any(col == None for col in row.values()):
                continue
            uflow_data_where_and_list = []
            ufname = self._flowrecord_to_uflowdata_name(
                FlowRecordNames[FlowRecordFields.FLOWREC_VROUTER_IP])
            val = row[FlowRecordNames[FlowRecordFields.FLOWREC_VROUTER_IP]]
            sip = OpServerUtils.Match(name=ufname,
                                      value=val,
                                      op=OpServerUtils.MatchOp.EQUAL)
            uflow_data_where_and_list.append(sip.__dict__)
            ufname = self._flowrecord_to_uflowdata_name(
                FlowRecordNames[FlowRecordFields.FLOWREC_OTHER_VROUTER_IP])
            val = \
                row[FlowRecordNames[FlowRecordFields.FLOWREC_OTHER_VROUTER_IP]]
            dip = OpServerUtils.Match(name=ufname,
                                      value=val,
                                      op=OpServerUtils.MatchOp.EQUAL)
            uflow_data_where_and_list.append(dip.__dict__)
            ufname = self._flowrecord_to_uflowdata_name(
                FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_SPORT])
            val = row[FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_SPORT]]
            sport = OpServerUtils.Match(name=ufname,
                                        value=val,
                                        op=OpServerUtils.MatchOp.EQUAL)
            ufname = self._flowrecord_to_uflowdata_name(
                FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_PROTO])
            val = row[FlowRecordNames[FlowRecordFields.FLOWREC_UNDERLAY_PROTO]]
            # get the protocol from tunnel_type
            val = OpServerUtils.tunnel_type_to_protocol(val)
            protocol = OpServerUtils.Match(name=ufname,
                                           value=val,
                                           op=OpServerUtils.MatchOp.EQUAL,
                                           suffix=sport)
            uflow_data_where_and_list.append(protocol.__dict__)
            uflow_data_where.append(uflow_data_where_and_list)

        # if the where clause is empty, then no need to send
        # the UFlowData query
        if not len(uflow_data_where):
            return []

        # populate UFlowData select
        uflow_data_select = []
        for select in self.query_json['select_fields']:
            uflow_data_select.append(self._underlay_to_uflowdata_name(select))

        # sort_fields specified in the query?
        uflow_data_sort_fields = None
        if self.query_json.get('sort_fields'):
            uflow_data_sort_fields = []
            for field in self.query_json['sort_fields']:
                uflow_data_sort_fields.append(
                    self._underlay_to_uflowdata_name(field))
        uflow_data_sort_type = self.query_json.get('sort')

        # does the query contain limit attribute?
        uflow_data_limit = self.query_json.get('limit')

        # add filter if specified
        uflow_data_filter = None
        if self.query_json.get('filter') is not None:
            uflow_data_filter = list(self.query_json['filter'])
            if len(uflow_data_filter):
                if not isinstance(uflow_data_filter[0], list):
                    uflow_data_filter = [uflow_data_filter]
            for filter_and in uflow_data_filter:
                for match_term in filter_and:
                    match_term['name'] = self._underlay_to_uflowdata_name(
                        match_term['name'])

        uflow_data_query = OpServerUtils.Query(
            table='StatTable.UFlowData.flow',
            start_time=self._start_time,
            end_time=self._end_time,
            select_fields=uflow_data_select,
            where=uflow_data_where,
            sort=uflow_data_sort_type,
            sort_fields=uflow_data_sort_fields,
            limit=uflow_data_limit,
            filter=uflow_data_filter)
        return self._send_query(json.dumps(uflow_data_query.__dict__))
Ejemplo n.º 4
0
    def query(self):
        if self._args.f and (self._args.send_syslog or self._args.reverse
                             or self._args.start_time or self._args.end_time):
            invalid_combination = " --f"
            if self._args.send_syslog:
                invalid_combination += ", --send-syslog"
            if self._args.reverse:
                invalid_combination += ", --reverse"
            if self._args.start_time:
                invalid_combination += ", --start-time"
            if self._args.end_time:
                invalid_combination += ", --end-time"
            print "Combination of options" + invalid_combination + " are not valid."
            return -1
        start_time, end_time = self._start_time, self._end_time
        messages_url = OpServerUtils.opserver_query_url(
            self._args.analytics_api_ip, self._args.analytics_api_port)
        where_msg = []
        where_obj = []
        and_filter = []
        or_filter = []
        if self._args.source is not None:
            source_match = OpServerUtils.Match(name=VizConstants.SOURCE,
                                               value=self._args.source,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(source_match.__dict__)

        if self._args.module is not None:
            module_match = OpServerUtils.Match(name=VizConstants.MODULE,
                                               value=self._args.module,
                                               op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(module_match.__dict__)

        if self._args.category is not None:
            category_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.category,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(category_match.__dict__)

        if self._args.message_type is not None:
            message_type_match = OpServerUtils.Match(
                name=VizConstants.MESSAGE_TYPE,
                value=self._args.message_type,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(message_type_match.__dict__)

        if self._args.level is not None:
            level_match = OpServerUtils.Match(
                name=VizConstants.LEVEL,
                value=SandeshLevel._NAMES_TO_VALUES[self._args.level],
                op=OpServerUtils.MatchOp.LEQ)
            and_filter.append(level_match.__dict__)

        if self._args.node_type is not None:
            node_type_match = OpServerUtils.Match(
                name=VizConstants.NODE_TYPE,
                value=self._args.node_type,
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(node_type_match.__dict__)

        if self._args.instance_id is not None:
            instance_id_match = OpServerUtils.Match(
                name=VizConstants.INSTANCE_ID,
                value=self._args.instance_id,
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(instance_id_match.__dict__)

        # Object logs :
        # --object-type <> : All logs for the particular object type
        # --object-type <> --object-values : Object-id values for the particular
        #     object tye
        # --object-type <> --object-id <> : All logs matching object-id for
        #     particular object type
        if (self._args.object_type is not None
                or self._args.object_id is not None
                or self._args.object_select_field is not None
                or self._args.object_values is True):
            # Validate object-type
            if self._args.object_type is not None:
                if self._args.object_type in OBJECT_TYPE_LIST:
                    if self._args.object_type in OBJECT_TABLE_MAP:
                        table = OBJECT_TABLE_MAP[self._args.object_type]
                    else:
                        print 'Table not found for object-type [%s]' % \
                            (self._args.object_type)
                        return -1
                else:
                    print 'Unknown object-type [%s]' % (self._args.object_type)
                    return -1
            else:
                print 'Object-type required for query'
                return -1
            # Validate object-id and object-values
            if self._args.object_id is not None and \
               self._args.object_values is False:
                object_id = self._args.object_id
                if object_id.endswith("*"):
                    id_match = OpServerUtils.Match(
                        name=OpServerUtils.OBJECT_ID,
                        value=object_id[:-1],
                        op=OpServerUtils.MatchOp.PREFIX)
                else:
                    id_match = OpServerUtils.Match(
                        name=OpServerUtils.OBJECT_ID,
                        value=object_id,
                        op=OpServerUtils.MatchOp.EQUAL)
                where_obj.append(id_match.__dict__)
            elif self._args.object_id is not None and \
               self._args.object_values is True:
                print 'Please specify either object-id or object-values but not both'
                return -1

            if self._args.object_values is False:
                if self._args.object_select_field is not None:
                    if ((self._args.object_select_field !=
                         VizConstants.OBJECT_LOG)
                            and (self._args.object_select_field !=
                                 VizConstants.SYSTEM_LOG)):
                        print 'Invalid object-select-field. '\
                            'Valid values are "%s" or "%s"' \
                            % (VizConstants.OBJECT_LOG,
                               VizConstants.SYSTEM_LOG)
                        return -1
                    obj_sel_field = [self._args.object_select_field]
                    self._args.object_select_field = obj_sel_field
                else:
                    self._args.object_select_field = obj_sel_field = [
                        VizConstants.OBJECT_LOG, VizConstants.SYSTEM_LOG
                    ]
                select_list = [
                    VizConstants.TIMESTAMP,
                    VizConstants.SOURCE,
                    VizConstants.MODULE,
                    VizConstants.MESSAGE_TYPE,
                ] + obj_sel_field
            else:
                if self._args.object_select_field:
                    print 'Please specify either object-id with ' + \
                        'object-select-field or only object-values'
                    return -1
                if len(where_msg):
                    options = [where['name'] for where in where_msg]
                    print 'Invalid/unsupported where-clause options %s for object-values query' % str(
                        options)
                    return -1
                select_list = [OpServerUtils.OBJECT_ID]

            if len(where_obj) or len(where_msg):
                where = [where_obj + where_msg]
            else:
                where = None

        elif self._args.trace is not None:
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            if self._args.source is None:
                print 'Source is required for trace buffer dump'
                return -1
            if self._args.module is None:
                print 'Module is required for trace buffer dump'
                return -1
            trace_buf_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.trace,
                op=OpServerUtils.MatchOp.EQUAL)
            where_msg.append(trace_buf_match.__dict__)
            where = [where_msg]
            select_list = [
                VizConstants.TIMESTAMP, VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM, VizConstants.DATA,
                VizConstants.SANDESH_TYPE
            ]
            sandesh_type_filter = OpServerUtils.Match(
                name=VizConstants.SANDESH_TYPE,
                value=str(SandeshType.TRACE),
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(sandesh_type_filter.__dict__)
        else:
            # Message Table Query
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            # Message Table contains both systemlog and objectlog.
            # Add a filter to return only systemlogs
            if not self._args.all:
                sandesh_type_filter = OpServerUtils.Match(
                    name=VizConstants.SANDESH_TYPE,
                    value=str(SandeshType.SYSTEM),
                    op=OpServerUtils.MatchOp.EQUAL)
                or_filter.append(sandesh_type_filter.__dict__)
                sandesh_type_filter = OpServerUtils.Match(
                    name=VizConstants.SANDESH_TYPE,
                    value=str(SandeshType.SYSLOG),
                    op=OpServerUtils.MatchOp.EQUAL)
                or_filter.append(sandesh_type_filter.__dict__)

            if len(where_msg):
                where = [where_msg]
            else:
                where = None

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.CATEGORY,
                VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM,
                VizConstants.DATA,
                VizConstants.SANDESH_TYPE,
                VizConstants.LEVEL,
                VizConstants.NODE_TYPE,
                VizConstants.INSTANCE_ID,
            ]

        filter = None
        if len(or_filter):
            filter = [and_filter + [filt] for filt in or_filter]
        elif len(and_filter):
            filter = [and_filter]

        if self._args.keywords is not None:
            p = re.compile('\s*,\s*|\s+')
            if where is None:
                where = [[]]
            for kwd in p.split(self._args.keywords):
                message_type_match = OpServerUtils.Match(
                    name=VizConstants.KEYWORD,
                    value=kwd,
                    op=OpServerUtils.MatchOp.EQUAL)
                for wc in where:
                    wc.append(message_type_match.__dict__)

        # Add sort by timestamp for non object value queries
        sort_op = None
        sort_fields = None
        if self._args.object_values is False:
            if self._args.reverse:
                sort_op = OpServerUtils.SortOp.DESCENDING
            else:
                sort_op = OpServerUtils.SortOp.ASCENDING
            sort_fields = [VizConstants.TIMESTAMP]

        if self._args.limit:
            limit = int(self._args.limit)
        else:
            limit = None

        messages_query = OpServerUtils.Query(table,
                                             start_time=start_time,
                                             end_time=end_time,
                                             select_fields=select_list,
                                             where=where,
                                             filter=filter,
                                             sort=sort_op,
                                             sort_fields=sort_fields,
                                             limit=limit)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(messages_query.__dict__))
        resp = OpServerUtils.post_url_http(messages_url,
                                           json.dumps(messages_query.__dict__))
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(
                self._args.analytics_api_ip, self._args.analytics_api_port,
                qid)
        return result
Ejemplo n.º 5
0
    def query(self):
        if self._args.tail and (self._args.send_syslog or self._args.reverse or
                                self._args.start_time or self._args.end_time):
            invalid_combination = " --tail"
            if self._args.send_syslog:
                invalid_combination += ", --send-syslog"
            if self._args.reverse:
                invalid_combination += ", --reverse"
            if self._args.start_time:
                invalid_combination += ", --start-time"
            if self._args.end_time:
                invalid_combination += ", --end-time"
            print "Combination of options" + invalid_combination + " are not valid."
            return -1
        global output_file_handle
        if self._args.output_file is not None:
            if output_file_handle is None:
                #Open the file for writing
                try:
                    if self._args.tail:
                        output_file_handle = open(self._args.output_file, "a")
                    else:
                        output_file_handle = open(self._args.output_file, "w")
                except Exception as e:
                    print e
                    print "Exception occured when creating/opening file %s" % \
                          self._args.output_file
                    return -1

        start_time, end_time = self._start_time, self._end_time
        if self._args.message_types is True:
            command_str = (
                "contrail-stats --table FieldNames.fields" +
                " --where name=MessageTable:Messagetype --select name fields.value"
                + " --start-time " + str(start_time) + " --end-time " +
                str(end_time) + " --analytics-api-ip " +
                str(self._args.analytics_api_ip) + " --analytics-api-port " +
                str(self._args.analytics_api_port))
            res = commands.getoutput(command_str)
            res = res.splitlines()
            res = res[1:]
            for r in res:
                print ast.literal_eval(r)['fields.value']
            return None
        messages_url = OpServerUtils.opserver_query_url(
            self._args.analytics_api_ip, self._args.analytics_api_port)
        where_or_list = []
        and_filter = []
        or_filter = []
        if self._args.source is not None:
            for source in self._args.source:
                if source.endswith('*'):
                    val = source[:-1]
                    oper = OpServerUtils.MatchOp.PREFIX
                else:
                    val = source
                    oper = OpServerUtils.MatchOp.EQUAL
                source_match = OpServerUtils.Match(name=VizConstants.SOURCE,
                                                   value=val,
                                                   op=oper)
                where_or_list.append([])
                where_or_list[-1].append(source_match.__dict__)

        if self._args.module is not None:
            module_list = []
            for module in self._args.module:
                module_match = OpServerUtils.Match(
                    name=VizConstants.MODULE,
                    value=module,
                    op=OpServerUtils.MatchOp.EQUAL)
                module_list.append(module_match.__dict__)
            if not where_or_list:
                where_or_list = [[module] for module in module_list]
            else:
                where_or_list = [where_msg + [module] for where_msg in where_or_list \
                                                      for module in module_list]

        if self._args.category is not None:
            if self._args.category.endswith('*'):
                val = self._args.category[:-1]
                oper = OpServerUtils.MatchOp.PREFIX
            else:
                val = self._args.category
                oper = OpServerUtils.MatchOp.EQUAL
            category_match = OpServerUtils.Match(name=VizConstants.CATEGORY,
                                                 value=val,
                                                 op=oper)
            if not where_or_list:
                where_or_list = [[]]
            for where_msg in where_or_list:
                where_msg.append(category_match.__dict__)

        if self._args.message_type is not None:
            message_type_list = []
            for message_type in self._args.message_type:
                if message_type.endswith('*'):
                    val = message_type[:-1]
                    oper = OpServerUtils.MatchOp.PREFIX
                else:
                    val = message_type
                    oper = OpServerUtils.MatchOp.EQUAL
                message_type_match = OpServerUtils.Match(
                    name=VizConstants.MESSAGE_TYPE, value=val, op=oper)
                message_type_list.append(message_type_match.__dict__)
            if not where_or_list:
                where_or_list = [[message_type]
                                 for message_type in message_type_list]
            else:
                where_or_list = [where_msg + [message_type] for where_msg in where_or_list \
                                                            for message_type in message_type_list]

        if self._args.level is not None:
            level_match = OpServerUtils.Match(
                name=VizConstants.LEVEL,
                value=SandeshLevel._NAMES_TO_VALUES[self._args.level],
                op=OpServerUtils.MatchOp.LEQ)
            and_filter.append(level_match.__dict__)

        if self._args.node_type is not None:
            node_type_match = OpServerUtils.Match(
                name=VizConstants.NODE_TYPE,
                value=self._args.node_type,
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(node_type_match.__dict__)

        if self._args.instance_id is not None:
            instance_id_match = OpServerUtils.Match(
                name=VizConstants.INSTANCE_ID,
                value=self._args.instance_id,
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(instance_id_match.__dict__)

        # Object logs :
        # --object-type <> : All logs for the particular object type
        # --object-type <> --object-values : Object-id values for the particular
        #     object tye
        # --object-type <> --object-id <> : All logs matching object-id for
        #     particular object type
        if (self._args.object_type is not None
                or self._args.object_id is not None
                or self._args.object_select_field is not None
                or self._args.object_values is True):
            # Validate object-type
            if self._args.object_type is not None:
                if self._args.object_type in OBJECT_TYPE_LIST:
                    if self._args.object_type in OBJECT_TABLE_MAP:
                        table = OBJECT_TABLE_MAP[self._args.object_type]
                    else:
                        print 'Table not found for object-type [%s]' % \
                            (self._args.object_type)
                        return -1
                else:
                    print 'Unknown object-type [%s]' % (self._args.object_type)
                    return -1
            else:
                print 'Object-type required for query'
                return -1
            # Validate object-id and object-values
            if self._args.object_id is not None and \
               self._args.object_values is False:
                object_id = self._args.object_id
                if object_id.endswith("*"):
                    id_match = OpServerUtils.Match(
                        name=OpServerUtils.OBJECT_ID,
                        value=object_id[:-1],
                        op=OpServerUtils.MatchOp.PREFIX)
                else:
                    id_match = OpServerUtils.Match(
                        name=OpServerUtils.OBJECT_ID,
                        value=object_id,
                        op=OpServerUtils.MatchOp.EQUAL)
                if not where_or_list:
                    where_or_list.append([])
                for where_msg in where_or_list:
                    where_msg.append(id_match.__dict__)
            elif self._args.object_id is not None and \
               self._args.object_values is True:
                print 'Please specify either object-id or object-values but not both'
                return -1

            if self._args.object_values is False:
                if self._args.object_select_field is not None:
                    obj_sel_field = self._args.object_select_field
                    if not isinstance(self._args.object_select_field, list):
                        obj_sel_field = [self._args.object_select_field]
                    if VizConstants.OBJECT_LOG or VizConstants.SYSTEM_LOG \
                       in obj_sel_field:
                        self._args.object_select_field = obj_sel_field
                    else:
                        print 'Invalid object-select-field. '\
                           'Valid values are "%s" or "%s"' \
                           % (VizConstants.OBJECT_LOG,
                              VizConstants.SYSTEM_LOG)
                        return -1
                else:
                    self._args.object_select_field = obj_sel_field = [
                        VizConstants.OBJECT_LOG, VizConstants.SYSTEM_LOG
                    ]
                select_list = [
                    VizConstants.TIMESTAMP,
                    VizConstants.SOURCE,
                    VizConstants.MODULE,
                    VizConstants.MESSAGE_TYPE,
                ] + obj_sel_field
            else:
                if self._args.object_select_field:
                    print 'Please specify either object-id with ' + \
                        'object-select-field or only object-values'
                    return -1
                if len(where_or_list):
                    options = [
                        where['name'] for where_msg in where_or_list
                        for where in where_msg
                    ]
                    print 'Invalid/unsupported where-clause options %s for object-values query' % str(
                        options)
                    return -1
                select_list = [OpServerUtils.OBJECT_ID]

        elif self._args.trace is not None:
            table = VizConstants.COLLECTOR_GLOBAL_TABLE
            if self._args.source is None:
                print 'Source is required for trace buffer dump'
                return -1
            if self._args.module is None:
                print 'Module is required for trace buffer dump'
                return -1
            trace_buf_match = OpServerUtils.Match(
                name=VizConstants.CATEGORY,
                value=self._args.trace,
                op=OpServerUtils.MatchOp.EQUAL)
            if not where_or_list:
                where_or_list = [[]]
            for where_msg in where_or_list:
                where_msg.append(trace_buf_match.__dict__)
            select_list = [
                VizConstants.TIMESTAMP, VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM, VizConstants.DATA,
                VizConstants.SANDESH_TYPE
            ]
            sandesh_type_filter = OpServerUtils.Match(
                name=VizConstants.SANDESH_TYPE,
                value=str(SandeshType.TRACE),
                op=OpServerUtils.MatchOp.EQUAL)
            and_filter.append(sandesh_type_filter.__dict__)
        else:
            # Message Table Query
            table = VizConstants.COLLECTOR_GLOBAL_TABLE

            select_list = [
                VizConstants.TIMESTAMP,
                VizConstants.SOURCE,
                VizConstants.MODULE,
                VizConstants.CATEGORY,
                VizConstants.MESSAGE_TYPE,
                VizConstants.SEQUENCE_NUM,
                VizConstants.DATA,
                VizConstants.SANDESH_TYPE,
                VizConstants.LEVEL,
                VizConstants.NODE_TYPE,
                VizConstants.INSTANCE_ID,
            ]

        filter = None
        if len(or_filter):
            filter = [and_filter + [filt] for filt in or_filter]
        elif len(and_filter):
            filter = [and_filter]

        if self._args.keywords is not None:
            p = re.compile('\s*,\s*|\s+')
            if not where_or_list:
                where_or_list = [[]]
            for kwd in p.split(self._args.keywords):
                message_type_match = OpServerUtils.Match(
                    name=VizConstants.KEYWORD,
                    value=kwd,
                    op=OpServerUtils.MatchOp.EQUAL)
                for where_msg in where_or_list:
                    where_msg.append(message_type_match.__dict__)

        if not where_or_list:
            where = None
        else:
            where = where_or_list

        # Add sort by timestamp for non object value queries
        sort_op = None
        sort_fields = None
        if self._args.object_values is False:
            if self._args.reverse:
                sort_op = OpServerUtils.SortOp.DESCENDING
            else:
                sort_op = OpServerUtils.SortOp.ASCENDING
            sort_fields = [VizConstants.TIMESTAMP]

        if self._args.limit:
            limit = int(self._args.limit)
        else:
            limit = None

        messages_query = OpServerUtils.Query(table,
                                             start_time=start_time,
                                             end_time=end_time,
                                             select_fields=select_list,
                                             where=where,
                                             filter=filter,
                                             sort=sort_op,
                                             sort_fields=sort_fields,
                                             limit=limit)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(messages_query.__dict__))
        resp = OpServerUtils.post_url_http(messages_url,
                                           json.dumps(messages_query.__dict__),
                                           self._args.admin_user,
                                           self._args.admin_password)
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(
                self._args.analytics_api_ip, self._args.analytics_api_port,
                qid, self._args.admin_user, self._args.admin_password)
        return result
Ejemplo n.º 6
0
    def query(self):
        start_time, end_time = self._start_time, self._end_time
        flow_url = OpServerUtils.opserver_query_url(
            self._args.analytics_api_ip, self._args.analytics_api_port)
        where = []
        filter = []
        if self._args.vrouter is not None:
            vrouter_match = OpServerUtils.Match(name=self._VROUTER,
                                                value=self._args.vrouter,
                                                op=OpServerUtils.MatchOp.EQUAL)
            where.append(vrouter_match.__dict__)

        if self._args.source_vn is not None:
            source_vn_match = OpServerUtils.Match(
                name=self._SOURCE_VN,
                value=self._args.source_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_vn_match.__dict__)

        if self._args.destination_vn is not None:
            dest_vn_match = OpServerUtils.Match(
                name=self._DESTINATION_VN,
                value=self._args.destination_vn,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_vn_match.__dict__)

        if self._args.source_ip is not None:
            source_ip_match = OpServerUtils.Match(
                name=self._SOURCE_IP,
                value=self._args.source_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_ip_match.__dict__)

        if self._args.destination_ip is not None:
            dest_ip_match = OpServerUtils.Match(
                name=self._DESTINATION_IP,
                value=self._args.destination_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_ip_match.__dict__)

        if self._args.protocol is not None:
            protocol_match = OpServerUtils.Match(
                name=self._PROTOCOL,
                value=self._args.protocol,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(protocol_match.__dict__)

        if self._args.source_port is not None:
            source_port_match = OpServerUtils.Match(
                name=self._SOURCE_PORT,
                value=self._args.source_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(source_port_match.__dict__)

        if self._args.destination_port is not None:
            dest_port_match = OpServerUtils.Match(
                name=self._DESTINATION_PORT,
                value=self._args.destination_port,
                op=OpServerUtils.MatchOp.EQUAL)
            where.append(dest_port_match.__dict__)

        if self._args.action is not None:
            action_match = OpServerUtils.Match(name=self._ACTION,
                                               value=self._args.action,
                                               op=OpServerUtils.MatchOp.EQUAL)
            filter.append(action_match.__dict__)

        if self._args.vrouter_ip is not None:
            vrouter_ip_match = OpServerUtils.Match(
                name=self._VROUTER_IP,
                value=self._args.vrouter_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(vrouter_ip_match.__dict__)

        if self._args.other_vrouter_ip is not None:
            other_vrouter_ip_match = OpServerUtils.Match(
                name=self._OTHER_VROUTER_IP,
                value=self._args.other_vrouter_ip,
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(other_vrouter_ip_match.__dict__)

        if self._args.vmi_uuid is not None:
            vmi_match = OpServerUtils.Match(
                name=self._VMI_UUID,
                value=str(uuid.UUID(self._args.vmi_uuid)),
                op=OpServerUtils.MatchOp.EQUAL)
            filter.append(vmi_match.__dict__)

        # Flow Record Table Query
        table = VizConstants.FLOW_TABLE
        if len(where) == 0:
            where = None
        else:
            where = [where]

        select_list = [
            VizConstants.FLOW_TABLE_UUID, self._VROUTER, self._SETUP_TIME,
            self._TEARDOWN_TIME, self._SOURCE_VN, self._DESTINATION_VN,
            self._SOURCE_IP, self._DESTINATION_IP, self._PROTOCOL,
            self._SOURCE_PORT, self._DESTINATION_PORT, self._ACTION,
            self._DIRECTION, VizConstants.FLOW_TABLE_AGG_BYTES,
            VizConstants.FLOW_TABLE_AGG_PKTS, self._SG_RULE_UUID,
            self._NW_ACE_UUID, self._VROUTER_IP, self._OTHER_VROUTER_IP,
            self._VMI_UUID, self._DROP_REASON
        ]
        if self._args.tunnel_info:
            select_list.append(self._UNDERLAY_PROTO)
            select_list.append(self._UNDERLAY_SPORT)

        if len(filter) == 0:
            filter = None

        flow_query = OpServerUtils.Query(table,
                                         start_time=start_time,
                                         end_time=end_time,
                                         select_fields=select_list,
                                         where=where,
                                         filter=filter,
                                         dir=self._args.direction)
        if self._args.verbose:
            print 'Performing query: {0}'.format(
                json.dumps(flow_query.__dict__))
        print ''
        resp = OpServerUtils.post_url_http(flow_url,
                                           json.dumps(flow_query.__dict__),
                                           self._args.admin_user,
                                           self._args.admin_password)
        result = {}
        if resp is not None:
            resp = json.loads(resp)
            qid = resp['href'].rsplit('/', 1)[1]
            result = OpServerUtils.get_query_result(
                self._args.analytics_api_ip, self._args.analytics_api_port,
                qid, self._args.admin_user, self._args.admin_password)
        return result