Manual work

One small problem when provisioning new machines is all the manual steps you have to take.
In my case, these are:

  • Create VM via custom KVM webinterface
  • Configure puppet for the new node
  • Provision machine via Puppet
  • Add machine to Nagios for monitoring

 

This is how I automated the addition of new machines to Nagios

First off, we need to have all nodes include a class that exports a virtual resource. In my base class that every node uses, I’ve included my new nagios::export-class like this:

include profile::nagios::export

This class then defines the virtual resources we should export

class profile::nagios::export {
  if ($::fqdn == 'machine1.rm-rf.se') {
    $hostgroups = 'remote-ssh-debian-servers,ssh-servers,temperature'
  } elsif ($::fqdn in ['machine2.rm-rf.se', 'machine3.rm-rf.se', 'machine15.rm-rf.se']) {
    $hostgroups = 'remote-ssh-debian-servers,ssh-servers,http-servers'
  } else {
    $hostgroups = 'remote-ssh-debian-servers,ssh-servers'
  }

  @@nagios_host { $::hostname:
    use => 'generic-host,host-pnp',
    address => $::fqdn,
    alias => $::hostname,
    hostgroups => $hostgroups,
    target => "/etc/nagios3/conf.d/resource.d/host_${::fqdn}.cfg"
  }
}

If I had named my servers after what they do, the hostgroup assigning logic in the export-class could be much better.
Once you’ve assigned all hostgroups needed for each node, your puppetmaster will keep track of the information from the nodes.
Now we need to make sure that the Nagios-server becomes aware of the these definitions.
This is done via “subscribing” to the exported virtual resources we just created.

To do this, we create a new class that is called profile::nagios::server and assign it to the nagios server node. The class looks like this

class profile::nagios::server {
  file { 'resource-d':
    path => '/etc/nagios3/conf.d/resource.d',
    ensure => directory,
    owner => 'nagios',
  }

  Nagios_host <<||>> {
    require => File[resource-d],
    notify => [Exec[nagios_chown],Service[nagios3]],
  }

  exec { 'nagios_chown':
    command => "/bin/chown -R nagios:nagios /etc/nagios3/conf.d/resource.d",
    require => File['resource-d'],
    onlyif => '/bin/ls -ld /etc/nagios3/conf.d/resource.d/* | /bin/grep -v "nagios nagios"',
  }

  service { nagios3:
    ensure => running,
    enable => true,
    require => Exec['nagios_chown'],
  }
}

This class makes sure we have a special folder called “/etc/nagios3/conf.d/resources.d” which will store our dynamic resources. Once we’ve filled our folder with the resources from puppet, we need to make sure nagios can read them. This is done via the “nagios chown” exec-statement. It checks if any file in resource.d-folder is not owned by nagios, and if so, it changes the ownership of the files.

Auto add new hosts to Nagios (with puppet)

Leave a Reply

Your email address will not be published. Required fields are marked *