[SOLVED] Trouble Upgrading from 2.1.0p29 to 2.2.0p4 with custom active check

CMK version:
2.1.0p29 upgrade to 2.2.0p4
OS version:
Debian 11
Error message:

 01/04 Rulesets...
-| Failed to load plugin /omd/sites/monitoring/local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| Traceback (most recent call last):
-|   File "/omd/sites/monitoring/lib/python3/cmk/gui/utils/__init__.py", line 66, in load_web_plugins
-|     exec(f.read(), globalvars)
-|   File "<string>", line 21, in <module>
-| ImportError: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| Failed to load plugin /omd/sites/monitoring/local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| Traceback (most recent call last):
-|   File "/omd/sites/monitoring/lib/python3/cmk/gui/utils/__init__.py", line 66, in load_web_plugins
-|     exec(f.read(), globalvars)
-|   File "<string>", line 21, in <module>
-| ImportError: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| 'check_mk --automation get-section-information' returned 'Error in plugin file /omd/sites/monitoring/local/share/check_mk/checks/check_support_period.py: cannot import name 'quote_shell_string' from 'cmk.utils' (/omd/sites/monitoring/lib/python3/cmk/utils/__init__.py)
-| '
-| 'check_mk --automation get-check-information' returned 'Error in plugin file /omd/sites/monitoring/local/share/check_mk/checks/check_support_period.py: cannot import name 'quote_shell_string' from 'cmk.utils' (/omd/sites/monitoring/lib/python3/cmk/utils/__init__.py)
-| '
-|  02/04 UI extensions...
-| Failed to load plugin /omd/sites/monitoring/local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| Traceback (most recent call last):
-|   File "/omd/sites/monitoring/lib/python3/cmk/gui/utils/__init__.py", line 66, in load_web_plugins
-|     exec(f.read(), globalvars)
-|   File "<string>", line 21, in <module>
-| ImportError: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)
-| Incompatible local file 'check_support_period_parameters_ui'.
-| Error: cannot import name 'RulespecGroupActiveChecks' from 'cmk.gui.plugins.wato.active_checks' (/omd/sites/monitoring/lib/python3/cmk/gui/plugins/wato/active_checks/__init__.py)

Here is the active check I’ve written, it worked well in 2.0 and 2.1:

#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# ./local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py

# CP 30.06.2022 14:00

from cmk.gui.i18n import _
from cmk.gui.valuespec import (
    Dictionary,
    TextUnicode,
    Integer,
    TextAscii
)

from cmk.gui.plugins.wato import (
    rulespec_registry,
    HostRulespec
)

# We import the RulespecGroupActiveChecks
from cmk.gui.plugins.wato.active_checks import RulespecGroupActiveChecks


def _valuespec_active_check_support_period():
    return Dictionary(
        title=_("Check Support Expiration"),
        help=_("This check alerts when a date for support expires"
               "This check uses the active check <tt>check_support_period</tt>. "),
        optional_keys=["Date", "Warning", "Critical", "ignore"],
        elements=[
            (
                "description",
                TextUnicode(
                    title=_("Service Description"),
                    help=_("The name of this active service to be displayed."),
                    allow_empty=False,
                )
            ),
            (
                "service",
                TextAscii(
                    title=_("Support Type"),
                    help=_("Input the Support Type. Default is Hardware Support"),
                    default_value="Hardware Support",
                )
            ),
            (
                "Date",
                TextAscii(
                    title=_("Date when support expires, DD.MM.YYYY"),
                    help=_('defaults to 01.01.2022'),
                    allow_empty=False,
                    default_value="01.01.2022",
                )
            ),
            (
                "Warning",
                TextAscii(
                    title=_("Warn threshold"),
                    help=_('default 60'),
                    default_value=60,
                )
            ),
            (
                "Critical",
                TextAscii(
                    title=_("Crit threshold"),
                    help=_('default 30'),
                    default_value=30,
                )
            ),
            (
                "ignore",
                TextAscii(
                    title=_("Ignore Check if 1"),
                    help=_('Check is always OK if 1. If 0, then Check executes normally.'),
                    default_value=0,
                )
            ),
        ]
    )


# enddef _valuespec_active_check_support_period

rulespec_registry.register(
    HostRulespec(
        group=RulespecGroupActiveChecks,
        match_type="all",
        name="active_checks:check_support_period",
        valuespec=_valuespec_active_check_support_period,
    )
)

I think this stopped working. But I don’t know why:

cmk.gui.plugins.wato.active_checks import RulespecGroupActiveChecks
1 Like

This is only part of your own check.
There are two problematic files.

  • /omd/sites/monitoring/local/share/check_mk/checks/check_support_period.py

The import for “quote_shell_string” is not existing anymore.
Here you can use “shlex.quote()” → Quote_shell_string now CEE exclusive? - #2 by r.sander

  • /omd/sites/monitoring/local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py

Here is the import statement for “RulespecGroupActiveChecks” the problem.
The following line should fix this.

from cmk.gui.plugins.wato.active_checks.common import RulespecGroupActiveChecks

2 Likes

Please have a look here (deep link):

The incompatibilities you experience might be caused by this Werk:

1 Like

Hello, yes you are right. I forgot to look at the actual check file:

#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# ./local/share/check_mk/checks/check_support_period.py

# CP 30.06.2022 14:00

from cmk.utils import quote_shell_string
from cmk.base.config import active_check_info


def check_support_period_arguments(params):
    args = ""

    if "Date" in params:
        args += "-D %s " % quote_shell_string(params["Date"])
    # endif

    if "Warning" in params:
        args += "-W %s " % quote_shell_string(params["Warning"])
    # endif

    if "Critical" in params:
        args += "-C %s " % quote_shell_string(params["Critical"])
        # endif

    if "ignore" in params:
        args += "-i %s " % quote_shell_string(params["ignore"])
    # endif

    if "service" in params:
        args += "-s %s " % quote_shell_string(params["service"])
    # endif

    return args


# enddef


# $USER2$ ... ~/local/lib/nagios/plugins --- more info in ~/etc/nagios/resource.cfg
active_check_info['check_support_period'] = {
    "command_line": '$USER2$/check_support_period.py $ARG1$',
    "argument_function": check_support_period_arguments,
    "service_description": lambda args: args["description"],
}

There I indeed import from cmk.utils import quote_shell_string

I will try to use the “shlex.quote()” command.

Thank you for telling me how to solve the “RulespecGroupActiveChecks” problem.

Thank you! I’ve updated my code and it works now.

#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# ./local/share/check_mk/web/plugins/wato/check_support_period_parameters_ui.py

# CP 06.07.2023 12:00

from cmk.gui.i18n import _
from cmk.gui.valuespec import (
    Dictionary,
    TextUnicode,
    Integer,
    TextAscii
)

from cmk.gui.plugins.wato import (
    rulespec_registry,
    HostRulespec
)

# We import the RulespecGroupActiveChecks
from cmk.gui.plugins.wato.active_checks.common import RulespecGroupActiveChecks

def _valuespec_active_check_support_period():
    return Dictionary(
        title=_("Check Support Expiration"),
        help=_("This check alerts when a date for support expires"
               "This check uses the active check <tt>check_support_period</tt>. "),
        optional_keys=["Date", "Warning", "Critical", "ignore"],
        elements=[
            (
                "description",
                TextUnicode(
                    title=_("Service Description"),
                    help=_("The name of this active service to be displayed."),
                    allow_empty=False,
                )
            ),
            (
                "service",
                TextAscii(
                    title=_("Support Type"),
                    help=_("Input the Support Type. Default is Hardware Support"),
                    default_value="Hardware Support",
                )
            ),
            (
                "Date",
                TextAscii(
                    title=_("Date when support expires, DD.MM.YYYY"),
                    help=_('defaults to 01.01.2022'),
                    allow_empty=False,
                    default_value="01.01.2022",
                )
            ),
            (
                "Warning",
                TextAscii(
                    title=_("Warn threshold"),
                    help=_('default 60'),
                    default_value=60,
                )
            ),
            (
                "Critical",
                TextAscii(
                    title=_("Crit threshold"),
                    help=_('default 30'),
                    default_value=30,
                )
            ),
            (
                "ignore",
                TextAscii(
                    title=_("Ignore Check if 1"),
                    help=_('Check is always OK if 1. If 0, then Check executes normally.'),
                    default_value=0,
                )
            ),
        ]
    )


# enddef _valuespec_active_check_support_period

rulespec_registry.register(
    HostRulespec(
        group=RulespecGroupActiveChecks,
        match_type="all",
        name="active_checks:check_support_period",
        valuespec=_valuespec_active_check_support_period,
    )
)
#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# ./local/share/check_mk/checks/check_support_period.py

# CP 06.07.2023 12:00

# from cmk.utils import quote_shell_string
import shlex
from cmk.base.config import active_check_info


def check_support_period_arguments(params):
    args = ""

    if "Date" in params:
        args += "-D %s " % shlex.quote(params["Date"])
    # endif

    if "Warning" in params:
        args += "-W %s " % shlex.quote(params["Warning"])
    # endif

    if "Critical" in params:
        args += "-C %s " % shlex.quote(params["Critical"])
        # endif

    if "ignore" in params:
        args += "-i %s " % shlex.quote(params["ignore"])
    # endif

    if "service" in params:
        args += "-s %s " % shlex.quote(params["service"])
    # endif

    return args


# enddef


# $USER2$ ... ~/local/lib/nagios/plugins --- more info in ~/etc/nagios/resource.cfg
active_check_info['check_support_period'] = {
    "command_line": '$USER2$/check_support_period.py $ARG1$',
    "argument_function": check_support_period_arguments,
    "service_description": lambda args: args["description"],
}