Parsing icinga conf files in python -


i'm trying write wrapper icinga2 instance. objets inside config files this;

object object_type "object_name" {   some_property = "some_value" } 

example;

object host "server1" {   import "generic-host"   address = "192.168.0.1"   vars.os = "linux" }  object host "server2" {   import "generic-host"   address = "192.168.0.2"   vars.os = "linux" } 

i'm looking like:

icinga = icinga("/etc/icinga2/conf.d/hosts.conf")  print icinga.hosts_list()  icinga.hosts_add("server3","192.168.0.3") icinga.hosts_remove("server1") 

so tried using pynag, like;

nc = config('/etc/icinga2/conf.d/hosts.conf') nc.parse() print nc.get_host('server1') 

but i'm getting;

file "./icinga.py", line 51, in <module>     print nc.get_host('server1')   file "/library/python/2.7/site-packages/pynag/parsers/__init__.py", line 1259, in get_host     return self.get_object('host', object_name, user_key=user_key)   file "/library/python/2.7/site-packages/pynag/parsers/__init__.py", line 1238, in get_object     item in self.data['all_%s' % object_type]: keyerror: 'all_host' 

is there easy way work kind of formar?

using pyparsing, not hard work through structured text format this.

here parser might like:

from pyparsing import (suppress, keyword, word, alphas, alphanums, combine,      oneormore, quotedstring, removequotes, group, zeroormore)  lbrace,rbrace,eq = map(suppress, "{}=")  object = keyword("object") import = keyword("import")  ident = word(alphas, alphanums) dottedident = combine(ident + oneormore("." + ident))  quotedstring.setparseaction(removequotes)  propertydefn = group((dottedident | ident)("name") + eq + quotedstring("value"))  importdirective = group(import + quotedstring('source'))  objectbodydefn = group(zeroormore(propertydefn("properties*") |                                    importdirective("imports*")))  objectdefn = group(object + ident("type") + quotedstring("name") +                     lbrace + objectbodydefn("body") + rbrace)  parser = zeroormore(objectdefn) 

here's how apply parser , access parsed data:

# parsing sample, , accessing parsed data fields obj in parser.parsestring(sample):     print(obj.dump())     print("%(name)s (%(type)s)" % obj)     print("imports:", ','.join(imp.source imp in obj.body.imports))     print("properties:")     if obj.body.properties:         prop in obj.body.properties:             print('-', prop.name, ':', prop.value)     else:         print(' ','<none>')     print() 

with output:

['object', 'host', 'server1', [['import', 'generic-host'], ['address', '192.168.0.1'], ['vars.os', 'linux']]] - body: [['import', 'generic-host'], ['address', '192.168.0.1'], ['vars.os', 'linux']]   - imports:      [0]:       ['import', 'generic-host']       - source: generic-host   - properties:      [0]:       ['address', '192.168.0.1']       - name: address       - value: 192.168.0.1     [1]:       ['vars.os', 'linux']       - name: vars.os       - value: linux - name: server1 - type: host server1 (host) imports: generic-host properties: - address : 192.168.0.1 - vars.os : linux  ['object', 'host', 'server2', [['import', 'generic-host'], ['address', '192.168.0.2'], ['vars.os', 'linux']]] - body: [['import', 'generic-host'], ['address', '192.168.0.2'], ['vars.os', 'linux']]   - imports:      [0]:       ['import', 'generic-host']       - source: generic-host   - properties:      [0]:       ['address', '192.168.0.2']       - name: address       - value: 192.168.0.2     [1]:       ['vars.os', 'linux']       - name: vars.os       - value: linux - name: server2 - type: host server2 (host) imports: generic-host properties: - address : 192.168.0.2 - vars.os : linux 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -