Skip to content

Commit

Permalink
Migrate agent_smb_share to SSC and ruleset APIs
Browse files Browse the repository at this point in the history
CMK-17402

Change-Id: I21c4ad33b9e9ab2634ede4d00eac7d4077f8ad54
  • Loading branch information
racicLuka committed Oct 23, 2024
1 parent 4c05d85 commit 09626fa
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 153 deletions.
37 changes: 0 additions & 37 deletions cmk/base/legacy_checks/agent_smb_share.py

This file was deleted.

93 changes: 0 additions & 93 deletions cmk/gui/plugins/wato/special_agents/smb_share.py

This file was deleted.

141 changes: 141 additions & 0 deletions cmk/plugins/smb/rulesets/special_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env python3
# Copyright (C) 2024 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 collections.abc import Mapping

from cmk.utils.hostaddress import HostAddress

from cmk.rulesets.v1 import Help, Label, Message, Title
from cmk.rulesets.v1.form_specs import (
BooleanChoice,
DictElement,
Dictionary,
FieldSize,
List,
migrate_to_password,
Password,
String,
validators,
)
from cmk.rulesets.v1.rule_specs import SpecialAgent, Topic


def _parameter_form() -> Dictionary:
return Dictionary(
title=Title("SMB Share fileinfo"),
elements={
"hostname": DictElement(
required=False,
parameter_form=String(
title=Title("Host name"),
custom_validate=(validators.LengthInRange(min_value=1),),
help_text=Help(
"<p>Usually Checkmk will use the host name of the host it is attached to. "
"With this option you can override this parameter.</p>"
),
),
),
"ip_address": DictElement(
required=False,
parameter_form=String(
title=Title("IP address"),
custom_validate=(
validators.LengthInRange(min_value=1),
_validate_hostname,
),
help_text=Help(
"<p>Usually Checkmk will use the primary IP address of the host it is "
"attached to. With this option you can override this parameter.</p>"
),
),
),
"authentication": DictElement(
required=False,
parameter_form=Dictionary(
title=Title("Authentication"),
elements={
"username": DictElement(
required=True,
parameter_form=String(
title=Title("Username"),
custom_validate=(validators.LengthInRange(min_value=1),),
),
),
"password": DictElement(
required=True,
parameter_form=Password(
title=Title("Password"),
custom_validate=(validators.LengthInRange(min_value=1),),
migrate=migrate_to_password,
),
),
},
migrate=_migrate_tuple_to_dict,
),
),
"patterns": DictElement(
required=True,
parameter_form=List(
title=Title("File patterns"),
help_text=Help(
"<p>Here you can specify a list of filename patterns to be sent by the "
"agent in the section <tt>fileinfo</tt>. UNC paths with globbing patterns "
"are used here, e.g. <tt>\\\\hostname\\share name\\*\\foo\\*.log</tt>. "
"Wildcards are not allowed in host or share names. "
"Per default each found file will be monitored for size and age. "
"By building groups you can alternatively monitor a collection "
"of files as an entity and monitor the count, total size, the largest, "
"smallest oldest or newest file. Note: if you specify more than one matching rule, then "
"<b>all</b> matching rules will be used for defining pattern - not just the "
" first one.</p>"
),
element_template=String(field_size=FieldSize.LARGE),
add_element_label=Label("Add pattern"),
remove_element_label=Label("Remove pattern"),
editable_order=False,
),
),
"recursive": DictElement(
required=False,
parameter_form=BooleanChoice(
title=Title("Recursive pattern search"),
label=Label("Match multiple directories with **"),
help_text=Help(
"If ** is used in the pattern, the agent will recursively look into all the subfolders, "
"so use this carefully on a deeply nested filesystems."
),
),
),
},
)


def _validate_hostname(value: str) -> None:
try:
HostAddress(value)
except ValueError as exception:
raise validators.ValidationError(
message=Message(
"Please enter a valid host name or IPv4 address. "
"Only letters, digits, dash, underscore and dot are allowed."
)
) from exception


def _migrate_tuple_to_dict(param: object) -> Mapping[str, object]:
match param:
case (username, password):
return {"username": username, "password": password}
case dict() as already_migrated:
return already_migrated
raise ValueError(param)


rule_spec_special_agent_smb_share = SpecialAgent(
name="smb_share",
title=Title("SMB Share fileinfo"),
topic=Topic.GENERAL,
parameter_form=_parameter_form,
)
53 changes: 53 additions & 0 deletions cmk/plugins/smb/server_side_calls/special_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# Copyright (C) 2024 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 collections.abc import Iterable, Mapping, Sequence

from pydantic import BaseModel

from cmk.server_side_calls.v1 import HostConfig, SpecialAgentCommand, SpecialAgentConfig
from cmk.server_side_calls.v1._utils import Secret


class Params(BaseModel):
hostname: str | None = None
ip_address: str | None = None
authentication: Mapping[str, str | Secret] | None = None
patterns: Sequence[str]
recursive: bool | None = None


def command_function(params: Params, host_config: HostConfig) -> Iterable[SpecialAgentCommand]:
default_ipaddress = (
params.ip_address if params.ip_address else host_config.primary_ip_config.address
)
command_arguments: list[str | Secret] = [
params.hostname if params.hostname else host_config.name,
params.ip_address if params.ip_address else default_ipaddress,
]

if params.authentication:
command_arguments.append("--username")
command_arguments.append(params.authentication["username"])
command_arguments.append("--password")
assert isinstance(password := params.authentication["password"], Secret)
command_arguments.append(password.unsafe())

if params.patterns:
command_arguments.append("--patterns")
command_arguments.extend(params.patterns)

if params.recursive:
command_arguments.append("--recursive")

yield SpecialAgentCommand(command_arguments=command_arguments)


special_agent_smb_share = SpecialAgentConfig(
name="smb_share",
parameter_parser=Params.model_validate,
commands_function=command_function,
)
1 change: 1 addition & 0 deletions cmk/utils/password_store/hack.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"splunk": True,
"vnx_quotas": True,
"ucs_bladecenter": True,
"smb_share": True,
}


Expand Down
22 changes: 0 additions & 22 deletions tests/unit/checks/test_agent_smb_share.py

This file was deleted.

Loading

0 comments on commit 09626fa

Please sign in to comment.