Please help me to parse the file of Nagios config via Python script. I got the array with hostnames, and from this need to parse file and get their members. How i can do this?
There is my code :
import os
hostgroups=os.popen('grep hostgroup_name /var/log/nagios/objects.cache | cut -f3').read().split('\n')
for i in hostgroups[:-1]:
print i
how parse file again or what to do to get members. Example see below :
define hostgroup {
hostgroup_name test
alias test
members server1,server2
}
Thanks.
1 Answer 1
Maybe you should parse the file with python instead of grep:
For example:
with open("/var/log/nagios/objects.cache") as nagiosFile:
hostgroups = {}
hostgroup_name = None
for line in nagiosFile.readlines():
if "hostgroup_name" in line:
hostgroup_name = line.split()[-1]
if "members" in line:
for members in line.split():
hostgroups[hostgroup_name] = members.split(",")
print hostgroups
answered Dec 18, 2015 at 16:01
djangoliv
1,79814 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
lang-py