Beispiel #1
0
class CfgParser(object):
    CONF_DEFAULT_PATHS = ServicesDefaultConfigurationFiles.get(
        SERVICE_SNMP_COLLECTOR, None)
    def __init__(self, argv):
        self._devices = []
        self._args = None
        self.__pat = None
        self._argv = argv or ' '.join(sys.argv[1:])
        self._name = ModuleNames[Module.CONTRAIL_SNMP_COLLECTOR]
        self._cb = None

    def set_cb(self, cb=None):
        self._cb = cb

    def parse(self):
        '''
            command line example
contrail-snmp-scanner --log_level SYS_DEBUG
                      --logging_level DEBUG
                      --log_category test
                      --log_file <stdout>
                      --use_syslog
                      --syslog_facility LOG_USER
                      --conf_file /etc/contrail/contrail-snmp-scanner.conf

            conf file example:


[DEFAULTS]
log_local = 0
log_level = SYS_DEBUG
log_category =
log_file = /var/log/contrail/contrail-analytics-api.log
file = /etc/contrail/snmp-dev.ini

            /etc/contrail/snmp-dev.ini example:

#snmp version 1 or 2
[1.1.1.190]
Community = public
Version = 2

#snmp version 3
[1.1.1.191]
Version = 3
SecLevel = authPriv
AuthProto = SHA
AuthPass = foo
PrivProto = AES
PrivPass = foo
SecName = snmpuser
# Mibs default to all, to get a subset
Mibs = LldpTable, ArpTable

        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)

        kwargs = {'help': "Specify config file", 'metavar':"FILE",
                'action':'append', 'default':self.CONF_DEFAULT_PATHS}
        conf_parser.add_argument("-c", "--conf_file", **kwargs)
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'collectors'          : None,
            'log_local'           : False,
            'log_level'           : SandeshLevel.SYS_DEBUG,
            'log_category'        : '',
            'log_file'            : Sandesh._DEFAULT_LOG_FILE,
            'use_syslog'          : False,
            'syslog_facility'     : Sandesh._DEFAULT_SYSLOG_FACILITY,
            'scan_frequency'      : 600,
            'fast_scan_frequency' : 60,
            'http_server_port'    : HttpPortSnmpCollector,
            'zookeeper'           : '127.0.0.1:2181',
            'sandesh_send_rate_limit': SandeshSystem.get_sandesh_send_rate_limit(),
            'cluster_id'          :'',
        }
        api_opts = {
            'api_server_list' : ['127.0.0.1:8082'],
            'api_server_use_ssl' : False
        }
        ksopts = {
            'auth_host': '127.0.0.1',
            'auth_protocol': 'http',
            'auth_port': 35357,
            'admin_user': '******',
            'admin_password': '******',
            'admin_tenant_name': 'default-domain'
        }
        sandesh_opts = {
            'sandesh_keyfile': '/etc/contrail/ssl/private/server-privkey.pem',
            'sandesh_certfile': '/etc/contrail/ssl/certs/server.pem',
            'sandesh_ca_cert': '/etc/contrail/ssl/certs/ca-cert.pem',
            'sandesh_ssl_enable': False,
            'introspect_ssl_enable': False
        }

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items("DEFAULTS")))
            if 'API_SERVER' in config.sections():
                api_opts.update(dict(config.items("API_SERVER")))
            if 'KEYSTONE' in config.sections():
                ksopts.update(dict(config.items("KEYSTONE")))
            if 'SANDESH' in config.sections():
                sandesh_opts.update(dict(config.items('SANDESH')))
                if 'sandesh_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['sandesh_ssl_enable'] = config.getboolean(
                        'SANDESH', 'sandesh_ssl_enable')
                if 'introspect_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['introspect_ssl_enable'] = config.getboolean(
                        'SANDESH', 'introspect_ssl_enable')
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
        defaults.update(api_opts)
        defaults.update(ksopts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument("--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument(
            "--log_file",
            help="Filename for the logs to be written to")
        parser.add_argument("--log_local", action="store_true",
            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
            action="store_true",
            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
            help="Syslog facility to receive log lines")
        parser.add_argument("--scan_frequency", type=int,
            help="Time between snmp full poll")
        parser.add_argument("--fast_scan_frequency", type=int,
            help="Time between snmp interface status poll")
        parser.add_argument("--http_server_port", type=int,
            help="introspect server port")
        parser.add_argument("--auth_host",
                            help="ip of keystone server")
        parser.add_argument("--auth_protocol",
                            help="keystone authentication protocol")
        parser.add_argument("--auth_port", type=int,
                            help="ip of keystone server")
        parser.add_argument("--admin_user",
                            help="Name of keystone admin user")
        parser.add_argument("--admin_password",
                            help="Password of keystone admin user")
        parser.add_argument("--admin_tenant_name",
                            help="Tenant name for keystone admin user")
        parser.add_argument("--zookeeper",
            help="ip:port of zookeeper server")
        parser.add_argument("--cluster_id",
            help="Used for database keyspace separation")
        parser.add_argument("--sandesh_keyfile",
            help="Sandesh ssl private key")
        parser.add_argument("--sandesh_certfile",
            help="Sandesh ssl certificate")
        parser.add_argument("--sandesh_ca_cert",
            help="Sandesh CA ssl certificate")
        parser.add_argument("--sandesh_ssl_enable", action="store_true",
            help="Enable ssl for sandesh connection")
        parser.add_argument("--introspect_ssl_enable", action="store_true",
            help="Enable ssl for introspect connection")
        parser.add_argument("--api_server_list",
            help="List of api-servers in ip:port format separated by space",
            nargs="+")
        parser.add_argument("--api_server_use_ssl",
            help="Use SSL to connect to api-server")
        group = parser.add_mutually_exclusive_group(required=False)
        group.add_argument("--device-config-file",
            help="where to look for snmp credentials")
        group.add_argument("--sandesh_send_rate_limit", type=int,
            help="Sandesh send rate limit in messages/sec.")
        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.api_server_list) is str:
            self._args.api_server_list = self._args.api_server_list.split()
        self._args.config_sections = config
        self._args.conf_file = args.conf_file

    def devices(self):
        if self._args.device_config_file:
            self._devices = DeviceConfig.fom_file(
                    self._args.device_config_file)
        elif self._args.api_server_list:
            self._devices = DeviceConfig.fom_api_server(
                    self._args.api_server_list,
                    self._args.admin_user, self._args.admin_password,
                    self._args.admin_tenant_name,
                    self._args.api_server_use_ssl,
                    self._args.auth_host, self._args.auth_port,
                    self._args.auth_protocol, self._cb)
        return self._devices

    def collectors(self):
        return self._args.collectors

    def zookeeper_server(self):
        return self._args.zookeeper

    def cluster_id(self):
        return self._args.cluster_id
 
    def log_local(self):
        return self._args.log_local

    def log_category(self):
        return self._args.log_category

    def log_level(self):
        return self._args.log_level

    def log_file(self):
        return self._args.log_file

    def use_syslog(self):
        return self._args.use_syslog

    def syslog_facility(self):
        return self._args.syslog_facility

    def fast_scan_freq(self):
        return self._args.fast_scan_frequency

    def frequency(self):
        return self._args.scan_frequency

    def http_port(self):
        return self._args.http_server_port

    def sandesh_send_rate_limit(self):
        return self._args.sandesh_send_rate_limit

    def sandesh_config(self):
        return SandeshConfig(self._args.sandesh_keyfile,
                             self._args.sandesh_certfile,
                             self._args.sandesh_ca_cert,
                             self._args.sandesh_ssl_enable,
                             self._args.introspect_ssl_enable)
Beispiel #2
0
class CfgParser(object):
    CONF_DEFAULT_PATHS = ServicesDefaultConfigurationFiles.get(
        SERVICE_TOPOLOGY, None)

    def __init__(self, argv):
        self._args = None
        self.__pat = None
        self._argv = argv or ' '.join(sys.argv[1:])

    def parse(self):
        '''
            command line example
contrail-topology [-h] [-c FILE]
                         [--analytics_api ANALYTICS_API [ANALYTICS_API ...]]
                         [--collectors COLLECTORS [COLLECTORS ...]]
                         [--log_file LOG_FILE] [--log_local]
                         [--log_category LOG_CATEGORY] [--log_level LOG_LEVEL]
                         [--use_syslog] [--syslog_facility SYSLOG_FACILITY]
                         [--scan_frequency SCAN_FREQUENCY]
                         [--http_server_port HTTP_SERVER_PORT]

optional arguments:
  -h, --help            show this help message and exit
  -c FILE, --conf_file FILE
                        Specify config file
  --analytics_api ANALYTICS_API [ANALYTICS_API ...]
                        List of analytics-api IP addresses in ip:port format
  --collectors COLLECTORS [COLLECTORS ...]
                        List of Collector IP addresses in ip:port format
  --log_file LOG_FILE   Filename for the logs to be written to
  --log_local           Enable local logging of sandesh messages
  --log_category LOG_CATEGORY
                        Category filter for local logging of sandesh messages
  --log_level LOG_LEVEL
                        Severity level for local logging of sandesh messages
  --use_syslog          Use syslog for logging
  --syslog_facility SYSLOG_FACILITY
                        Syslog facility to receive log lines
  --scan_frequency SCAN_FREQUENCY
                        Time between snmp poll
  --http_server_port HTTP_SERVER_PORT
                        introspect server port

        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)

        kwargs = {
            'help': "Specify config file",
            'metavar': "FILE",
            'action': 'append',
            'default': self.CONF_DEFAULT_PATHS,
        }
        conf_parser.add_argument("-c", "--conf_file", **kwargs)
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'collectors': None,
            'analytics_api': ['127.0.0.1:' + str(OpServerAdminPort)],
            'log_local': False,
            'log_level': SandeshLevel.SYS_DEBUG,
            'log_category': '',
            'log_file': Sandesh._DEFAULT_LOG_FILE,
            'use_syslog': False,
            'syslog_facility': Sandesh._DEFAULT_SYSLOG_FACILITY,
            'scan_frequency': 60,
            'http_server_port': HttpPortTopology,
            'zookeeper': '127.0.0.1:2181',
            'sandesh_send_rate_limit':
            SandeshSystem.get_sandesh_send_rate_limit(),
            'cluster_id': '',
        }
        ksopts = {
            'auth_host': '127.0.0.1',
            'auth_protocol': 'http',
            'auth_port': 35357,
            'admin_user': '******',
            'admin_password': '******',
            'admin_tenant_name': 'default-domain'
        }
        sandesh_opts = {
            'sandesh_keyfile': '/etc/contrail/ssl/private/server-privkey.pem',
            'sandesh_certfile': '/etc/contrail/ssl/certs/server.pem',
            'sandesh_ca_cert': '/etc/contrail/ssl/certs/ca-cert.pem',
            'sandesh_ssl_enable': False,
            'introspect_ssl_enable': False
        }

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items("DEFAULTS")))
            if 'KEYSTONE' in config.sections():
                ksopts.update(dict(config.items("KEYSTONE")))
            if 'SANDESH' in config.sections():
                sandesh_opts.update(dict(config.items('SANDESH')))
                if 'sandesh_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['sandesh_ssl_enable'] = config.getboolean(
                        'SANDESH', 'sandesh_ssl_enable')
                if 'introspect_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['introspect_ssl_enable'] = config.getboolean(
                        'SANDESH', 'introspect_ssl_enable')
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
        defaults.update(ksopts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument(
            "--analytics_api",
            help="List of analytics-api IP addresses in ip:port format",
            nargs="+")
        parser.add_argument(
            "--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--log_file",
                            help="Filename for the logs to be written to")
        parser.add_argument("--log_local",
                            action="store_true",
                            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
                            action="store_true",
                            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
                            help="Syslog facility to receive log lines")
        parser.add_argument("--scan_frequency",
                            type=int,
                            help="Time between snmp poll")
        parser.add_argument("--http_server_port",
                            type=int,
                            help="introspect server port")
        parser.add_argument("--zookeeper", help="ip:port of zookeeper server")
        parser.add_argument("--sandesh_send_rate_limit",
                            type=int,
                            help="Sandesh send rate limit in messages/sec.")
        parser.add_argument("--cluster_id",
                            help="Used for database keyspace separation")
        parser.add_argument("--auth_host", help="ip of keystone server")
        parser.add_argument("--auth_protocol",
                            help="keystone authentication protocol")
        parser.add_argument("--auth_port",
                            type=int,
                            help="ip of keystone server")
        parser.add_argument("--admin_user", help="Name of keystone admin user")
        parser.add_argument("--admin_password",
                            help="Password of keystone admin user")
        parser.add_argument("--admin_tenant_name",
                            help="Tenant name for keystone admin user")
        parser.add_argument("--sandesh_keyfile",
                            help="Sandesh ssl private key")
        parser.add_argument("--sandesh_certfile",
                            help="Sandesh ssl certificate")
        parser.add_argument("--sandesh_ca_cert",
                            help="Sandesh CA ssl certificate")
        parser.add_argument("--sandesh_ssl_enable",
                            action="store_true",
                            help="Enable ssl for sandesh connection")
        parser.add_argument("--introspect_ssl_enable",
                            action="store_true",
                            help="Enable ssl for introspect connection")

        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.analytics_api) is str:
            self._args.analytics_api = self._args.analytics_api.split()

        self._args.config_sections = config
        self._args.conf_file = args.conf_file

    def _pat(self):
        if self.__pat is None:
            self.__pat = re.compile(', *| +')
        return self.__pat

    def _mklist(self, s):
        return self._pat().split(s)

    def collectors(self):
        return self._args.collectors

    def zookeeper_server(self):
        return self._args.zookeeper

    def cluster_id(self):
        return self._args.cluster_id

    def analytics_api(self):
        return self._args.analytics_api

    def log_local(self):
        return self._args.log_local

    def log_category(self):
        return self._args.log_category

    def log_level(self):
        return self._args.log_level

    def log_file(self):
        return self._args.log_file

    def use_syslog(self):
        return self._args.use_syslog

    def syslog_facility(self):
        return self._args.syslog_facility

    def frequency(self):
        return self._args.scan_frequency

    def http_port(self):
        return self._args.http_server_port

    def admin_user(self):
        return self._args.admin_user

    def admin_password(self):
        return self._args.admin_password

    def sandesh_send_rate_limit(self):
        return self._args.sandesh_send_rate_limit

    def sandesh_config(self):
        return SandeshConfig(self._args.sandesh_keyfile,
                             self._args.sandesh_certfile,
                             self._args.sandesh_ca_cert,
                             self._args.sandesh_ssl_enable,
                             self._args.introspect_ssl_enable)

    def api_svrs(self):
        a = self._disc.subscribe(API_SERVER_DISCOVERY_SERVICE_NAME, 0)
        x = a.read()
        return map(lambda d: d['ip-address'] + ':' + d['port'], x)

    def vnc_api(self, notifycb=None):
        e = SystemError('Cant connect to API server')
        for rt in (5, 2, 7, 9, 16, 25):
            for api_server in self.api_svrs():
                srv = api_server.split(':')
                if len(srv) == 2:
                    ip, port = srv[0], int(srv[1])
                else:
                    ip, port = '127.0.0.1', int(srv[0])
                try:
                    vnc = VncApi(self._args.admin_user,
                                 self._args.admin_password,
                                 self._args.admin_tenant_name,
                                 auth_host=self._args.auth_host,
                                 auth_port=self._args.auth_port,
                                 auth_protocol=self._args.auth_protocol)
                    if callable(notifycb):
                        notifycb('api', 'Connected', servers=api_server)
                    return vnc
                except Exception as e:
                    traceback.print_exc()
                    if callable(notifycb):
                        notifycb('api',
                                 'Not connected',
                                 servers=api_server,
                                 up=False)
                    time.sleep(rt)
        raise e
Beispiel #3
0
    def parse(self):
        '''
            command line example
            contrail-alarm-gen  --log_level SYS_DEBUG
                    --logging_level DEBUG
                    --log_category test
                    --log_file <stdout>
                    --use_syslog
                    --syslog_facility LOG_USER
                    --worker_id 0
                    --partitions 5
                    --redis_password
                    --http_server_port 5995
                    --redis_server_port 6379
                    --redis_uve_list 127.0.0.1:6379
                    --alarmgen_list 127.0.0.1:0
                    --kafka_broker_list 127.0.0.1:9092
                    --zk_list 127.0.0.1:2181
                    --rabbitmq_server_list 127.0.0.1:5672
                    --conf_file /etc/contrail/contrail-alarm-gen.conf
        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)
        conf_parser.add_argument("-c",
                                 "--conf_file",
                                 action="append",
                                 help="Specify config file",
                                 metavar="FILE",
                                 default=ServicesDefaultConfigurationFiles.get(
                                     SERVICE_ALARM_GENERATOR, None))
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'host_ip': '127.0.0.1',
            'collectors': [],
            'kafka_broker_list': ['127.0.0.1:9092'],
            'log_local': False,
            'log_level': SandeshLevel.SYS_DEBUG,
            'log_category': '',
            'log_file': Sandesh._DEFAULT_LOG_FILE,
            'use_syslog': False,
            'syslog_facility': Sandesh._DEFAULT_SYSLOG_FACILITY,
            'http_server_port': 5995,
            'worker_id': '0',
            'partitions': 15,
            'zk_list': None,
            'rabbitmq_server_list': None,
            'rabbitmq_port': 5672,
            'rabbitmq_user': '******',
            'rabbitmq_password': '******',
            'rabbitmq_vhost': None,
            'rabbitmq_ha_mode': False,
            'rabbitmq_use_ssl': False,
            'kombu_ssl_version': '',
            'kombu_ssl_keyfile': '',
            'kombu_ssl_certfile': '',
            'kombu_ssl_ca_certs': '',
            'alarmgen_list': ['127.0.0.1:0'],
            'sandesh_send_rate_limit':
            SandeshSystem.get_sandesh_send_rate_limit(),
            'cluster_id': '',
        }

        redis_opts = {
            'redis_server_port': 6379,
            'redis_password': None,
            'redis_uve_list': ['127.0.0.1:6379'],
        }

        keystone_opts = {
            'auth_host': '127.0.0.1',
            'auth_protocol': 'http',
            'auth_port': 35357,
            'admin_user': '******',
            'admin_password': '******',
            'admin_tenant_name': 'default-domain'
        }

        sandesh_opts = {
            'sandesh_keyfile': '/etc/contrail/ssl/private/server-privkey.pem',
            'sandesh_certfile': '/etc/contrail/ssl/certs/server.pem',
            'sandesh_ca_cert': '/etc/contrail/ssl/certs/ca-cert.pem',
            'sandesh_ssl_enable': False,
            'introspect_ssl_enable': False
        }

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items('DEFAULTS')))
            if 'REDIS' in config.sections():
                redis_opts.update(dict(config.items('REDIS')))
            if 'KEYSTONE' in config.sections():
                keystone_opts.update(dict(config.items('KEYSTONE')))
            if 'SANDESH' in config.sections():
                sandesh_opts.update(dict(config.items('SANDESH')))
                if 'sandesh_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['sandesh_ssl_enable'] = config.getboolean(
                        'sandesh', 'sandesh_ssl_enable')
                if 'introspect_ssl_enable' in config.options('SANDESH'):
                    sandesh_opts['introspect_ssl_enable'] = config.getboolean(
                        'sandesh', 'introspect_ssl_enable')
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )

        defaults.update(redis_opts)
        defaults.update(keystone_opts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument("--host_ip", help="Host IP address")
        parser.add_argument(
            "--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--log_file",
                            help="Filename for the logs to be written to")
        parser.add_argument("--log_local",
                            action="store_true",
                            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
                            action="store_true",
                            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
                            help="Syslog facility to receive log lines")
        parser.add_argument("--http_server_port",
                            type=int,
                            help="introspect server port")
        parser.add_argument("--worker_id", help="Worker Id")
        parser.add_argument("--partitions",
                            type=int,
                            help="Number of partitions for hashing UVE keys")
        parser.add_argument("--redis_server_port",
                            type=int,
                            help="Redis server port")
        parser.add_argument("--redis_password", help="Redis server password")
        parser.add_argument(
            "--kafka_broker_list",
            help="List of bootstrap kafka brokers in ip:port format",
            nargs="+")
        parser.add_argument("--zk_list",
                            help="List of zookeepers in ip:port format",
                            nargs="+")
        parser.add_argument(
            "--rabbitmq_server_list",
            type=str,
            help="List of Rabbitmq server ip address separated by comma")
        parser.add_argument("--rabbitmq_port", help="Rabbitmq server port")
        parser.add_argument("--rabbitmq_user", help="Username for Rabbitmq")
        parser.add_argument("--rabbitmq_password",
                            help="Password for Rabbitmq")
        parser.add_argument("--rabbitmq_vhost", help="vhost for Rabbitmq")
        parser.add_argument(
            "--rabbitmq_ha_mode",
            action="store_true",
            help="True if the rabbitmq cluster is mirroring all queue")
        parser.add_argument(
            "--redis_uve_list",
            help="List of redis-uve in ip:port format. For internal use only",
            nargs="+")
        parser.add_argument(
            "--alarmgen_list",
            help="List of alarmgens in ip:inst format. For internal use only",
            nargs="+")
        parser.add_argument("--sandesh_send_rate_limit",
                            type=int,
                            help="Sandesh send rate limit in messages/sec")
        parser.add_argument("--cluster_id", help="Analytics Cluster Id")
        parser.add_argument("--auth_host", help="ip of keystone server")
        parser.add_argument("--auth_protocol",
                            help="keystone authentication protocol")
        parser.add_argument("--auth_port",
                            type=int,
                            help="ip of keystone server")
        parser.add_argument("--admin_user", help="Name of keystone admin user")
        parser.add_argument("--admin_password",
                            help="Password of keystone admin user")
        parser.add_argument("--admin_tenant_name",
                            help="Tenant name for keystone admin user")
        parser.add_argument("--sandesh_keyfile",
                            help="Sandesh ssl private key")
        parser.add_argument("--sandesh_certfile",
                            help="Sandesh ssl certificate")
        parser.add_argument("--sandesh_ca_cert",
                            help="Sandesh CA ssl certificate")
        parser.add_argument("--sandesh_ssl_enable",
                            action="store_true",
                            help="Enable ssl for sandesh connection")
        parser.add_argument("--introspect_ssl_enable",
                            action="store_true",
                            help="Enable ssl for introspect connection")
        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.kafka_broker_list) is str:
            self._args.kafka_broker_list = self._args.kafka_broker_list.split()
        if type(self._args.zk_list) is str:
            self._args.zk_list = self._args.zk_list.split()
        if type(self._args.redis_uve_list) is str:
            self._args.redis_uve_list = self._args.redis_uve_list.split()
        if type(self._args.alarmgen_list) is str:
            self._args.alarmgen_list = self._args.alarmgen_list.split()
        self._args.conf_file = args.conf_file
Beispiel #4
0
class CfgParser(object):
    CONF_DEFAULT_PATHS = ServicesDefaultConfigurationFiles.get(
        SERVICE_TOPOLOGY, None)

    def __init__(self, argv):
        self._args = None
        self.__pat = None
        self._argv = argv or ' '.join(sys.argv[1:])

    def parse(self):
        '''
            command line example
tf-topology [-h] [-c FILE]
                 [--analytics_api ANALYTICS_API [ANALYTICS_API ...]]
                 [--collectors COLLECTORS [COLLECTORS ...]]
                 [--log_file LOG_FILE] [--log_local]
                 [--log_category LOG_CATEGORY] [--log_level LOG_LEVEL]
                 [--use_syslog] [--syslog_facility SYSLOG_FACILITY]
                 [--scan_frequency SCAN_FREQUENCY]
                 [--http_server_port HTTP_SERVER_PORT]

optional arguments:
  -h, --help            show this help message and exit
  -c FILE, --conf_file FILE
                        Specify config file
  --analytics_api ANALYTICS_API [ANALYTICS_API ...]
                        List of analytics-api IP addresses in ip:port format
  --collectors COLLECTORS [COLLECTORS ...]
                        List of Collector IP addresses in ip:port format
  --log_file LOG_FILE   Filename for the logs to be written to
  --log_local           Enable local logging of sandesh messages
  --log_category LOG_CATEGORY
                        Category filter for local logging of sandesh messages
  --log_level LOG_LEVEL
                        Severity level for local logging of sandesh messages
  --use_syslog          Use syslog for logging
  --syslog_facility SYSLOG_FACILITY
                        Syslog facility to receive log lines
  --scan_frequency SCAN_FREQUENCY
                        Time between snmp poll
  --http_server_port HTTP_SERVER_PORT
                        introspect server port

        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)

        kwargs = {
            'help': "Specify config file",
            'metavar': "FILE",
            'action': 'append',
            'default': self.CONF_DEFAULT_PATHS,
        }
        conf_parser.add_argument("-c", "--conf_file", **kwargs)
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'collectors': None,
            'analytics_api': ['127.0.0.1:' + str(OpServerAdminPort)],
            'log_local': False,
            'log_level': SandeshLevel.SYS_DEBUG,
            'log_category': '',
            'log_file': Sandesh._DEFAULT_LOG_FILE,
            'use_syslog': False,
            'syslog_facility': Sandesh._DEFAULT_SYSLOG_FACILITY,
            'scan_frequency': 60,
            'http_server_port': HttpPortTopology,
            'zookeeper': '127.0.0.1:2181',
            'cluster_id': '',
        }
        defaults.update(SandeshConfig.get_default_options(['DEFAULTS']))
        ksopts = {
            'auth_host': '127.0.0.1',
            'auth_protocol': 'http',
            'auth_port': 35357,
            'admin_user': '******',
            'admin_password': '******',
            'admin_tenant_name': 'default-domain'
        }
        configdb_opts = {
            'rabbitmq_server_list': None,
            'rabbitmq_port': 5672,
            'rabbitmq_user': '******',
            'rabbitmq_password': '******',
            'rabbitmq_vhost': None,
            'rabbitmq_ha_mode': False,
            'rabbitmq_use_ssl': False,
            'rabbitmq_ssl_version': '',
            'rabbitmq_ssl_keyfile': '',
            'rabbitmq_ssl_certfile': '',
            'rabbitmq_ssl_ca_certs': '',
            'config_db_server_list': None,
            'config_db_username': None,
            'config_db_password': None,
            'config_db_use_ssl': False,
            'config_db_ca_certs': None
        }
        sandesh_opts = SandeshConfig.get_default_options()

        config = None
        if args.conf_file:
            config = configparser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items("DEFAULTS")))
            if 'KEYSTONE' in config.sections():
                ksopts.update(dict(config.items("KEYSTONE")))
            if 'CONFIGDB' in config.sections():
                configdb_opts.update(dict(config.items('CONFIGDB')))
            SandeshConfig.update_options(sandesh_opts, config)
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
        defaults.update(ksopts)
        defaults.update(configdb_opts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument(
            "--analytics_api",
            help="List of analytics-api IP addresses in ip:port format",
            nargs="+")
        parser.add_argument(
            "--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--log_file",
                            help="Filename for the logs to be written to")
        parser.add_argument("--log_local",
                            action="store_true",
                            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
                            action="store_true",
                            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
                            help="Syslog facility to receive log lines")
        parser.add_argument("--scan_frequency",
                            type=int,
                            help="Time between snmp poll")
        parser.add_argument("--http_server_port",
                            type=int,
                            help="introspect server port")
        parser.add_argument("--zookeeper", help="ip:port of zookeeper server")
        parser.add_argument("--cluster_id",
                            help="Used for database keyspace separation")
        parser.add_argument("--auth_host", help="ip of keystone server")
        parser.add_argument("--auth_protocol",
                            help="keystone authentication protocol")
        parser.add_argument("--auth_port",
                            type=int,
                            help="ip of keystone server")
        parser.add_argument("--admin_user", help="Name of keystone admin user")
        parser.add_argument("--admin_password",
                            help="Password of keystone admin user")
        parser.add_argument("--admin_tenant_name",
                            help="Tenant name for keystone admin user")
        parser.add_argument("--rabbitmq_server_list",
                            help="List of Rabbitmq servers in ip:port format")
        parser.add_argument("--rabbitmq_user", help="Username for Rabbitmq")
        parser.add_argument("--rabbitmq_password",
                            help="Password for Rabbitmq")
        parser.add_argument("--rabbitmq_vhost", help="vhost for Rabbitmq")
        parser.add_argument(
            "--rabbitmq_ha_mode",
            action="store_true",
            help="True if the rabbitmq cluster is mirroring all queue")
        parser.add_argument("--rabbitmq_use_ssl",
                            action="store_true",
                            help="Use SSL for RabbitMQ connection")
        parser.add_argument("--rabbitmq_ssl_keyfile",
                            help="Keyfile for SSL RabbitMQ connection")
        parser.add_argument(
            "--rabbitmq_ssl_certfile",
            help="Certificate file for SSL RabbitMQ connection")
        parser.add_argument(
            "--rabbitmq_ssl_ca_certs",
            help="CA Certificate file for SSL RabbitMQ connection")
        parser.add_argument("--config_db_server_list",
                            help="List of cassandra servers in ip:port format",
                            nargs='+')
        parser.add_argument("--config_db_username", help="Cassandra user name")
        parser.add_argument("--config_db_password", help="Cassandra password")
        parser.add_argument("--config_db_use_ssl",
                            help="Cassandra SSL enable flag")
        parser.add_argument("--config_db_ca_certs",
                            help="Cassandra CA certs file path")
        SandeshConfig.add_parser_arguments(parser)

        self._args = parser.parse_args(remaining_argv)
        if isinstance(self._args.collectors, (basestring, str)):
            self._args.collectors = self._args.collectors.split()
        if isinstance(self._args.analytics_api, (basestring, str)):
            self._args.analytics_api = self._args.analytics_api.split()
        if isinstance(self._args.config_db_server_list, (basestring, str)):
            self._args.config_db_server_list = \
                self._args.config_db_server_list.split()
        self._args.config_sections = config
        self._args.conf_file = args.conf_file
        self._args.config_db_use_ssl = (str(
            self._args.config_db_use_ssl).lower() == 'true')

    def _pat(self):
        if self.__pat is None:
            self.__pat = re.compile(', *| +')
        return self.__pat

    def _mklist(self, s):
        return self._pat().split(s)

    def collectors(self):
        return self._args.collectors

    def zookeeper_server(self):
        return self._args.zookeeper

    def cluster_id(self):
        return self._args.cluster_id

    def analytics_api(self):
        return self._args.analytics_api

    def log_local(self):
        return self._args.log_local

    def log_category(self):
        return self._args.log_category

    def log_level(self):
        return self._args.log_level

    def log_file(self):
        return self._args.log_file

    def use_syslog(self):
        return self._args.use_syslog

    def syslog_facility(self):
        return self._args.syslog_facility

    def frequency(self):
        return self._args.scan_frequency

    def http_port(self):
        return self._args.http_server_port

    def admin_user(self):
        return self._args.admin_user

    def admin_password(self):
        return self._args.admin_password

    def sandesh_config(self):
        return SandeshConfig.from_parser_arguments(self._args)

    def rabbitmq_params(self):
        return {
            'servers': self._args.rabbitmq_server_list,
            'port': self._args.rabbitmq_port,
            'user': self._args.rabbitmq_user,
            'password': self._args.rabbitmq_password,
            'vhost': self._args.rabbitmq_vhost,
            'ha_mode': self._args.rabbitmq_ha_mode,
            'use_ssl': self._args.rabbitmq_use_ssl,
            'ssl_version': self._args.rabbitmq_ssl_version,
            'ssl_keyfile': self._args.rabbitmq_ssl_keyfile,
            'ssl_certfile': self._args.rabbitmq_ssl_certfile,
            'ssl_ca_certs': self._args.rabbitmq_ssl_ca_certs
        }

    def cassandra_params(self):
        return {
            'servers': self._args.config_db_server_list,
            'user': self._args.config_db_username,
            'password': self._args.config_db_password,
            'use_ssl': self._args.config_db_use_ssl,
            'ca_certs': self._args.config_db_ca_certs,
            'cluster_id': self._args.cluster_id
        }
    def parse(self):
        '''
            command line example
            contrail-alarm-gen  --log_level SYS_DEBUG
                    --logging_level DEBUG
                    --log_category test
                    --log_file <stdout>
                    --use_syslog
                    --syslog_facility LOG_USER
                    --worker_id 0
                    --partitions 5
                    --redis_password
                    --http_server_port 5995
                    --redis_server_port 6379
                    --redis_uve_list 127.0.0.1:6379
                    --alarmgen_list 127.0.0.1:0
                    --kafka_broker_list 127.0.0.1:9092
                    --zk_list 127.0.0.1:2181
                    --rabbitmq_server_list 127.0.0.1:5672
                    --conf_file /etc/contrail/contrail-alarm-gen.conf
        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)
        conf_parser.add_argument("-c",
                                 "--conf_file",
                                 action="append",
                                 help="Specify config file",
                                 metavar="FILE",
                                 default=ServicesDefaultConfigurationFiles.get(
                                     SERVICE_ALARM_GENERATOR, None))
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'host_ip': '127.0.0.1',
            'collectors': [],
            'log_local': False,
            'log_level': SandeshLevel.SYS_DEBUG,
            'log_category': '',
            'log_file': Sandesh._DEFAULT_LOG_FILE,
            'use_syslog': False,
            'syslog_facility': Sandesh._DEFAULT_SYSLOG_FACILITY,
            'http_server_port': 5995,
            'worker_id': '0',
            'partitions': 15,
            'zk_list': None,
            'alarmgen_list': ['127.0.0.1:0'],
            'cluster_id': '',
        }
        defaults.update(SandeshConfig.get_default_options(['DEFAULTS']))

        redis_opts = {
            'redis_server_port': 6379,
            'redis_password': None,
            'redis_uve_list': ['127.0.0.1:6379'],
        }

        configdb_opts = {
            'rabbitmq_server_list': None,
            'rabbitmq_port': 5672,
            'rabbitmq_user': '******',
            'rabbitmq_password': '******',
            'rabbitmq_vhost': None,
            'rabbitmq_ha_mode': False,
            'rabbitmq_use_ssl': False,
            'kombu_ssl_version': '',
            'kombu_ssl_keyfile': '',
            'kombu_ssl_certfile': '',
            'kombu_ssl_ca_certs': '',
            'config_db_server_list': None,
            'config_db_username': None,
            'config_db_password': None,
            'config_db_use_ssl': False,
            'config_db_ca_certs': None
        }

        kafka_opts = {
            'kafka_broker_list': ['127.0.0.1:9092'],
            'kafka_ssl_enable': False,
            'kafka_keyfile': '/etc/contrail/ssl/private/server-privkey.pem',
            'kafka_certfile': '/etc/contrail/ssl/certs/server.pem',
            'kafka_ca_cert': '/etc/contrail/ssl/certs/ca-cert.pem'
        }

        sandesh_opts = SandeshConfig.get_default_options()

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items('DEFAULTS')))
            if 'REDIS' in config.sections():
                redis_opts.update(dict(config.items('REDIS')))
            if 'CONFIGDB' in config.sections():
                configdb_opts.update(dict(config.items('CONFIGDB')))
            if 'KAFKA' in config.sections():
                kafka_opts.update(dict(config.items('KAFKA')))
            SandeshConfig.update_options(sandesh_opts, config)
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )

        defaults.update(redis_opts)
        defaults.update(configdb_opts)
        defaults.update(sandesh_opts)
        defaults.update(kafka_opts)
        parser.set_defaults(**defaults)
        parser.add_argument("--host_ip", help="Host IP address")
        parser.add_argument(
            "--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--log_file",
                            help="Filename for the logs to be written to")
        parser.add_argument("--log_local",
                            action="store_true",
                            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
                            action="store_true",
                            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
                            help="Syslog facility to receive log lines")
        parser.add_argument("--http_server_port",
                            type=int,
                            help="introspect server port")
        parser.add_argument("--worker_id", help="Worker Id")
        parser.add_argument("--partitions",
                            type=int,
                            help="Number of partitions for hashing UVE keys")
        parser.add_argument("--redis_server_port",
                            type=int,
                            help="Redis server port")
        parser.add_argument("--redis_password", help="Redis server password")
        parser.add_argument(
            "--kafka_broker_list",
            help="List of bootstrap kafka brokers in ip:port format",
            nargs="+")
        parser.add_argument("--zk_list",
                            help="List of zookeepers in ip:port format",
                            nargs="+")
        parser.add_argument(
            "--rabbitmq_server_list",
            type=str,
            help="List of Rabbitmq server ip address separated by comma")
        parser.add_argument("--rabbitmq_port", help="Rabbitmq server port")
        parser.add_argument("--rabbitmq_user", help="Username for Rabbitmq")
        parser.add_argument("--rabbitmq_password",
                            help="Password for Rabbitmq")
        parser.add_argument("--rabbitmq_vhost", help="vhost for Rabbitmq")
        parser.add_argument(
            "--rabbitmq_ha_mode",
            action="store_true",
            help="True if the rabbitmq cluster is mirroring all queue")
        parser.add_argument("--config_db_server_list",
                            help="List of cassandra servers in ip:port format",
                            nargs='+')
        parser.add_argument("--config_db_username", help="Cassandra user name")
        parser.add_argument("--config_db_password", help="Cassandra password")
        parser.add_argument("--config_db_use_ssl",
                            help="Cassandra SSL enable flag")
        parser.add_argument("--config_db_ca_certs",
                            help="Cassandra CA certs file path")
        parser.add_argument(
            "--redis_uve_list",
            help="List of redis-uve in ip:port format. For internal use only",
            nargs="+")
        parser.add_argument(
            "--alarmgen_list",
            help="List of alarmgens in ip:inst format. For internal use only",
            nargs="+")
        parser.add_argument("--cluster_id", help="Analytics Cluster Id")
        parser.add_argument("--kafka_ssl_enable",
                            action='store_true',
                            help="Enable SSL encryption for kafka connection")
        parser.add_argument("--kafka_keyfile",
                            type=str,
                            help="Location of kafka ssl private key")
        parser.add_argument("--kafka_certfile",
                            type=str,
                            help="Location of kafka ssl host certificate")
        parser.add_argument("--kafka_ca_cert",
                            type=str,
                            help="Location of kafka ssl CA certificate")
        SandeshConfig.add_parser_arguments(parser)
        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.kafka_broker_list) is str:
            self._args.kafka_broker_list = self._args.kafka_broker_list.split()
        if type(self._args.zk_list) is str:
            self._args.zk_list = self._args.zk_list.split()
        if type(self._args.redis_uve_list) is str:
            self._args.redis_uve_list = self._args.redis_uve_list.split()
        if type(self._args.alarmgen_list) is str:
            self._args.alarmgen_list = self._args.alarmgen_list.split()
        if type(self._args.config_db_server_list) is str:
            self._args.config_db_server_list = \
                self._args.config_db_server_list.split()
        self._args.conf_file = args.conf_file
        redis_agg_db_offset = os.getenv('ALARMGEN_REDIS_AGGREGATE_DB_OFFSET',
                                        "1")
        self._args._redis_agg_db = ALARMGEN_REDIS_AGGREGATE_DB_BASE_INDEX + \
                                   int(redis_agg_db_offset) - 1
        self._args.kafka_ssl_enable = (str(
            self._args.kafka_ssl_enable).lower() == 'true')
        self._args.config_db_use_ssl = (str(
            self._args.config_db_use_ssl).lower() == 'true')
    def parse(self):
        '''
            command line example
            contrail-alarm-gen  --log_level SYS_DEBUG
                    --logging_level DEBUG
                    --log_category test
                    --log_file <stdout>
                    --use_syslog
                    --syslog_facility LOG_USER
                    --worker_id 0
                    --partitions 5
                    --redis_password
                    --http_server_port 5995
                    --redis_server_port 6379
                    --redis_uve_list 127.0.0.1:6379
                    --alarmgen_list 127.0.0.1:0
                    --kafka_broker_list 127.0.0.1:9092
                    --zk_list 127.0.0.1:2181
                    --rabbitmq_server_list 127.0.0.1:5672
                    --conf_file /etc/contrail/contrail-alarm-gen.conf
        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)
        conf_parser.add_argument("-c", "--conf_file", action="append",
            help="Specify config file", metavar="FILE",
            default=ServicesDefaultConfigurationFiles.get(
                SERVICE_ALARM_GENERATOR, None))
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'host_ip'           : '127.0.0.1',
            'collectors'        : [],
            'kafka_broker_list' : ['127.0.0.1:9092'],
            'log_local'         : False,
            'log_level'         : SandeshLevel.SYS_DEBUG,
            'log_category'      : '',
            'log_file'          : Sandesh._DEFAULT_LOG_FILE,
            'use_syslog'        : False,
            'syslog_facility'   : Sandesh._DEFAULT_SYSLOG_FACILITY,
            'http_server_port'  : 5995,
            'worker_id'         : '0',
            'partitions'        : 15,
            'zk_list'           : None,
            'alarmgen_list'     : ['127.0.0.1:0'],
            'cluster_id'     :'',
        }
        defaults.update(SandeshConfig.get_default_options(['DEFAULTS']))

        redis_opts = {
            'redis_server_port'  : 6379,
            'redis_password'     : None,
            'redis_uve_list'    : ['127.0.0.1:6379'],
        }

        configdb_opts = {
            'rabbitmq_server_list': None,
            'rabbitmq_port': 5672,
            'rabbitmq_user': '******',
            'rabbitmq_password': '******',
            'rabbitmq_vhost': None,
            'rabbitmq_ha_mode': False,
            'rabbitmq_use_ssl': False,
            'kombu_ssl_version': '',
            'kombu_ssl_keyfile': '',
            'kombu_ssl_certfile': '',
            'kombu_ssl_ca_certs': '',
            'config_db_server_list': None,
            'config_db_username': None,
            'config_db_password': None
        }

        sandesh_opts = SandeshConfig.get_default_options()

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items('DEFAULTS')))
            if 'REDIS' in config.sections():
                redis_opts.update(dict(config.items('REDIS')))
            if 'CONFIGDB' in config.sections():
                configdb_opts.update(dict(config.items('CONFIGDB')))
            SandeshConfig.update_options(sandesh_opts, config)
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )

        defaults.update(redis_opts)
        defaults.update(configdb_opts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument("--host_ip",
            help="Host IP address")
        parser.add_argument("--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument(
            "--log_file",
            help="Filename for the logs to be written to")
        parser.add_argument("--log_local", action="store_true",
            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category", 
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",  
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
            action="store_true",
            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
            help="Syslog facility to receive log lines")
        parser.add_argument("--http_server_port", type=int,
            help="introspect server port")
        parser.add_argument("--worker_id",
            help="Worker Id")
        parser.add_argument("--partitions", type=int,
            help="Number of partitions for hashing UVE keys")
        parser.add_argument("--redis_server_port",
            type=int,
            help="Redis server port")
        parser.add_argument("--redis_password",
            help="Redis server password")
        parser.add_argument("--kafka_broker_list",
            help="List of bootstrap kafka brokers in ip:port format",
            nargs="+")
        parser.add_argument("--zk_list",
            help="List of zookeepers in ip:port format",
            nargs="+")
        parser.add_argument("--rabbitmq_server_list", type=str,
            help="List of Rabbitmq server ip address separated by comma")
        parser.add_argument("--rabbitmq_port",
            help="Rabbitmq server port")
        parser.add_argument("--rabbitmq_user",
            help="Username for Rabbitmq")
        parser.add_argument("--rabbitmq_password",
            help="Password for Rabbitmq")
        parser.add_argument("--rabbitmq_vhost",
            help="vhost for Rabbitmq")
        parser.add_argument("--rabbitmq_ha_mode",
            action="store_true",
            help="True if the rabbitmq cluster is mirroring all queue")
        parser.add_argument("--config_db_server_list",
            help="List of cassandra servers in ip:port format",
            nargs='+')
        parser.add_argument("--config_db_username",
            help="Cassandra user name")
        parser.add_argument("--config_db_password",
            help="Cassandra password")
        parser.add_argument("--redis_uve_list",
            help="List of redis-uve in ip:port format. For internal use only",
            nargs="+")
        parser.add_argument("--alarmgen_list",
            help="List of alarmgens in ip:inst format. For internal use only",
            nargs="+")
        parser.add_argument("--cluster_id",
            help="Analytics Cluster Id")
        SandeshConfig.add_parser_arguments(parser)
        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.kafka_broker_list) is str:
            self._args.kafka_broker_list= self._args.kafka_broker_list.split()
        if type(self._args.zk_list) is str:
            self._args.zk_list= self._args.zk_list.split()
        if type(self._args.redis_uve_list) is str:
            self._args.redis_uve_list = self._args.redis_uve_list.split()
        if type(self._args.alarmgen_list) is str:
            self._args.alarmgen_list = self._args.alarmgen_list.split()
        if type(self._args.config_db_server_list) is str:
            self._args.config_db_server_list = \
                self._args.config_db_server_list.split()
        self._args.conf_file = args.conf_file
