Creating custom okconfig templates

For this example I have a host (google.com) with HTTP, HTTPS, DNS, and Ping checks.

google-1

I’ve customized some of the service checks and want to create a template called “Google Server” from this host and it’s services.

To do this I will have to combine the config files for all services into a template. Templates are by default stored in /etc/nagios/okconfig/examples/, and have the file extension .cfg-example.

Locate all config files for this host, this can be done in a few ways, but the easiest is probably with pynag:

[root@adagios ~]# pynag list --quiet filename where host_name=google.com and register=1 | sort | uniq
/etc/nagios/okconfig/hosts/default/google.com-dns.cfg
/etc/nagios/okconfig/hosts/default/google.com-host.cfg
/etc/nagios/okconfig/hosts/default/google.com-http.cfg
/etc/nagios/okconfig/hosts/default/google.com-https.cfg
[root@adagios ~]#

I can do this in the Adagios web interface too, by searching for the host, selecting all services and clicking the bulk edit button (I won’t actually be editing anything, bulk edit will just show me all file names).
google-2

Next I want to combine all services into a template file: /etc/nagios/okconfig/examples/google-server.cfg-example

[root@adagios ~]# cat /etc/nagios/okconfig/hosts/default/google.com-dns.cfg > /etc/nagios/okconfig/examples/google-server.cfg-example
[root@adagios ~]# cat /etc/nagios/okconfig/hosts/default/google.com-http.cfg >> /etc/nagios/okconfig/examples/google-server.cfg-example
[root@adagios ~]# cat /etc/nagios/okconfig/hosts/default/google.com-https.cfg >> /etc/nagios/okconfig/examples/google-server.cfg-example

NOTE: I didn’t include the host config because I haven’t defined any custom services in it. If there are any services in the host config you would like in the template (chances are there will be), add them to the template file yourself. To see what services are defined in the host config, use: pynag list where object_type=service and register=1 and filename=<location of host config>

[root@adagios ~]# pynag list where object_type=service and register=1 and filename=/etc/nagios/okconfig/hosts/default/google.com-host.cfg
object_type          shortname            filename
--------------------------------------------------------------------------------
service              google.com/Ping      /etc/nagios/okconfig/hosts/default/google.com-host.cfg
service              google.com/test      /etc/nagios/okconfig/hosts/default/google.com-host.cfg
----------2 objects matches search condition------------------------------------
[root@adagios ~]#

As seen above, I added a “test” service to the host through the Adagios web interface, and it was saved in the host config file. The define service {…} part is what you would add to the template config.

[root@adagios ~]# cat /etc/nagios/okconfig/hosts/default/google.com-host.cfg
...
define service {
         service_description            test
         use                            generic-service
         host_name                      google.com
        check_command                 okc-check_dummy
        __EXIT_CODE                   0
        __MESSAGE                     Cool!
}

To prepare the template so it can be applied to other hosts, replace the host name and group name with HOSTNAME and GROUP. Okconfig will substitute HOSTNAME and GROUP with the host (and if any, group) name you specify when adding the template. In my case the hostname was google.com and group was default.

sed -i 's/google.com/HOSTNAME/g;s/default/GROUP/g' /etc/nagios/okconfig/examples/google-server.cfg-example

Example how a service defenition should change:

Before:

define service {
        host_name               google.com
        contact_groups          default
        service_description     HTTPS google.com
        check_command           okc-check_https
        use                     okc-check_https
        __URI                   /
        __SEARCH_STRING
        __RESPONSE_WARNING      2
        __RESPONSE_CRITICAL     10
        __VIRTUAL_HOST          google.com
        __PORT                  443
}

After:

define service {
        host_name               HOSTNAME
        contact_groups          GROUP
        service_description     HTTPS HOSTNAME
        check_command           okc-check_https
        use                     okc-check_https
        __URI                   /
        __SEARCH_STRING
        __RESPONSE_WARNING      2
        __RESPONSE_CRITICAL     10
        __VIRTUAL_HOST          HOSTNAME
        __PORT                  443

}

Next, add the host and select the newly created Google Server template:

google-3

google-4

I deleted the google.com host, removed all config files, and added it again with only the template I created:

google-5

All this could probably be done in one (or a few) pynag copy commands, but I haven’t tested that.

Advertisements
Creating custom okconfig templates

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s