#!/usr/bin/env python # Copyright (C) 2007 Platform Computing Inc. # # 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 # # # $Id$ # import os from Cheetah.Template import Template from kusu.addhost import * db_available = True try: import kusu.core.database as db import kusu.core.db except ImportError: db_available = False def createDB(): engine = os.getenv('KUSU_DB_ENGINE') if engine == 'mysql': dbdriver = 'mysql' else: dbdriver = 'postgres' dbdatabase = 'kusudb' dbuser = 'apache' dbpassword = 'None' return db.DB(dbdriver, dbdatabase, dbuser, dbpassword) from path import path global COMPONENT_NAME COMPONENT_NAME = "component-nagios-installer-v2_12" class AddHostPlugin(AddHostPluginBase): def enabled(self): return True def scriptGenerate(self): self.nodegroup_tpl = '/etc/nagios/nodes/nodegroup.tpl' self.node_tpl = '/etc/nagios/nodes/node.tpl' if db_available: self.dbs = createDB() self.dbconn = kusu.core.db.KusuDB() self.dbconn.connect('kusudb') #self.componentNodegroups = self.checkAvailableComponent() self.generate() def finished(self, nodeList, prePopulateMode): if db_available: self.dbs = createDB() self.generate() os.system("cfmsync -f") print "Restarting Nagios..." os.system("service nagios reload") def generate(self): self.componentNodegroups = list(self.checkAvailableComponent()) nodegroups = self.dbs.NodeGroups.select() self.node_cfg_dir = path() self.orig_cfg_dir = path() self.nodegroup_tpl = '/etc/nagios/nodes/nodegroup.tpl' self.node_tpl = '/etc/nagios/nodes/node.tpl' self.command_template_dir = path('/etc/nagios/commands') self.generate_nodegroups(nodegroups) nodes_and_types = [] for nodegroup in nodegroups: # If nodegroups is inside self.componentNodegroups then we generate a 'installer_%s.cfg' file instead of a node. if nodegroup.ngname in self.componentNodegroups: for node in nodegroup.nodes: nodes_and_types.append((node, 'installer')) else: for node in nodegroup.nodes: nodes_and_types.append((node, nodegroup.type)) self.clean_all_nodes() self.generate_nodes(nodes_and_types, str(self.node_tpl)) def clean_all_nodes(self): # Clean up existing node config files. try: for cfg_file in self.node_cfg_dir.walkfiles(pattern='node_*.cfg'): cfg_file.remove() for orig_file in self.orig_cfg_dir.walkfiles(pattern='node_*.cfg'): orig_file.remove() except: pass # We cant fail, the directories dont exist yet. def generate_nodegroups(self, nodegroups): #, nodegroup_template): for componentng in self.componentNodegroups: for nodegroup in nodegroups: self.orig_cfg_dir = path('/etc/nagios/nodes') self.node_cfg_dir = path('/etc/cfm/%s/etc/nagios/nodes' % componentng) if nodegroup.nodes: nodegroup_cfg_filename = \ self.node_cfg_dir / ('nodegroup_%s.cfg' % nodegroup.ngname) nodegroup_orig_filename = \ self.orig_cfg_dir / ('nodegroup_%s.cfg' % nodegroup.ngname) if nodegroup_cfg_filename.exists(): nodegroup_cfg_filename.remove() if nodegroup_orig_filename.exists(): nodegroup_orig_filename.remove() fptr = open(nodegroup_cfg_filename, 'w') fptr.write(self.generate_nodegroup(nodegroup, str(self.nodegroup_tpl))) fptr.close() else: nodegroup_cfg_filename = \ self.node_cfg_dir / ('nodegroup_%s.cfg' % nodegroup.ngname) nodegroup_orig_filename = \ self.orig_cfg_dir / ('nodegroup_%s.cfg' % nodegroup.ngname) if nodegroup_cfg_filename.exists(): nodegroup_cfg_filename.remove() if nodegroup_orig_filename.exists(): nodegroup_orig_filename.remove() def generate_nodes(self, nodes_and_types, node_template): for node_and_type in nodes_and_types: node = node_and_type[0] type = node_and_type[1] node_cfg_filename = self.node_cfg_dir / ('node_%s.cfg' % node.name) f = open(node_cfg_filename, 'w') f.write(self.generate_node(node, type, node_template)) f.close() def generate_nodegroup(self, nodegroup, nodegroup_template): d = {} d['nodegroup_name'] = nodegroup.ngname d['nodegroup_description'] = nodegroup.ngname d['nodegroup_nodes'] = ', '.join(node.name for node in nodegroup.nodes) t = Template(file=nodegroup_template, searchList=[d]) return str(t) def generate_node(self, node, type, node_template): d = {} d['node_name'] = node.name d['node_alias'] = node.name d['node_ip'] = node.name d['service_definitions'] = \ '\n'.join([self.generate_service(node, str(service_template)) \ for service_template \ in self.command_template_dir.walkfiles(pattern='*.tpl') if service_template.basename().startswith('check') or service_template.basename().startswith(type)]) t = Template(file=node_template, searchList=[d]) return str(t) def generate_service(self, node, service_template): d = {} d['node_name'] = node.name d['node_alias'] = node.name d['node_ip'] = node.name t = Template(file=service_template, searchList=[d]) return str(t) def checkAvailableComponent(self): """ Return a list of all nodegroups that have the nagios components in use """ items = [] query = ('select nodegroups.ngname from nodegroups, ng_has_comp, components where ' 'nodegroups.ngid = ng_has_comp.ngid and ng_has_comp.cid = components.cid and ' 'components.cname = "%s"' % COMPONENT_NAME) try: self.dbconn.execute(query) for item in self.dbconn.fetchall(): items.append(item[0]) return items except: return [] # FIXME: We have to call this plugin as a script when installing the nagios kit because we dont have any genconfig scripts. So to mimic this, call this # as a script instead. if __name__ == "__main__": myApp = AddHostPlugin("test") myApp.scriptGenerate()