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
  • Select an option

  • Save or1gb1u3/2e5188537a23da5c62d0 to your computer and use it in GitHub Desktop.

Select an option

Save or1gb1u3/2e5188537a23da5c62d0 to your computer and use it in GitHub Desktop.

Revisions

  1. @unicolet unicolet revised this gist Jun 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reader.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    hostgroups=set()
    hosts = csv.reader(open('hosts.csv'), delimiter=',', quotechar='"')
    for h in hosts:
    if h[0]!="" and h[0]!="ip"::
    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
  2. @unicolet unicolet revised this gist Jun 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reader.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    hosts = csv.reader(open('hosts.csv'), delimiter=',', quotechar='"')
    for h in hosts:
    if h[0]!="" and h[0]!="ip"::
    tmp.append(h) # skim rows w/o ip address
    tmp.append(h) # skip rows w/o ip address
    for x in h[3].split(',') : hostgroups.add(x)
    hosts=tmp

  3. @unicolet unicolet revised this gist Jun 27, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions templates_services
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # intentionally left blank
  4. @unicolet unicolet revised this gist Jun 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reader.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    hostgroups=set()
    hosts = csv.reader(open('hosts.csv'), delimiter=',', quotechar='"')
    for h in hosts:
    if h[0]!="":
    if h[0]!="" and h[0]!="ip"::
    tmp.append(h) # skim rows w/o ip address
    for x in h[3].split(',') : hostgroups.add(x)
    hosts=tmp
  5. @unicolet unicolet revised this gist Jun 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion templates_hosts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    % for record in hosts %}
    {% for record in hosts %}
    define host{
    host_name {{ record[1].replace(' ','_') }}
    alias {{ record[1] }}
  6. @unicolet unicolet revised this gist Jun 27, 2012. 2 changed files with 25 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions templates_hostgroups
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {% 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 %}
    9 changes: 9 additions & 0 deletions templates_hosts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    % 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 %}
  7. @unicolet unicolet created this gist Jun 27, 2012.
    25 changes: 25 additions & 0 deletions reader.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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]!="":
    tmp.append(h) # skim 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]