Sha256: e0e0f933ae84d586c4b3b27ec6f0aa745e9ad1f50e176bcf1e090cdefc256a0f

Contents?: true

Size: 1.08 KB

Versions: 19

Compression:

Stored size: 1.08 KB

Contents

#!/usr/bin/env ruby
#
# Generate Nagios hosts files from unix hosts file
#
# e.g.
# 10.1.1.1 gw.failmode.com
# 10.1.1.5 mail.failmode.com 

require 'erb'

hosts_file = ARGV[0] || '/etc/hosts'

template = ERB.new <<-EOF
define host{
  use        generic-host
  hostgroups linux-servers
  host_name  <%= host_name %>
  address    <%= ip %>
}
EOF

ValidIpAddressRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;

ValidHostnameRegex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/;

# XXX Add hostgroups to hosts
# HOSTGROUPS = 'linux-servers', 'windows-servers', 'debian-servers', 'vmware-hosts'

open(hosts_file).each do |line| 
  ip, host_name = line.sub(/#.*/,'').split[0,2]
  hostgroups = []


  if ip =~ ValidIpAddressRegex and host_name =~ ValidHostnameRegex
    filename = host_name + '.cfg'
    File.open(filename, 'w') do |file|
      file.write template.result(binding)
      puts "writing #{filename}"
    end
  else
    puts "not writing anything for '#{line}'"
  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
deprec-3.1.0.rc16 bin/hosts.rb
deprec-3.1.0.rc15 bin/hosts.rb
deprec-3.1.0.rc13 bin/hosts.rb
deprec-3.1.0.rc12 bin/hosts.rb
deprec-3.1.0.rc11 bin/hosts.rb
deprec-3.1.0.rc10 bin/hosts.rb
deprec-3.1.0.rc9 bin/hosts.rb
deprec-3.1.0.rc8 bin/hosts.rb
deprec-3.1.0.rc7 bin/hosts.rb
deprec-3.1.0.rc6 bin/hosts.rb
deprec-3.1.0.rc5 bin/hosts.rb
deprec-3.1.0.rc4 bin/hosts.rb
deprec-3.1.0.rc3 bin/hosts.rb
deprec-3.1.0.rc2 bin/hosts.rb
deprec-3.1.0.rc1 bin/hosts.rb
deprec-2.2.3 bin/hosts.rb
deprec-2.2.2 bin/hosts.rb
deprec-2.2.1 bin/hosts.rb
deprec-2.2.0 bin/hosts.rb