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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.ibm_mq import *  # pylint: disable=wildcard-import,unused-wildcard-import

# <<<ibm_mq_plugin:sep(58)>>>
# version|2.0.4
# dspmq|OK
# runmqsc|Not executable


def parse_ibm_mq_plugin(info):
    parsed = {}
    for line in info:
        key = line[0].strip()
        value = line[1].strip()
        parsed[key] = value
    return parsed


def inventory_ibm_mq_plugin(parsed):
    if parsed:
        return [(None, {})]


def check_tool(tool_name, parsed):
    if tool_name not in parsed:
        return 3, "%s: No agent info" % tool_name

    status, text = 0, parsed[tool_name]
    if text != "OK":
        status = 2
    return status, "%s: %s" % (tool_name, text)


def check_ibm_mq_plugin(_no_item, params, parsed):
    if not parsed:
        return

    actual_version = parsed.get("version")
    yield ibm_mq_check_version(actual_version, params, "Plugin version")
    yield check_tool("dspmq", parsed)
    yield check_tool("runmqsc", parsed)


check_info["ibm_mq_plugin"] = {
    "parse_function": parse_ibm_mq_plugin,
    "check_function": check_ibm_mq_plugin,
    "inventory_function": inventory_ibm_mq_plugin,
    "service_description": "IBM MQ Plugin",
    "has_perfdata": False,
    "group": "ibm_mq_plugin",
}
