Dude to nagios with map coordinates

Script to make nagios hosts files from dude export.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import xml.etree.ElementTree as ET
tree = ET.parse(sys.argv[1])
root = tree.getroot()
idip={}
idnazwa={'0':'0'}
idparent ={ '0':'0' }
idcoords={}
for device in root.findall('Device'):
  id = device.find('sys-id').text
  name = device.find('sys-name').text
  addr = device.find('addresses').text
  try:
    parent = device.find('parentIDs').text
  except AttributeError:
    parent = "0"
  idip[id]=addr
  idnazwa[id]=name
  idparent[id]=parent


for element  in root.findall('NetworkMapElement'):
  try:
    itid = element.find('itemID').text
  except AttributeError:
    continue
  x = element.find('itemX').text
  y = element.find('itemY').text
  idcoords[itid]=str(x) +"," +str(y)

for idx in idip:
  #print "ip "+idip[idx] +"   nazwa " +idnazwa[idx] +" rodzic " +idnazwa[(idparent[idx])] +" pozycja " +idcoords[idx]
  f =open("./host_"+idnazwa[idx]+".cfg","w")
  f.write("define host {\n")
  f.write("\tuse host-template\n")
  f.write("\thost_name\t"+idnazwa[idx]+"\n")
  f.write("\talias\t"+idnazwa[idx]+"\n")
  f.write("\taddress\t"+idip[idx]+"\n")
  if ( idparent[idx] != "0"  ):
    f.write("\tparents\t"+idnazwa[(idparent[idx])]+"\n")
  f.write("\t2d_coords\t"+idcoords[idx]+"\n")
  f.write("\tcontact_groups\tadmins\n")
  f.write("}")
  f.close

Damn I wish I would’ve seen this before using nconf to manually enter 200+ devices. After I was done I thought, I should write a perl script to parse the XML backup file for the dude to import this information.

Haven’t used the script yet, but nicely done.

some other weird resources :
http://somewierdthings.blogspot.com/

I’m putting there stuff I used to write some time ago