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

# exemplary output of the special agent ucs_bladecenter (separator is <TAB> and means tabulator):
#
#<<<ucsc_topsystem:sep(9)>>>
#topSystem<TAB>dn sys<TAB>address 192.168.1.1<TAB>currentTime Wed Feb  6 09:12:12 2019<TAB>mode stand-alone<TAB>name CIMC-istreamer2a-etn


def parse_ucs_c_rack_server_topsystem(info):
    """
    Input: Single line info with a rack server topsystem information.
    Output: Returns dict with dn, address, current time, mode and name as keys (with corresponding values).
    """
    def format_data_and_time(date_and_time):
        """Converts date and time and returns in time format.

        E.g. Wed Feb  6 09:12:12 2019 -> 2019-02-06 09:12:12
        """
        #time.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
        struct_time = time.strptime(date_and_time[4:], '%b %d %H:%M:%S %Y')
        formatted = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)
        return formatted

    parsed = []
    # The element count of info lines is under our control (agent output) and
    # ensured to have expected length. It is ensured that elements contain a
    # string. No bad case handling required here.
    for _, dn, ip, date_and_time, mode, name in info:
        # If more than one info line given or in case of unexpected info list element format the
        # parsed dict will be empty.
        parsed.extend([
            ('DN', dn.replace("dn ", "")),
            ('IP', ip.replace("address ", "")),
            ('Mode', mode.replace("mode ", "")),
            ('Name', name.replace("name ", "")),
        ])
        try:
            date_time_value = format_data_and_time(date_and_time.replace("currentTime ", ""))
        except ValueError:
            # indicate date and time format not supported
            date_time_value = 'unknown[%s]' % date_and_time[4:]
        parsed.append(('Date and time', date_time_value))
    return parsed


def inventory_ucs_c_rack_server_topsystem(parsed):
    if parsed:
        return [(None, None)]


#@get_parsed_item_data
def check_ucs_c_rack_server_topsystem(item, _no_params, data):
    for title, value in data:
        yield 0, "%s: %s" % (title, value)


check_info["ucs_c_rack_server_topsystem"] = {
    'parse_function': parse_ucs_c_rack_server_topsystem,
    'inventory_function': inventory_ucs_c_rack_server_topsystem,
    'check_function': check_ucs_c_rack_server_topsystem,
    'service_description': 'UCS C-Series Rack Server TopSystem Info',
}
