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

# <<<emcvnx_sp_util:sep(58)>>>
# Controller busy ticks: 1639432
# Controller idle ticks: 1773844

# suggested by customer
emcvnx_sp_util_default_levels = (50.0, 60.0)


def inventory_emcvnx_sp_util(info):
    return [ (None, "emcvnx_sp_util_default_levels") ]


def check_emcvnx_sp_util(item, params, info):
    for line in info:
        if len(line) == 2 and "busy" in line[0]:
            busy_ticks = float(line[1])
        elif len(line) == 2 and "idle" in line[0]:
            idle_ticks = float(line[1])

    now = time.time()
    warn, crit = params
    busy_ticks_rate = get_rate("emcvnx_sp_util.busy_ticks", now, busy_ticks )
    idle_ticks_rate = get_rate("emcvnx_sp_util.idle_ticks", now, idle_ticks )
    if busy_ticks_rate + idle_ticks_rate == 0:
        sp_util = 0
    else:
        sp_util = 100 * (busy_ticks_rate / (busy_ticks_rate + idle_ticks_rate))
    infotext = "%.1f%%" % sp_util
    if sp_util >= crit:
        state = 2
    elif sp_util >= warn:
        state = 1
    else:
        state = 0

    if state > 0:
        infotext += " (warn/crit at %.1f%%/%.1f%%)" % (warn, crit)

    return state, infotext, [ ("storage_processor_util", sp_util, warn, crit, 0, 100.0) ]


check_info['emcvnx_sp_util'] = {
    "inventory_function"      : inventory_emcvnx_sp_util,
    "check_function"          : check_emcvnx_sp_util,
    "service_description"     : "Storage Processor Utilization",
    "group"                   : "sp_util",
    "has_perfdata"            : True,
}
