Skip to content

Instantly share code, notes, and snippets.

@or1gb1u3
Forked from unicolet/reader.py
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save or1gb1u3/2e5188537a23da5c62d0 to your computer and use it in GitHub Desktop.
Save or1gb1u3/2e5188537a23da5c62d0 to your computer and use it in GitHub Desktop.
import csv
from jinja2 import Template,FileSystemLoader,Environment
loader = FileSystemLoader('templates')
env = Environment(loader=FileSystemLoader('templates'))
tmp=[]
hostgroups=set()
hosts = csv.reader(open('hosts.csv'), delimiter=',', quotechar='"')
for h in hosts:
if h[0]!="" and h[0]!="ip":
tmp.append(h) # skip rows w/o ip address
for x in h[3].split(',') : hostgroups.add(x)
hosts=tmp
files = {'hostgroups':'','services':'','hosts':''}
templates = {}
for k in files.keys():
templates[k]=env.get_template(k)
for k in files.keys():
files[k] += templates[k].render(hosts=hosts, hostgroups=hostgroups)
for k in sorted(files.keys(),reverse=True):
print files[k]
{% for hg in hostgroups %}
define hostgroup{
hostgroup_name {{ hg }}
alias {{ hg }}
}
# host hostgroup template
define host{
check_command check-host-alive
notification_options d,u,r
max_check_attempts 5
name {{ hg }}
register 0
}
{% endfor %}
{% for record in hosts %}
define host{
host_name {{ record[1].replace(' ','_') }}
alias {{ record[1] }}
address {{ record[0] }}
hostgroups {{ record[3]}}
use {{ record[3].split(",")[0] }}
}
{% endfor %}
# intentionally left blank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment