Beispiel #1
0
def validate_args(args):
    if args.controller_only and args.first_broker_only:
        terminate(
            status_code.WARNING,
            prepare_terminate_message(
                "Only one of controller_only and first_broker_only should be used",
            ),
            args.json,
        )

    if args.controller_only or args.first_broker_only:
        if args.broker_id is None:
            terminate(
                status_code.WARNING,
                prepare_terminate_message("broker_id is not specified"),
                args.json,
            )
        elif args.broker_id == -1:
            try:
                args.broker_id = get_broker_id(args.data_path)
            except Exception as e:
                terminate(
                    status_code.WARNING,
                    prepare_terminate_message("{}".format(e)),
                    args.json,
                )

    if args.head != -1 and not args.verbose:
        terminate(
            status_code.WARNING,
            prepare_terminate_message(
                "--head option works only as addition to --verbose option"),
            args.json,
        )
Beispiel #2
0
def run():
    """Verify command-line arguments and run commands"""
    args = parse_args()
    logging.basicConfig(level=logging.WARN)

    # to prevent flooding for sensu-check.
    logging.getLogger('kafka').setLevel(logging.CRITICAL)

    if args.controller_only and args.first_broker_only:
        terminate(
            status_code.WARNING,
            prepare_terminate_message(
                "Only one of controller_only and first_broker_only should be used",
            ),
            args.json,
        )

    if args.controller_only or args.first_broker_only:
        if args.broker_id is None:
            terminate(
                status_code.WARNING,
                prepare_terminate_message("broker_id is not specified"),
                args.json,
            )
        elif args.broker_id == -1:
            try:
                args.broker_id = get_broker_id(args.data_path)
            except Exception as e:
                terminate(
                    status_code.WARNING,
                    prepare_terminate_message("{}".format(e)),
                    args.json,
                )

    try:
        cluster_config = config.get_cluster_config(
            args.cluster_type,
            args.cluster_name,
            args.discovery_base_path,
        )
        code, msg = args.command(cluster_config, args)
    except ConfigurationError as e:
        terminate(
            status_code.CRITICAL,
            prepare_terminate_message("ConfigurationError {0}".format(e)),
            args.json,
        )

    terminate(code, msg, args.json)
Beispiel #3
0
def get_broker_id(data_path):
    """This function will look into the data folder to get the automatically created
    broker_id.

    :param string data_path: the path to the kafka data folder
    :returns int: the real broker_id
    """

    # Path to the meta.properties file. This is used to read the automatic broker id
    # if the given broker id is -1
    META_FILE_PATH = "{data_path}/meta.properties"

    if not data_path:
        terminate(status_code.WARNING, "You need to specify the data_path if broker_id == -1")
    meta_properties_path = META_FILE_PATH.format(data_path=data_path)
    return read_generated_broker_id(meta_properties_path)
Beispiel #4
0
def get_broker_id(data_path):
    """This function will look into the data folder to get the automatically created
    broker_id.

    :param string data_path: the path to the kafka data folder
    :returns int: the real broker_id
    """

    # Path to the meta.properties file. This is used to read the automatic broker id
    # if the given broker id is -1
    META_FILE_PATH = "{data_path}/meta.properties"

    if not data_path:
        terminate(status_code.WARNING,
                  "You need to specify the data_path if broker_id == -1")
    meta_properties_path = META_FILE_PATH.format(data_path=data_path)
    return read_generated_broker_id(meta_properties_path)
Beispiel #5
0
def check_run_on_controller(zk, args):
    """Kafka 0.9 supports automatic broker ids. If args.broker_id is set to -1,
    it will call get_broker_id() for parse broker_id from meta.properties file.
    """
    if args.broker_id is None:
        terminate(status_code.WARNING, "Broker id is not specified")

    if args.broker_id != -1:
        broker_id = args.broker_id
    else:
        broker_id = get_broker_id(args.data_path)

    controller_id = get_controller_id(zk)

    # This check is only executed by the controller
    if broker_id != controller_id:
        terminate(status_code.OK, 'Broker %s is not the controller, nothing to check' % (broker_id))
Beispiel #6
0
def run():
    """Verify command-line arguments and run commands"""
    args = parse_args()
    logging.basicConfig(level=logging.WARN)

    # to prevent flooding for sensu-check.
    logging.getLogger('kafka').setLevel(logging.CRITICAL)

    try:
        cluster_config = config.get_cluster_config(
            args.cluster_type,
            args.cluster_name,
            args.discovery_base_path,
        )
        code, msg = args.command(cluster_config, args)
    except ConfigurationError as e:
        terminate(status_code.CRITICAL, "ConfigurationError {0}".format(e))

    terminate(code, msg)
