def perform_check(host, plugin, check_id):
    a = Agent(host.host, host.auth_key, verify_certs=host.check_certificate)
    error = None
    result_type = "plugin"
    try:
        update_required = a.check_plugin_verison(plugin.id, plugin.version)
        if update_required:
            plugin_file = os.path.join(get_config_value(config, "plugin_repo"),
                                       plugin.archive_file)
            if plugin.signature_file:
                signature_file = os.path.join(
                    get_config_value(config, "plugin_repo"),
                    plugin.signature_file)
            with open(plugin_file, "rb") as pf:
                if plugin.signature_file:
                    with open(signature_file, "rb") as sf:
                        a.update_plugin(plugin.id, pf, sf)
                else:
                    a.update_plugin(plugin.id, pf)
        (value, message) = a.get_plugin_data(plugin.id)
    except CommandUnsuccessfulError as e:
        error = "CommandUnsuccessfulError: {}".format(str(e))
        result_type = "command_unsuccessful"
    except AuthenticationError as e:
        error = "AuthenticationError: {}".format(str(e))
        result_type = "authentication_error"
    except RequestError as e:
        error = "RequestError: {}".format(str(e))
        result_type = "request_error"
    except ConnectionError as e:
        error = "ConnectionError: {}".format(str(e))
        result_type = "connection_error"
    except Timeout as e:
        error = "Timeout: {}".format(str(e))
        result_type = "connection_timeout"

    if error:
        log_message("Dispatcher", error)
        message = error
        value = None

    old_health = Host.query.get(host.id).health

    pr = PluginResult(host_id=host.id,
                      plugin_id=plugin.id,
                      check_id=check_id,
                      value=value,
                      message=message,
                      result_type=result_type,
                      timestamp=datetime.now())

    pr.health_status = classify(pr, check_id)
    session.add(pr)
    session.commit()

    new_health = Host.query.get(host.id).health
    process_alerts(host.id, plugin.id, check_id, old_health, new_health)
def perform_check(host, plugin, check_id):
    a = Agent(host.host, host.auth_key, verify_certs=host.check_certificate)
    error = None
    result_type = "plugin"
    try:
        update_required = a.check_plugin_verison(plugin.id, plugin.version)
        if update_required:
            plugin_file = os.path.join(get_config_value(config, "plugin_repo"),
                plugin.archive_file)
            if plugin.signature_file:
                signature_file = os.path.join(get_config_value(config,
                    "plugin_repo"), plugin.signature_file)
            with open(plugin_file, "rb") as pf:
                if plugin.signature_file:
                    with open(signature_file, "rb") as sf:
                        a.update_plugin(plugin.id, pf, sf)
                else:
                    a.update_plugin(plugin.id, pf)
        (value, message) = a.get_plugin_data(plugin.id)
    except CommandUnsuccessfulError as e:
        error = "CommandUnsuccessfulError: {}".format(str(e))
        result_type = "command_unsuccessful"
    except AuthenticationError as e:
        error = "AuthenticationError: {}".format(str(e))
        result_type = "authentication_error"
    except RequestError as e:
        error = "RequestError: {}".format(str(e))
        result_type = "request_error"
    except ConnectionError as e:
        error = "ConnectionError: {}".format(str(e))
        result_type = "connection_error"
    except Timeout as e:
        error = "Timeout: {}".format(str(e))
        result_type = "connection_timeout"

    if error:
        log_message("Dispatcher", error)
        message = error
        value = None

    old_health = Host.query.get(host.id).health

    pr = PluginResult(host_id=host.id, plugin_id=plugin.id, check_id=check_id,
        value=value, message=message, result_type=result_type,
        timestamp=datetime.now())

    pr.health_status = classify(pr, check_id)
    session.add(pr)
    session.commit()

    new_health = Host.query.get(host.id).health
    process_alerts(host.id, plugin.id, check_id, old_health, new_health)
def classify(latest_result, check_id):
    # Try and get a check specific threshold, if there isn't one, get default
    threshold = PluginThreshold.query.filter(
        PluginThreshold.plugin_id == latest_result.plugin_id,
        PluginThreshold.check_id == check_id,
        PluginThreshold.default == False).all()

    if not threshold:
        threshold = PluginThreshold.query.filter(
            PluginThreshold.plugin_id == latest_result.plugin_id,
            PluginThreshold.default == True).all()

    try:
        threshold = threshold[0]
    except KeyError:
        log_message(
            "Classification", "No classifier code could be found for "
            "plugin: {}".format(latest_result.plugin_id))
        return "unknown"

    # Get n-1 historical values and then append the latest onto this list
    historical_values = PluginResult.query.filter(
        PluginResult.plugin_id == latest_result.plugin_id,
        PluginResult.host_id == latest_result.host_id).order_by(
            PluginResult.id.desc()).limit(threshold.n_historical - 1).all()

    values_list = []
    messages_list = []
    result_types_list = []
    for value in historical_values:
        values_list.append(value.value)
        messages_list.append(value.message)
        result_types_list.append(value.result_type)
    values_list.append(latest_result.value)
    messages_list.append(latest_result.message)
    result_types_list.append(latest_result.result_type)

    try:
        classification = execute_classifier(threshold.classification_code,
                                            values_list, messages_list,
                                            result_types_list)
    except (lupa._lupa.LuaSyntaxError, lupa._lupa.LuaError) as e:
        log_message(
            "Classification",
            "Classification code for plugin {} gave error: {}".format(
                latest_result.plugin_id, str(e)))
        classification = "unknown"

    if classification not in ["major", "minor", "critical", "ok", "unknown"]:
        log_message(
            "Classification", "\"{}\" is not a valid classification for"
            " plugin {}".format(classification, latest_result.plugin_id))
        classification = "unknown"

    return classification
def classify(latest_result, check_id):
    # Try and get a check specific threshold, if there isn't one, get default
    threshold = PluginThreshold.query.filter(
        PluginThreshold.plugin_id == latest_result.plugin_id,
        PluginThreshold.check_id == check_id,
        PluginThreshold.default == False
    ).all()

    if not threshold:
        threshold = PluginThreshold.query.filter(
            PluginThreshold.plugin_id == latest_result.plugin_id,
            PluginThreshold.default == True
        ).all()

    try:
        threshold = threshold[0]
    except KeyError:
        log_message("Classification", "No classifier code could be found for "
            "plugin: {}".format(latest_result.plugin_id))
        return "unknown"

    # Get n-1 historical values and then append the latest onto this list
    historical_values = PluginResult.query.filter(
        PluginResult.plugin_id == latest_result.plugin_id,
        PluginResult.host_id == latest_result.host_id).order_by(
            PluginResult.id.desc()
        ).limit(threshold.n_historical-1).all()

    values_list = []
    messages_list = []
    result_types_list = []
    for value in historical_values:
        values_list.append(value.value)
        messages_list.append(value.message)
        result_types_list.append(value.result_type)
    values_list.append(latest_result.value)
    messages_list.append(latest_result.message)
    result_types_list.append(latest_result.result_type)

    try:
        classification = execute_classifier(threshold.classification_code,
            values_list, messages_list, result_types_list)
    except (lupa._lupa.LuaSyntaxError, lupa._lupa.LuaError) as e:
        log_message("Classification",
            "Classification code for plugin {} gave error: {}".format(
            latest_result.plugin_id, str(e)))
        classification = "unknown"

    if classification not in ["major", "minor", "critical", "ok", "unknown"]:
        log_message("Classification", "\"{}\" is not a valid classification for"
            " plugin {}".format(classification, latest_result.plugin_id))
        classification = "unknown"

    return classification