Crosspost - injecting custom host attribute into service script

CMK version: Raw Edition: 2.2.0p27
OS version: AZ Linux

Error message: During service discovery I get “UNKNOWN - Database names not provided”

I’m trying to inject a variable (mongo_db_name) from a custom host attribute (WATO → Custom Host Attribute) into a check script.

I’ve got the attribute data type set to Simple Text and assigned to a host. In my script I’m trying to reference the variable by:

if [ -n "$1" ]; then
  db_names="$1"
else
  db_names="${mongo_db_name}"
fi

but it seems like this variable is not being passed from the host config to my script. Has anyone had success doing this? Is my approach way off base?

Is your script a Nagios plugin that runs as active check on the monitoring server?

There is some documentation missing on how to actually use them in check commands, @mschlenker.
E.g. how is the macro called for a custom host attribute and how to use it with active checks and notifications.

Yes, its runs as an active check on the monitoring server.

Also, this is a custom script, not a nagios plugin.

Hi - just reaching out to see if there’s been any updates on this?

If you want to use custom host/service attributes as macros in your Nagios checks then you can use the following syntax:

Custom Host attributes:       $HOST_[attr_id_in_capitals]$

Custom Service attributes:   $SERVICE_[attr_id_in_capitals]$

Hi - thanks for the info! I still must be doing something incorrectly, though. I’m trying to use this:

# Determine the database names
if [ -n "$1" ]; then
  db_name="$1"
else
  db_name="$HOST_[MONGO_DB_NAME]$"
fi

This is how I have the custom attribute defined in CMK. (assigned to the host)
image

When I run the agent it looks like the variable isnt being passed to my script:

Executing MongoDB size check for database: $
Result: UNKNOWN - Error: database names cannot contain the character '$'
3 DataBase.$.Size  UNKNOWN UNKNOWN - Error: database names cannot contain the character '$

Have you already selected the following like in the screenshot?

I do. This is my config:

active checks will not have access to host attributes unless I have missed something.
you will have access to nagios variables however.

this is at least how we have done our active checks using the nagios macros file

You could lookup information from livestatus in your nagios compatible check.

  • checkmk runs your nagios script with $HOSTNAME$ as parameter
  • Your nagios script then does a lq query with the help of $1 on the running checkmk instance to get whatever you need.