#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.oracle import *  # pylint: disable=wildcard-import,unused-wildcard-import

# <<<oracle_version>>>
# XE Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production


def inventory_oracle_version(info):
    oracle_handle_ora_errors_discovery(info)
    return [(line[0], None) for line in info if len(line) >= 2]


def check_oracle_version(item, _no_params, info):
    for line in info:
        if line[0] == item:
            err = oracle_handle_ora_errors(line)
            if err is False:
                continue
            elif isinstance(err, tuple):
                return err

            return (0, "Version: " + " ".join(line[1:]))
    return (3, "no version information, database might be stopped")


check_info["oracle_version"] = {
    "check_function": check_oracle_version,
    "inventory_function": inventory_oracle_version,
    "service_description": "ORA Version %s",
}
