Object Inheritance in Nagios
Server - Nagios

Objects

An object in Nagios is a unit of information, such as a host, service, contact, group, timeperiod, etc. Since these units or objects represent text files, they can inherit properties from other units or objects.

 

Object Inheritance

Object inheritance is an important aspect in managing a larger Nagios environment. This section will look at how that inheritance works from the perspective Nagios Core.

Understanding the Basics

When a host or service is created a template is used to create that host or service. In this example the host sql1 has been created.

 

define host{

use linux-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

}

 

Three variables determine object inheritance; name, use and register. The “name” variable is the text string that labels the template. The “use” variable lists the name of a template where settings can be pulled from thus creating inheritance. The “register” variable determines whether a variable should be registered with Nagios.

 

define someobjecttype{

object-specific variables ...

name template_name

use name_of_template_to_use

register [0/1]

}

 

The importance of object inheritance cannot be overstated. This is especially important as an organization begins to grow as managing devices individually is much more time consuming than managing using hostgroups.

 

Planning and implementing hostgroups can make a significant difference.

Local vs. Inherited Variables

Create a host, in the example a MySQL server is using the linux-server template. This template contains the basic settings needed. However, you can see this basic template also inherits the settings from the generic-host template.

 

define host{

name linux-server

use generic-host

check_period 24x7

check_interval 5

retry_interval 1

max_check_attempts 10

check_command check-host-alive

notification_period 24x7

notification_interval 120

notification_options d,u,r

contact_groups admins

register 0

}

 

Below the generic-host template is listed. This is often a place of misunderstanding as it is easy to overlook the connections between the two templates.

 

define host{

name generic-host

notifications_enabled 1

event_handler_enabled 1

flap_detection_enabled 1

failure_prediction_enabled 1

process_perf_data 1

retain_status_information 1

retain_nonstatus_information 1

notification_period 24x7

register 0

}

 

Local variables always take precedence over inherited variables. So in this example if you changed the linux-server template to:

notification_period workhours

 

The outcome will be workhours even though the inherited value is 24x7 from the generic-host template.

 

notification_period 24x7

 

The workhours setting takes precedence as host definition uses linux-server as the local template.

 

define host{

use linux-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

}

 

 

Often is is easier to work with templates by creating a new template for a group of servers which will be similar. Here is a base template that will be used for MySQL servers called sql-server.

 

define host{

name sql-server

check_period 24x7

check_interval 15

retry_interval 1

max_check_attempts 5

check_command check-host-alive

notification_period 24x7

notification_interval 120

notification_options d,u,r

contact_groups admins

notifications_enabled 1

event_handler_enabled 1

flap_detection_enabled 1

failure_prediction_enabled 1

process_perf_data 1

retain_status_information 1

retain_nonstatus_information 1

register 0

}

 

Change the base template to sql-server.

 

define host{

use sql-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

}

 

That should be reflected when you go to the web interface System/Configuration and choose hosts.

 

 

 

Chaining

Once you have a base template you can chain other templates to it. Create a new template that reflects changes you may want for Red Hat servers, not necessarily MySQL servers. Three changes are seen here and are highlighted.

 

define host{

name rhel-server

check_period 24x7

max_check_attempts 5

check_command check-host-alive

notification_period 24x7

notification_options d,u,r

contact_groups rhel-admins

icon_image redhat.png

statusmap_image redhat.png

notifications_enabled 1

retain_nonstatus_information 1

register 0

}

 

In order to chain that template to the existing template, add a comma and append the template to the use line.

 

define host{

use sql-server,rhel-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

}

 

Be sure to also create the contactgroup rhel-admins.

 

define contactgroup{

contactgroup_name rhel_admins

alias nagios group

members tom

}

 

Restart Nagios and check the changes.

 

However, the contactgroup is not inherited because of the principle that local variables take precedence over inherited variables, meaning the admins contactgroup was already in place.

 

If you wanted to use both contact groups you would have to chain that feature.

 

define host{

use sql-server,rhel-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

contact_groups admins,rhel-admins

}

 

So in the example you are chaining the templates and contact_groups used.

Precedence in Multiple Sources

Create another template used for debian servers that use MySQL. The primary difference being the contact_groups and icons used.

 

 

define host{

name debian-server

check_period 24x7

max_check_attempts 5

check_command check-host-alive

notification_period 24x7

notification_options d,u,r

contact_groups debian-admins

icon_image debian.png

statusmap_image debian.png

notifications_enabled 1

retain_nonstatus_information 1

register 0

}

 

 

define host{

use sql-server,debian-server,rhel-server

host_name sql1

alias MySQL Server_1

address 192.168.5.197

contact_groups admins

}

 

Now the debian image shows in the map because of the order placed in the host definition. This demonstrates that inheritance is influenced by order as well.

 

 

 

 

 

 

 

Incomplete Object Definitions

Object definitions do not have to be complete to be used. An incomplete object definition can be used by another template to create chaining or it could be used for a specific purpose for some hosts, for example.

 

 

 

Creating Custom Variables

When custom host variables are used the first step is to create the template for the custom variable. In this example two variables have been added to this template (snmp-var), the SNMP community and the SNMP version. Note that the values need to be entered as the variable name when it is inserted and it is important that the variable name be started with a “_” so it does not conflict with other macros. The variable name will be converted into upper case before it is used so it may be easier to understand the function by making these upper case.

 

define host{

name snmp-var

_snmp_community public

_snmp_version 2c

register 0

}

 

The next step is to add the variable to the host. Here you can see chaining of three templates.

 

define host{

use sql-server,rhel-server,snmp-var

host_name sql1

alias MySQL Server_1

address 192.168.5.197

contact_groups admins

}

 

Now the variable can be used with service checks. Note: In order to use this variable “HOST” must be added to the front of the variable to indicate it is a host variable. If it was a service variable it would be “SERVICE” that would be added.

 

define service{

use generic-service

host_name sql1

check_command check_snmp_load!$_HOSTSNMP_COMMUNITY$!90%!95%

service_description SNMP CPU usage

}

 

 

Canceling Inheritance

It may be desirable to cancel inheritance of a value from the template. In other words, you may be using a template that has settings you do want to inherit but there may be a setting that you do not want to inherit.

 

Many of the variables available on the system have three options for that variable, inherit (standard), append (+) and do not inherit (null). In this example two typically inherited values from the rhel-server template are canceled. In order to cancel a value it must be listed with the null option.

 

 

define host{

use sql-server,rhel-server,snmp-var

host_name sql1

alias MySQL Server_1

icon_image null

statusmap_image null

address 192.168.5.197

contact_groups admins

}

 

Additive Inheritance

Additive inheritance appends the variable to an existing variable. In this example, the admins is a local variable for contact_groups so the contact_groups value of rhel-admins will not be added based on the fact the local value exists. However, if the template is modified the rhel-admins can be added to the local value by using a “+”.

 

 

define host{

use sql-server,rhel-server,snmp-var

host_name sql1

alias MySQL Server_1

address 192.168.5.197

contact_groups +rhel-admins

}

Now both contact_groups will be used.