Struggling to add custom graph to my plugin

I’m trying to monitor ink in a printer. I’ve written an agent script, and a plugin, and it all works - I can do a service discovery and it finds 4 “services” - one for each color.

I get a graph of the remaining ink of each color, but it’d be waaaay more useful to have all four on one graph, but I can’t see how to do this.

Internally it’s basically right out of the docs:

 def check_com_falkensweb_hp_officejet(item, section):
    for ink, remain in section:
        if ink == item:

            remain = int(remain)

            yield Metric(
                "remaining",
                remain,
                levels=(20,5),
                boundaries=(0,100))

I’ve read Writing your own check plug-ins but don’t understand how I would write a graph_info section for this.

How do I name the graph ?
How do I refer to each of the 4 metrics the plugin publishes ?
Where would it show up in the web interface ?

I’ve tried, for instance, having a different yield Metric( for each color, then doing

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

graph_info["hp_officejet_remaining_combined"] = {
    "metrics": [
        ("hp_officejet_remaining_Yellow", "line"),
        ("hp_officejet_remaining_Magenta", "line"),
        ("hp_officejet_remaining_Cyan", "line"),
        ("hp_officejet_remaining_Black", "line"),
    ],
}

But… where’s the graph ?

“Service graphs of host” doesn’t have it listed, for instance.

First you need to have a yield Metric for each color with a different name (like by adding the name of the color)

yield Metric(
    f"remaining_{ink}",
    remain,
    levels=(20,5),
    boundaries=(0,100)
)

Then in your graph definition, the metrics have to match these names exactly

graph_info["hp_officejet_remaining_combined"] = { 
    "metrics": [ 
        ("remaining_Yellow", "line"), 
        ("remaining_Magenta", "line"), 
        ("remaining_Cyan", "line"), 
        ("remaining_Black", "line"),
    ],
}
1 Like

Yup, that’s what I’ve done - the code is here : falken/checkmk-hp-officejet - Codeberg.org

“Services of host” lists

OK	HP Officejet Black	[Open the action menu]
	Black 50% remaining	22 h	12.1 s	
OK	HP Officejet Cyan	[Open the action menu]
	Cyan 60% remaining	22 h	12.1 s	
OK	HP Officejet Magenta	[Open the action menu]
	Magenta 40% remaining	22 h	12.1 s	
OK	HP Officejet Yellow	[Open the action menu]
	Yellow 30% remaining	22 h	12.1 s

But I can’t find my graph anywhere, or even a message saying it’s not drawn for some reason.

Is there a way to debug this? Or have a made a stupid typo somewhere ?!?

Hello Tom,

I have looked at your code at Codeberg.

In the file "com_falkensweb_hp_officejet_metric.py " you have only created a combined graph but have not defined the individual graph lines.

You must first define the individual line for the colors as follows:

metric_info["hp_officejet_remaining_Yellow"] = {
    "title": _("Remaining Yellow"),
    "unit": "%",
    "color": "23/a",
}

Repeat this with other colors.

and below that comes your block

graph_info["hp_officejet_remaining_combined"] = {
    "metrics": [
        ("hp_officejet_remaining_Yellow", "line"),
        ("hp_officejet_remaining_Magenta", "line"),
        ("hp_officejet_remaining_Cyan", "line"),
        ("hp_officejet_remaining_Black", "line"),
    ],
}

You can select the colors of the lines to match the ink from the following palette:

 Colors:

                   red
  magenta                       orange
            11 12 13 14 15 16
         46                   21
         45                   22
   blue  44                   23  yellow
         43                   24
         42                   25
         41                   26
            36 35 34 33 32 31
     cyan                       yellow-green
                  green

Special colors:
51  gray
52  brown 1
53  brown 2

For a new metric_info you have to choose a color. No more hex-codes are needed!
Instead you can choose a number of the above color ring and a letter 'a' or 'b
where 'a' represents the basic color and 'b' is a nuance/shading of the basic color.
Both number and letter must be declared!

Example:
"color" : "23/a" (basic color yellow)
"color" : "23/b" (nuance of color yellow)

As an alternative you can call indexed_color with a color index and the maximum
number of colors you will need to generate a color. This function tries to return
high contrast colors for "close" indices, so the colors of idx 1 and idx 2 may
have stronger contrast than the colors at idx 3 and idx 10.
retrieve an indexed color.
param idx: the color index
param total: the total number of colors needed in one graph.
1 Like

I didn’t think the docs were totally clear on requiring both a single line def. and a chart, if the chart just uses already yield’d metrics.

That color square is also really useful.

Is there a way to suggest documentation changes?

After adding the four metric_info, it’s drawing those four metrics differently, so has picked up the changes.

There’s sill no sign of the combined graph :confused:

With your check you cannot gain a combined graph as you have 4 different checks (items).
A combined graph inside a check is only possible if all metrics are from the same check.

That means you need only one check like (HP ink levels) with 4 metrics.

1 Like

AH HA; Thanks so much.

That’s got the graph appearing, but is there a delay after ..._metric.py is changed before Service (host name), HP Officejet updates ?

I’ve tried to title the combined graph - the docs themselves say there are not docs, but it seems like a 'title': _('HP Officejet'), in graph_info is the way to do it.

I’ve even changed one of the metrics to _("Remainingx Yellow"), but this hasn’t changed anything…

If you change the .py files the save way is to restart the Apache inside your site.

I’m running this on Fedora, so not sure which Apache was meant, the main one that listens on port 80/443, or the one that listens on a local port only, or maybe something in checkmk itself.

Anyway, I came back to do this today and it’s started to work by itself, along with the perf-o-meter.

That’s correct if you wait some time between some minutes and hours the Apache should use your files. This is a caching problem.
The only Apache you need to restart is the one that runs inside your OMD site.

su sitename
omd restart apache

Ta.

Guess I’d better go read up on how to add some preferences and package up a extension now :slight_smile: