#!/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_ceph_status(info):
    try:
        import json
    except:
        import simplejson as json

    parsed = ""
    for line in info:
        parsed += " ".join(line)
    return json.loads(parsed)


def ceph_check_epoch(_id, epoch, params):
    warn, crit, avg_interval_min = params.get("epoch", (None, None, 1))
    epoch_rate = get_rate("%s.epoch.rate" % _id, time.time(), epoch, avg_interval_min)
    epoch_avg  = get_average("%s.epoch.avg" % _id, time.time(), epoch_rate, avg_interval_min)
    state      = 0
    infotext   = 'Epoch: %s/%s' % (epoch_rate, get_age_human_readable(avg_interval_min*60))

    if warn is not None and crit is not None:
        if epoch_rate >= crit:
            state = 2
        elif epoch_rate >= warn:
            state = 1
        if state > 0:
            infotext += " (warn/crit at %.1f/%.1f)" % (warn, crit)

    return state, infotext


#   .--status--------------------------------------------------------------.
#   |                         _        _                                   |
#   |                     ___| |_ __ _| |_ _   _ ___                       |
#   |                    / __| __/ _` | __| | | / __|                      |
#   |                    \__ \ || (_| | |_| |_| \__ \                      |
#   |                    |___/\__\__,_|\__|\__,_|___/                      |
#   |                                                                      |
#   '----------------------------------------------------------------------'


# Suggested by customer: 1,3 per 30 min
factory_settings['ceph_status_default_levels'] = {
    'epoch' : (1, 3, 30),
}


def inventory_ceph_status(parsed):
    return [ (None, {}) ]


# TODO genereller Status -> ceph health (Ausnahmen für "too many PGs per OSD" als Option ermöglichen)
def check_ceph_status(_no_item, params, parsed):
    map_health_states = {
        "HEALTH_OK"   : (0, "OK"),
        "HEALTH_WARN" : (1, "warning"),
        "HEALTH_CRIT" : (2, "critical"),
        "HEALTH_ERR" : (2, "error"),
    }

    overall_status = parsed["health"]["overall_status"]
    state, state_readable = map_health_states.get(overall_status,
                            (3, "unknown[%s]" % overall_status))
    yield state, 'Status: %s' % state_readable
    yield ceph_check_epoch("ceph_status", parsed["election_epoch"], params)


check_info['ceph_status'] = {
    'parse_function'            : parse_ceph_status,
    'inventory_function'        : inventory_ceph_status,
    'check_function'            : check_ceph_status,
    'service_description'       : 'Ceph Status',
    'default_levels_variable'   : 'ceph_status_default_levels',
}


#.
#   .--osds----------------------------------------------------------------.
#   |                                       _                              |
#   |                          ___  ___  __| |___                          |
#   |                         / _ \/ __|/ _` / __|                         |
#   |                        | (_) \__ \ (_| \__ \                         |
#   |                         \___/|___/\__,_|___/                         |
#   |                                                                      |
#   '----------------------------------------------------------------------'


# Suggested by customer: 50, 100 per 15 min
factory_settings['ceph_osds_default_levels'] = {
    'epoch'         : (50, 100, 15),
    'num_out_osds'  : (7.0, 5.0),
    'num_down_osds' : (7.0, 5.0),
}


def inventory_ceph_status_osds(parsed):
    if "osdmap" in parsed:
        return [(None, {})]


def check_ceph_status_osds(_no_item, params, parsed):
    data     = parsed["osdmap"]["osdmap"]
    num_osds = int(data["num_osds"])
    yield ceph_check_epoch("ceph_osds", data["epoch"], params)

    for ds, title, state in [
        ('full',     'Full',      2),
        ('nearfull', 'Near full', 1),
    ]:
        ds_value = data[ds]
        if ds_value:
            yield state, "%s: %s" % (title, ", ".join(ds_value))

    yield 0, "OSDs: %s, Remapped PGs: %s" % (num_osds, data["num_remapped_pgs"])

    for ds, title, param_key in [
        ('num_in_osds', 'OSDs out',  'num_out_osds'),
        ('num_up_osds', 'OSDs down', 'num_down_osds'),
    ]:
        state      = 0
        value      = num_osds - data[ds]
        value_perc = 100 * float(value) / num_osds
        infotext   = "%s: %s, %s" % (title, value, get_percent_human_readable(value_perc))
        if params.get(param_key):
            warn, crit = params[param_key]
            if value_perc >= crit:
                state = 2
            elif value_perc >= crit:
                state = 1
            if state > 0:
                infotext += " (warn/crit at %s/%s)" % \
                            (get_percent_human_readable(warn),
                             get_percent_human_readable(crit))

        yield state, infotext


