Hey Sirius,
ich hol besser etwas weiter aus.
Code: Alles auswählen
[system_1]
description=Server 1
address=10.113.96.1
port=161
communityro=blubb
[system_2]
description=Server 2
address=10.113.96.2
port=161
communityro=blubb
[check_1]
description=ifInOctets
oid=1.1.1.1.1.1.1.1.1.1.1.1
system=system_1,system_2
[check_2]
description=ifOutOctets
oid=2.2.2.2.2.2.2.2.2.2.2.2
system=system_2,system_3,system_4
Das möchte ich gern in ein Dict importieren.
Aktuell hat add_system keine Funktion, liegt aber nur daran, weil ich es nicht aufrufe.
Mein Ergebnis :
Code: Alles auswählen
{'system_2': {'checks': {'check_2': {'oid': '2.2.2.2.2.2.2.2.2.2.2.2', 'description': 'ifOutOctets'}, 'check_1': {'oid': '1.1.1.1.1.1.1.1.1.1.1.1', 'description': 'ifInOctets'}}}, 'system_3': {'checks': {'check_2': {'oid': '2.2.2.2.2.2.2.2.2.2.2.2', 'description': 'ifOutOctets'}}}, 'system_1': {'checks': {'check_1': {'oid': '1.1.1.1.1.1.1.1.1.1.1.1', 'description': 'ifInOctets'}}}, 'system_4': {'checks': {'check_2': {'oid': '2.2.2.2.2.2.2.2.2.2.2.2', 'description': 'ifOutOctets'}}}}
So sieht mein Programm aus.
Code: Alles auswählen
#!/usr/bin/python
from ConfigParser import SafeConfigParser
#systems = dict()
def read_ini():
parser = SafeConfigParser()
return parser#.read('systems.ini')
def add_systems(parser):
dict_systems= dict()
parser.read('systems.ini')
for system in parser.sections() :
if system.startswith('system'):
dict_systems[system] = dict_systems.get(system, {})
for attribute_names , attribute_value in parser.items(system):
dict_systems[system][attribute_names] = dict_systems.get(attribute_names, attribute_value)
#print dict_systems
return dict_systems
def add_checks(parser,dict_systems):
checks_dict = dict()
systems_in_check = []
# print dict_systems.keys()
for checks in parser.sections() :
if checks.startswith('check'):
x = parser.items(checks)
systems = x[2][1].split(',')
for system in systems:
checks_dict[system] = checks_dict.get(system, {})
for checks in parser.sections() :
if checks.startswith('check'):
x = parser.items(checks)
systems = x[2][1].split(',')
for system in systems:
checks_dict[system]['checks'] = checks_dict.get('check',{})
for checks in parser.sections() :
if checks.startswith('check'):
x = parser.items(checks)
systems = x[2][1].split(',')
for system in systems:
checks_dict[system]['checks'][checks] = checks_dict.get(checks, {})
for checks in parser.sections() :
if checks.startswith('check'):
x = parser.items(checks)
systems = x[2][1].split(',')
for system in systems:
for attribute_names, attribute_value in parser.items(checks):
if attribute_names != 'system':
checks_dict[system]['checks'][checks][attribute_names] = checks_dict.get(attribute_names, attribute_value)
# {system1: {checks: {check_1 : ''
# check_2 : ''}
x=read_ini()
#print add_systems(x)
#print add_systems(x).keys()
#print add_systems(x)
add_checks(x,add_systems(x))
So sieht mein Workaround aus zum erstellen dieser Datenstruktur. Das kann aber ja nicht die Lösung sein
. Ich hoffe es ist jetzt etwas deutlicher ? Im nächsten Schritt führe ich die zwei Dict's zusammen, aber eigentlich war es geplant direkt in Dict_systems zu schreiben.
Gruß Sharky