#!/usr/bin/env python3 # pylint: disable=line-too-long """HAL9002 checks for users""" # import necessary elements from API version 2 from cmk.agent_based.v2 import ( AgentSection, CheckPlugin, Service, Result, State, render, check_levels, ) def parse_hal9002_users(string_table): """the parse function""" parsed_data = {} # convert the raw output of the agent section in an meaningful structure # do type conversions and so on parsed_data = {} for line in string_table: userid = line[0] realname = line[1] quota_used = float(line[2]) quota_total = float(line[3]) parsed_data[f"{userid}"] = [realname, quota_used, quota_total] return parsed_data def discover_hal9002_users(section): """the discover function""" # since we have a service with item here we must create one service per item for key in section: yield Service(item=key) def check_hal9002_users(item, params, section): """the check function""" attr = section.get(item) if not attr: # if a previously found user does not exist anymore, create a meaningful result yield Result( state=State.UNKNOWN, summary="User not found anymore, maybe it was deleted?" ) return # this refers to the definition of the parameters # within the appropriate ruleset dictionary element levels = params["quota_used_percent"] # get all info of the user user_name = attr[0] used_bytes = attr[1] quota_max_bytes = attr[2] # calculate percentage of quota used quota_used_percent = used_bytes * 100 / quota_max_bytes # create graphs and results for used quota # this replaces all calls of yield Result|Metric # metric_name must be used within the graphing templates yield from check_levels( value=quota_used_percent, levels_upper=levels, metric_name="hal_user_quota_used_percent", render_func=render.percent, label=f"Used quota of {user_name}", notice_only=False, ) yield from check_levels( value=used_bytes, metric_name="hal_user_used_bytes", render_func=render.bytes, label=f"Used from {render.disksize(quota_max_bytes)}", notice_only=False, ) # create the new agent section, must begin with "agent_section_" # and must be an instance of "AgentSection" agent_section_hal9002_users = AgentSection( # "name" must exactly match the section name within the agent output name="hal9002_users", # define the parse function, name is arbitrary, a good choice is to choose # "parse_" as prefix and append the section name parse_function=parse_hal9002_users, ) # create the new check plugin, must begin with "check_plugin_" # and must be an instance of "CheckPlugin" check_plugin_hal9002_users = CheckPlugin( # "name" should be the same as the corresponding section within the agent output name="hal9002_users", # this is a service with item, so you have to include a place holder for the item id service_name="HAL9002 user %s", # define the discovery function, name is arbitrary, a good choice is to choose # "discover_" as prefix and append the section name discovery_function=discover_hal9002_users, # define the check function, name is arbitrary, a good choice is to choose # "check_" as prefix and append the section name check_function=check_hal9002_users, # define the default parameters check_default_parameters={"quota_used_percent": ("fixed", (80.0, 90.0))}, # connect to the ruleset where parameters can be defined # must match the name of the ruleset exactly check_ruleset_name="hal9002_users", )