#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# <<<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',
}
