Guest User

Untitled

a guest
Apr 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. module Puppet::Parser::Functions
  2. newfunction(:nagios_rdiff_backup_config) do |args|
  3. module NagiosCommonTop
  4. return if lookupvar('config_path').empty?
  5.  
  6. hostname = lookupvar('long_name')
  7.  
  8. #generate nagios rdiff backup monitoring config file
  9. service_definition = '# Do not modify this file by hand; it is automatically generated by Puppet.
  10. # Any changes you make here will be over-written by Puppet on its next run.
  11. '
  12.  
  13. nagios_host = `grep 'host_name #{hostname}' #{lookupvar('config_path')}../hosts/hosts.cfg | grep -v ^#`
  14. end
  15. include NagiosCommonTop
  16.  
  17. if nagios_host.empty? then
  18. service_definition += "
  19. # This host is not currently being monitored by nagios
  20. # To enable this service monitor, add the host to
  21. # #{lookupvar('config_path')}../hosts/hosts.cfg
  22. "
  23. else
  24. service_definition += "
  25. define service {
  26. use generic-service
  27. hosts "+hostname+"
  28. service_description New backed-up files
  29. check_command check_rdiff_backup_age!2
  30. }
  31. "
  32. end
  33.  
  34. service_definition = service_definition.gsub(/^\t+/, '') #remove ugly leading tabs
  35. service_definition = service_definition.gsub(/^ /, "\t") #replace leading spaces with tabs
  36.  
  37. original_config = ''
  38. begin
  39. on_disk = File.open(lookupvar('config_path') + hostname + '_rdiff_backup.cfg', File::RDONLY)
  40. original_config = on_disk.readlines.join
  41. rescue
  42. #we don't care if it fails, because "original_file" will already be empty
  43. end
  44.  
  45. if original_config != service_definition
  46. File.open(lookupvar('config_path') + hostname + '_rdiff_backup.cfg', 'w') do |fd|
  47. fd.puts service_definition
  48. end
  49. #we won't check for error handling here, because there's nothing we can do about it if it fails
  50. end
  51.  
  52. end
  53. end
Add Comment
Please, Sign In to add comment