REST-API - Create Service Labels for Services

Hi CheckMK Community,

i was recently checking the REST API and i want to create Service Labels.

The Goal ist to create Service Labels, Assign this Labels to Services, after that the Labels should be assigned to a Service Group too.

I know how to add this to the rules.mk but i did not find a example in the API Documentation how to solve this.

Is there anyone who can help with an example how to create, update and delete this scenario?

Thank you very much and Regards,

I assume you use the REST API and not the WEB API?

Yes because WEB API is deprecated :slight_smile:

Service Labels can either be assigned from within the implementation of the check plugin or via ruleset.

So to do it via REST API you have to edit the ruleset “Service labels”.

that sounds interesting. was it planned to have such functionality even by autochecks/discovery?

I just ask because when you look into documentation like Understanding and configuring services - Detecting and monitoring elements of a host
, you will find unused service_labels in generated files.
Those are not mentioned anywhere in the documentation.

An example how this is done from the discovery_function of a check plugin can be found here:

The Service() object yielded by the discovery function can have a list of ServiceLabel() objects passed to it via the labels parameter.

1 Like

Yes, the discovery function can return service labels.
See this werk: Service labels can now be discovered or this simple example:

def discover_my_service(section):

    service_labels = [
        ServiceLabel("key1", "value1"),
        ServiceLabel("key2", "value2"),
    ]

    for item in section:
        yield Service( item=item, labels=service_labels )
2 Likes

Thank you this is quite helpful.

But now i wonder why there are not more integration points for several other service items.

Example: mysql_capacity is the only i can find there.

~/git/github.com/tribe29/checkmk/cmk/base/plugins/agent_based$ grep -rni 'name="mysql'
mysql_capacity.py:31:register.agent_section(name="mysql_capacity", parse_function=parse_size)
mysql_capacity.py:55:    name="mysql_capacity",
mysql_capacity.py:56:    service_name="MySQL DB Size %s",
mysql_capacity.py:59:    check_ruleset_name="mysql_db_size",

I would have expected that i will find something for those others too:

mysql_connections
mysql_db_size
mysql_innodb_io
mysql_sessions
mysql_slave

Is that really missing and can be added like the mysql_capacity or is there something more to undestand?

Maybe better not to implement such things and use api calls in a post-configuration step?

Background: I created a pull request for the mk_mysql agent plugin to make it easier ( and the current implementation was not correct working for me ) to discover many mysql instances on a host. Currently it delivers only socket/alias ( essentially the alias could be a service label too ).

The other check plugins for MySQL have not been converted to the new check API yet.

You can find them in $OMD_ROOT/share/check_mk/checks.

U need service_label_rules

curl -X 'GET' \
  'http://server/sitename/check_mk/api/1.0/objects/ruleset/service_label_rules' \
  -H 'accept: application/json'

a code snippet, using perl

...
  my $token = "myuser somepassword";
  my $ua = LWP::UserAgent->new;
  my $url = $envvars{external_protocol}.'://'.$envvars{external_address}.'/'.$envvars{omdsite}.'/check_mk/api/1.0/domain-types/rule/collections/all?ruleset_name=service_label_rules';
  my $header = [
    'Authorization' => "Bearer $token",
    'Accept'        => 'application/json',
    'Content-Type'  => 'application/json',
  ];
  my $content = {
    host_name            => $host_name,
  };
  my $request = HTTP::Request->new('GET', $url, $header, encode_json($content));
  my $response = $ua->request($request);

  if ($response->is_success) {
    $a = $response->decoded_content;
    my $j = decode_json($a);
    my @rules = @{ $j->{'value'} };
    foreach my $rule ( @rules ) {
      if ( "$rule->{'extensions'}{'conditions'}{'host_name'}{'match_on'}[0]" eq "$host_name" 
        && index($rule->{'extensions'}{'conditions'}{'service_description'}{'match_on'}[0],$service_name) != -1
      ) {
...

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.