backend = FileBackend(objectFilename, statusFilename) mapNames = [] mapNodes = doc.xpath("/balbec/map") for mapNode in mapNodes: mapName = mapNode.get("name") if mapName in mapNames: raise Exception('Map "'+mapName+'" is defined more than once.') else: mapNames.append(mapName) map = Map(mapName) hostgroups = [] filters = [] hostgroupNodes = mapNode.xpath("hostgroup") for hostgroupNode in hostgroupNodes: name = hostgroupNode.text hostgroups.append(name) filterNodes = mapNode.xpath("filter") for filterNode in filterNodes: name = filterNode.text
socketPath = doc.xpath("/balbec/nagios/livestatus/socket_path")[0].text backend = LivestatusBackend(socketPath) mapNames = [] mapNodes = doc.xpath("/balbec/map") for mapNode in mapNodes: mapName = mapNode.get("name") if mapName in mapNames: raise Exception('Map "'+mapName+'" is defined more than once.') else: mapNames.append(mapName) map = Map(mapName) hostgroups = [] expression = self.buildExpression(mapNode) #self.printExpression(expression) map.expression = expression maps.append(map) return maps, backend def buildExpression(self, node): expression = []
class XmlHandler: def __init__(self, documentRoot): self.documentRoot = documentRoot def readConfig(self): maps = [] schemaFile = open(ROOT("schema/config.xsd"), 'r') schemaDoc = etree.parse(schemaFile) schema = etree.XMLSchema(schemaDoc) configFile = open(os.path.join(self.documentRoot, 'config.xml'), 'r') try: doc = etree.parse(configFile) schema.assertValid(doc) except etree.XMLSyntaxError, e: raise Exception('Invalid Config file: "' + str(e) + '"') mysqlNodes = doc.xpath("/balbec/nagios/ndo2db") filesNodes = doc.xpath("/balbec/nagios/files") livestatusNodes = doc.xpath("/balbec/nagios/livestatus") if len(mysqlNodes) == 1: from balbec.mysqlbackend import MysqlBackend mysql = MysqlBackend() mysqlNode = doc.xpath("/balbec/nagios/ndo2db")[0] databaseNodes = mysqlNode.xpath('database') if len(databaseNodes) == 1: mysql.database = databaseNodes[0].text hostnameNodes = mysqlNode.xpath('hostname') if len(hostnameNodes) == 1: mysql.hostname = hostnameNodes[0].text usernameNodes = mysqlNode.xpath('username') if len(usernameNodes) == 1: mysql.username = usernameNodes[0].text passwordNodes = mysqlNode.xpath('password') if len(passwordNodes) == 1 and passwordNodes[0] != None: mysql.password = passwordNodes[0].text prefixNodes = mysqlNode.xpath('prefix') if len(prefixNodes) == 1 and prefixNodes[0] != None: mysql.prefix = prefixNodes[0].text mysql.connect() backend = mysql elif len(filesNodes) == 1: from balbec.filebackend import FileBackend objectFilename = doc.xpath( "/balbec/nagios/files/object_file")[0].text statusFilename = doc.xpath( "/balbec/nagios/files/status_file")[0].text backend = FileBackend(objectFilename, statusFilename) elif len(livestatusNodes) == 1: from balbec.livestatusbackend import LivestatusBackend socketPath = doc.xpath( "/balbec/nagios/livestatus/socket_path")[0].text backend = LivestatusBackend(socketPath) mapNames = [] mapNodes = doc.xpath("/balbec/map") for mapNode in mapNodes: mapName = mapNode.get("name") if mapName in mapNames: raise Exception('Map "' + mapName + '" is defined more than once.') else: mapNames.append(mapName) map = Map(mapName) hostgroups = [] expression = self.buildExpression(mapNode) #self.printExpression(expression) map.expression = expression maps.append(map) return maps, backend