1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Object Inheritance</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<meta name="keywords" content="Supervision, Icinga, Nagios, Linux">
<link rel="home" href="index.html" title="Icinga Version 1.0.2 Documentation">
<link rel="up" href="ch06.html" title="Chapter 6. Advanced Topics">
<link rel="prev" href="cgiincludes.html" title="Custom CGI Headers and Footers">
<link rel="next" href="objecttricks.html" title="Time-Saving Tricks For Object Definitions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<CENTER><IMG src="../images/logofullsize.png" border="0" alt="Icinga" title="Icinga"></CENTER>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Object Inheritance</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="cgiincludes.html">Prev</a> </td>
<th width="60%" align="center">Chapter 6. Advanced Topics</th>
<td width="20%" align="right"> <a accesskey="n" href="objecttricks.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section" title="Object Inheritance">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="objectinheritance"></a><a name="object_inheritance"></a>Object Inheritance</h2></div></div></div>
<p><span class="bold"><strong>Introduction</strong></span></p>
<p>This documentation attempts to explain object inheritance and how it can be used in your <a class="link" href="objectdefinitions.html" title="Object Definitions">object
definitions</a>.</p>
<p>If you are confused about how recursion and inheritance work after reading this, take a look at the sample object config files provided in
the Icinga distribution. If that still doesn't help, drop an email message with a <span class="emphasis"><em>detailed</em></span> description of your problem
to the <span class="emphasis"><em>nagios-users</em></span> mailing list.</p>
<p><span class="bold"><strong>Basics</strong></span></p>
<p>There are three variables affecting recursion and inheritance that are present in all object definitions. They are
<span class="emphasis"><em>"indicated"</em></span> as follows...</p>
<pre class="screen"> define <span class="emphasis"><em> someobjecttype</em></span>{
<span class="emphasis"><em> object-specific variables</em></span> ...
name <span class="emphasis"><em> template_name</em></span>
use <span class="emphasis"><em> name_of_template_to_use</em></span>
register [0/1]
}</pre>
<p>The first variable is <span class="emphasis"><em>name</em></span>. Its just a "template" name that can be referenced in other object definitions so they can
inherit the objects properties/variables. Template names must be unique amongst objects of the same type, so you can't have two or more host
definitions that have "hosttemplate" as their template name.</p>
<p>The second variable is <span class="emphasis"><em>use</em></span>. This is where you specify the name of the template object that you want to inherit
properties/variables from. The name you specify for this variable must be defined as another object's template named (using the
<span class="emphasis"><em>name</em></span> variable).</p>
<p>The third variable is <span class="emphasis"><em>register</em></span>. This variable is used to indicate whether or not the object definition should be
"registered" with Icinga. By default, all object definitions are registered. If you are using a partial object definition as a template,
you would want to prevent it from being registered (an example of this is provided later). Values are as follows: 0 = do NOT register object
definition, 1 = register object definition (this is the default). This variable is NOT inherited; every (partial) object definition used as a
template must explicitly set the <span class="emphasis"><em>register</em></span> directive to be <span class="emphasis"><em>0</em></span>. This prevents the need to override an
inherited <span class="emphasis"><em>register</em></span> directive with a value of <span class="emphasis"><em>1</em></span> for every object that should be registered.</p>
<p><span class="bold"><strong>Local Variables vs. Inherited Variables</strong></span></p>
<p>One important thing to understand with inheritance is that "local" object variables always take precedence over variables defined in the
template object. Take a look at the following example of two host definitions (not all required variables have been supplied):</p>
<pre class="screen"> define host{
host_name bighost1
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
name hosttemplate1
}
define host{
host_name bighost2
max_check_attempts 3
use hosttemplate1
}</pre>
<p>You'll note that the definition for host <span class="emphasis"><em>bighost1</em></span> has been defined as having <span class="emphasis"><em>hosttemplate1</em></span> as its
template name. The definition for host <span class="emphasis"><em>bighost2</em></span> is using the definition of <span class="emphasis"><em>bighost1</em></span> as its template
object. Once Icinga processes this data, the resulting definition of host <span class="emphasis"><em>bighost2</em></span> would be equivalent to this
definition:</p>
<pre class="screen"> define host{
host_name bighost2
check_command check-host-alive
notification_options d,u,r
max_check_attempts 3
}</pre>
<p>You can see that the <span class="emphasis"><em>check_command</em></span> and <span class="emphasis"><em>notification_options</em></span> variables were inherited from the
template object (where host <span class="emphasis"><em>bighost1</em></span> was defined). However, the <span class="emphasis"><em>host_name</em></span> and
<span class="emphasis"><em>max_check_attempts</em></span> variables were not inherited from the template object because they were defined locally. Remember, locally
defined variables override variables that would normally be inherited from a template object. That should be a fairly easy concept to
understand.</p>
<div class="tip" title="Tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top">
<p>If you would like local string variables to be appended to inherited string values, you can do so. Read more about how to accomplish
this <a class="link" href="objectinheritance.html#objectinheritance-add_string">below</a>.</p>
</td></tr>
</table></div>
<p><span class="bold"><strong>Inheritance Chaining</strong></span></p>
<p>Objects can inherit properties/variables from multiple levels of template objects. Take the following example:</p>
<pre class="screen"> define host{
host_name bighost1
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
name hosttemplate1
}
define host{
host_name bighost2
max_check_attempts 3
use hosttemplate1
name hosttemplate2
}
define host{
host_name bighost3
use hosttemplate2
}</pre>
<p>You'll notice that the definition of host <span class="emphasis"><em>bighost3</em></span> inherits variables from the definition of host
<span class="emphasis"><em>bighost2</em></span>, which in turn inherits variables from the definition of host <span class="emphasis"><em>bighost1</em></span>. Once Icinga
processes this configuration data, the resulting host definitions are equivalent to the following:</p>
<pre class="screen"> define host{
host_name bighost1
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
}
define host{
host_name bighost2
check_command check-host-alive
notification_options d,u,r
max_check_attempts 3
}
define host{
host_name bighost3
check_command check-host-alive
notification_options d,u,r
max_check_attempts 3
}</pre>
<p>There is no inherent limit on how "deep" inheritance can go, but you'll probably want to limit yourself to at most a few levels in order to
maintain sanity.</p>
<p><span class="bold"><strong>Using Incomplete Object Definitions as Templates</strong></span></p>
<p>It is possible to use imcomplete object definitions as templates for use by other object definitions. By "incomplete" definition, I mean
that all required variables in the object have not been supplied in the object definition. It may sound odd to use incomplete definitions as
templates, but it is in fact recommended that you use them. Why? Well, they can serve as a set of defaults for use in all other object
definitions. Take the following example:</p>
<pre class="screen"> define host{
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
name generichosttemplate
register 0
}
define host{
host_name bighost1
address 192.168.1.3
use generichosthosttemplate
}
define host{
host_name bighost2
address 192.168.1.4
use generichosthosttemplate
}</pre>
<p>Notice that the first host definition is incomplete because it is missing the required <span class="emphasis"><em>host_name</em></span> variable. We don't
need to supply a host name because we just want to use this definition as a generic host template. In order to prevent this definition from being
registered with Icinga as a normal host, we set the <span class="emphasis"><em>register</em></span> variable to 0.</p>
<p>The definitions of hosts <span class="emphasis"><em>bighost1</em></span> and <span class="emphasis"><em>bighost2</em></span> inherit their values from the generic host
definition. The only variable we've chosed to override is the <span class="emphasis"><em>address</em></span> variable. This means that both hosts will have the
exact same properties, except for their <span class="emphasis"><em>host_name</em></span> and <span class="emphasis"><em>address</em></span> variables. Once Icinga processes
the config data in the example, the resulting host definitions would be equivalent to specifying the following:</p>
<pre class="screen"> define host{
host_name bighost1
address 192.168.1.3
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
}
define host{
host_name bighost2
address 192.168.1.4
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
}</pre>
<p>At the very least, using a template definition for default variables will save you a lot of typing. It'll also save you a lot of headaches
later if you want to change the default values of variables for a large number of hosts.</p>
<p><span class="bold"><strong>Custom Object Variables</strong></span></p>
<p>Any <a class="link" href="customobjectvars.html" title="Custom Object Variables">custom object variables</a> that you define in your host, service, or contact definition templates
will be inherited just like other standard variables. Take the following example:</p>
<pre class="screen"> define host{
_customvar1 somevalue ; <-- Custom host variable
_snmp_community public ; <-- Custom host variable
name generichosttemplate
register 0
}
define host{
host_name bighost1
address 192.168.1.3
use generichosthosttemplate
}</pre>
<p>The host <span class="emphasis"><em>bighost1</em></span> will inherit the custom host variables <span class="emphasis"><em>_customvar1</em></span> and
<span class="emphasis"><em>_snmp_community</em></span>, as well as their respective values, from the <span class="emphasis"><em>generichosttemplate</em></span> definition. The
effective result is a definition for <span class="emphasis"><em>bighost1</em></span> that looks like this:</p>
<pre class="screen"> define host{
host_name bighost1
address 192.168.1.3
_customvar1 somevalue
_snmp_community public
}</pre>
<p><a name="objectinheritance-cancel_string"></a><span class="bold"><strong>Cancelling Inheritance of String Values</strong></span></p>
<p>In some cases you may not want your host, service, or contact definitions to inherit values of string variables from the templates they
reference. If this is the case, you can specify "<span class="bold"><strong>null</strong></span>" (without quotes) as the value of the variable that you do
not want to inherit. Take the following example:</p>
<pre class="screen"> define host{
event_handler my-event-handler-command
name generichosttemplate
register 0
}
define host{
host_name bighost1
address 192.168.1.3
event_handler null
use generichosthosttemplate
}</pre>
<p>In this case, the host <span class="emphasis"><em>bighost1</em></span> will not inherit the value of the <span class="emphasis"><em>event_handler</em></span> variable that is
defined in the <span class="emphasis"><em>generichosttemplate</em></span>. The resulting effective definition of <span class="emphasis"><em>bighost1</em></span> is the
following:</p>
<pre class="screen"> define host{
host_name bighost1
address 192.168.1.3
}</pre>
<p><a name="objectinheritance-add_string"></a><span class="bold"><strong>Additive Inheritance of String Values</strong></span></p>
<p>Icinga gives preference to local variables instead of values inherited from templates. In most cases local variable values override
those that are defined in templates. In some cases it makes sense to allow Icinga to use the values of inherited <span class="emphasis"><em>and</em></span>
local variables together.</p>
<p>This "additive inheritance" can be accomplished by prepending the local variable value with a plus sign (<span class="bold"><strong>+</strong></span>). This features is only available for standard (non-custom) variables that contain string values. Take the following
example:</p>
<pre class="screen"> define host{
hostgroups all-servers
name generichosttemplate
register 0
}
define host{
host_name linuxserver1
hostgroups +linux-servers,web-servers
use generichosthosttemplate
}</pre>
<p>In this case, the host <span class="emphasis"><em>linuxserver1</em></span> will append the value of its local <span class="emphasis"><em>hostgroups</em></span> variable to that
from <span class="emphasis"><em>generichosttemplate</em></span>. The resulting effective definition of <span class="emphasis"><em>linuxserver1</em></span> is the following:</p>
<pre class="screen"> define host{
host_name linuxserver1
hostgroups all-servers,linux-servers,web-servers
}</pre>
<p><a name="objectinheritance-implied_inheritance"></a><span class="bold"><strong>Implied Inheritance</strong></span></p>
<p>Normally you have to either explicitly specify the value of a required variable in an object definition or inherit it from a template. There
are a few exceptions to this rule, where Icinga will assume that you want to use a value that instead comes from a related object. For
example, the values of some service variables will be copied from the host the service is associated with if you don't otherwise specify
them.</p>
<p>The following table lists the object variables that will be implicitly inherited from related objects if you don't explicitly specify their
value in your object definition or inherit them from a template.</p>
<div class="informaltable">
<table border="1">
<colgroup>
<col>
<col>
<col>
</colgroup>
<tbody>
<tr>
<td><p> <span class="bold"><strong>Object Type</strong></span> </p></td>
<td><p> <span class="bold"><strong>Object Variable</strong></span> </p></td>
<td><p> <span class="bold"><strong>Implied Source</strong></span> </p></td>
</tr>
<tr>
<td rowspan="3" align="left" valign="middle"><p> <span class="bold"><strong>Services</strong></span> </p></td>
<td><p> <span class="emphasis"><em>contact_groups</em></span> </p></td>
<td><p><span class="emphasis"><em>contact_groups</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>notification_interval</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_interval</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>notification_period</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_period</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td rowspan="3" align="left" valign="middle"><p> <span class="bold"><strong>Host Escalations</strong></span> </p></td>
<td><p> <span class="emphasis"><em>contact_groups</em></span> </p></td>
<td><p><span class="emphasis"><em>contact_groups</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>notification_interval</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_interval</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>escalation_period</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_period</em></span> in the associated host definition</p></td>
</tr>
<tr>
<td rowspan="3" align="left" valign="middle"><p> <span class="bold"><strong>Service Escalations</strong></span> </p></td>
<td><p> <span class="emphasis"><em>contact_groups</em></span> </p></td>
<td><p><span class="emphasis"><em>contact_groups</em></span> in the associated service definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>notification_interval</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_interval</em></span> in the associated service definition</p></td>
</tr>
<tr>
<td><p> <span class="emphasis"><em>escalation_period</em></span> </p></td>
<td><p><span class="emphasis"><em>notification_period</em></span> in the associated service definition</p></td>
</tr>
</tbody>
</table>
</div>
<p><a name="objectinheritance-impliedescalations"></a><span class="bold"><strong>Implied/Additive Inheritance in Escalations</strong></span></p>
<p>Service and host escalation definitions can make use of a special rule that combines the features of implied and additive inheritance. If
escalations 1) do not inherit the values of their <span class="emphasis"><em>contact_groups</em></span> or <span class="emphasis"><em>contacts</em></span> directives from another
escalation template and 2) their <span class="emphasis"><em>contact_groups</em></span> or <span class="emphasis"><em>contacts</em></span> directives begin with a plus sign (+), then
the values of their corresponding host or service definition's <span class="emphasis"><em>contact_groups</em></span> or <span class="emphasis"><em>contacts</em></span> directives
will be used in the additive inheritance logic.</p>
<p>Confused? Here's an example:</p>
<pre class="screen"> define host{
name linux-server
contact_groups linux-admins
...
}
define hostescalation{
host_name linux-server
contact_groups +management
...
}</pre>
<p>This is a much simpler equivalent to:</p>
<pre class="screen"> define hostescalation{
host_name linux-server
contact_groups linux-admins,management
...
}</pre>
<p><a name="objectinheritance-important_values"></a><span class="bold"><strong>Important values</strong></span></p>
<p>Service templates can make use of a special rule which gives precedence to their check_command value. If the check_command is prefixed with
an exclamation mark (!), then the template's check_command is marked as important and will be used over the check_command defined for the service
(this is styled after CSS syntax, which uses ! as an important attribute).</p>
<p>Why is this useful? It is mainly useful when setting a different check_command for distributed systems. You may want to set a freshness
threshold and a check_command that forces the service into a failed state, but this doesn't work with the normal templating system. Using this
important flag allows the custom check_command to be written, but a general distributed template can be used to overrule the check_command when
used on a central Icinga-erver.</p>
<p>For instance:</p>
<pre class="screen"># On master
define service {
name service-distributed
register 0
active_checks_enabled 0
check_freshness 1
check_command !set_to_stale
}
# On slave
define service {
name service-distributed
register 0
active_checks_enabled 1
}
# Service definition, used by master and slave
define service {
host_name host1
service_description serviceA
check_command check_http...
use service-distributed
...
}</pre>
<p><a name="objectinheritance-multiple_templates"></a><span class="bold"><strong>Multiple Inheritance Sources</strong></span></p>
<p>Thus far, all examples of inheritance have shown object definitions inheriting variables/values from just a single source. You are also able
to inherit variables/values from multiple sources for more complex configurations, as shown below.</p>
<div class="informaltable">
<table border="0">
<colgroup>
<col>
<col>
</colgroup>
<tbody><tr>
<td>
<p> </p>
<pre class="screen"># Generic host template
define host{
name generic-host
active_checks_enabled 1
check_interval 10
...
register 0
}
# Development web server template
define host{
name development-server
check_interval 15
notification_options d,u,r
...
register 0
}
# Development web server
define host{
use generic-host,development-server
host_name devweb1
...
}</pre>
<p> </p>
</td>
<td align="center" valign="top"><p> <span class="inlinemediaobject"><img src="../images/multiple-templates1.png"></span> </p></td>
</tr></tbody>
</table>
</div>
<p>In the example above, <span class="emphasis"><em>devweb1</em></span> is inheriting variables/values from two sources: <span class="emphasis"><em>generic-host</em></span> and
<span class="emphasis"><em>development-server</em></span>. You'll notice that a <span class="emphasis"><em>check_interval</em></span> variable is defined in both sources. Since
<span class="emphasis"><em>generic-host</em></span> was the first template specified in <span class="emphasis"><em>devweb1</em></span>'s <span class="emphasis"><em>use</em></span> directive, its value
for the <span class="emphasis"><em>check_interval</em></span> variable is inherited by the <span class="emphasis"><em>devweb1</em></span> host. After inheritance, the effective
definition of <span class="emphasis"><em>devweb1</em></span> would be as follows:</p>
<pre class="screen"># Development web server
define host{
host_name devweb1
active_checks_enabled 1
check_interval 10
notification_options d,u,r
...
}</pre>
<p><span class="bold"><strong>Precedence With Multiple Inheritance Sources</strong></span></p>
<p>When you use multiple inheritance sources, it is important to know how Icinga handles variables that are defined in multiple sources.
In these cases Icinga will use the variable/value from the first source that is specified in the <span class="emphasis"><em>use</em></span> directive. Since
inheritance sources can themselves inherit variables/values from one or more other sources, it can get tricky to figure out what variable/value
pairs take precedence.</p>
<div class="informaltable">
<table border="0">
<colgroup>
<col>
<col>
</colgroup>
<tbody><tr>
<td align="left" valign="top">
<p> Consider the following host definition that references three templates:</p> <pre class="screen"> # Development web server
define host{
use 1, 4, 8
host_name devweb1 ...
} </pre> <p>If some of those referenced templates themselves inherit variables/values from one or more other templates, the precendence rules
are shown to the right.</p> <p>Testing, trial, and error will help you better understand exactly how things work in complex
inheritance situations like this. :-)</p>
</td>
<td align="center" valign="top"><p> <span class="inlinemediaobject"><img src="../images/multiple-templates2.png"></span> </p></td>
</tr></tbody>
</table>
</div>
<a class="indexterm" name="id2002269"></a>
<a class="indexterm" name="id2002287"></a>
<a class="indexterm" name="id2002300"></a>
<a class="indexterm" name="id2002312"></a>
<a class="indexterm" name="id2002326"></a>
<a class="indexterm" name="id2002337"></a>
<a class="indexterm" name="id2002352"></a>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="cgiincludes.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="objecttricks.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Custom CGI Headers and
Footers </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Time-Saving Tricks For Object Definitions</td>
</tr>
</table>
</div>
<P class="copyright">© 2009-2010 Icinga Development Team, http://www.icinga.org</P>
</body>
</html>
|