#!/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_dataguard_stats:sep(124)>>>
# TUX12C|TUXSTDB|PHYSICAL STANDBY|transport lag|+00 00:00:00
# TUX12C|TUXSTDB|PHYSICAL STANDBY|apply lag|+00 00:28:57
# TUX12C|TUXSTDB|PHYSICAL STANDBY|apply finish time|+00 00:00:17.180
# TUX12C|TUXSTDB|PHYSICAL STANDBY|estimated startup time|20

def inventory_oracle_dataguard_stats(info):
    inventory = []
    for line in info:
            inventory.append(("%s.%s" % (line[0], line[1]), {}))
    return inventory

def check_oracle_dataguard_stats(item, params, info):
    def get_seconds(timestamp):
        if str(timestamp)[0:1] == '+':
            days = int(timestamp[1:3])
            h = int(timestamp[4:6])
            min = int(timestamp[7:9])
            sec = int(timestamp[10:12])

            seconds = int(sec + min*60 + h*3600 + days*24*3600)
            return seconds
        return int(-1)


    state = 0

    perfdata = []
    infotext = ''

    itemfound = False

    for line in info:

        if line[0] + '.' + line[1] == item:
            db_name, db_unique_name, database_role, parameter, value = line

            itemfound = True
            if infotext == '':
                infotext = 'Database Role %s' % (database_role.lower())

            if parameter in('transport lag', 'apply lag', 'apply finish time'):

                if parameter == 'apply lag':
                    params_value = 'apply_lag'

                elif parameter == 'transport lag':
                    params_value = 'transport_lag'

                else:
                    params_value = ''

                state_marker = ''

                seconds = int(get_seconds(value))

                infotext += ' %s %s' % (parameter,  get_age_human_readable(seconds))

                if params.get(params_value):
                    infotext +=' levels at ('

                    if parameter == 'apply lag' and params.get('apply_lag_min'):

                        # minimum apply lag needs a configured apply lag rule!
                        warn, crit = params.get('apply_lag_min')
                        infotext += '%s/%s .. ' % (get_age_human_readable(warn), \
                                      get_age_human_readable(crit))

                        # apply_lag_min is a MINIMUM value!
                        if crit >= seconds:
                            state = 2
                            state_marker = '(!!)'
                        elif warn >= seconds:
                            state = max(state, 1)
                            state_marker = '(!)'

                    warn, crit = params.get(params_value)
                    infotext += '%s/%s)' % (get_age_human_readable(warn), \
                                  get_age_human_readable(crit))

                    if crit <= seconds:
                        state = 2
                        state_marker = '(!!)'
                    elif warn <= seconds:
                        state = max(state, 1)
                        state_marker = '(!)'

                    infotext += state_marker

                    perfdata.append([parameter.replace(' ', '_'), seconds, warn, crit])
                else:
                    perfdata.append([parameter.replace(' ', '_'), seconds])

    if itemfound == True:
        return state, infotext, perfdata

    # 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("Dataguard disabled or Instance not running")

check_info['oracle_dataguard_stats'] = {
    "check_function"          : check_oracle_dataguard_stats,
    "inventory_function"      : inventory_oracle_dataguard_stats,
    "service_description"     : "ORA %s Dataguard-Stats",
    "has_perfdata"            : True,
    "group"                   : "oracle_dataguard_stats",
}
