Is it possible to use Variables in definition files?

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
stn021
Posts: 9
Joined: Tue Mar 11, 2014 6:31 am

Is it possible to use Variables in definition files?

Post 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
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Is it possible to use Variables in definition files?

Post 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.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
technick
Posts: 49
Joined: Tue Feb 04, 2014 10:30 am
Location: Denver, CO

Re: Is it possible to use Variables in definition files?

Post by technick »

You can look at /usr/local/nagios/var/objects.cache and view your custom variable.
----------------------
Nagios Jedi in training.
stn021
Posts: 9
Joined: Tue Mar 11, 2014 6:31 am

Re: Is it possible to use Variables in definition files?

Post 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)
stn021
Posts: 9
Joined: Tue Mar 11, 2014 6:31 am

Re: Is it possible to use Variables in definition files?

Post 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
technick
Posts: 49
Joined: Tue Feb 04, 2014 10:30 am
Location: Denver, CO

Re: Is it possible to use Variables in definition files?

Post 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
----------------------
Nagios Jedi in training.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Is it possible to use Variables in definition files?

Post by abrist »

Technick is dead-on once again. Let us know if you have any further questions regarding templates, object inheritance, or custom variables. . .
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
stn021
Posts: 9
Joined: Tue Mar 11, 2014 6:31 am

Re: Is it possible to use Variables in definition files?

Post 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...
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Is it possible to use Variables in definition files?

Post 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?
Former Nagios employee
stn021
Posts: 9
Joined: Tue Mar 11, 2014 6:31 am

Re: Is it possible to use Variables in definition files?

Post 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 :)
Locked