check_info['ceph_status.osds'] = {
    'inventory_function'        : inventory_ceph_status_osds,
    'check_function'            : check_ceph_status_osds,
    'service_description'       : 'Ceph OSDs',
    'default_levels_variable'   : 'ceph_osds_default_levels',
    'group'                     : 'ceph_osds',
}


#.
#   .--pgs-----------------------------------------------------------------.
#   |                                                                      |
#   |                           _ __   __ _ ___                            |
#   |                          | '_ \ / _` / __|                           |
#   |                          | |_) | (_| \__ \                           |
#   |                          | .__/ \__, |___/                           |
#   |                          |_|    |___/                                |
#   '----------------------------------------------------------------------'


def inventory_ceph_status_pgs(parsed):
    if "pgmap" in parsed:
        return [(None, {})]


def check_ceph_status_pgs(_no_item, params, parsed):
    # Suggested by customer
    map_pg_states = {
        "active"           : (0, "active"),
        "backfill"         : (0, "backfill"),
        "backfill_wait"    : (1, "backfill wait"),
        "backfilling"      : (1, "backfilling"),
        "backfill_toofull" : (0, "backfill too full"),
        "clean"            : (0, "clean"),
        "creating"         : (0, "creating"),
        "degraded"         : (1, "degraded"),
        "down"             : (2, "down"),
        "deep"             : (0, "deep"),
        "incomplete"       : (2, "incomplete"),
        "inconsistent"     : (2, "inconsistent"),
        "peered"           : (2, "peered"),
        "perring"          : (0, "peering"),
        "recovering"       : (0, "recovering"),
        "recovery_wait"    : (0, "recovery wait"),
        "remapped"         : (0, "remapped"),
        "repair"           : (0, "repair"),
        "replay"           : (1, "replay"),
        "scrubbing"        : (0, "scrubbing"),
        "stale"            : (2, "stale"),
        "undersized"       : (0, "undersized"),
        "wait_backfill"    : (0, "wait backfill"),
    }

    data      = parsed["pgmap"]
    num_pgs   = data["num_pgs"]
    pgs_info  = "PGs: %s" % num_pgs
    states    = [0]
    infotexts = []

    for pgs_by_state in data["pgs_by_state"]:
        statetexts = []
        for status in pgs_by_state["state_name"].split("+"):
            state, state_readable = map_pg_states.get(status, (3, "UNKNOWN[%s]" % status))
            states.append(state)
            statetexts.append("%s%s" % (state_readable, state_markers[state]))
        infotexts.append("Status '%s': %s" % ("+".join(statetexts), pgs_by_state["count"]))

    return max(states), "%s, %s" % (pgs_info, ", ".join(infotexts))


check_info['ceph_status.pgs'] = {
    'inventory_function'    : inventory_ceph_status_pgs,
    'check_function'        : check_ceph_status_pgs,
    'service_description'   : 'Ceph PGs',
}


#.
#   .--mgrs----------------------------------------------------------------.
#   |                                                                      |
#   |                      _ __ ___   __ _ _ __ ___                        |
#   |                     | '_ ` _ \ / _` | '__/ __|                       |
#   |                     | | | | | | (_| | |  \__ \                       |
#   |                     |_| |_| |_|\__, |_|  |___/                       |
#   |                                |___/                                 |
#   '----------------------------------------------------------------------'


# Suggested by customer: 1, 2 per 5 min
factory_settings['ceph_mgrs_default_levels'] = {
    'epoch'         : (1, 2, 5),
}


def inventory_ceph_status_mgrs(parsed):
    if "mgrmap" in parsed:
        return [(None, {})]


def check_ceph_status_mgrs(_no_item, params, parsed):
    data = parsed["mgrmap"]
    yield ceph_check_epoch("ceph_mgrs", data["epoch"], params)


check_info['ceph_status.mgrs'] = {
    'inventory_function'        : inventory_ceph_status_mgrs,
    'check_function'            : check_ceph_status_mgrs,
    'service_description'       : 'Ceph MGRs',
    'default_levels_variable'   : 'ceph_mgrs_default_levels',
    'group'                     : 'ceph_mgrs',
}
