Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Nagios cfg from Google Docs
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