#!/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-
# tails. 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 serial# machine process osuser program last_call_el sql_id

factory_settings["oracle_longactivesessions_defaults"] = {
    "levels"         : (500, 1000),
}


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


def check_oracle_longactivesessions(item, params, info):
    sessioncount = 0
    state = 3
    itemfound = False

    for line in info:

        warn, crit = params["levels"]

        if line[0] == item:
            itemfound = True

        if line[0] == item and line[1] != '':

            sessioncount += 1
            sid, sidnr, serial, machine, process, osuser, program, \
            last_call_el, sql_id = line

            longoutput = 'Session (sid,serial,proc) %s %s %s active for %s from %s osuser %s program %s sql_id %s ' \
                        % (sidnr, serial, process, get_age_human_readable(int(last_call_el)), machine, osuser, program, sql_id)

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

        if sessioncount == 0:
            return 0, "%d long active sessions (%d, warn/crit at %d/%d)" \
                % (sessioncount, sessioncount, warn, crit), \
                [("count", sessioncount, warn, crit)]
        elif sessioncount <= 10:
            return state, "%d long active sessions (%d, warn/crit at %d/%d) %s" \
                % (sessioncount, sessioncount, warn, crit, longoutput), \
                [("count", sessioncount, warn, crit)]
        elif sessioncount > 10:
            return state, "%d long active sessions (%d, warn/crit at %d/%d)" \
                % (sessioncount, sessioncount, warn, crit), \
                [("count", sessioncount, warn, 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("no info from database. Check ORA %s Instance" % item)


check_info['oracle_longactivesessions'] = {
    "check_function"          : check_oracle_longactivesessions,
    "inventory_function"      : inventory_oracle_longactivesessions,
    "service_description"     : "ORA %s Long Active Sessions",
    "has_perfdata"            : True,
    "default_levels_variable" : "oracle_longactivesessions_defaults",
    "group"                   : "oracle_longactivesessions",
}
