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

# <<<splunk_jobs>>>
# splunk-system-user, , 0.91, False, 61440, False
# admin, search, 0.101, False, 69632, False

JobCount = collections.namedtuple(  # pylint: disable=collections-namedtuple-call
    "JobCount", ["jobs", "failed", "zombies", "output"]
)


def parse_splunk_jobs(info):
    parsed = {}
    long_output = ""
    job_count, failed_count, zombie_count = 0, 0, 0

    for job_detail in info:
        try:
            published, author, app, dispatchstate, iszombie = job_detail
            job_count += 1

            if dispatchstate == "FAILED":
                failed_count += 1
            if iszombie == "True":
                zombie_count += 1

            long_output += "%s - Author: %s, Application: %s, State: %s, Zombie: %s\n" % (
                published,
                author,
                app,
                dispatchstate,
                iszombie,
            )

        except (IndexError, ValueError):
            pass

    parsed.setdefault("job", []).append(
        JobCount(job_count, failed_count, zombie_count, long_output)
    )

    return parsed


def inventory_splunk_jobs(parsed):
    yield None, {}


def check_splunk_jobs(no_item, params, parsed):
    data = parsed["job"][0]

    for key, value, infotext in [
        ("job_total", data.jobs, "Job Count"),
        ("failed_jobs", data.failed, "Failed jobs"),
        ("zombie_jobs", data.zombies, "Zombie jobs"),
    ]:

        yield check_levels(value, key, params.get(key), human_readable_func=int, infoname=infotext)

    if data.output:
        yield 0, "\n%s" % data.output


check_info["splunk_jobs"] = {
    "parse_function": parse_splunk_jobs,
    "check_function": check_splunk_jobs,
    "inventory_function": inventory_splunk_jobs,
    "service_description": "Splunk Jobs",
    "group": "splunk_jobs",
    "has_perfdata": True,
}
