#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.diskstat import *  # pylint: disable=wildcard-import,unused-wildcard-import

# <<<hpux_lunstats>>>
# WWID:  0x600508b1001cf7f0d25c51941cf5e2d7
#         STATISTICS FOR LUN :/dev/rdisk/disk11
# Bytes read                                       : 841717976279
# Bytes written                                    : 430393024512
# Total I/Os processed                             : 206684834
# I/O failures                                     : 0
# Retried I/O failures                             : 0
# I/O failures due to invalid IO size              : 0
# IO failures due to misallignment or boundary      : 0
# WWID:  0x60a98000572d44745634645076556357
#         STATISTICS FOR LUN :/dev/rdisk/disk5
# Bytes read                                       : 1035897815087
# Bytes written                                    : 113475461120
# Total I/Os processed                             : 23920189
# I/O failures                                     : 24
# Retried I/O failures                             : 0
# I/O failures due to invalid IO size              : 0
# IO failures due to misallignment or boundary      : 0
# WWID:  0x60a98000572d4474563464507665446d
#         STATISTICS FOR LUN :/dev/rdisk/disk6


# Convert info to output needed for generic diskstat check
def parse_hpux_lunstats(info):
    # TODO use 'check_diskstat_dict' which needs the following format
    # disks = { "sda" : {
    #       'node'                       : None,
    #       'average_read_request_size'  : 0.0,
    #       'average_read_wait'          : 0.0,
    #       'average_request_size'       : 40569.90476190476,
    #       'average_wait'               : 0.761904761904762,
    #       'average_write_request_size' : 40569.90476190476,
    #       'average_write_wait'         : 0.0007619047619047619,
    #       'read_ios'                   : 0.0,
    #       'read_throughput'            : 0.0,
    #       'latency'                    : 0.00038095238095238096,
    #       'utilization'                : 0.0006153846153846154,
    #       'write_ios'                  : 1.6153846153846154,
    #       'write_throughput'           : 65536.0,
    #       'queue_length'               : 0.0,
    #       'read_ql'                    : 0.0,
    #       'write_ql'                   : 0.0,
    # }}
    luns = []
    for line in info:
        if len(line) == 2:
            left = line[0].strip()
            right = line[1].strip()
            if left == "STATISTICS FOR LUN":
                lun = right
            elif left == "Bytes read":
                bytes_read = int(right) // 512
            elif left == "Bytes written":
                bytes_written = int(right) // 512
                luns.append((None, lun, bytes_read, bytes_written))
    return luns


def check_hpux_lunstats(item, params, parsed):
    return check_diskstat_generic(item, params, time.time(), parsed)


check_info["hpux_lunstats"] = {
    "parse_function": parse_hpux_lunstats,
    "check_function": check_hpux_lunstats,
    "inventory_function": inventory_diskstat_generic,
    "service_description": "Disk IO %s",
    "has_perfdata": True,
    "group": "disk_io",
}
