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


def check_elasticsearch_query_arguments(params):
    args = []

    if "protocol" in params:
        args += ["-P", params["protocol"]]
    if "user" in params:
        args += ["-u", params["user"]]
    if "password" in params:
        args += ["-s", passwordstore_get_cmdline("%s", params["password"])]
    if "port" in params:
        args += ["-p", params["port"]]

    if "index" in params:
        args += ["-i", " ".join(params["index"])]
    if "pattern" in params:
        args += ["-q", params["pattern"]]
    if "fieldname" in params:
        args += ["-f", " ".join(params["fieldname"])]
    if "timerange" in params:
        args += ["-t", params["timerange"]]
    if "count" in params:
        warn, crit = params["count"]
        args.append("--warn=%d" % warn)
        args.append("--crit=%d" % crit)

    if "hostname" in params:
        args += ["-H", params["hostname"]]
    else:
        args += ["-H", "$HOSTADDRESS$"]

    return args


active_check_info["elasticsearch_query"] = {
    "command_line": "check_elasticsearch_query $ARG1$",
    "argument_function": check_elasticsearch_query_arguments,
    "service_description": lambda params: "Elasticsearch Query %s" % params["svc_item"],
    "has_perfdata": True,
}
