Nagios Parser to configure Nagios configuration files through Chef

25 / May / 2016 by Prakashul 0 comments

chef                                   Nagios-logo

Nagios Parser is a ruby library that can be used as a gem to parse values from the Nagios configuration files. These values can used as arrays or hashes to suit the use case.

In this blog, we will see how to use the Nagios Parser library to define a configuration file using the data in other configuration files. The deployment process is orchestrated through Chef, so we will be include the library as a gem in the recipes.

For our use case, we have various individual contacts file for each hostgroup. We want to create a singular contactgroup file that has all the contact of each hostgroup listed in individual contacts file. By the use of Nagios Object Parser we will be able to achieve the use case.

This is one of the contacts file with one of the contact name as contact_L1. We have many such files with different contact names.

Let the contacts files reside within /etc/nagios3/contacts.d/ such that the path in included in nagios.cfg

define contact{
contact_name contact_L1
alias contact_L1
service_notification_options w,u,c,r
host_notification_commands notify-host-by-email
email prakashul@gmail.com
can_submit_commands 1
}

Now we will make use of the following recipe to traverse all contacts configuration files and pick up the different contacts name  and then store them as comma separated values in an array. We can later traverse that array to populate the contactgroups configuration file.

chef_gem 'nagios_parser'
require 'nagios_parser/object/parser'

contacts_file_path='/etc/nagios3/contacts.d/'

_cg = []
# reads config files
Dir.glob("#{contacts_file_path}*.cfg") do |config_file|

 config_parsed = NagiosParser::Object::Parser.parse(File.read(config_file))
      config_parsed['contact'].each do |node|

_cg.push node['contact_name']

   end
 end

_contgroups = _cg.join ","
	   puts _contgroups

template "/etc/nagios3/contactgroup.d/cg-generic.cfg" do
source "contactgroup-generic.erb"
owner "root"
group "root"
mode "0755"
variables( :_contgroups => _contgroups)
end

To understand the recipe we must know that at the beginning of each contacts configuration file there is a “define” keyword. The Nagios Object Parser reads all key value pairs under each define section (in case there are more than one) and captures the value of the contact_name key. Once that is done the captured value is pushed into the _cg array that was earlier initialized as an empty array. We also do a join on the elements of the array to make them comma separated and as a single string as the Nagios configuration files accept comma separated values.

We have a template file for contactgroup and the elements of the _cg array  joined as a string variable _contgroups is passed into that template file as follows:

define contactgroup{
contactgroup_name Basic_contacts
alias Basic_contacts
members <%= @_contgroups %>
}

This will result in a file at  /etc/nagios3/contactgroup.d/cg-generic.cfg (defined in the template file) that has all the members of the individual contacts file.

Of course, the same parser can be used to fetch data from other files and create more configuration files such as from hosts file to hostgroup files or from hostgroup files to services files and so on.

An additional reload block may be added to the recipe that reloads Nagios after the files have been created (using attributes or execute resource).

FOUND THIS USEFUL? SHARE IT

Leave a comment -