MKP 0.2.0, Domain checks added
This commit is contained in:
@@ -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",
|
||||
)
|
||||
Reference in New Issue
Block a user