Michał Ł.

Michał Ł. Administrator sieci

Temat: Dude to nagios with map coordinates



#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.dom import minidom
import sys
import os
os.system("echo '<xml>'>temp-dude2nag.xml")
os.system("cat " +sys.argv[1] +"|sed -n '/<Device>/,/<\/Device>/p'>>temp-dude2nag.xml")
os.system("cat " +sys.argv[1] +"|sed -n '/<NetworkMapElement>/,/<\/NetworkMapElement>/p'>>temp-dude2nag.xml")
os.system("echo '</xml>'>>temp-dude2nag.xml")
idip={}
idnazwa={}
idparent ={'brak':'brak'}
idcoords={}
xmldoc = minidom.parse("temp-dude2nag.xml")

elementymapy = xmldoc.getElementsByTagName("NetworkMapElement")


for elementmapy in elementymapy:
try:
idelementu = elementmapy.getElementsByTagName("itemID")[0]
except IndexError:
continue
idelementu = idelementu.firstChild.nodeValue
pozycjax = elementmapy.getElementsByTagName("itemX")[0]
pozycjax = pozycjax.firstChild.nodeValue
xcoord = pozycjax
pozycjay = elementmapy.getElementsByTagName("itemY")[0]
pozycjay = pozycjay.firstChild.nodeValue
ycoord = pozycjay
idcoords[idelementu] = str(pozycjax) +"," +str(pozycjay)

xmldoc = minidom.parse("temp-dude2nag.xml")
urzadzenia = xmldoc.getElementsByTagName("Device")

for urzadzenie in urzadzenia:

id_stacji = urzadzenie.getElementsByTagName("sys-id")[0]
id_stacji = id_stacji.firstChild.nodeValue

nazwa_stacji = urzadzenie.getElementsByTagName("sys-name")[0]
nazwa_stacji = nazwa_stacji.firstChild.nodeValue
idnazwa[id_stacji] = nazwa_stacji
for urzadzenie in urzadzenia:

id_stacji = urzadzenie.getElementsByTagName("sys-id")[0]
id_stacji = id_stacji.firstChild.nodeValue
ip = urzadzenie.getElementsByTagName("addresses")[0]
ip = ip.firstChild.nodeValue

idip[id_stacji] = ip
for urzadzenie in urzadzenia:

id_stacji = urzadzenie.getElementsByTagName("sys-id")[0]
id_stacji = id_stacji.firstChild.nodeValue
try:
parent = urzadzenie.getElementsByTagName("parentIDs")[0]
parent = parent.firstChild.nodeValue
p = idnazwa[parent]
except IndexError:
p = "brak"

idparent[id_stacji] = p


for idx in idnazwa:
print "id " +idx
print "Nazwa " +idnazwa[idx]
print "ip " +idip[idx]
print "parent " +idparent[idx]
print "Koordynaty " +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")
f.write("\tparents\t"+idparent[idx]+"\n")
f.write("\t2d_coords\t"+idcoords[idx]+"\n")
f.write("\tcontact_groups\tadmins\n")
f.write("}")
f.close