Esempio n. 1
0
    def parse_args(self):
        """
        Eg. python flow.py --opserver-ip 127.0.0.1
                          --opserver-port 8081
                          --vrouter a6s23
                          --source-vn default-domain:default-project:vn1
                          --destination-vn default-domain:default-project:vn2
                          --source-ip 1.1.1.1
                          --destination-ip 2.2.2.2
                          --protocol TCP
                          --source-port 32678
                          --destination-port 80
                          --action drop
                          --direction ingress
                          [--start-time now-10m --end-time now] | --last 10m
        """
        defaults = {
            'opserver_ip': '127.0.0.1',
            'opserver_port': '8081',
            'start_time': 'now-10m',
            'end_time': 'now',
            'direction' : 'ingress',
        }

        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.set_defaults(**defaults)
        parser.add_argument("--opserver-ip", help="IP address of OpServer")
        parser.add_argument("--opserver-port", help="Port of OpServer")
        parser.add_argument(
            "--start-time", help="Flow record start time (format now-10m, now-1h)")
        parser.add_argument("--end-time", help="Flow record end time")
        parser.add_argument(
            "--last", help="Flow records from last time period (format 10m, 1d)")
        parser.add_argument("--vrouter", help="Flow records from vrouter")
        parser.add_argument("--source-vn",
            help="Flow records with source virtual network")
        parser.add_argument("--destination-vn",
            help="Flow records with destination virtual network")
        parser.add_argument("--source-ip",
            help="Flow records with source IP address")
        parser.add_argument("--destination-ip",
            help="Flow records with destination IP address")
        parser.add_argument("--protocol", help="Flow records with protocol")
        parser.add_argument("--source-port",
            help="Flow records with source port", type=int)
        parser.add_argument("--destination-port",
            help="Flow records with destination port", type=int)
        parser.add_argument("--action", help="Flow records with action")
        parser.add_argument("--direction", help="Flow direction",
            choices=['ingress', 'egress'])
        parser.add_argument(
            "--verbose", action="store_true", help="Show internal information")        
        self._args = parser.parse_args()

        # Validate start-time and end-time
        if self._args.last is not None:
            self._args.last = '-' + self._args.last
            self._start_time = OpServerUtils.convert_to_utc_timestamp_usec(
                self._args.last)
            self._end_time = OpServerUtils.convert_to_utc_timestamp_usec('now')
        else:
            try:
                if (self._args.start_time.isdigit() and
                        self._args.end_time.isdigit()):
                    self._start_time = int(self._args.start_time)
                    self._end_time = int(self._args.end_time)
                else:
                    self._start_time =\
                        OpServerUtils.convert_to_utc_timestamp_usec(
                            self._args.start_time)
                    self._end_time =\
                        OpServerUtils.convert_to_utc_timestamp_usec(
                            self._args.end_time)
            except:
                print 'Incorrect start-time (%s) or end-time (%s) format' %\
                    (self._args.start_time, self._args.end_time)
                return -1

        # Validate flow arguments
        if self._args.source_ip is not None and self._args.source_vn is None:
            print 'Please provide source virtual network in addtion to '\
                'source IP address'
            return -1
        if self._args.destination_ip is not None and \
                self._args.destination_vn is None:
            print 'Please provide destination virtual network in addtion to '\
                'destination IP address'
            return -1
        if self._args.source_port is not None and self._args.protocol is None:
            print 'Please provide protocol in addtion to source port'
            return -1
        if self._args.destination_port is not None and \
                self._args.protocol is None:
            print 'Please provide protocol in addtion to '\
                'destination port'
            return -1

        # Convert direction
        if self._args.direction.lower() == "ingress":
            self._args.direction = 1
        elif self._args.direction.lower() == "egress":
            self._args.direction = 0
        else:
            print 'Direction should be ingress or egress'
            return -1

        # Protocol
        if self._args.protocol is not None:
            if self._args.protocol.isalpha():
                protocol = OpServerUtils.str_to_ip_protocol(
                    self._args.protocol)
                if protocol == -1:
                    print 'Please provide valid protocol'
                    return -1
                self._args.protocol = protocol

        return 0
