#!/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
#<<<scaleio_sds>>>
#SDS 3c7af8db00000000:
#        ID                                                 3c7af8db00000000
#        NAME                                               sds03
#        PROTECTION_DOMAIN_ID                               91ebcf4500000000
#        STATE                                              REMOVE_STATE_NORMAL
#        MEMBERSHIP_STATE                                   JOINED
#        MDM_CONNECTION_STATE                               MDM_CONNECTED
#        MAINTENANCE_MODE_STATE                             NO_MAINTENANCE
#        MAX_CAPACITY_IN_KB                                 21.8 TB (22353 GB)
#        UNUSED_CAPACITY_IN_KB                              13.2 TB (13471 GB)
#
#SDS 3c7ad1cc00000001:
#        ID                                                 3c7ad1cc00000001
#        NAME                                               sds01
#        PROTECTION_DOMAIN_ID                               91ebcf4500000000
#        STATE                                              REMOVE_STATE_NORMAL
#        MEMBERSHIP_STATE                                   JOINED
#        MDM_CONNECTION_STATE                               MDM_CONNECTED
#        MAINTENANCE_MODE_STATE                             NO_MAINTENANCE
#        MAX_CAPACITY_IN_KB                                 21.8 TB (22353 GB)
#        UNUSED_CAPACITY_IN_KB                              13.2 TB (13477 GB)
#
#SDS 3c7af8dc00000002:
#        ID                                                 3c7af8dc00000002
#        NAME                                               sds02
#        PROTECTION_DOMAIN_ID                               91ebcf4500000000
#        STATE                                              REMOVE_STATE_NORMAL
#        MEMBERSHIP_STATE                                   JOINED
#        MDM_CONNECTION_STATE                               MDM_CONNECTED
#        MAINTENANCE_MODE_STATE                             NO_MAINTENANCE
#        MAX_CAPACITY_IN_KB                                 21.8 TB (22353 GB)
#        UNUSED_CAPACITY_IN_KB                              13.2 TB (13477 GB)
#


def inventory_scaleio_sds(parsed):
    for entry in parsed:
        yield entry, {}


def check_scaleio_sds(item, params, parsed):
    data = get_scaleio_data(item, parsed)
    if not data:
        return

    # How will the data be represented? It's magic and the only
    # indication is the unit. We need to handle this!
    unit    = data["MAX_CAPACITY_IN_KB"][3].strip(")")
    total   = convert_scaleio_space(unit,
                                    int(data["MAX_CAPACITY_IN_KB"][2].strip("(")))
    free    = convert_scaleio_space(unit,
                                    int(data["UNUSED_CAPACITY_IN_KB"][2].strip("(")))

    yield df_check_filesystem_list(item, params, [ (item, total, free, 0) ])


check_info['scaleio_sds'] = {
    'parse_function'        : lambda info: parse_scaleio(info, "SDS"),
    'inventory_function'    : inventory_scaleio_sds,
    'check_function'        : check_scaleio_sds,
    'service_description'   : 'ScaleIO SDS capacity %s',
    'group'                 : "filesystem",
    'has_perfdata'          : True,
    'includes'              : [ 'scaleio.include', 'size_trend.include', 'df.include' ],
}


def inventory_scaleio_sds_status(parsed):
    for entry in parsed:
        yield entry, {}


def check_scaleio_sds_status(item, params, parsed):
    data = get_scaleio_data(item, parsed)
    if not data:
        return

    name, pd_id = data["NAME"][0], data["PROTECTION_DOMAIN_ID"][0]
    yield 0, "Name: %s, PD: %s" % (name, pd_id)

    status = data["STATE"][0]
    if "normal" not in status.lower():
        yield 2, "State: %s" % status

    status_maint = data["MAINTENANCE_MODE_STATE"][0]
    if "no_maintenance" not in status_maint.lower():
        yield 1, "Maintenance: %s" % status_maint

    status_conn = data["MDM_CONNECTION_STATE"][0]
    if "connected" not in status_conn.lower():
        yield 2, "Connection state: %s" % status_conn

    status_member = data["MEMBERSHIP_STATE"][0]
    if "joined" not in status_member.lower():
        yield 2, "Membership state: %s" % status_member


check_info['scaleio_sds.status'] = {
    'parse_function'        : lambda info: parse_scaleio(info, "SDS"),
    'inventory_function'    : inventory_scaleio_sds_status,
    'check_function'        : check_scaleio_sds_status,
    'service_description'   : 'ScaleIO SDS status %s',
}
