1

I have the following resource:

exec{'regen-nagios-hosts':
  command     => '/usr/local/bin/generate-nagios-host-definitions-from-hostfile < /etc/hosts > /etc/nagios3/conf.d/sv-hosts.cfg',
  user        => 'root',
  before      => Class['nagios::server'],
  notify      => Service['nagios3'],
  require     => File['/etc/hosts'],
}

Ideally, I'd want to restart Nagios only if the file changed. It seems what I really want is a file resource, but file resources have a template or the actual content, not a script. How can I generate the file (or change my method) to restart Nagios only when /etc/nagios3/conf.d/sv-hosts.cfg changes?

2
2

Assuming that each time your script runs it will generate the file exactly the same way for a given hosts file you could make a minor adjustment.

# generate the file into some arbitrary location, that is not actually referenced by nagios
exec{'regen-nagios-hosts':
  command     => '/usr/local/bin/generate-nagios-host-definitions-from-hostfile < /etc/hosts > /etc/nagios3/generatedtmpsv-hosts.cfg',
  user        => 'root',
  before      => Class['nagios::server'],
  require     => File['/etc/hosts'],
}

# have a file resource use the generated file to update the production file
# the update will only happen when the checksums have changed
# on update notify the service to restart.
file { '/etc/nagios3/conf.d/sv-hosts.cfg'':
  ensure => present,
  source => '/etc/nagios3/generatedtmp/sv-hosts.cfg'
  require => Exec['regen-nagios-hosts'],
  notify => Service['nagios3'],
}

In the long term though it might be better to try and find ways to convert your script into a template. Or find some other way that fits better into the puppet way of doing things.

0
0

The problem with your exec definition is that it will change/update the file regardless if the content was actually changed. Have a look at the onlyif in typedef specification

If I were you I would generate the sv-hosts.cfg file in a temporary file and compare it with the current one. If there are some differences just copy the tmp file to the new location and restart the nagios.

Also have a look at the type reference documentation, because there are so many nagios specific types and maybe one of them will do the trick for you :)

1
  • 1
    If you are looking at the onlyif option then something like this might be what you want? onlyif => 'test /etc/hosts -nt /etc/nagios3/conf.d/sv-hosts.cfg'
    – Zoredache
    Aug 16 '12 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.