MKP 0.2.0, Domain checks added

This commit is contained in:
Ralf Mellis 2023-08-26 15:02:28 +02:00
parent c0cb43d894
commit 4edb6adccd
6 changed files with 308 additions and 0 deletions

View File

@ -0,0 +1,151 @@
#!/usr/bin/env python3
import time
import random
from pprint import pprint
from .agent_based_api.v1 import get_value_store, get_rate, register, render, Service, Result, State, Metric
def getStateUpper(levels, value):
warn, crit = levels
if value >= crit:
return State.CRIT
if value >= warn:
return State.WARN
return State.OK
# services with item --> storageid and usage of value_store (to be able to calculate rates)
def discover_mailcow_domains(section):
for key in section:
yield(Service(item = key))
def check_mailcow_domains(item, params, section):
domain = item
# get all values from section
active = section[domain][0]
create_time = section[domain][1]
modify_time = section[domain][2]
max_number_of_mailboxes = section[domain][3]
number_of_mailboxes = section[domain][4]
percent_used_mailboxes = section[domain][5]
max_number_of_aliases = section[domain][6]
number_of_aliases = section[domain][7]
percent_used_aliases = section[domain][8]
total_number_of_messages = section[domain][9]
total_number_of_bytes_used = section[domain][10]
quota = section[domain][11]
percent_storage_used_for_messages = section[domain][12]
# create (main) service for used storage (domain quota)
warn, crit = params["levels_mailcow_domains_quota_used"]
levels = (warn, crit)
state_quota = getStateUpper(levels, percent_storage_used_for_messages)
# create graph for used quota
yield Metric("mailcow_domains_used_quota", percent_storage_used_for_messages, levels=levels)
summary_quota = f"Quota used is {render.percent(percent_storage_used_for_messages)}"
details_quota = f"Quota: {render.bytes(total_number_of_bytes_used)} of {render.bytes(quota)} used"
# create service
yield(Result(state=state_quota, summary=summary_quota, details=details_quota))
# create some additional services and information only details
notice = f"Active: {active}"
yield(Result(state=State.OK, notice=notice))
notice = f"Creation date: {create_time}"
yield(Result(state=State.OK, notice=notice))
notice = f"Last modified: {modify_time}"
yield(Result(state=State.OK, notice=notice))
# create service for number of configured mailboxes
warn, crit = params["levels_mailcow_domains_mailboxes_used"]
levels = (warn, crit)
state_mailboxes = getStateUpper(levels, percent_used_mailboxes)
yield Metric("mailcow_domains_mailboxes", percent_used_mailboxes, levels=levels)
notice = f"Used mailboxes: {render.percent(percent_used_mailboxes)}, {number_of_mailboxes} of {max_number_of_mailboxes} in use"
yield(Result(state=state_mailboxes, notice=notice))
# create service for number of configured aliases
warn, crit = params["levels_mailcow_domains_aliases_used"]
levels = (warn, crit)
state_aliases = getStateUpper(levels, percent_used_aliases)
yield Metric("mailcow_domains_aliases", percent_used_aliases, levels=levels)
notice = f"Used aliases: {render.percent(percent_used_aliases)}, {number_of_aliases} of {max_number_of_aliases} in use"
yield(Result(state=state_aliases, notice=notice))
# create service for number of messages
warn, crit = params["levels_mailcow_domains_num_messages"]
levels = (warn, crit)
state_messages = getStateUpper(levels, total_number_of_messages)
yield Metric("mailcow_domains_messages", total_number_of_messages, levels=levels)
notice = f"Number of messages: {total_number_of_messages}"
yield(Result(state=state_messages, notice=notice))
def parse_mailcow_domains_section(string_table):
# convert the raw output of the agent section into a meaningful structure, do type conversions and so on
parsed_data = {}
for line in string_table:
domainname = line[0]
value_active = int(line[1])
if value_active == 1:
active = "yes"
else:
active = "no"
# calculate creation and last modification date in human readable format
create_time_value = line[2]
if create_time_value == "None":
create_time_data = "Not available"
else:
create_time = int(create_time_value)
curr_time = int(time.time())
diff_time = curr_time - create_time
create_time_data = render.timespan(diff_time)
modify_time_value = line[3]
if modify_time_value == "None":
modify_time_data = "Never"
else:
modify_time = int(modify_time_value)
curr_time = int(time.time())
diff_time = curr_time - modify_time
modify_time_data = render.timespan(diff_time)
# calculate percentage of used mailboxes
max_number_of_mailboxes = int(line[4])
number_of_mailboxes = int(line[5])
percent_used_mailboxes = number_of_mailboxes * 100 / max_number_of_mailboxes
# calculate percentage of used aliases
max_number_of_aliases = int(line[6])
number_of_aliases = int(line[7])
percent_used_aliases = number_of_aliases * 100 / max_number_of_aliases
# number of messages within domain
total_number_of_messages = int(line[8])
# calculate storage used for all messages in domain
total_number_of_bytes_used = int(line[9])
quota = int(line[10])
percent_storage_used_for_messages = total_number_of_bytes_used * 100 / quota
# store all (calculated) data
parsed_data[f"{domainname}"] = [active, create_time_data, modify_time_data,
max_number_of_mailboxes, number_of_mailboxes, percent_used_mailboxes,
max_number_of_aliases, number_of_aliases, percent_used_aliases,
total_number_of_messages,
total_number_of_bytes_used, quota, percent_storage_used_for_messages
]
return parsed_data
register.agent_section(
name = "mailcow_domains",
parse_function = parse_mailcow_domains_section,
)
register.check_plugin(
name = "mailcow_domains",
service_name = "Mailcow domain %s",
discovery_function = discover_mailcow_domains,
check_function = check_mailcow_domains,
check_default_parameters = {
"levels_mailcow_domains_quota_used": (65.0, 85.0),
"levels_mailcow_domains_mailboxes_used": (65.0, 85.0),
"levels_mailcow_domains_aliases_used": (65.0, 85.0),
"levels_mailcow_domains_num_messages": (10000, 25000),
"levels_mailcow_domains_num_aliases": (100, 250),
},
check_ruleset_name="mailcow_domains",
)

