#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             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_instance:sep(124)>>>
# XE|11.2.0.2.0|OPEN|ALLOWED|STOPPED|3524|2752243048|NOARCHIVELOG|PRIMARY|NO|XE|080220151025
# last entry: db creation time 'ddmmyyyyhh24mi'


def _parse_raw_db_creation_time(raw_db_creation_time):
    if raw_db_creation_time is None:
        return None

    # We just translate the format to another
    # "%d%m%Y%H%M%S" => "%Y-%m-%d %H:%M"
    # we cannot use time because we have no information about the timezone here.
    try:
        year = int(raw_db_creation_time[4:8])
        month = int(raw_db_creation_time[2:4])
        day = int(raw_db_creation_time[:2])
        hours = int(raw_db_creation_time[8:10])
        minutes = int(raw_db_creation_time[10:])
    except (IndexError, ValueError):
        return None
    return "%s-%02d-%02d %02d:%02d" % (year, month, day, hours, minutes)


def inv_oracle_instance(info, inventory_tree, status_data_tree):
    path = "software.applications.oracle.instance:"
    inv_node = inventory_tree.get_list(path)
    status_node = status_data_tree.get_list(path)
    for line in info:
        raw_db_creation_time = None
        raw_up_seconds = None
        logmode = None

        if len(line) == 6:
            sid, version, openmode, logins, _unused, _unused2 = line

        elif len(line) == 11:
            sid, version, openmode, logins, _archiver, raw_up_seconds, _dbid, \
                logmode, _database_role, _force_logging, _name = line

        elif len(line) == 12:
            sid, version, openmode, logins, _archiver, raw_up_seconds, _dbid, \
                logmode, _database_role, _force_logging, _name, raw_db_creation_time = line

        elif len(line) == 22:
            sid, version, openmode, logins, _archiver, raw_up_seconds, _dbid, \
                logmode, _database_role, _force_logging, _name, raw_db_creation_time, _pluggable, \
                _con_id, _pname, _pdbid, _popenmode, _prestricted, _ptotal_size, _precovery_status, \
                _pup_seconds, _pblock_size = line

        else:
            continue

        try:
            up_seconds = int(raw_up_seconds)
        except (TypeError, ValueError):
            up_seconds = None

        inv_node.append({
            "sid": sid,
            "version": version,
            "openmode": openmode,
            "logmode": logmode,
            "logins": logins,
            "db_creation_time": _parse_raw_db_creation_time(raw_db_creation_time),
        })

        status_node.append({
            "sid": sid,
            "db_uptime": up_seconds,
        })


inv_info['oracle_instance'] = {
    'inv_function': inv_oracle_instance,
}