Beispiel #7
0
class CfgParser(object):
    CONF_DEFAULT_PATHS = ServicesDefaultConfigurationFiles.get(
        SERVICE_SNMP_COLLECTOR, None)

    def __init__(self, argv):
        self._args = None
        self.__pat = None
        self._argv = argv or ' '.join(sys.argv[1:])
        self._name = ModuleNames[Module.CONTRAIL_SNMP_COLLECTOR]
        self._cb = None

    def set_cb(self, cb=None):
        self._cb = cb

    def parse(self):
        '''
            command line example
contrail-snmp-scanner --log_level SYS_DEBUG
                      --logging_level DEBUG
                      --log_category test
                      --log_file <stdout>
                      --use_syslog
                      --syslog_facility LOG_USER
                      --conf_file /etc/contrail/contrail-snmp-scanner.conf

            conf file example:


[DEFAULTS]
log_local = 0
log_level = SYS_DEBUG
log_category =
log_file = /var/log/contrail/contrail-analytics-api.log
file = /etc/contrail/snmp-dev.ini

            /etc/contrail/snmp-dev.ini example:

#snmp version 1 or 2
[1.1.1.190]
Community = public
Version = 2

#snmp version 3
[1.1.1.191]
Version = 3
SecLevel = authPriv
AuthProto = SHA
AuthPass = foo
PrivProto = AES
PrivPass = foo
SecName = snmpuser
# Mibs default to all, to get a subset
Mibs = LldpTable, ArpTable

        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)

        kwargs = {
            'help': "Specify config file",
            'metavar': "FILE",
            'action': 'append',
            'default': self.CONF_DEFAULT_PATHS
        }
        conf_parser.add_argument("-c", "--conf_file", **kwargs)
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'collectors': None,
            'log_local': False,
            'log_level': SandeshLevel.SYS_DEBUG,
            'log_category': '',
            'log_file': Sandesh._DEFAULT_LOG_FILE,
            'use_syslog': False,
            'syslog_facility': Sandesh._DEFAULT_SYSLOG_FACILITY,
            'scan_frequency': 600,
            'fast_scan_frequency': 60,
            'http_server_port': HttpPortSnmpCollector,
            'zookeeper': '127.0.0.1:2181',
            'cluster_id': '',
        }
        defaults.update(SandeshConfig.get_default_options(['DEFAULTS']))

        configdb_opts = {
            'rabbitmq_server_list': None,
            'rabbitmq_port': 5672,
            'rabbitmq_user': '******',
            'rabbitmq_password': '******',
            'rabbitmq_vhost': None,
            'rabbitmq_ha_mode': False,
            'rabbitmq_use_ssl': False,
            'rabbitmq_ssl_version': '',
            'rabbitmq_ssl_keyfile': '',
            'rabbitmq_ssl_certfile': '',
            'rabbitmq_ssl_ca_certs': '',
            'config_db_server_list': None,
            'config_db_username': None,
            'config_db_password': None
        }

        sandesh_opts = SandeshConfig.get_default_options()

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items("DEFAULTS")))
            if 'CONFIGDB' in config.sections():
                configdb_opts.update(dict(config.items('CONFIGDB')))
            SandeshConfig.update_options(sandesh_opts, config)
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
        defaults.update(configdb_opts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument(
            "--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--log_file",
                            help="Filename for the logs to be written to")
        parser.add_argument("--log_local",
                            action="store_true",
                            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
                            action="store_true",
                            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
                            help="Syslog facility to receive log lines")
        parser.add_argument("--scan_frequency",
                            type=int,
                            help="Time between snmp full poll")
        parser.add_argument("--fast_scan_frequency",
                            type=int,
                            help="Time between snmp interface status poll")
        parser.add_argument("--http_server_port",
                            type=int,
                            help="introspect server port")
        parser.add_argument("--rabbitmq_server_list",
                            help="List of Rabbitmq servers in ip:port format")
        parser.add_argument("--rabbitmq_user", help="Username for Rabbitmq")
        parser.add_argument("--rabbitmq_password",
                            help="Password for Rabbitmq")
        parser.add_argument("--rabbitmq_vhost", help="vhost for Rabbitmq")
        parser.add_argument(
            "--rabbitmq_ha_mode",
            action="store_true",
            help="True if the rabbitmq cluster is mirroring all queue")
        parser.add_argument("--rabbitmq_use_ssl",
                            action="store_true",
                            help="Use SSL for RabbitMQ connection")
        parser.add_argument("--rabbitmq_ssl_keyfile",
                            help="Keyfile for SSL RabbitMQ connection")
        parser.add_argument(
            "--rabbitmq_ssl_certfile",
            help="Certificate file for SSL RabbitMQ connection")
        parser.add_argument(
            "--rabbitmq_ssl_ca_certs",
            help="CA Certificate file for SSL RabbitMQ connection")
        parser.add_argument("--config_db_server_list",
                            help="List of cassandra servers in ip:port format",
                            nargs='+')
        parser.add_argument("--config_db_username", help="Cassandra user name")
        parser.add_argument("--config_db_password", help="Cassandra password")
        parser.add_argument("--zookeeper", help="ip:port of zookeeper server")
        parser.add_argument("--cluster_id",
                            help="Used for database keyspace separation")
        SandeshConfig.add_parser_arguments(parser)
        group = parser.add_mutually_exclusive_group(required=False)
        group.add_argument("--device-config-file",
                           help="where to look for snmp credentials")
        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.config_db_server_list) is str:
            self._args.config_db_server_list = \
                self._args.config_db_server_list.split()
        self._args.config_sections = config
        self._args.conf_file = args.conf_file

    def collectors(self):
        return self._args.collectors

    def zookeeper_server(self):
        return self._args.zookeeper

    def cluster_id(self):
        return self._args.cluster_id

    def log_local(self):
        return self._args.log_local

    def log_category(self):
        return self._args.log_category

    def log_level(self):
        return self._args.log_level

    def log_file(self):
        return self._args.log_file

    def use_syslog(self):
        return self._args.use_syslog

    def syslog_facility(self):
        return self._args.syslog_facility

    def fast_scan_freq(self):
        return self._args.fast_scan_frequency

    def frequency(self):
        return self._args.scan_frequency

    def http_port(self):
        return self._args.http_server_port

    def sandesh_config(self):
        return SandeshConfig.from_parser_arguments(self._args)

    def rabbitmq_params(self):
        return {
            'servers': self._args.rabbitmq_server_list,
            'port': self._args.rabbitmq_port,
            'user': self._args.rabbitmq_user,
            'password': self._args.rabbitmq_password,
            'vhost': self._args.rabbitmq_vhost,
            'ha_mode': self._args.rabbitmq_ha_mode,
            'use_ssl': self._args.rabbitmq_use_ssl,
            'ssl_version': self._args.rabbitmq_ssl_version,
            'ssl_keyfile': self._args.rabbitmq_ssl_keyfile,
            'ssl_certfile': self._args.rabbitmq_ssl_certfile,
            'ssl_ca_certs': self._args.rabbitmq_ssl_ca_certs
        }

    def cassandra_params(self):
        return {
            'servers': self._args.config_db_server_list,
            'user': self._args.config_db_username,
            'password': self._args.config_db_password,
            'cluster_id': self._args.cluster_id
        }
Beispiel #8
0
class CfgParser(object):
    CONF_DEFAULT_PATHS = ServicesDefaultConfigurationFiles.get(
        SERVICE_TOPOLOGY, None)
    def __init__(self, argv):
        self._args = None
        self.__pat = None
        self._argv = argv or ' '.join(sys.argv[1:])

    def parse(self):
        '''
            command line example
contrail-topology [-h] [-c FILE]
                         [--analytics_api ANALYTICS_API [ANALYTICS_API ...]]
                         [--collectors COLLECTORS [COLLECTORS ...]]
                         [--log_file LOG_FILE] [--log_local]
                         [--log_category LOG_CATEGORY] [--log_level LOG_LEVEL]
                         [--use_syslog] [--syslog_facility SYSLOG_FACILITY]
                         [--scan_frequency SCAN_FREQUENCY]
                         [--http_server_port HTTP_SERVER_PORT]

optional arguments:
  -h, --help            show this help message and exit
  -c FILE, --conf_file FILE
                        Specify config file
  --analytics_api ANALYTICS_API [ANALYTICS_API ...]
                        List of analytics-api IP addresses in ip:port format
  --collectors COLLECTORS [COLLECTORS ...]
                        List of Collector IP addresses in ip:port format
  --log_file LOG_FILE   Filename for the logs to be written to
  --log_local           Enable local logging of sandesh messages
  --log_category LOG_CATEGORY
                        Category filter for local logging of sandesh messages
  --log_level LOG_LEVEL
                        Severity level for local logging of sandesh messages
  --use_syslog          Use syslog for logging
  --syslog_facility SYSLOG_FACILITY
                        Syslog facility to receive log lines
  --scan_frequency SCAN_FREQUENCY
                        Time between snmp poll
  --http_server_port HTTP_SERVER_PORT
                        introspect server port

        '''
        # Source any specified config/ini file
        # Turn off help, so we print all options in response to -h
        conf_parser = argparse.ArgumentParser(add_help=False)

        kwargs = {'help': "Specify config file", 'metavar':"FILE",
                  'action':'append', 'default': self.CONF_DEFAULT_PATHS,
                 }
        conf_parser.add_argument("-c", "--conf_file", **kwargs)
        args, remaining_argv = conf_parser.parse_known_args(self._argv.split())

        defaults = {
            'collectors'      : None,
            'analytics_api'   : ['127.0.0.1:' + str(OpServerAdminPort)],
            'log_local'       : False,
            'log_level'       : SandeshLevel.SYS_DEBUG,
            'log_category'    : '',
            'log_file'        : Sandesh._DEFAULT_LOG_FILE,
            'use_syslog'      : False,
            'syslog_facility' : Sandesh._DEFAULT_SYSLOG_FACILITY,
            'scan_frequency'  : 60,
            'http_server_port': HttpPortTopology,
            'zookeeper'       : '127.0.0.1:2181',
            'cluster_id'      : '',
        }
        defaults.update(SandeshConfig.get_default_options(['DEFAULTS']))
        api_opts = {
            'api_server_list' : ['127.0.0.1:8082'],
            'api_server_use_ssl' : False
        }
        ksopts = {
            'auth_host': '127.0.0.1',
            'auth_protocol': 'http',
            'auth_port': 35357,
            'admin_user': '******',
            'admin_password': '******',
            'admin_tenant_name': 'default-domain'
        }
        sandesh_opts = SandeshConfig.get_default_options()

        config = None
        if args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.optionxform = str
            config.read(args.conf_file)
            if 'DEFAULTS' in config.sections():
                defaults.update(dict(config.items("DEFAULTS")))
            if 'API_SERVER' in config.sections():
                api_opts.update(dict(config.items("API_SERVER")))
            if 'KEYSTONE' in config.sections():
                ksopts.update(dict(config.items("KEYSTONE")))
            SandeshConfig.update_options(sandesh_opts, config)
        # Override with CLI options
        # Don't surpress add_help here so it will handle -h
        parser = argparse.ArgumentParser(
            # Inherit options from config_parser
            parents=[conf_parser],
            # print script description with -h/--help
            description=__doc__,
            # Don't mess with format of description
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
        defaults.update(api_opts)
        defaults.update(ksopts)
        defaults.update(sandesh_opts)
        parser.set_defaults(**defaults)
        parser.add_argument("--analytics_api",
            help="List of analytics-api IP addresses in ip:port format",
            nargs="+")
        parser.add_argument("--collectors",
            help="List of Collector IP addresses in ip:port format",
            nargs="+")
        parser.add_argument(
            "--log_file",
            help="Filename for the logs to be written to")
        parser.add_argument("--log_local", action="store_true",
            help="Enable local logging of sandesh messages")
        parser.add_argument(
            "--log_category",
            help="Category filter for local logging of sandesh messages")
        parser.add_argument(
            "--log_level",
            help="Severity level for local logging of sandesh messages")
        parser.add_argument("--use_syslog",
            action="store_true",
            help="Use syslog for logging")
        parser.add_argument("--syslog_facility",
            help="Syslog facility to receive log lines")
        parser.add_argument("--scan_frequency", type=int,
            help="Time between snmp poll")
        parser.add_argument("--http_server_port", type=int,
            help="introspect server port")
        parser.add_argument("--zookeeper",
            help="ip:port of zookeeper server")
        parser.add_argument("--cluster_id",
            help="Used for database keyspace separation")
        parser.add_argument("--auth_host",
                            help="ip of keystone server")
        parser.add_argument("--auth_protocol",
                            help="keystone authentication protocol")
        parser.add_argument("--auth_port", type=int,
                            help="ip of keystone server")
        parser.add_argument("--admin_user",
                            help="Name of keystone admin user")
        parser.add_argument("--admin_password",
                            help="Password of keystone admin user")
        parser.add_argument("--admin_tenant_name",
                            help="Tenant name for keystone admin user")
        parser.add_argument("--api_server_list",
            help="List of api-servers in ip:port format separated by space",
            nargs="+")
        parser.add_argument("--api_server_use_ssl",
            help="Use SSL to connect to api-server")
        SandeshConfig.add_parser_arguments(parser)

        self._args = parser.parse_args(remaining_argv)
        if type(self._args.collectors) is str:
            self._args.collectors = self._args.collectors.split()
        if type(self._args.analytics_api) is str:
            self._args.analytics_api = self._args.analytics_api.split()
        if type(self._args.api_server_list) is str:
            self._args.api_server_list = self._args.api_server_list.split()

        self._args.config_sections = config
        self._args.conf_file = args.conf_file

    def _pat(self):
        if self.__pat is None:
           self.__pat = re.compile(', *| +')
        return self.__pat

    def set_api_server_list(self, api_servers):
        self._args.api_server_list = api_servers

    def _mklist(self, s):
        return self._pat().split(s)

    def collectors(self):
        return self._args.collectors

    def api_server_list(self):
        return self._args.api_server_list

    def zookeeper_server(self):
        return self._args.zookeeper

    def cluster_id(self):
        return self._args.cluster_id

    def analytics_api(self):
        return self._args.analytics_api

    def log_local(self):
        return self._args.log_local

    def log_category(self):
        return self._args.log_category

    def log_level(self):
        return self._args.log_level

    def log_file(self):
        return self._args.log_file

    def use_syslog(self):
        return self._args.use_syslog

    def syslog_facility(self):
        return self._args.syslog_facility

    def frequency(self):
        return self._args.scan_frequency

    def http_port(self):
        return self._args.http_server_port

    def admin_user(self):
        return self._args.admin_user

    def admin_password(self):
        return self._args.admin_password

    def sandesh_config(self):
        return SandeshConfig.from_parser_arguments(self._args)

    def vnc_api(self):
        e = SystemError('Cant connect to API server')
        api_servers = [srv.split(':')[0] for srv in self._args.api_server_list]
        api_server_port = self._args.api_server_list[0].split(':')[1] \
            if self._args.api_server_list else None
        try:
            vnc = VncApi(self._args.admin_user,
                         self._args.admin_password,
                         self._args.admin_tenant_name,
                         api_servers, api_server_port,
                         api_server_use_ssl=self._args.api_server_use_ssl,
                         auth_host=self._args.auth_host,
                         auth_port=self._args.auth_port,
                         auth_protocol=self._args.auth_protocol)
        except Exception as e:
            ConnectionState.update(conn_type=ConnectionType.APISERVER,
                name='Config', status=ConnectionStatus.DOWN, message=str(e),
                server_addrs=api_servers)
            return None
        else:
            ConnectionState.update(conn_type=ConnectionType.APISERVER,
                name='Config', status=ConnectionStatus.UP,
                server_addrs=api_servers)
            return vnc