View File

@ -0,0 +1,20 @@
title: Mailcow: Mailboxes, Aliases and Storage used by Email Domain
agents: linux
catalog: unsorted
license: GPL
distribution: check_mk
description:
Tested with Mailcow versions 2022-07a and higher (use at your own risk with lower versions).
Tested only with fully dockerized Mailcow instances.
You have to provide at least the hostname/IP of your Mailcow server and an API key.
Got to configuration settings and create a new API key (read-only access is sufficient).
Allow API access to at least your CheckMK server IP address.
Shows several information about a Mailcow email domain, e.g. number of configured mailboxes/aliases, used space for all messages etc.
The check will raise WARN/CRIT if quota usage is above the configurable levels.
The check will raise WARN/CRIT if number of configured mailboxes is above the configurable levels.
The check will raise WARN/CRIT if number of configured aliases is above the configurable levels.
The check will raise WARN/CRIT if number of total messages is above the configurable levels.
item:
domainname
inventory:
one service is created for each domain

View File

@ -21,4 +21,28 @@ metric_info["mc_num_global_messages"] = {
"title": _("Number of Messages"),
"unit": "count",
"color": "42/a",
}
metric_info["mailcow_domains_used_quota"] = {
"title": _("Domain Quota Used"),
"unit": "%",
"color": "24/a",
}
metric_info["mailcow_domains_mailboxes"] = {
"title": _("Domain Mailboxes Quota Used"),
"unit": "%",
"color": "44/b",
}
metric_info["mailcow_domains_aliases"] = {
"title": _("Domain Aliases Quota Used"),
"unit": "%",
"color": "44/a",
}
metric_info["mailcow_domains_messages"] = {
"title": _("Global Number of Messages"),
"unit": "count",
"color": "24/a",
}

View File

@ -11,6 +11,17 @@ perfometer_info.append({
],
})
perfometer_info.append({
"type": "stacked",
"perfometers": [
{
"type": "linear",
"segments": ["mailcow_domains_used_quota"],
"total": 100.0,
},
],
})
#perfometer_info.append({
# "type": "stacked",
# "perfometers": [

View File

@ -0,0 +1,102 @@
from cmk.gui.i18n import _
from cmk.gui.plugins.wato import (
CheckParameterRulespecWithItem,
rulespec_registry,
RulespecGroupCheckParametersApplications
)
from cmk.gui.valuespec import (
Dictionary,
ListChoice,
TextAscii,
Percentage,
Tuple,
Float,
Integer
)
def _item_spec_mailcow_domains():
return TextAscii(
title=_("Domain")
)
def _parameter_spec_mailcow_domains():
return Dictionary(
elements=[
("levels_mailcow_domains_quota_used", Tuple(
title=_("Mailcow domains quota usage for storage"),
elements=[
Percentage(
title=_("Warning at"),
default_value=65.0,
),
Percentage(
title=_("Critical at"),
default_value=85.0,
)
],
)),
("levels_mailcow_domains_mailboxes_used", Tuple(
title=_("Mailcow domains mailboxes usage"),
elements=[
Percentage(
title=_("Warning at"),
default_value=65.0,
),
Percentage(
title=_("Critical at"),
default_value=85.0,
)
],
)),
("levels_mailcow_domains_aliases_used", Tuple(
title=_("Mailcow domains aliases usage"),
elements=[
Percentage(
title=_("Warning at"),
default_value=65.0,
),
Percentage(
title=_("Critical at"),
default_value=85.0,
)
],
)),
("levels_mailcow_domains_num_messages", Tuple(
title=_("Number of messages"),
elements=[
Integer(
title=_("Warning at"),
default_value=10000,
),
Integer(
title=_("Critical at"),
default_value=25000,
)
],
)),
("levels_mailcow_domains_num_aliases", Tuple(
title=_("Number of aliases"),
elements=[
Integer(
title=_("Warning at"),
default_value=100,
),
Integer(
title=_("Critical at"),
default_value=250,
)
],
)),
],
)
rulespec_registry.register(
CheckParameterRulespecWithItem(
check_group_name="mailcow_domains",
group=RulespecGroupCheckParametersApplications,
match_type="dict",
item_spec=_item_spec_mailcow_domains,
parameter_valuespec=_parameter_spec_mailcow_domains,
title=lambda: _("Levels for Mailcow domains"),
)
)

BIN
mkp/Mailcow-0.2.0.mkp Executable file

Binary file not shown.