Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
What's in the file? –  Shane Madden Aug 16 '12 at 3:28
    
Can you use the concatenation pattern instead? –  larsks Aug 16 '12 at 3:35

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Ah exactly what I was looking for! Thanks! –  François Beausoleil Aug 16 '12 at 15:37

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

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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