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

# .1.3.6.1.4.1.9694.1.5.2.1.0 No Fault --> PEAKFLOW-TMS-MIB::tmsHostFault.0
# .1.3.6.1.4.1.9694.1.5.2.2.0 101420100 --> PEAKFLOW-TMS-MIB::tmsHostUpTime.0
# .1.3.6.1.4.1.9694.1.5.2.3.0 46 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg1min.0
# .1.3.6.1.4.1.9694.1.5.2.4.0 64 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg5min.0
# .1.3.6.1.4.1.9694.1.5.2.5.0 67 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg15min.0
# .1.3.6.1.4.1.9694.1.5.2.6.0 6 --> PEAKFLOW-TMS-MIB::deviceDiskUsage.0
# .1.3.6.1.4.1.9694.1.5.2.7.0 4 --> PEAKFLOW-TMS-MIB::devicePhysicalMemoryUsage.0
# .1.3.6.1.4.1.9694.1.5.2.8.0 0 --> PEAKFLOW-TMS-MIB::deviceSwapSpaceUsage.0


def parse_peakflow_tms(info):
    health = info[0][0]
    updates = info[1][0]
    return {
        'cpu_loads': health[:3],
        'disk': health[3],
        'memory': health[4:6],
        'host_fault': health[6],
        'update': {
            "Device": updates[0],
            "Mitigation": updates[1]
        }
    }


check_info["arbor_peakflow_tms"] = {
    "check_function": check_arbor_memory,
    "inventory_function": inventory_arbor_memory,
    "parse_function": parse_peakflow_tms,
    "service_description": "Memory",
    "has_perfdata": True,
    "group": "memory_arbor",
    'default_levels_variable': 'arbor_memory_default_levels',
    "snmp_info": [
        (
            ".1.3.6.1.4.1.9694.1.5.2",
            [
                "3.0",  # deviceCpuLoadAvg1min
                "4.0",  # deviceCpuLoadAvg5min
                "5.0",  # deviceCpuLoadAvg15min
                "6.0",  # deviceDiskUsage
                "7.0",  # devicePhysicalMemoryUsage
                "8.0",  # deviceSwapSpaceUsage
                "1.0",  # tmsHostFault
            ]),
        (
            ".1.3.6.1.4.1.9694.1.5.5",
            [
                "1.2.0",  # tmsLastUpdate
                "2.1.0",  # tmsMitigationLastUpdate
            ])
    ],
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Peakflow"),
    "includes": ["arbor.include"],
}

check_info["arbor_peakflow_tms.cpu_load"] = {
    "check_function": check_arbor_cpu_load,
    "inventory_function": inventory_arbor_cpu_load,
    "service_description": "CPU load",
    "has_perfdata": True,
    "group": "cpu_load",
    "includes": ["cpu_load.include"],
}

check_info["arbor_peakflow_tms.disk_usage"] = {
    "check_function": check_arbor_disk_usage,
    "inventory_function": inventory_arbor_disk_usage,
    "service_description": "Disk Usage %s",
    "has_perfdata": True,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}

check_info["arbor_peakflow_tms.host_fault"] = {
    "check_function": check_arbor_host_fault,
    "inventory_function": inventory_arbor_host_fault,
    "service_description": "Host Fault",
}


def inventory_peakflow_tms_updates(parsed):
    for name in parsed['update']:
        yield name, None


def check_peakflow_tms_updates(item, no_params, parsed):
    if item in parsed['update']:
        return 0, parsed['update'][item]


check_info["arbor_peakflow_tms.updates"] = {
    "check_function": check_peakflow_tms_updates,
    "inventory_function": inventory_peakflow_tms_updates,
    "service_description": "Config Update %s",
}
