#!/usr/bin/env python # # $Id: gangliaconfig.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 kusu.core import rcplugin from kusu.core.db import KusuDB from primitive.system.software.dispatcher import Dispatcher from path import path import os import shutil # Change this to 1 to see debug logs in /var/log/kusu/kusu.log DEBUG = 0 MASTER_COMPONENT = "component-ganglia-server-v3_1_1" COMPUTE_COMPONENT = "component-ganglia-agent-v3_1_1" KIT_COMPONENTS = (MASTER_COMPONENT, COMPUTE_COMPONENT) class KusuRC(rcplugin.Plugin): def __init__(self): rcplugin.Plugin.__init__(self) self.name = 'cfm-ganglia' self.desc = 'Setting up cfm for ganglia' self.ngtypes = ['installer'] self.delete = False global DEBUG if DEBUG: import kusu.util.log as kusulog self.logger = kusulog.getKusuLog() 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 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 get_ng_device(self, ng): for network in ng.networks: if network.type == 'provision' and not network.device == 'bmc': return network.device def setup_dirs_and_symlinks(self, nodegroups): """ Set up directories and symlinks for nodegroups associated with COMPONENTS. """ for ng in nodegroups: DIRS = ['/etc/cfm/%s/etc/ganglia' % ng.ngname] self.make_dirs(DIRS, mode=0755) # If ng is installer then don't copy make symlink only if ng.ngid == 1: FILES = ['/etc/ganglia/gmond.conf', '/etc/ganglia/gmetad.conf'] self.make_symlinks(FILES, ng.ngname) else: shutil.copy("/etc/ganglia/gmond.conf", "/etc/cfm/%s/etc/ganglia/gmond.conf" % ng.ngname) # Get the interfaces available for nodegroup dev = self.get_ng_device(ng) #query = ('select device from networks, nodegroups, ng_has_net ' #'WHERE networks.type="provision" AND ng_has_net.netid=networks.netid ' #'AND ng_has_net.ngid=nodegroups.ngid AND nodegroups.ngname="%s" ' #'AND NOT networks.device="bmc"' % ng.ngname) if ng.ngid == 1: fptr = open("/etc/ganglia/gmond.conf", 'a') rfptr = open("/etc/ganglia/gmond.conf", 'r') else: fptr = open("/etc/cfm/%s/etc/ganglia/gmond.conf" % ng.ngname, 'a') rfptr = open("/etc/cfm/%s/etc/ganglia/gmond.conf" % ng.ngname, 'r') #self.dbconn.execute(query) data = [] #for dev in self.dbconn.fetchall(): flag = 0 mcastInfo = """ udp_send_channel { mcast_join = 239.2.11.71 port = 8649 ttl = 1 mcast_if = %s } udp_recv_channel { mcast_join = 239.2.11.71 port = 8649 bind = 239.2.11.71 mcast_if = %s } """ % (dev, dev) #""" % (dev[0], dev[0]) # If the lines exist, don't write again data = rfptr.readlines() rfptr.seek(0,0) for line in data: if line.find(dev) > 0: flag = 1 break if flag: continue else: fptr.writelines(mcastInfo) fptr.close() rfptr.close() 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 make_symlinks(self, files, ngname): for f in files: source = path(f) dest = path('/etc/cfm/%s%s' % (ngname, f)) if not dest.islink(): self.log('Symlink from %s to %s' % (source, dest)) source.symlink(dest) def remove_dirs(self, nodegroups): """ Remove the symlinks for those nodegroups that are not associated with KIT_COMPONENTS. """ for ng in nodegroups: d = path('/etc/cfm/%s/etc/ganglia' % ng.ngname) if d.exists(): self.log('Removing directory %s' % d) d.rmtree() d = path('/opt/kusu/cfm/%d/etc/ganglia' % ng.ngid) if d.exists(): self.log('Removing directory %s' % d) d.rmtree()