Last active
December 16, 2015 02:19
-
-
Save kinkerl/5362033 to your computer and use it in GitHub Desktop.
converts icinga status files (*.dat) to a json file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
import json | |
tobj = list() | |
statusfile = 'serverstatus.dat' | |
statusfile_output = 'serverstatus.json' | |
currentobj = dict() | |
reg = re.compile(r'^\s*$') | |
with open(statusfile, 'r') as f: | |
for line in f.read().split('\n'): | |
if line.startswith('#'): | |
continue | |
if reg.match(line): | |
continue | |
if '{' in line: | |
currentobj = dict() | |
currentobj['serverstatus-name'] = line[:-2] | |
continue | |
elif '}' in line: | |
if currentobj: | |
tobj.append(currentobj) | |
currentobj = None | |
continue | |
else: | |
if currentobj: | |
key, value = [x.strip() for x in line.split('=',1)] | |
currentobj[key] = value | |
with open(statusfile_output, 'w') as w: | |
json.dump(tobj, w) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment