Esempio n. 1
0
    def check_arguments(cls, conf):
        """
        Sanity check plugin options values.

        As a side effect, it also converts the specified interval and port
        to an integer.

        """
        # Checking the interval
        if not conf['tcp_check_interval']:
            raise ArgsError("A TCP health-test interval needs to be "
                            "specified (--tcp_check_interval).")

        if not (1 <= conf['tcp_check_interval'] <= 3600):
            raise ArgsError("Specified TCP health-test interval must be "
                            "between 1 and 3600 seconds")

        # Checking the port
        if not conf['tcp_check_port']:
            raise ArgsError("A port for the TCP health-test needs to be "
                            "specified (--tcp_check_port).")

        if not (1 <= conf['tcp_check_port'] <= 65535):
            raise ArgsError("Specified port for TCP health-test must be "
                            "between 1 and 65535")
Esempio n. 2
0
def _parse_args(args_list, watcher_plugin_class, health_plugin_class):
    """
    Parse command line arguments and return relevant values in a dict.

    Also perform basic sanity checking on some arguments.

    If plugin classes have been provided then a callback into those classes is
    used to extend the arguments with plugin-specific options.

    Likewise, the sanity checking will then also invoke a callback into the
    plugins, in order to perform a sanity check on the plugin options.

    """
    conf = {}

    # Setting up the command line argument parser. Note that we pass the
    # complete list of all plugins, so that their parameter can be added to the
    # official parameter handling, the help screen, etc. Some plugins may even
    # add further plugins themselves, but will handle this themselves.
    parser, arglist = _setup_arg_parser(args_list, watcher_plugin_class,
                                        health_plugin_class)
    args = parser.parse_args(args_list)

    # Transcribe argument values into our own dict
    for argname in arglist:
        conf[argname] = getattr(args, argname)

    # Sanity checking of arguments. Let the watcher and health-monitor plugin
    # class check their own arguments.
    for plugin_class in [watcher_plugin_class, health_plugin_class]:
        if plugin_class:
            try:
                plugin_class.check_arguments(conf)
            except ArgsError as e:
                parser.print_help()
                raise e

    # Sanity checking of other args
    if conf['route_recheck_interval'] < 5 and \
                        conf['route_recheck_interval'] != 0:
        raise ArgsError("route_recheck_interval argument must be either 0 "
                        "or at least 5")

    if not 0 < conf['port'] < 65535:
        raise ArgsError("Invalid listen port '%d' for built-in http server." %
                        conf['port'])

    if not conf['addr'] == "localhost":
        # Check if a proper address was specified (already raises a suitable
        # ArgsError if not)
        utils.ip_check(conf['addr'])

    # Store a reference to the config dict in the current state
    CURRENT_STATE.conf = conf

    return conf
Esempio n. 3
0
    def check_arguments(cls, conf):
        """
        Sanity check plugin options values.

        As a side effect, it also converts the specified interval to a
        float.

        """
        if not conf['icmp_check_interval']:
            raise ArgsError("An ICMPecho interval needs to be specified "
                            "(--icmp_check_interval).")

        if not (1 <= conf['icmp_check_interval'] <= 3600):
            raise ArgsError("Specified ICMPecho interval must be between "
                            "1 and 3600 seconds")
Esempio n. 4
0
def check_valid_ip_or_cidr(val, return_as_cidr=False):
    """
    Checks that the value is a valid IP address or a valid CIDR.

    Returns the specified value.

    If 'return_as_cidr' is set then the return value will always be in the form
    of a CIDR, even if a plain IP address was specified.

    """
    is_ip = True
    if "/" in val:
        ip_check(val, netmask_expected=True)
        is_ip = False
    else:
        ip_check(val, netmask_expected=False)

    if return_as_cidr and is_ip:
        # Convert a plain IP to a CIDR
        if val == "0.0.0.0":
            # Special case for the default route
            val = "0.0.0.0/0"
        else:
            val = "%s/32" % val

    try:
        ipaddress.IPv4Network(unicode(val))
    except Exception as e:
        raise ArgsError("Not a valid network: %s" % str(e))

    return val
Esempio n. 5
0
def ip_check(ip, netmask_expected=False):
    """
    Sanity check that the specified string is indeed an IP address or mask.

    """
    try:
        if netmask_expected:
            if "/" not in ip:
                raise netaddr.core.AddrFormatError()
            netaddr.IPNetwork(ip)
        else:
            netaddr.IPAddress(ip)
    except netaddr.core.AddrFormatError:
        if netmask_expected:
            raise ArgsError("Not a valid CIDR (%s)" % ip)
        else:
            raise ArgsError("Not a valid IP address (%s)" % ip)
Esempio n. 6
0
    def check_arguments(cls, conf):
        """
        Sanity check options needed for Romana mode.

        """
        if 'etcd_port' not in conf:
            raise ArgsError("The etcd port needs to be specified "
                            "(--etcd_port parameter)")
        if 'etcd_addr' not in conf:
            raise ArgsError("The etcd address needs to be specified "
                            "(--etcd_addr parameter)")
        if not 0 < conf['etcd_port'] < 65535:
            raise ArgsError("Invalid etcd port '%d' for Romana mode." %
                            conf['etcd_port'])
        cert_args = [conf.get('ca_cert'), conf.get('priv_key'),
                     conf.get('cert_chain')]
        if any(cert_args):
            if not all(cert_args):
                raise ArgsError("Either set all SSL auth options "
                                "(--etcd_ca_cert, --etcd_priv_key, "
                                "--etcd_cert_chain), or none of them.")
            else:
                # Check that the three specified files are accessible.
                for fname in cert_args:
                    try:
                        with open(fname) as f:
                            d = f.read()
                            if not d:
                                raise ArgsError("No contents in file '%s'" %
                                                fname)
                    except Exception as e:
                        raise ArgsError("Cannot access file '%s': %s" %
                                        (fname, str(e)))
Esempio n. 7
0
    def check_arguments(cls, conf):
        """
        Sanity check plugin options values.

        """
        # Checking the specified list of basic health monitor plugins, which
        # should be run by the multi plugin.
        if not conf.get('multi_plugins'):
            raise ArgsError("A specification of health monitor plugins "
                            "is required (--multi_plugins).")

        # Now check parameters for all sub-plugins. We use the list of classes
        # for sub-plugins that we discovered earlier while adding parameters
        # for those sub-plugins.
        for mpc in cls.multi_plugin_classes:
            mpc.check_arguments(conf)