#!/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.

# In cooperation with Thorsten Bruhns from OPITZ Consulting

# <<<oracle_processes>>>
# TUX2 51 300
# FOOBAR 11 4780

# Columns: SID PROCESSES_COUNT PROCESSES_LIMIT

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

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

def check_oracle_processes(item, params, info):
    for line in info:
        if line[0] == item:

            processes_num = int(line[1])
            processes_max = int(line[2])
            processes_pct = float(processes_num) / float(processes_max) * 100

            warn, crit = params["levels"]
            processes_warn = processes_max * warn / 100
            processes_crit = processes_max * crit / 100

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

            return state, "%d of %d processes are used (%d%%, levels at %d%%/%d%%)" \
                % (processes_num, processes_max, processes_pct, warn, crit), \
                 [("processes", processes_num, processes_warn, processes_crit)]

    # 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_processes'] = {
    "check_function"          : check_oracle_processes,
    "inventory_function"      : inventory_oracle_processes,
    "service_description"     : "ORA %s Processes",
    "has_perfdata"            : True,
    "default_levels_variable" : "oracle_processes_defaults",
    "group"                   : "oracle_processes",
}
