Skip to content

Commit

Permalink
migrate agent_elasticsearch to new ssc API
Browse files Browse the repository at this point in the history
Change-Id: I3b648b56aa8ac14c6250430fcb0306a620e4f9d2
  • Loading branch information
mo-ki committed Nov 15, 2023
1 parent ca4a09f commit e01cb6a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 93 deletions.
40 changes: 0 additions & 40 deletions cmk/base/legacy_checks/agent_elasticsearch.py

This file was deleted.

55 changes: 55 additions & 0 deletions cmk/plugins/collection/agent_based/agent_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk 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.

# {
# 'port': 443,
# 'password': 'baem',
# 'infos': ['cluster_health', 'nodestats', 'stats'],
# 'user': 'blub'
# }


from collections.abc import Iterable, Mapping

from cmk.server_side_calls.v1 import (
get_secret_from_params,
HostConfig,
HTTPProxy,
noop_parser,
Secret,
SpecialAgentCommand,
SpecialAgentConfig,
)


def _agent_elasticsearch_arguments(
params: Mapping[str, object], hostconfig: HostConfig, proxy_config: Mapping[str, HTTPProxy]
) -> Iterable[SpecialAgentCommand]:
args: list[str | Secret] = [
"-P",
str(params["protocol"]),
"-m",
*(str(i) for i in params["infos"]), # type: ignore[attr-defined]
]

if "user" in params:
args.extend(["-u", str(params["user"])])
if "password" in params:
args.extend(["-s", get_secret_from_params(*params["password"])]) # type: ignore[misc]
if "port" in params:
args.extend(["-p", str(params["port"])])
if params.get("no-cert-check", False):
args.append("--no-cert-check")

args.extend(str(h) for h in params["hosts"]) # type: ignore[attr-defined]

yield SpecialAgentCommand(args)


special_agent_elasticsearch = SpecialAgentConfig(
name="elasticsearch",
parameter_parser=noop_parser,
commands_function=_agent_elasticsearch_arguments,
)
53 changes: 0 additions & 53 deletions tests/unit/checks/test_agent_elasticsearch.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk 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.


from cmk.plugins.collection.agent_based.agent_elasticsearch import special_agent_elasticsearch
from cmk.server_side_calls.v1 import HostConfig, IPAddressFamily, PlainTextSecret

TEST_HOST_CONFIG = HostConfig("my_host", "1.2.3.4", "my_alias", IPAddressFamily.IPV4)


def test_agent_elasticsearch_arguments_cert_check() -> None:
params: dict[str, object] = {
"hosts": ["testhost"],
"protocol": "https",
"infos": ["cluster_health", "nodestats", "stats"],
}
(cmd,) = special_agent_elasticsearch.commands_function(params, TEST_HOST_CONFIG, {})
assert "--no-cert-check" not in cmd.command_arguments

params["no-cert-check"] = True
(cmd,) = special_agent_elasticsearch.commands_function(params, TEST_HOST_CONFIG, {})
assert "--no-cert-check" in cmd.command_arguments


def test_agent_elasticsearch_arguments_password_store() -> None:
params: dict[str, object] = {
"hosts": ["testhost"],
"protocol": "https",
"infos": ["cluster_health", "nodestats", "stats"],
"user": "user",
"password": ("password", "pass"),
}
(cmd,) = special_agent_elasticsearch.commands_function(params, TEST_HOST_CONFIG, {})
assert cmd.command_arguments == [
"-P",
"https",
"-m",
"cluster_health",
"nodestats",
"stats",
"-u",
"user",
"-s",
PlainTextSecret("pass"),
"testhost",
]

0 comments on commit e01cb6a

Please sign in to comment.