1

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.

asked Dec 18, 2015 at 15:49

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, this. It's can help. But issue in the next - some hosgroups didn't have any members, so how i know, this code will be crashed, no?
This code will not crash but the result will be false. This is just an example... You need to adapt it for your specific need.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.