Checkmk cmk2 Unkategorisiert

Checkmk: Build Notification plugins which hijack the build in Mails Plugin in 2.4

Warning: This is risky and can break with updates.

Situation: You have the need to send mails from Checkmk, but you need to overwrite some Parameter for the Mail. Or you need some more logic. In this case, instead of creating your own script which sends mail, you can just use the included one. Just Hijack it :)

Example to hijack a Plugin:

First, you can build your own Notification Plugin which uses just the shipped mail and overwrite the Environment. Then it calls mail.mail() the included one. For now, this works save in Checkmk for some years:

#!/usr/bin/env python3
# My Mail Notification
# Bulk: yes

import os, sys
from cmk.notification_plugins import mail

if __name__ == "__main__":
    #new_recipient = get_rufbereitschaft(env="prod-1")['email']
    old_recipient = os.environ['NOTIFY_CONTACTEMAIL']
    new_recipient = "alaram@mail.de"
    os.environ['NOTIFY_PARAMETER_GRAPHS_PER_NOTIFICATION'] = '5'

    print('RB_MAIL Rufbereitschaft')
    if old_recipient == 'dummy@FIRMEN-MAIL.de':
        # Fall 1: Aufruf für den RB Empfänger
        # Geht immer raus
        os.environ['NOTIFY_CONTACTEMAIL'] = new_recipient

    if old_recipient == 'it-service@FIRMEN-MAIL.de' and old_recipient == new_recipient:
        print(f'NO Mail send to {os.environ["NOTIFY_CONTACTEMAIL"]}')
        sys.exit(0)

    print(f'Mail send to {os.environ["NOTIFY_CONTACTEMAIL"]}')
    mail.main()

Other Example can be found in the df_inventory script.

Getting the User interface:

This is now the bigger point. I want to stretch again that this is risky and can break with every Checkmk Update.
For Example, the git version of today already differs from the stable 2.4. Commented out in the code the base to build something with that current version (unfinished).

But what is working for now, is to import the existing Notification Parameter and build your own class around it.

from cmk.gui.wato._notification_parameter import _mail as mail

from typing import cast
from cmk.gui.valuespec import Dictionary as ValueSpecDictionary
from cmk.gui.form_specs.vue.visitors.recomposers.unknown_form_spec import recompose

class NotificationParameterDfMail(mail.NotificationParameterMail):

    @property
    def ident(self) -> str:
        return "df_mail"


notification_parameter_registry.register(NotificationParameterDfMail)

I was already a bit playing around with the git version, and may this will work there (untested)

# This May work after 2.4
class NotificationParameterDfMail(NotificationParameter):
    """
    Notification parameter for DF Mail
    """
    @property
    def ident(self) -> str:
        return "df_mail"

    @property
    def spec(self) -> ValueSpecDictionary:
        # TODO needed because of mixed Form Spec and old style setup
        return cast(ValueSpecDictionary, recompose(self._form_spec()).valuespec)

    def _form_spec(self):
        return mail.form_spec_mail