#!/usr/bin/env python # 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 path import path import os from primitive.system.software.dispatcher import Dispatcher global COMPONENT_NAME COMPONENT_NAME = 'component-ntop-v3_3' class KusuRC(rcplugin.Plugin): def __init__(self): rcplugin.Plugin.__init__(self) self.name = 'ntop' self.desc = 'Setting up Ntop' self.ngtypes = ['installer', 'compute'] self.delete = False self.ntopConf = path(Dispatcher.get('ntop_conf')) if self.os_name in ['sles', 'opensuse', 'suse']: self.ntopConfig = """ NTOPD_IFACE="%s" NTOPD_PORT="3000" NTOPD_SSL_PORT="3001" NTOP_USER="wwwrun" NTOP_ARGS="" """ elif self.os_name in ['centos', 'rhel']: self.ntopConfig = """ --user ntop --db-file-path /var/lib/ntop --interface %s --use-syslog --https-server 3001 --daemon """ def getAvailableInterfaces(self): """ Return a list of interfaces for the nodegroup """ interfaces = [] node_name = self.dbs.AppGlobals.selectfirst_by(kname='PrimaryInstaller').kvalue nics = self.dbs.Nodes.selectfirst_by(name=node_name).nics for nic in nics: interfaces.append(nic.network.device) if not interfaces: return 'eth0' else: return ','.join(interfaces) def generateNtopConfig(self): """ Generate Ntop configuration for each nodegroup that is associated to the ntop component """ self.ntopConf.write_text(self.ntopConfig % self.getAvailableInterfaces()) def generateCert(self): self.runCommand("""cat << EOF | openssl req -new -x509 -sha1 -extensions v3_ca -nodes -days 365 -out cert.pem 2>/dev/null -- SomeState SomeCity SomeOrganization SomeOrganizationalUnit localhost.localdomain root@localhost.localdomain EOF """) self.runCommand("cat privkey.pem cert.pem > /etc/ntop/ntop-cert.pem") self.runCommand("/bin/rm -f privkey.pem cert.pem") def createAdminUser(self): pw = path('/var/lib/ntop/ntop_pw.db') if not pw.exists(): self.runCommand('/usr/bin/ntop --set-admin-password=admin') def run(self): # Not on compute nodes if not path('/etc/profile.nii').exists(): # Ensure MySQL is running engine = os.getenv('KUSU_DB_ENGINE') if engine == 'mysql': sql_server = Dispatcher.get('mysql_server') else: # postgres for now sql_server = Dispatcher.get('postgres_server') success, (retcode, out, err) = self.service(sql_server, 'start') self.generateNtopConfig() self.generateCert() self.createAdminUser() self.runCommand("service ntop restart >/dev/null 2>&1") return True if __name__ == "__main__": myApp = KusuRC() myApp.run()