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

# <<<netapp_api_vs_status:sep(9)>>>
# zcs1v    running
# zhs01    running
# zmppl01  running
# zmdp     running
# cdefs1v  running


def parse_netapp_api_vs_status(info):
    parsed = {}
    for line in info:
        if len(line) == 2:
            # pre v1.6.0 agent output
            name, state = line
            parsed[name] = {'state': state}
        else:
            parsed[line[0]] = dict(zip(line[1::2], line[2::2]))
    return parsed


@get_parsed_item_data
def check_netapp_api_vs_status(item, _no_params, data):
    server_state = data.get('state')
    if not server_state:
        return
    subtype = data.get('vserver-subtype')
    if server_state == 'running':
        state = 0
    elif server_state == 'stopped' and subtype == 'dp_destination':
        state = 0
    else:
        state = 2
    yield state, "State: %s" % server_state
    if subtype:
        yield 0, "Subtype: %s" % subtype


check_info["netapp_api_vs_status"] = {
    'parse_function': parse_netapp_api_vs_status,
    'inventory_function': discover(lambda k, values: 'state' in values),
    'check_function': check_netapp_api_vs_status,
    'service_description': 'vServer Status %s',
}
