#!/usr/bin/env python # $Id: ntop.rc.py 155 2009-02-25 14:13:51Z mkchew $ # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA # from path import path from kusu.core import rcplugin from primitive.system.software.dispatcher import Dispatcher # Change this to 1 to see debug logs in /var/log/kusu/kusu.log DEBUG = 0 COMPONENT_NAME = 'component-ntop-v3_3' KIT_COMPONENTS = (COMPONENT_NAME,) class KusuRC(rcplugin.Plugin): def __init__(self): rcplugin.Plugin.__init__(self) self.name = 'cfm-ntop' self.desc = 'Setting up cfm for ntop' self.ngtypes = ['installer'] self.delete = False global DEBUG if DEBUG: import kusu.util.log as kusulog self.logger = kusulog.getKusuLog() self.ntopConfig = {} self.ntopConfig['sles'] = """ NTOPD_IFACE="%s" NTOPD_PORT="3000" NTOPD_SSL_PORT="3001" NTOP_USER="wwwrun" NTOP_ARGS="" """ self.ntopConfig['rhel'] = """ --user ntop --db-file-path /var/lib/ntop --interface %s --use-syslog --https-server 3001 --daemon """ def log(self, msg): try: self.logger.debug(msg) except AttributeError: pass def run(self): global KIT_COMPONENTS associated_ngs, remainder_ngs = self.get_nodegroups(KIT_COMPONENTS) self.remove_dirs(remainder_ngs) if not associated_ngs: self.disable = True # Set up the links and dirs only if the # master component is installed. #global MASTER_COMPONENT #if self.is_installed(MASTER_COMPONENT): self.setup_dirs_and_symlinks(associated_ngs) return True #def is_installed(self, component): # retcode = self.runCommand('rpm -q %s' % component)[0] # return retcode == 0 def getAvailableInterfaces(self, nodegroup): """ Return a list of interfaces for the nodegroup """ interfaces = ','.join([net.device for net in nodegroup.networks if net]) if not interfaces: interfaces = 'eth0' return interfaces def get_nodegroups(self, kit_components): """ Returns a tuple consisting of two sets of nodegroups 1. nodegroups associated with KIT_COMPONENTS 2. remaining nodegroups, i.e. those that are not associated with KIT_COMPONENTS """ associated_nodegroups = self.dbs.NodeGroups.select( self.dbs.NodeGroups.join_to('components') & self.dbs.Components.c.cname.in_(*kit_components) ) all_nodegroups = self.dbs.NodeGroups.select() return (set(associated_nodegroups), set(all_nodegroups) - set(associated_nodegroups)) def setup_dirs_and_symlinks(self, nodegroups): """ Set up directories and symlinks for nodegroups associated with COMPONENTS. """ for ng in nodegroups: repo = ng.repo if not repo or not repo.oskit: continue _os = repo.oskit ntopConf = path(Dispatcher.get('ntop_conf', os_tuple=(_os.rname, _os.version, _os.arch)))[1:] ntpConf = path('/etc/cfm') / ng.ngname / ntopConf self.make_dirs([ntpConf.parent], mode=0755) if _os.rname in ['sles', 'opensuse', 'suse']: rname = 'sles' elif _os.rname in ['rhel', 'centos']: rname = 'rhel' ntpConf.write_text(self.ntopConfig[rname] % self.getAvailableInterfaces(ng)) def make_dirs(self, dirs, mode=0755): for d in dirs: d = path(d) if not d.exists(): self.log('Making directory %s' % d) d.makedirs(mode=mode) def remove_dirs(self, nodegroups): """ Remove the symlinks for those nodegroups that are not associated with KIT_COMPONENTS. """ for ng in nodegroups: ntpConf = path('/etc/cfm/%s/etc/ntop.conf' % ng.ngname) if ntpConf.exists(): ntpConf.remove() ntpConf = path('/etc/cfm/%s/etc/sysconfig/ntop' % ng.ngname) if ntpConf.exists(): ntpConf.remove()