#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# <<<oracle_recovery_area>>>
# TUX12C 0 4800 19 0

# Columns:
# ORACLE_SID used_pct size used reclaimable

factory_settings["oracle_recovery_area_defaults"] = {
    "levels"         : (70.0, 90.0),
}


def inventory_oracle_recovery_area(info):
    return [ (line[0], {}) for line in info ]


def check_oracle_recovery_area(item, params, info):
    for line in info:
        if line[0] == item:
            size_mb, used_mb, reclaimable_mb = map(int, line[2:5])
            if size_mb == 0:
                perc_used = 0
            else:
                perc_used = float(used_mb - reclaimable_mb) / size_mb * 100

            warn, crit = params["levels"]
            warn_mb = size_mb * warn / 100
            crit_mb = size_mb * crit / 100

            if perc_used >= crit:
                state = 2
            elif perc_used >= warn:
                state = 1
            else:
                state = 0

            mb = 1024*1024
            return state, "%s out of %s used (%.1f%%, levels at %s%%/%s%%), %s reclaimable" \
                % (get_bytes_human_readable(used_mb*mb), get_bytes_human_readable(size_mb*mb), \
                   perc_used, warn, crit, get_bytes_human_readable(reclaimable_mb*mb)), \
                   [('used', used_mb, warn_mb, crit_mb, 0, size_mb), ('reclaimable', reclaimable_mb)]

    # In case of missing information we assume that the login into
    # the database has failed and we simply skip this check. It won't
    # switch to UNKNOWN, but will get stale.
    raise MKCounterWrapped("Login into database failed")


check_info['oracle_recovery_area'] = {
    "check_function"          : check_oracle_recovery_area,
    "inventory_function"      : inventory_oracle_recovery_area,
    "service_description"     : "ORA %s Recovery Area",
    "has_perfdata"            : True,
    "default_levels_variable" : "oracle_recovery_area_defaults",
    "group"                   : "oracle_recovery_area",
}