Esempio n. 2
0
    def parse_args(self):
        """
        Eg. python flow.py --opserver-ip 127.0.0.1
                          --opserver-port 8081
                          --vrouter a6s23
                          --source-vn default-domain:default-project:vn1
                          --destination-vn default-domain:default-project:vn2
                          --source-ip 1.1.1.1
                          --destination-ip 2.2.2.2
                          --protocol TCP
                          --source-port 32678
                          --destination-port 80
                          --action drop
                          --direction ingress
                          [--start-time now-10m --end-time now] | --last 10m
        """
        defaults = {
            'opserver_ip': '127.0.0.1',
            'opserver_port': '8081',
            'start_time': 'now-10m',
            'end_time': 'now',
            'direction': 'ingress',
        }

        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.set_defaults(**defaults)
        parser.add_argument("--opserver-ip", help="IP address of OpServer")
        parser.add_argument("--opserver-port", help="Port of OpServer")
        parser.add_argument(
            "--start-time",
            help="Flow record start time (format now-10m, now-1h)")
        parser.add_argument("--end-time", help="Flow record end time")
        parser.add_argument(
            "--last",
            help="Flow records from last time period (format 10m, 1d)")
        parser.add_argument("--vrouter", help="Flow records from vrouter")
        parser.add_argument("--source-vn",
                            help="Flow records with source virtual network")
        parser.add_argument(
            "--destination-vn",
            help="Flow records with destination virtual network")
        parser.add_argument("--source-ip",
                            help="Flow records with source IP address")
        parser.add_argument("--destination-ip",
                            help="Flow records with destination IP address")
        parser.add_argument("--protocol", help="Flow records with protocol")
        parser.add_argument("--source-port",
                            help="Flow records with source port",
                            type=int)
        parser.add_argument("--destination-port",
                            help="Flow records with destination port",
                            type=int)
        parser.add_argument("--action", help="Flow records with action")
        parser.add_argument("--direction",
                            help="Flow direction",
                            choices=['ingress', 'egress'])
        parser.add_argument("--verbose",
                            action="store_true",
                            help="Show internal information")
        self._args = parser.parse_args()

        # Validate start-time and end-time
        if self._args.last is not None:
            self._args.last = '-' + self._args.last
            self._start_time = OpServerUtils.convert_to_utc_timestamp_usec(
                self._args.last)
            self._end_time = OpServerUtils.convert_to_utc_timestamp_usec('now')
        else:
            try:
                if (self._args.start_time.isdigit()
                        and self._args.end_time.isdigit()):
                    self._start_time = int(self._args.start_time)
                    self._end_time = int(self._args.end_time)
                else:
                    self._start_time =\
                        OpServerUtils.convert_to_utc_timestamp_usec(
                            self._args.start_time)
                    self._end_time =\
                        OpServerUtils.convert_to_utc_timestamp_usec(
                            self._args.end_time)
            except:
                print 'Incorrect start-time (%s) or end-time (%s) format' %\
                    (self._args.start_time, self._args.end_time)
                return -1

        # Validate flow arguments
        if self._args.source_ip is not None and self._args.source_vn is None:
            print 'Please provide source virtual network in addtion to '\
                'source IP address'
            return -1
        if self._args.destination_ip is not None and \
                self._args.destination_vn is None:
            print 'Please provide destination virtual network in addtion to '\
                'destination IP address'
            return -1
        if self._args.source_port is not None and self._args.protocol is None:
            print 'Please provide protocol in addtion to source port'
            return -1
        if self._args.destination_port is not None and \
                self._args.protocol is None:
            print 'Please provide protocol in addtion to '\
                'destination port'
            return -1

        # Convert direction
        if self._args.direction.lower() == "ingress":
            self._args.direction = 1
        elif self._args.direction.lower() == "egress":
            self._args.direction = 0
        else:
            print 'Direction should be ingress or egress'
            return -1

        # Protocol
        if self._args.protocol is not None:
            if self._args.protocol.isalpha():
                protocol = OpServerUtils.str_to_ip_protocol(
                    self._args.protocol)
                if protocol == -1:
                    print 'Please provide valid protocol'
                    return -1
                self._args.protocol = protocol

        return 0