#!/usr/bin/env python3 # pylint: disable=missing-module-docstring, unused-argument, missing-function-docstring # pylint: disable=line-too-long from collections.abc import Mapping from typing import NotRequired, TypedDict from cmk.agent_based.v2 import ( AgentSection, CheckPlugin, CheckResult, Metric, render, Result, Service, State, StringTable, DiscoveryResult, ) class _ItemData(TypedDict): dbtype: NotRequired[str] version: NotRequired[str] size: NotRequired[int] opcache_hit_rate: NotRequired[float] Section = Mapping[str, _ItemData] def get_state_lower(levels, value): warn, crit = levels if value < crit: return State.CRIT if value < warn: return State.WARN return State.OK def discover_nextcloud_database(section) -> DiscoveryResult: yield Service() def check_nextcloud_database(params, section) -> CheckResult: for key in section: if key == "database": opcache_hit_rate = section[key]["opcache_hit_rate"] size = section[key]["size"] dbtype = section[key]["dbtype"] version = section[key]["version"] _level_type, levels = params["levels_database_opcache_hit_rate"] # create graph for opcache hit rate yield Metric( "nc_database_opcache_hit_rate", opcache_hit_rate, levels=levels ) # create graph for database size yield Metric("nc_database_size", size) state = get_state_lower(levels, opcache_hit_rate) summary = f"PHP OPCache hit rate: {render.percent(opcache_hit_rate)}" if opcache_hit_rate == 0: state = State.UNKNOWN summary = "PHP OPCache hit rate: 0% - please check if opcache_get_status is enabled" details = f"\nDatabase type: {dbtype}\nDatabase version: {version}\nDatabase size: {render.bytes(size)}" yield Result(state=state, summary=summary, details=details) def parse_nextcloud_database(string_table: StringTable) -> Section: parsed_data = {"database": {}} params_list = [ "NC_Database_Type", "NC_Database_Version", "NC_Database_Size", "NC_OPCache_Hit_Rate", ] for line in string_table: if line[0] in params_list: param = line[0] value = line[1] if param == "NC_Database_Type": parsed_data["database"]["dbtype"] = value elif param == "NC_Database_Version": parsed_data["database"]["version"] = value elif param == "NC_Database_Size": parsed_data["database"]["size"] = int(value) elif param == "NC_OPCache_Hit_Rate": parsed_data["database"]["opcache_hit_rate"] = float(value) return parsed_data agent_section_nextcloud_database = AgentSection( name="nextcloud_database", parse_function=parse_nextcloud_database, ) check_plugin_nextcloud_database = CheckPlugin( name="nextcloud_database", service_name="Nextcloud Database", discovery_function=discover_nextcloud_database, check_function=check_nextcloud_database, check_default_parameters={ "levels_database_opcache_hit_rate": ("fixed", (95.0, 85.0)), }, check_ruleset_name="nextcloud_database", )