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

# 7mode
# <<<netapp_api_cpu:sep(9)>>>
# cpu_busy        8362860064
# num_processors  2

# clustermode
# cpu-info clu1-01        num_processors 2
# cpu-info clu1-02        num_processors 2
# cpu-info clu1-01        cpu_busy 5340000        nvram-battery-status battery_ok
# cpu-info clu1-02        cpu_busy 5400000        nvram-battery-status battery_ok

netapp_api_cpu_default_levels = (90.0, 95.0)

factory_settings["netapp_api_cpu_cm_default_levels"] = {
    "levels" : (90.0, 95.0)
}

def parse_netapp_api_cpu(info):
    cpu_info = {}
    for line in info:
        if line[0].startswith("cpu-info"): # clustermode
            _, node_name = line[0].split()
            cpu_info.setdefault("clustermode", {})
            for entry in line[1:]:
                key, value = entry.split()
                cpu_info["clustermode"].setdefault(node_name, {})
                cpu_info["clustermode"][node_name][key] = value
        else:
            cpu_info.setdefault("7mode", {})
            cpu_info["7mode"][line[0]] = line[1]
    return cpu_info

def inventory_netapp_api_cpu_utilization(parsed):
    if "7mode" in parsed:
        yield (None, "netapp_api_cpu_default_levels")

def inventory_netapp_api_cpu(parsed):
    if "clustermode" in parsed:
        for node in parsed.get("clustermode",{}).keys():
            yield node, {}

def check_netapp_api_cpu_utilization(item, params, parsed, mode):
    mode_data = parsed.get(mode)
    if item:
        data = mode_data.get(item)
    else:
        data = mode_data

    now = time.time()

    cpu_busy = int(data["cpu_busy"])
    num_cpus = int(data["num_processors"])
    ticks_per_sec = get_rate("netapp_api_cpu.utilization", now, cpu_busy, onwrap=RAISE)
    cpusecs_per_sec = ticks_per_sec / 1000000.0
    used_perc = 100.0 * cpusecs_per_sec

    # Due to timeing invariancies the measured level can become > 100%.
    # This makes users unhappy, so cut it off.
    if used_perc < 0:
        used_perc = 0
    elif used_perc > 100:
        used_perc = 100

    state, infotext, perfdata = check_cpu_util(used_perc, params, now).next()
    perfdata[0] = perfdata[0][:5] + (num_cpus,)
    infotext += ", %d CPUs" % num_cpus
    return state, infotext, perfdata

# Clustermode CPU utilization
check_info["netapp_api_cpu"] = {
    "parse_function"      : parse_netapp_api_cpu,
    "inventory_function"  : inventory_netapp_api_cpu,
    "check_function"      : lambda item, params, parsed: check_netapp_api_cpu_utilization(item, params, parsed, "clustermode"),
    "service_description" : "CPU utilization Node %s",
    "has_perfdata"        : True,
    "group"               : "cpu_utilization_multiitem",
    "includes"            : [ "cpu_util.include", "netapp_api.include" ]
}

# 7Mode CPU utilization
check_info["netapp_api_cpu.utilization"] = {
    "inventory_function"      : inventory_netapp_api_cpu_utilization,
    "check_function"          : lambda item, params, parsed: check_netapp_api_cpu_utilization(item, params, parsed, "7mode"),
    "service_description"     : "CPU utilization",
    "default_levels_variable" : "netapp_api_cpu_cm_default_levels",
    "has_perfdata"            : True,
    "group"                   : "cpu_utilization",
    "includes"                : [ "cpu_util.include" ]
}


def inventory_netapp_api_nvram_bat(parsed):
    for node, values in parsed.get("clustermode", {}).items():
        if "nvram-battery-status" in values:
            yield node, None

def check_netapp_api_nvram_bat(item, _no_params, parsed):
    state_map = {
          "battery_ok":                     0,
          "battery_partially_discharged":   0,
          "battery_fully_discharged ":      2,
          "battery_not_present":            2,
          "battery_near_end_of_life":       1,
          "battery_at_end_of_life":         2,
          "battery_unknown":                3,
          "battery_over_charged":           1,
          "battery_fully_charged":          0
        }

    info = parsed.get("clustermode", {}).get(item)
    if not info or "nvram-battery-status" not in info:
        return

    yield state_map.get(info["nvram-battery-status"], 3), "Status: %s" %\
                        info["nvram-battery-status"].replace("_", " ").title()


# Clustermode NVRAM Bat
check_info["netapp_api_cpu.nvram_bat"] = {
    "inventory_function"  : inventory_netapp_api_nvram_bat,
    "check_function"      : check_netapp_api_nvram_bat,
    "service_description" : "NVRAM Battery %s",
    "has_perfdata"        : True,
    "includes"            : [ "cpu_util.include", "netapp_api.include" ]
}
