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

# Example output:
# <<<solaris_multipath>>>
# /dev/rdsk/c4t600601608CB02A00DCFD2EEB19A0E111d0s2 4 4

# Note: the number of total paths is not correct. After maintainance
# they is too high. Also in case of broken paths the number of total
# paths sometimes changes. So we just use that for informational
# output. The discovery remembers the number of operational paths
# and we check agains that later.

def inventory_solaris_multipath(info):
    for device, total, operational in info:
        item = device.split('/')[-1]
        yield item, int(operational)

def check_solaris_multipath(item, params, info):
    for device, total, operational in info:
        if item == device.split('/')[-1]:

            operational = int(operational)
            total       = int(total)

            infotext = "%d paths operational, %d paths total" % (operational, total)

            if not params:
                state = 1
                infotext += ", expected paths unknown, please redo service discovery"
            else:
                if type(params) == tuple:
                    warn, crit = params
                    warn_num  = (warn / 100.0) * total
                    crit_num = (crit / 100.0) * total
                    levels = " (Warning/ Critical at %d/ %d)" % (warn_num, crit_num)
                    info = "paths active: %d" % (operational)
                    if operational <= crit_num:
                        return 2, info + levels
                    elif operational <= warn_num:
                        return 1, info + levels
                    else:
                        return 0, info
                else:
                    expected = int(params) # should be int, just for legacy reasons
                if operational > expected:
                    state = 1
                elif expected == operational:
                    state = 0
                elif expected >= operational * 2: # less than half of paths operational
                    state = 2
                else:
                    state = 1
                if state:
                    infotext += ", %d paths expected to be operational" % expected

            return state, infotext


check_info["solaris_multipath"] =  {
    "inventory_function"  : inventory_solaris_multipath,
    "check_function"      : check_solaris_multipath,
    "service_description" : "Multipath %s",
    "group"               : "multipath"
}
