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


# example output
# <<<mssql_databases>>>^M
# MSSQL_MSSQL46 CorreLog_Report_T ONLINE FULL 0 0^M
# MSSQL_MSSQL46 DASH_CONFIG_T ONLINE FULL 0 0^M
# MSSQL_MSSQL46 master ONLINE SIMPLE 0 0^M
# MSSQL_MSSQL46 model ONLINE FULL 0 0^M
# MSSQL_MSSQL46 msdb ONLINE SIMPLE 0 0^M
# MSSQL_MSSQL46 NOC_ALARM_T ONLINE FULL 0 1^M
# MSSQL_MSSQL46 NOC_CONFIG_T ONLINE FULL 0 0^M
# MSSQL_MSSQL46 tempdb ONLINE SIMPLE 0 0^M
# MSSQL_MSSQL46 test_autoclose ONLINE FULL 1 0^M


def parse_mssql_databases(info):
    parsed  = {}
    headers = ['Instance', 'DBname',     'Status',
               'Recovery', 'auto_close', 'auto_shrink']

    for line in info:
        if len(line) == 6:
            data = dict(zip(headers, line))
            parsed.setdefault("%s %s" % (data["Instance"], data["DBname"]), data)

    return parsed


def inventory_mssql_databases(parsed):
    return [ (db_name, {}) for db_name in parsed ]


def check_mssql_databases(item, params, parsed):
    map_states = {"1" : (1, "on"),
                  "0" : (0, "off"), }

    if item in parsed:
        data = parsed[item]
        yield 0, 'Status: %s, Recovery: %s' % (data["Status"], data["Recovery"])

        for what in ["close", "shrink"]:
            state, state_readable = map_states[data["auto_%s" % what]]
            state = params.get("map_auto_%s_state" % what, {}).get(state_readable, state)
            yield state, 'Auto %s: %s' % (what, state_readable)


check_info['mssql_databases'] = {
    'parse_function'        : parse_mssql_databases,
    'inventory_function'    : inventory_mssql_databases,
    'check_function'        : check_mssql_databases,
    'service_description'   : '%s Database',
    'group'                 : 'mssql_databases',
}
