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


# <<<statgrab_disk>>>
# 1/md0.disk_name 1/md0
# 1/md0.read_bytes 611352576
# 1/md0.systime 1471423620
# 1/md0.write_bytes 39462400
# 1/md1.disk_name 1/md1
# 1/md1.read_bytes 611352576
# 1/md1.systime 1471423620
# 1/md1.write_bytes 39462400
# 1/md2.disk_name 1/md2
# 1/md2.read_bytes 611351552


def parse_statgrab_disk(info):
    parsed = {}
    now    = time.time()
    for line in info:
        disk_name = line[0].split(".")[0]
        parsed.setdefault(disk_name, {})

        if line[0].endswith('read_bytes'):
            parsed[disk_name]["read_throughput"] = get_rate("statgrab_disk.read.%s" % disk_name, now, int(line[1]))

        elif line[0].endswith('write_bytes'):
            parsed[disk_name]["write_throughput"] = get_rate("statgrab_disk.write.%s" % disk_name, now, int(line[1]))

        elif line[0].endswith("systime"):
            parsed[disk_name]["systime"] = int(line[1])

    return parsed


def inventory_statgrab_disk(parsed):
    return inventory_diskstat_generic([[None, item] for item in parsed])


def check_statgrab_disk(item, params, parsed):
    return check_diskstat_dict(item, params, parsed)


check_info["statgrab_disk"] = {
    'parse_function'        : parse_statgrab_disk,
    'inventory_function'    : inventory_statgrab_disk,
    'check_function'        : check_statgrab_disk,
    'service_description'   : 'Disk IO %s',
    'has_perfdata'          : True,
    'group'                 : 'diskstat',
    'includes'              : [ "diskstat.include" ],
}