Beispiel #7
0
def run():
    """Verify command-line arguments and run commands"""
    args = parse_args()
    logging.basicConfig(level=logging.WARN)

    # to prevent flooding for sensu-check.
    logging.getLogger('kafka').setLevel(logging.CRITICAL)

    try:
        cluster_config = config.get_cluster_config(
            args.cluster_type,
            args.cluster_name,
            args.discovery_base_path,
        )
        code, msg = args.command(cluster_config, args)
    except ConfigurationError as e:
        terminate(status_code.CRITICAL, "ConfigurationError {0}".format(e))

    terminate(code, msg)
Beispiel #8
0
def check_run_on_controller(zk, args):
    """Kafka 0.9 supports automatic broker ids. If args.broker_id is set to -1,
    it will call get_broker_id() for parse broker_id from meta.properties file.
    """
    if args.broker_id is None:
        terminate(status_code.WARNING, "Broker id is not specified")

    if args.broker_id != -1:
        broker_id = args.broker_id
    else:
        broker_id = get_broker_id(args.data_path)

    controller_id = get_controller_id(zk)

    # This check is only executed by the controller
    if broker_id != controller_id:
        terminate(
            status_code.OK,
            'Broker %s is not the controller, nothing to check' % (broker_id))
Beispiel #9
0
def run():
    """Verify command-line arguments and run commands"""
    args = parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARN)

    try:
        cluster_config = config.get_cluster_config(
            args.cluster_type,
            args.cluster_name,
            args.discovery_base_path,
        )
        code, msg = args.command(cluster_config, args)
    except ConfigurationError as e:
        terminate(status_code.CRITICAL, "ConfigurationError {0}".format(e))

    terminate(code, msg)
    def run(self, cluster_config, args):
        self.cluster_config = cluster_config
        self.args = args
        with ZK(self.cluster_config) as self.zk:
            broker_ids = get_broker_ids(self.zk)

            if args.controller_only and not is_controller(
                    self.zk, args.broker_id):
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'Broker {} is not the controller, nothing to check'.
                        format(args.broker_id), ),
                    args.json,
                )
            if args.first_broker_only and not broker_ids:
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'No brokers detected, nothing to check'),
                    args.json,
                )
            if args.first_broker_only and not is_first_broker(
                    broker_ids, args.broker_id):
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'Broker {} has not the lowest id, nothing to check'.
                        format(args.broker_id), ),
                    args.json,
                )
            return self.run_command()
Beispiel #11
0
    def run(self, cluster_config, args):
        self.cluster_config = cluster_config
        self.args = args
        with ZK(self.cluster_config) as self.zk:
            broker_ids = get_broker_ids(self.zk)

            if args.controller_only and not is_controller(self.zk, args.broker_id):
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'Broker {} is not the controller, nothing to check'
                        .format(args.broker_id),
                    ),
                    args.json,
                )
            if args.first_broker_only and not broker_ids:
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'No brokers detected, nothing to check'
                    ),
                    args.json,
                )
            if args.first_broker_only and not is_first_broker(broker_ids, args.broker_id):
                terminate(
                    status_code.OK,
                    prepare_terminate_message(
                        'Broker {} has not the lowest id, nothing to check'
                        .format(args.broker_id),
                    ),
                    args.json,
                )
            return self.run_command()
Beispiel #12
0
def run():
    """Verify command-line arguments and run commands"""
    args = parse_args()

    try:
        cluster_config = config.get_cluster_config(
            args.cluster_type,
            args.cluster_name,
            args.discovery_base_path,
        )
        code, msg = args.command(cluster_config, args)
    except ConfigurationError as e:
        terminate(status_code.CRITICAL, "ConfigurationError {0}".format(e))
    except Exception as e:
        terminate(status_code.CRITICAL, "Got Exception: {0}".format(e))

    terminate(code, msg)
Beispiel #13
0
def read_generated_broker_id(meta_properties_path):
    """reads broker_id from meta.properties file.

    :param string meta_properties_path: path for meta.properties file
    :returns int: broker_id from meta_properties_path
    """
    try:
        with open(meta_properties_path, 'r') as f:
            broker_id = parse_meta_properties_file(f)
    except IOError:
        terminate(
            status_code.WARNING,
            "Cannot open meta.properties file: {path}".format(
                path=meta_properties_path, ),
        )
    except ValueError:
        terminate(status_code.WARNING, "Broker id not valid")

    if broker_id is None:
        terminate(status_code.WARNING,
                  "Autogenerated broker id missing from data directory")

    return broker_id
Beispiel #14
0
def read_generated_broker_id(meta_properties_path):
    """reads broker_id from meta.properties file.

    :param string meta_properties_path: path for meta.properties file
    :returns int: broker_id from meta_properties_path
    """
    try:
        with open(meta_properties_path, 'r') as f:
            broker_id = parse_meta_properties_file(f)
    except IOError:
        terminate(
            status_code.WARNING,
            "Cannot open meta.properties file: {path}".format(
                path=meta_properties_path,
            ),
        )
    except ValueError:
        terminate(status_code.WARNING, "Broker id not valid")

    if broker_id is None:
        terminate(status_code.WARNING, "Autogenerated broker id missing from data directory")

    return broker_id