Remove member from hostgroups?

110 views
Skip to first unread message

Ruslan Valiyev

unread,
Apr 7, 2014, 5:20:20 AM4/7/14
to pynag-...@googlegroups.com
Hi all,

I'm trying to implement a function to remove a host from hosts.cfg and hostgroups.cfg (and possible services.cfg)

Currently, I only managed to delete hosts from hosts.cfg

for i in Model.Host.objects.filter(host_name='host.foo.com'):
    i.delete()

I can't figure out how to do the same and remove 'host.foo.com' from all hostgroups where the host is present.

rv

Ruslan Valiyev

unread,
Apr 7, 2014, 7:54:33 AM4/7/14
to pynag-...@googlegroups.com
Turned out I was using a pretty outdated version.

Apparently, removing hosts is easier than I realized.

>>> from pynag.Parsers import config
>>> nc = config('/usr/local/nagios/etc/nagios.cfg')
>>> nc.parse()
>>> nc.delete_host('host.foo.com')
True
>>> nc.commit()

And thanks to this script, I think I get the idea of how to remove the host from the groups now. It's about manipulating the following list: c.get_hostgroup('mygroup')['members'].split(',')

Ruslan Valiyev

unread,
Apr 7, 2014, 8:31:05 AM4/7/14
to pynag-...@googlegroups.com
And it's done

#!/usr/bin/env python
import sys
from pynag.Parsers import config

hostname = sys.argv[1]

class NagiosTools:
    def __init__(self):
        self.nc = config('/usr/local/nagios/etc/nagios.cfg')
        self.nc.parse()

    def removeHost(self, hostname):
        host_obj = self.nc.get_host(hostname)
        if not host_obj:
            sys.stderr.write('Host %s not found\n' % hostname)
            sys.exit(2)

        # Remove from hosts.cfg
        print 'Removing %s...' % hostname
        self.nc.delete_host(hostname)

        # Remove from hostgroups.cfg
        for i in self.nc['all_hostgroup']:
            if hostname in i['members']:
                match_group = self.nc.get_hostgroup(i['hostgroup_name'])
                print 'Removing %s from %s' % (hostname, i['hostgroup_name'])
                new_group = match_group['members'].split(',')
                new_group.remove(hostname)
                new_group.sort()

                self.nc['all_hostgroup'].remove(match_group)
                match_group['members'] = ','.join(new_group)
                match_group['meta']['needs_commit'] = True
                self.nc['all_hostgroup'].append(match_group)
                self.nc.commit()

c = NagiosTools()
c.removeHost(hostname)

Páll Sigurðsson

unread,
Apr 7, 2014, 9:26:59 AM4/7/14
to pynag-...@googlegroups.com
Even better, in recent version of pynag your original code works and it cleans up references like removing from hostgroup :)



--
You received this message because you are subscribed to the Google Groups "pynag-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pynag-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruslan Valiyev

unread,
Apr 7, 2014, 1:52:02 PM4/7/14
to pynag-...@googlegroups.com
Wow, I did not see that coming! Great stuff, thanks!

rv
Reply all
Reply to author
Forward
0 new messages