#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.674.10892.5.4.300.50.1.8.1.1 2.5.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.130.3.1.26.1 1.03
# .1.3.6.1.4.1.674.10892.5.1.3.2.0 JG2NS12
# .1.3.6.1.4.1.674.10892.5.1.3.3.0 42330791846


def _get_date_format(date):
    # Beware: Dell's actual definition of the format supposed
    # to be here is yyyymmddHHMMSS.uuuuuu+ooo. This has *never*
    # been observed in the wild. More accurate appears to be
    # mm/dd/yyyy or 0mm/dd/yyyy or mm/0dd/yyyy. The 0 represents a
    # random 0 thrown in for good measure :/
    try:
        if date[2] == "/" and date[5] == "/":  # mm/dd/yyyy
            return "%m/%d/%Y"
        elif date[3] == "/" and date[6] == "/":  # 0mm/dd/yyyy
            return "0%m/%d/%Y"
        elif date[2] == "/" and date[6] == "/":  # mm/0dd/yyyy
            return "%m/0%d/%Y"
        elif "/" not in date[:8]:  # In case of Dell devices following the MIB
            return "%Y%m%d"
        return
    except IndexError:
        return

def inv_dell_idrac_info(info):
    serial, expresscode, bios_date, bios_version, \
        bios_vendor, raid_name, raid_version = info[0]

    node = inv_tree("hardware.system.")
    node['serial'] = serial
    node['expresscode'] = expresscode

    node = inv_tree("hardware.bios.")
    node['version'] = bios_version
    node['vendor'] = bios_vendor

    bios_date_fmt = _get_date_format(bios_date)
    if bios_date_fmt:
        node['date'] = time.mktime(time.strptime(bios_date, bios_date_fmt))

    node = inv_tree("hardware.storage.controller.")
    node['version'] = raid_version
    node['name'] = raid_name

inv_info['dell_hw_info'] = {
    'inv_function'       : inv_dell_idrac_info,
    'snmp_info'          : ('.1.3.6.1.4.1.674.10892.5', [
                                "1.3.2.0",              # IDRAC-MIB::systemServiceTag
                                "1.3.3.0",              # IDRAC-MIB::systemExpressServiceCode
                                "4.300.50.1.7.1.1",     # IDRAC-MIB::systemBIOSReleaseDateName
                                "4.300.50.1.8.1.1",     # IDRAC-MIB::systemBIOSVersionName
                                "4.300.50.1.11.1.1",    # IDRAC-MIB::systemBIOSManufacturerName
                                "5.1.20.130.1.1.2.1",   # IDRAC-MIB::controllerName
                                "5.1.20.130.1.1.8.1",   # IDRAC-MIB::controllerFWVersion
                           ]),
    'snmp_scan_function' : lambda oid: oid(".1.3.6.1.4.1.674.10892.5.1.1.1.0"),
}
