Graphs with lines or areas below the x axis (cmk 2.3+)

In the old metrics API it was possible to define graphs with lines and/or areas below the x axis, e.g.:

To achieve that, we wrote “line” and “-line” as linetype in the graph’s definition (or “area” and “-area”).

However, with the new graphing API I could not find a similar setting.

To display a metric as a line, we add it to the simple_lines attribute of the Graph object and to display it as an area, we add it to the compound_lines attribute:

graph_my_graph = Graph(
    name="my_graph",
    title=Title("Some Title"),
    simple_lines=(     # displayed as lines
        metric_a.name,
        metric_b.name,
    ),
    compound_lines=(   # displayed as area
        metric_c.name,
        metric_d.name,
    ),
)

But how do I define a metric that is displayed below the x axis? I tried a combination of Product(my_metric, -1). This works and shows the values below the x axis, but it also shows them as negative values (for example, a bandwith is then -1 GB/s).

I guess what you are looking for is the “Bidirectional” graph. Here a sample from my Meraki Special Agent.

graph_cisco_meraki_organisations_api_code = graphs.Bidirectional(
    name='cisco_meraki_organisations_api_code',
    title=Title('Cisco Meraki API response codes'),
    upper=graphs.Graph(
        name='api_code_ok',
        title=Title('Cisco Meraki API response codes'),
        simple_lines=[
            'api_code_2xx',
            'api_code_3xx',
        ],
    ),
    lower=graphs.Graph(
        name='api_code_bad',
        title=Title('Cisco Meraki API response codes'),
        simple_lines=[
            'api_code_4xx',
            'api_code_5xx',
        ],
    ),
)

2 Likes

Exactly. Thank you very much.

In the meantime I found the script ~/share/doc/check_mk/treasures/migration_helpers/graphing_v0_v1.py which converts “old” metric plugins into “new” graphing plugins. And after tweaking the import statements of an old plugin a bit, the script converted it to the new format and also made use of that Bidirectional(upper, lower) thingy.

How did you tweak the imports? The script fails with NameError: name 'metric_info' is not definedwhen it tries to import the libs under Checkmk 2.3 or just prints nothing.

Sorry for the late answer but Christmas kicked in :smiley:

Here is what I did:

Replace

from cmk.gui.plugins.metrics import (
    check_metrics,
    metric_info,
    graph_info
)

with:

from cmk.gui.graphing._utils import (
    check_metrics,
    metric_info,
    graph_info,
)
from cmk.gui.graphing._color import indexed_color

in the “old” (2.1 style) metric files.

Then I copied the metric files one by one into a separate (empty) directory and issued

graphing_v0_v1-master.py --debug --translations --balance-colors DIRECTORY | yapf3 > new-style-metric-file.py

The thing is that the tool can only handle directories and reads all files in that directory and then outputs all the metrics of all the files combined to stdout. So I wrote a little wrapper script that copies the old files one by one to an empty directory, calls the tool, collects the output and then empties the directory.

2 Likes

Thanks for the explanation. Worked good with the additional imports. And it only worked with one file in the working directory. I hope the documentation will improve on those APIs as there are still files dangling around as deprecated (like ~/local/share/[man|doc]/…).

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.