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

# <<<windows_os_bonding:sep(58)>>>
# Team Name: LAN
# Bonding Mode: Dynamic
# Status: Up
# Speed: 20 Gbps
#
# Slave Name: NIC1
# Slave Interface: Ethernet_14
# Slave Description: Intel(R) Ethernet 10G 2P X520-k bNDC #2
# Slave Status: Up
# Slave Speed: 10 Gbps
# Slave MAC address: 18-A9-9B-9F-AD-28
#
# Slave Name: NIC2
# Slave Interface: Ethernet_10
# Slave Description: Intel(R) Ethernet 10G 2P X520-k bNDC
# Slave Status: Up
# Slave Speed: 10 Gbps
# Slave MAC address: 18-A9-9B-9F-AD-2A

def parse_windows_os_bonding(info):
    bonds = {}

    for line in info:
        if len(line)>1:
            line[1] = re.sub("^ +", "", line[1])
        if line[0] == "Team Name":
            bond = line[1]
            bonds[bond] = {}
            bonds[bond]["interfaces"] = {}
        elif line[0] == "Bonding Mode":
            bonds[bond]["mode"] = line[1]
        elif line[0] == "Status":
            bonds[bond]["status"] = line[1].lower()
        elif line[0] == "Speed":
            bonds[bond]["speed"] = line[1]
        elif line[0] == "Slave Name":
            slave = line[1]
            bonds[bond]["interfaces"][slave] = {}
        elif line[0] == "Slave Status":
            bonds[bond]["interfaces"][slave]["status"] = line[1].lower()
        elif line[0] == "Slave MAC address":
            bonds[bond]["interfaces"][slave]["hwaddr"] = line[1].lower().replace("-", ":")
    return bonds


check_info['windows_os_bonding'] = {
    "check_function"          : lambda item,params,info: check_bonding(item, params, parse_windows_os_bonding(info)),
    "inventory_function"      : lambda info: inventory_bonding(parse_windows_os_bonding(info)),
    "service_description"     : "Bonding Interface %s",
    "group"                   : "bonding",
    "includes"                : [ "bonding.include" ],
}
