HowToUseMacros

From Wiki Centreon

Jump to: navigation, search

Using Macros in Centreon and Nagios

In the configuration pages, you might have seen the Macros tab. It's available for hosts, services, host templates and service templates. Macros provide "variables" to Nagios and help auto-configuration of hosts and services.

Let's say you have three SNMP communities to manage : PUBLIC, TELCO and PRIVATE. By default, Centreon will pass a default community value to scripts. This value is stored as a "Nagios Big Default Values" (USER1..USER32) : $USER2$. Unfortunately, it does not seem possible to overload this value on a per host/service basis.

So we will have to create a new variable (macro) : SNMPCOMMUNITY and we will set it's value either in the host/service template definition or in the host/service definition.

Go to Configuration/hosts/templates/generic-host/Macro tab and add a new macro, name it SNMPCOMMUNITY and set it's value to PUBLIC.

The name you type here will be modified by Centreon and Nagios, this is the primary goal of this how-to.

Nagios lets you define macros but they must begin with an underscore "_" character to prevent re-defining a "system" macro. Centreon WILL add the underscore. If you add it yourself and name the variable "_SNMPCOMMUNITY", Nagios will see it as "__SNMPCOMMUNITY" (with 2 underscore chars).

Then comes Nagios : when you define a macro inside a host definition, its final name will have the word "HOST" inserted after the underscore, if it's a service, it will have the word "SERVICE" inserted : defined in a host, our variable will be named "_HOSTSNMPCOMMUNITY".

Duplicate the generic-host template and modify the macro value to TELCO. Duplicate one last time and set the community to PRIVATE.

Now, you have to modify the commands or services or service templates to use the new variable. For example the check_centreon_memory command might have its command-line rewritten as :

$USER1$/check_centreon_snmp_memory -H $HOSTADDRESS$ -C $_HOSTSNMPCOMMUNITY$ -v 1 -w 80 -c 90

If the community is sent via parameters, you just substitute $USER2$ with $_HOSTSNMPCOMMUNITY$ in the service's "arguments" string. For example, the "SNMP-DISK-/" arguments' string might become :

!/!90!95!$_HOSTSNMPCOMMUNITY$!1!

To go further :

Personal tools