# 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 import os from kusu.addhost import * from kusu.core.db import KusuDB import sys try: import subprocess except: from popen5 import subprocess cactiCfgScript = '/opt/kusu/lib/cacti/kusu-cacti.php' cactiInstallDir = '/usr/share/cacti' kusuDB = KusuDB() kusuDB.connect() class AddHostPlugin(AddHostPluginBase): def common(self): try: from MySQLdb import * cactiDBCursor = connect(user='root', db = 'cacti').cursor() except: return try: nodeNames = [] kusuDB.execute('select distinct nodes.name ' 'from nics,nodes,nodegroups,ng_has_comp,components ' 'where nics.nid=nodes.nid ' 'and nodes.nid=nics.nid ' 'and nics.boot=True ' 'and nodes.ngid = nodegroups.ngid ' 'and nodegroups.ngid = ng_has_comp.ngid ' 'and ng_has_comp.cid = components.cid ' 'and components.cname = "component-cacti-monitored-node" ' 'and nodes.name != (select kvalue from appglobals where kname="PrimaryInstaller")') nodeNames = [row[0] for row in kusuDB.fetchall()] except: #catch the exception, but do not mark it as a fatal sys.stderr.write('error in sql statement') delNodes = [] try: cactiDBCursor.execute("select distinct hostname from host where hostname != '127.0.0.1'") data = cactiDBCursor.fetchall() for row in data: nodeName = row[0] if nodeName not in nodeNames: delNodes.append(nodeName) # nodes been deleted else: nodeNames.remove(nodeName) # nodes has been already added except: #catch the exception, but do not mark it as fatal sys.stderr.write('error in sql statement') for node in delNodes: p = subprocess.Popen("/usr/bin/php %s --remove_device='%s'" % (cactiCfgScript, node), cwd='%s' % cactiInstallDir, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() p = subprocess.Popen('rm -f %s/rra/%s*.rrd' % (cactiInstallDir, node), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() for node in nodeNames: p = subprocess.Popen("/usr/bin/php %s --add_device='%s'" % (cactiCfgScript, node), cwd='%s' % cactiInstallDir, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() def updated(self): self.common() def finished(self, nodelist, prePopulateMode): self.common()