#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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.

# <<<hp_msa_if>>>
# port 3 durable-id hostport_A1
# port 3 controller A
# port 3 controller-numeric 1
# port 3 port A1
# port 3 port-type FC
# port 3 port-type-numeric 6
# port 3 media FC(P)
# port 3 target-id 207000c0ff1e82bb
# port 3 status Up
# port 3 status-numeric 0
# port 3 actual-speed 8Gb
# port 3 actual-speed-numeric 7
# port 3 configured-speed Auto
# port 3 configured-speed-numeric 3
# port 3 health OK
# port 3 health-numeric 0
# port 3 health-reason
# port 3 health-recommendation
# fc-port 4 configured-topology PTP
# fc-port 4 primary-loop-id N/A
# fc-port 4 sfp-status OK
# fc-port 4 sfp-present Present
# fc-port 4 sfp-present-numeric 1
# fc-port 4 sfp-vendor DHS
# fc-port 4 sfp-part-number FTLF8529P3BCV-1H
# fc-port 4 sfp-revision A
# fc-port 4 sfp-supported-speeds 4G,8G,16G
# port 5 durable-id hostport_A2
# port 5 controller A
# port 5 controller-numeric 1
# port 5 port A2
# port 5 port-type FC
# port 5 port-type-numeric 6
# port 5 media FC(P)
# port 5 target-id 217000c0ff1e82bb
# port 5 status Up
# port 5 status-numeric 0
# port 5 actual-speed 8Gb
# port 5 actual-speed-numeric 7
# port 5 configured-speed Auto
# port 5 configured-speed-numeric 3
# port 5 health OK
# port 5 health-numeric 0
# port 5 health-reason
# port 5 health-recommendation
# fc-port 6 configured-topology PTP
# fc-port 6 primary-loop-id N/A
# fc-port 6 sfp-status OK
# fc-port 6 sfp-present Present
# fc-port 6 sfp-present-numeric 1
# fc-port 6 sfp-vendor DHS
# fc-port 6 sfp-part-number FTLF8529P3BCV-1H
# fc-port 6 sfp-revision A
# fc-port 6 sfp-supported-speeds 4G,8G,16G
# host-port-statistics 1 durable-id hostport_A1
# host-port-statistics 1 bytes-per-second 8881.6KB
# host-port-statistics 1 bytes-per-second-numeric 8881664
# host-port-statistics 1 iops 106
# host-port-statistics 1 number-of-reads 23978726
# host-port-statistics 1 number-of-writes 157185192
# host-port-statistics 1 data-read 2583.7GB
# host-port-statistics 1 data-read-numeric 2583779790336
# host-port-statistics 1 data-written 12.7TB
# host-port-statistics 1 data-written-numeric 12760541957120
# host-port-statistics 1 queue-depth 0
# host-port-statistics 1 avg-rsp-time 1355
# host-port-statistics 1 avg-read-rsp-time 7872
# host-port-statistics 1 avg-write-rsp-time 497
# host-port-statistics 1 reset-time 2015-05-22 13:54:37
# host-port-statistics 1 reset-time-numeric 1432302877
# host-port-statistics 1 start-sample-time 2015-08-21 11:51:43
# host-port-statistics 1 start-sample-time-numeric 1440157903
# host-port-statistics 1 stop-sample-time 2015-08-21 11:52:00
# host-port-statistics 1 stop-sample-time-numeric 1440157920
# host-port-statistics 2 durable-id hostport_A2
# host-port-statistics 2 bytes-per-second 5161.9KB
# host-port-statistics 2 bytes-per-second-numeric 5161984
# host-port-statistics 2 iops 220
# host-port-statistics 2 number-of-reads 43445061
# host-port-statistics 2 number-of-writes 343467343
# host-port-statistics 2 data-read 5127.7GB
# host-port-statistics 2 data-read-numeric 5127720481792
# host-port-statistics 2 data-written 28.0TB
# host-port-statistics 2 data-written-numeric 28069848090624
# host-port-statistics 2 queue-depth 0
# host-port-statistics 2 avg-rsp-time 1492
# host-port-statistics 2 avg-read-rsp-time 5309
# host-port-statistics 2 avg-write-rsp-time 1129
# host-port-statistics 2 reset-time 2015-05-22 13:54:37
# host-port-statistics 2 reset-time-numeric 1432302877
# host-port-statistics 2 start-sample-time 2015-08-21 11:51:43
# host-port-statistics 2 start-sample-time-numeric 1440157903
# host-port-statistics 2 stop-sample-time 2015-08-21 11:52:00
# host-port-statistics 2 stop-sample-time-numeric 1440157920

def parse_hp_msa_if(info):
    parsed = []
    for idx, (key, values) in enumerate(sorted(parse_hp_msa(info).items())):
        if values['actual-speed'] == '':
            speed = values['actual-speed'] = 0
        else:
            speed = int(values['actual-speed'].replace('Gb', '')) * 10 ** 9

        if values['status'] == 'Up':
            status = '1'
        else:
            status = '2'

        nic = ['0'] * 20
        nic[0]  = str(idx + 1)                          # Index
        nic[1]  = values['port']                        # Description
        nic[2]  = "6" # Fake ethernet                   # Type
        nic[3]  = speed                                 # Speed
        nic[4]  = status                                # Status
        # IN
        nic[5]  = int(values['data-read-numeric'])      # inoctets
        nic[6]  = 0                                     # inucast
        nic[7]  = 0                                     # inmcast
        nic[8]  = 0                                     # ibcast
        nic[9]  = 0                                     # indiscards
        nic[10] = 0                                     # inerrors
        # OUT
        nic[11] = int(values['data-written-numeric'])   # outoctets
        nic[12] = 0                                     # outucast
        nic[13] = 0                                     # outmcast
        nic[14] = 0                                     # outbcast
        nic[15] = 0                                     # outdiscards
        nic[16] = 0                                     # outspeed
        nic[17] = int(values['queue-depth'])            # outqlen
        nic[18] = ""                                    # Alias
        nic[19] = ""                                    # MAC

        parsed.append(nic)

    return parsed


def inventory_hp_msa_if(parsed):
    return inventory_if_common(parsed)


def check_hp_msa_if(item, params, parsed):
    yield check_if_common(item, params, parsed)


check_info['hp_msa_if'] = {
    'parse_function'            : parse_hp_msa_if,
    'inventory_function'        : inventory_hp_msa_if,
    'check_function'            : check_hp_msa_if,
    'service_description'       : 'Interface %s',
    'has_perfdata'              : True,
    'default_levels_variable'   : 'if_default_levels',
    'includes'                  : [ "hp_msa.include", "if.include" ],
}
