#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.


def parse_informix_status(info):
    parsed = {}
    instance = None
    for line in info:
        if line[0].startswith("[[[") and line[0].endswith("]]]"):
            instance = line[0][3:-3]

        elif instance is not None and len(line) >= 2:
            stripped_line = map(lambda x: x.strip(), line)
            parsed.setdefault(instance, {})
            parsed[instance].setdefault(stripped_line[0], " ".join(stripped_line[1:]))

    return parsed



def inventory_informix_status(parsed):
    return [(instance, {}) for instance in parsed]


def check_informix_status(item, params, parsed):
    map_states = {
        "0": (0, "initialization"),
        "1": (1, "quiescent"),
        "2": (1, "recovery"),
        "3": (1, "backup"),
        "4": (2, "shutdown"),
        "5": (0, "online"),
        "6": (1, "abort"),
        "7": (1, "single user"),
        "-1": (2, "offline"),
        "255": (2, "offline"),
    }

    if item in parsed:
        data = parsed[item]
        state, state_readable = map_states[data["Status"]]
        infotext = "Status: %s" % state_readable
        if "Server Version" in data:
            infotext += ", Version: %s" % data["Server Version"]
        if "PORT" in data:
            infotext += ", Port: %s" % data["PORT"].split(" ")[1]
        return state, infotext


check_info['informix_status'] = {
    'parse_function'        : parse_informix_status,
    'inventory_function'    : inventory_informix_status,
    'check_function'        : check_informix_status,
    'service_description'   : 'Informix Instance %s',
}
