Page 1 of 1
Is it possible to use Variables in definition files?
Posted: Tue Mar 25, 2014 12:44 pm
by stn021
Hi,
as described in
http://nagios.sourceforge.net/docs/3_0/ ... tvars.html variables can be used in nagios definitions.
I tried that and it doesn't seem to work. The defined variable is interpreted as "0". Here is what I did:
Code: Select all
define service{
_CHECK_INTERVAL 77
}
# and:
define service{
host_name <my host>
service_description <my desc>
use generic-service
check_command <my command>
check_interval $_SERVICECHECK_INTERVAL$
}
If I enter a number instead of $_SERVICECHECK_INTERVAL$ everything is fine, the service works as expected and in the webinterface the correct interval can be checked. With the definition above (and some variations I tried) the service_interval is shown as Zero in the web-interface.
Where did I go wrong ?
THX, stn
Re: Is it possible to use Variables in definition files?
Posted: Tue Mar 25, 2014 4:26 pm
by abrist
Custom variable are intended to be passed to plugins/scripts/etc. They are not intended for use as values for config directives and by all accounts, should not work in the method you are attempting.
Re: Is it possible to use Variables in definition files?
Posted: Tue Mar 25, 2014 7:14 pm
by technick
You can look at /usr/local/nagios/var/objects.cache and view your custom variable.
Re: Is it possible to use Variables in definition files?
Posted: Wed Mar 26, 2014 10:13 am
by stn021
technick wrote:You can look at /usr/local/nagios/var/objects.cache and view your custom variable.
Very cool, thank you. That files list every defined object with all its properties. Allows a quick check of config-changes.
(Side note: In my installation of nagios-core this file is in a different location /var/cache/nagios3/objects.cache)
Re: Is it possible to use Variables in definition files?
Posted: Wed Mar 26, 2014 10:27 am
by stn021
abrist wrote:Custom variable are intended to be passed to plugins/scripts/etc. They are not intended for use as values for config directives and by all accounts, should not work in the method you are attempting.
No, they don't, apparently
Let me rephrase my question.
I have several identical hosts with identical services. The only differences between the hosts are host_name and address.
So I was wondering
- Is there a way to duplicate the complete config of a host without having to copy every codeblock (define host{}, define service{}) individually ?
- or: is it possible to define host{} and services in a single definition so that the whole definition can be replicated in a single short block (or even a single line) with just a different host_name and address ?
- and: suppose I would like to quickly change a single variable for a host and all its services, for example set check_interval to 1 or 1440 instead of the default 5, and optionally quickly change it back (after testing). What is the quickest way to do that, if possible without rewriting every service ?
THX, stn
Re: Is it possible to use Variables in definition files?
Posted: Wed Mar 26, 2014 10:39 am
by technick
stn021 wrote:abrist wrote:
- Is there a way to duplicate the complete config of a host without having to copy every codeblock (define host{}, define service{}) individually ?
Yes, you can use templates to save yourself A LOT of time. Here is a quick example of how templates save you time.
Code: Select all
define host {
name linux-server
check_command check-host-alive!!!!!!!!
use generic-host
max_check_attempts 4
check_interval 1
retry_interval 1
active_checks_enabled 1
check_period 24x7
contact_groups nagios-ops
notification_interval 5
notification_period 24x7
notification_options d,u,r,
notifications_enabled 1
icon_image linux.png
register 0
}
The above is a template called linux-server. This template is chained to another template called generic-service as well. If you are unaware of how chaining works, please read up on inheritances in Nagios. Now Here are some defined hosts that all use the linux-server template.
define host {
host_name dertcn00
use linux-server
display_name dertcn00
address dertcn00
register 1
}
define host {
host_name dertcn01
use linux-server
display_name dertcn01
address dertcn01
register 1
}
define host {
host_name dertcn02
use linux-server
display_name dertcn02
address dertcn02
register 1
}
Only the bare minimum is being defined with each host and its inheriting the rest of the stuff from the linux-server template.
http://nagios.sourceforge.net/docs/3_0/ ... tance.html
Also you should look into this link, it will save you a lot of time...
http://nagios.sourceforge.net/docs/3_0/ ... ricks.html
Re: Is it possible to use Variables in definition files?
Posted: Wed Mar 26, 2014 1:18 pm
by abrist
Technick is dead-on once again. Let us know if you have any further questions regarding templates, object inheritance, or custom variables. . .
Re: Is it possible to use Variables in definition files?
Posted: Thu Mar 27, 2014 4:22 pm
by stn021
technick wrote:stn021 wrote:abrist wrote:
- Is there a way to duplicate the complete config of a host without having to copy every codeblock (define host{}, define service{}) individually ?
Yes, you can use templates to save yourself A LOT of time. Here is a quick example of ..
Ok, lets See what else is possible...
Re: Is it possible to use Variables in definition files?
Posted: Thu Mar 27, 2014 4:32 pm
by tmcdonald
stn021 wrote:Ok, lets See what else is possible...
Are you asking a question or just stating that you are going to try some things?
Re: Is it possible to use Variables in definition files?
Posted: Thu Mar 27, 2014 5:06 pm
by stn021
tmcdonald wrote:Are you asking a question or just stating that you are going to try some things?
That was shorthand for "thank you all for the valuable hints and I will now now explore the numerous possibilies to make config files shorter". Sorry, was in a bit of a hurry
