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


# NAME    SIZE  ALLOC   FREE  CAP  HEALTH  ALTROOT
# app02  39.8G  14.1G  25.6G  35%  ONLINE  -
# rpool  39.8G  32.9G  6.81G  82%  ONLINE  -

# Or also:
# NAME        SIZE   USED  AVAIL    CAP  HEALTH  ALTROOT
# sth_ds      278G   127G   151G    45%  ONLINE  -



def parse_zpool(info):

    def canonize_header_entry(entry):
        if entry == "used":
            return "alloc"
        elif entry == "avail":
            return "free"
        else:
            return entry

    result = {}
    if len(info) == 0:
        return result
    else:
        header = [ canonize_header_entry(item.lower()) for item in info[0]]
        for line in info[1:]:
            result[line[0]] = dict(zip(header, line))
        return result

def inventory_zpool(parsed):
    return df_inventory(parsed.keys())


def check_zpool(item, params, parsed):
    def mb(val):
        idx=None
        # split number from unit
        for idx, ch in enumerate(val):
            if ch not in "0123456789.-":
                break
        num = float(val[:idx])
        unit = val[idx:].lstrip().lower()
        unit = ["b", "k", "m", "g", "t"].index(unit)

        return num * (1024 ** (unit - 2))

    fslist = []
    for pool, entry in parsed.iteritems():
        if "patterns" in params or item == pool:
            fslist.append((pool, mb(entry['size']), mb(entry['free']), 0))

    return df_check_filesystem_list(item, params, fslist)


check_info['zpool'] = {
    "check_function"      : check_zpool,
    "inventory_function"  : inventory_zpool,
    "parse_function"      : parse_zpool,
    "service_description" : "Storage Pool %s",
    "has_perfdata"        : True,
    "group"               : "filesystem",
    "includes"            : ["df.include"],
}

