def monitorUninstallation(system, component, cpu=None, hostname=None): """ Register the uninstallation of a component in the ComponentMonitoringDB """ monitoringClient = ComponentMonitoringClient() # Retrieve user uninstalling the component result = getProxyInfo() if result['OK']: user = result['Value']['username'] else: return result if not user: user = '******' if not cpu: cpu = 'Not available' for line in open('/proc/cpuinfo'): if line.startswith('model name'): cpu = line.split(':')[1][0:64] cpu = cpu.replace('\n', '').lstrip().rstrip() if not hostname: hostname = socket.getfqdn() instance = component[0:32] result = monitoringClient.updateInstallations \ ( { 'Instance': instance, 'UnInstallationTime': None }, { 'System': system }, { 'HostName': hostname, 'CPU': cpu }, { 'UnInstallationTime': datetime.datetime.utcnow(), 'UnInstalledBy': user } ) return result
def manageService(self, service, action): """ Manage services running on this machine usage: service <action> <serviceName> """ client = ComponentMonitoringClient() result = client.getInstallations({'UninstallationTime': None}, { 'System': 'External', 'Module': service, 'Type': 'External' }, {'HostName': self.host}, False) if not result['OK']: self._errMsg(result['Message']) return elif len(result['Value']) < 1: self._errMsg('%s is not installed' % (service)) return client = SystemAdministratorClient(self.host, self.port) if action == 'start': result = client.startService(service) elif action == 'stop': result = client.stopService(service) elif action == 'restart': result = client.restartService(service) elif action == 'status': result = client.statusService(service) if not result['OK']: self._errMsg(result['Message']) return gLogger.notice(result['Value'])
def monitorInstallation(componentType, system, component, module=None, cpu=None, hostname=None): """ Register the installation of a component in the ComponentMonitoringDB """ monitoringClient = ComponentMonitoringClient() if not module: module = component # Retrieve user installing the component result = getProxyInfo() if result['OK']: user = result['Value']['username'] else: return result if not user: user = '******' if not cpu: cpu = 'Not available' for line in open('/proc/cpuinfo'): if line.startswith('model name'): cpu = line.split(':')[1][0:64] cpu = cpu.replace('\n', '').lstrip().rstrip() if not hostname: hostname = socket.getfqdn() instance = component[0:32] result = monitoringClient.installationExists \ ( { 'Instance': instance, 'UnInstallationTime': None }, { 'Type': componentType, 'System': system, 'Module': module }, { 'HostName': hostname, 'CPU': cpu } ) if not result['OK']: return result if result['Value']: return S_OK('Monitoring of %s is already enabled' % component) result = monitoringClient.addInstallation \ ( { 'InstallationTime': datetime.datetime.utcnow(), 'InstalledBy': user, 'Instance': instance }, { 'Type': componentType, 'System': system, 'Module': module }, { 'HostName': hostname, 'CPU': cpu }, True ) return result
def main(): global force from DIRAC.FrameworkSystem.Client.ComponentInstaller import gComponentInstaller gComponentInstaller.exitOnError = True Script.registerSwitch("f", "force", "Forces the removal of the logs", setForce) Script.parseCommandLine() args = Script.getPositionalArgs() if len(args) == 1: args = args[0].split('/') if len(args) < 2: Script.showHelp(exitCode=1) system = args[0] component = args[1] monitoringClient = ComponentMonitoringClient() result = monitoringClient.getInstallations({'Instance': component, 'UnInstallationTime': None}, {'System': system}, {'HostName': socket.getfqdn()}, True) if not result['OK']: gLogger.error(result['Message']) DIRACexit(1) if len(result['Value']) < 1: gLogger.warn('Given component does not exist') DIRACexit(1) if len(result['Value']) > 1: gLogger.error('Too many components match') DIRACexit(1) removeLogs = False if force: removeLogs = True else: if result['Value'][0]['Component']['Type'] in gComponentInstaller.componentTypes: result = promptUser('Remove logs?', ['y', 'n'], 'n') if result['OK']: removeLogs = result['Value'] == 'y' else: gLogger.error(result['Message']) DIRACexit(1) result = gComponentInstaller.uninstallComponent(system, component, removeLogs) if not result['OK']: gLogger.error(result['Message']) DIRACexit(1) result = MonitoringUtilities.monitorUninstallation(system, component) if not result['OK']: gLogger.error(result['Message']) DIRACexit(1) gLogger.notice('Successfully uninstalled component %s/%s' % (system, component)) DIRACexit()
def getListOfHosts(): """ Return the list of all hosts that constitute a DIRAC instance """ client = ComponentMonitoringClient() result = client.getHosts({}, False, False) if result['OK']: hosts = [host['HostName'] for host in result['Value']] return S_OK(hosts) return S_ERROR('Cannot get list of hosts: %s' % result['Message'])
def export_getHostInfo(self): """ Retrieve host parameters """ client = ComponentMonitoringClient() result = client.getLog(socket.getfqdn()) if result['OK']: return S_OK(result['Value'][0]) return self.__readHostInfo()
def monitorUninstallation(system, component, cpu=None, hostname=None): """ Register the uninstallation of a component in the ComponentMonitoringDB """ monitoringClient = ComponentMonitoringClient() # Retrieve user uninstalling the component user = None result = getProxyInfo() if result["OK"]: proxyInfo = result["Value"] if "username" in proxyInfo: user = proxyInfo["username"] else: return result if not user: user = "******" if not cpu: cpu = "Not available" for line in open("/proc/cpuinfo"): if line.startswith("model name"): cpu = line.split(":")[1][0:64] cpu = cpu.replace("\n", "").lstrip().rstrip() if not hostname: hostname = socket.getfqdn() instance = component[0:32] result = monitoringClient.updateInstallations( { "Instance": instance, "UnInstallationTime": None }, {"DIRACSystem": system}, { "HostName": hostname, "CPU": cpu }, { "UnInstallationTime": datetime.datetime.utcnow(), "UnInstalledBy": user }, ) return result
def __storeHostInfo(): """ Retrieves and stores into a MySQL database information about the host """ result = SystemAdministratorHandler.__readHostInfo() if not result['OK']: gLogger.error(result['Message']) return result fields = result['Value'] fields['Timestamp'] = datetime.utcnow() fields['Extension'] = fields['Extensions'] client = ComponentMonitoringClient() result = client.updateLog(socket.getfqdn(), fields) if not result['OK']: gLogger.error(result['Message']) return result return S_OK('Profiling information logged correctly')
def setUp(self): self.host = 'localhost' self.notificationPort = 9154 self.rootPwd = '' self.csClient = CSAPI() self.monitoringClient = ComponentMonitoringClient() self.client = SystemAdministratorClientCLI(self.host) self.csClient.downloadCSData() result = self.csClient.getCurrentCFG() if not result['OK']: raise Exception(result['Message']) cfg = result['Value'] setup = cfg.getOption('DIRAC/Setup', 'JenkinsSetup') self.frameworkSetup = cfg.getOption('DIRAC/Setups/' + setup + '/Framework') self.rootPwd = cfg.getOption('Systems/Databases/Password') self.diracPwd = self.rootPwd
def setUp(self): self.host = "localhost" self.notificationPort = 9154 self.rootPwd = "" self.csClient = CSAPI() self.monitoringClient = ComponentMonitoringClient() self.client = SystemAdministratorClientCLI(self.host) self.csClient.downloadCSData() result = self.csClient.getCurrentCFG() if not result["OK"]: raise Exception(result["Message"]) cfg = result["Value"] setup = cfg.getOption("DIRAC/Setup", "dirac-JenkinsSetup") self.frameworkSetup = cfg.getOption("DIRAC/Setups/" + setup + "/Framework") self.rootPwd = cfg.getOption("Systems/Databases/Password") self.diracPwd = self.rootPwd result = getProxyInfo() if not result["OK"]: raise Exception(result["Message"]) chain = result["Value"]["chain"] result = chain.getCertInChain(-1) if not result["OK"]: raise Exception(result["Message"]) result = result["Value"].getSubjectDN() if not result["OK"]: raise Exception(result["Message"]) userDN = result["Value"] result = getUsernameForDN(userDN) if not result["OK"]: raise Exception(result["Message"]) self.user = result["Value"] if not self.user: self.user = "******"
def setUp(self): self.host = 'localhost' self.notificationPort = 9154 self.rootPwd = '' self.csClient = CSAPI() self.monitoringClient = ComponentMonitoringClient() self.client = SystemAdministratorClientCLI(self.host) self.csClient.downloadCSData() result = self.csClient.getCurrentCFG() if not result['OK']: raise Exception(result['Message']) cfg = result['Value'] setup = cfg.getOption('DIRAC/Setup', 'dirac-JenkinsSetup') self.frameworkSetup = cfg.getOption('DIRAC/Setups/' + setup + '/Framework') self.rootPwd = cfg.getOption('Systems/Databases/Password') self.diracPwd = self.rootPwd result = getProxyInfo() if not result['OK']: raise Exception(result['Message']) chain = result['Value']['chain'] result = chain.getCertInChain(-1) if not result['OK']: raise Exception(result['Message']) result = result['Value'].getSubjectDN() if not result['OK']: raise Exception(result['Message']) userDN = result['Value'] result = getUsernameForDN(userDN) if not result['OK']: raise Exception(result['Message']) self.user = result['Value'] if not self.user: self.user = '******'
def monitorUninstallation( system, component, cpu = None, hostname = None ): """ Register the uninstallation of a component in the ComponentMonitoringDB """ monitoringClient = ComponentMonitoringClient() if not cpu: cpu = 'Not available' for line in open( '/proc/cpuinfo' ): if line.startswith( 'model name' ): cpu = line.split( ':' )[1][0:64] cpu = cpu.replace( '\n', '' ).lstrip().rstrip() if not hostname: hostname = socket.getfqdn() instance = component[ 0 : 32 ] result = monitoringClient.updateInstallations \ ( { 'Instance': instance, 'UnInstallationTime': None }, { 'System': system }, { 'HostName': hostname, 'CPU': cpu }, { 'UnInstallationTime': datetime.datetime.utcnow() } ) return result
for db in allDB: # Check for DIRAC only databases if db in availableDB.keys() and db != 'InstalledComponentsDB': # Check for 'installed' databases isSection = cfg.isSection \ ( 'Systems/' + availableDB[ db ][ 'System' ] + '/' + cfg.getOption( 'DIRAC/Setups/' + setup + '/' + availableDB[ db ][ 'System' ] ) + '/Databases/' + db + '/' ) if isSection: record = {'Installation': {}, 'Component': {}, 'Host': {}} record['Component']['System'] = availableDB[db]['System'] record['Component']['Module'] = db record['Component']['Type'] = 'DB' record['Host']['HostName'] = host record['Host']['CPU'] = cpu record['Installation']['Instance'] = db record['Installation'][ 'InstallationTime'] = datetime.utcnow() records.append(record) monitoringClient = ComponentMonitoringClient() # Add the installations to the database for record in records: result = monitoringClient.addInstallation \ ( record[ 'Installation' ], record[ 'Component' ], record[ 'Host' ], True ) if not result['OK']: gLogger.error(result['Message'])
def main(): global excludedHosts Script.registerSwitch( "e:", "exclude=", "Comma separated list of hosts to be excluded from the scanning process", setExcludedHosts) Script.parseCommandLine(ignoreErrors=False) componentType = "" # Get my setup mySetup = gConfig.getValue("DIRAC/Setup") # Retrieve information from all the hosts client = SystemAdministratorIntegrator(exclude=excludedHosts) resultAll = client.getOverallStatus() if not resultAll["OK"]: gLogger.error(resultAll["Message"]) DIRACexit(-1) # Retrieve user installing the component result = getProxyInfo() if result["OK"]: user = result["Value"]["username"] else: DIRACexit(-1) if not user: user = "******" for host in resultAll["Value"]: if not resultAll["Value"][host]["OK"]: # If the host cannot be contacted, exclude it and send message excludedHosts.append(host) result = NotificationClient().sendMail( Operations().getValue("EMail/Production", []), "Unreachable host", "\ndirac-populate-component-db: Could not fill the database with the components from unreachable host %s\n" % host, ) if not result["OK"]: gLogger.error( "Can not send unreachable host notification mail: %s" % result["Message"]) resultHosts = client.getHostInfo() if not resultHosts["OK"]: gLogger.error(resultHosts["Message"]) DIRACexit(-1) resultInfo = client.getInfo() if not resultInfo["OK"]: gLogger.error(resultInfo["Message"]) DIRACexit(-1) resultMySQL = client.getMySQLStatus() if not resultMySQL["OK"]: gLogger.error(resultMySQL["Message"]) DIRACexit(-1) resultAllDB = client.getDatabases() if not resultAllDB["OK"]: gLogger.error(resultAllDB["Message"]) DIRACexit(-1) resultAvailableDB = client.getAvailableDatabases() if not resultAvailableDB["OK"]: gLogger.error(resultAvailableDB["Message"]) DIRACexit(-1) records = [] finalSet = list(set(resultAll["Value"]) - set(excludedHosts)) for host in finalSet: hasMySQL = True result = resultAll["Value"][host] hostResult = resultHosts["Value"][host] infoResult = resultInfo["Value"][host] mySQLResult = resultMySQL["Value"][host] allDBResult = resultAllDB["Value"][host] availableDBResult = resultAvailableDB["Value"][host] if not result["OK"]: gLogger.error("Host %s: %s" % (host, result["Message"])) continue if not hostResult["OK"]: gLogger.error("Host %s: %s" % (host, hostResult["Message"])) continue if not infoResult["OK"]: gLogger.error("Host %s: %s" % (host, infoResult["Message"])) continue if mySQLResult["OK"]: if not allDBResult["OK"]: gLogger.error("Host %s: %s" % (host, allDBResult["Message"])) continue if not availableDBResult["OK"]: gLogger.error("Host %s: %s" % (host, availableDBResult["Message"])) continue else: hasMySQL = False setup = infoResult["Value"]["Setup"] if setup != mySetup: continue cpu = hostResult["Value"]["CPUModel"].strip() rDict = result["Value"] # Components other than databases for compType in rDict: if componentType and componentType != compType: continue for system in rDict[compType]: components = sorted(rDict[compType][system]) for component in components: record = {"Installation": {}, "Component": {}, "Host": {}} if rDict[compType][system][component][ "Installed"] and component != "ComponentMonitoring": runitStatus = str( rDict[compType][system][component]["RunitStatus"]) if runitStatus != "Unknown": module = str( rDict[compType][system][component]["Module"]) record["Component"]["System"] = system record["Component"]["Module"] = module # Transform 'Services' into 'service', 'Agents' into 'agent' ... record["Component"]["Type"] = compType.lower()[:-1] record["Host"]["HostName"] = host record["Host"]["CPU"] = cpu record["Installation"]["Instance"] = component record["Installation"][ "InstallationTime"] = datetime.utcnow() record["Installation"]["InstalledBy"] = user records.append(record) # Databases csClient = CSAPI() cfg = csClient.getCurrentCFG()["Value"] if hasMySQL: allDB = allDBResult["Value"] availableDB = availableDBResult["Value"] for db in allDB: # Check for DIRAC only databases if db in availableDB and db != "InstalledComponentsDB": # Check for 'installed' databases isSection = cfg.isSection( "Systems/" + availableDB[db]["System"] + "/" + cfg.getOption("DIRAC/Setups/" + setup + "/" + availableDB[db]["System"]) + "/Databases/" + db + "/") if isSection: record = { "Installation": {}, "Component": {}, "Host": {} } record["Component"]["System"] = availableDB[db][ "System"] record["Component"]["Module"] = db record["Component"]["Type"] = "DB" record["Host"]["HostName"] = host record["Host"]["CPU"] = cpu record["Installation"]["Instance"] = db record["Installation"][ "InstallationTime"] = datetime.utcnow() record["Installation"]["InstalledBy"] = user records.append(record) monitoringClient = ComponentMonitoringClient() # Add the installations to the database for record in records: result = MonitoringUtilities.monitorInstallation( record["Component"]["Type"], record["Component"]["System"], record["Installation"]["Instance"], record["Component"]["Module"], record["Host"]["CPU"], record["Host"]["HostName"], ) if not result["OK"]: gLogger.error(result["Message"])
def do_show( self, args ): """ Show list of components with various related information usage: show software - show components for which software is available show installed - show components installed in the host with runit system show setup - show components set up for automatic running in the host show project - show project to install or upgrade show status - show status of the installed components show database - show status of the databases show mysql - show status of the MySQL server show log <system> <service|agent> [nlines] - show last <nlines> lines in the component log file show info - show version of software and setup show doc <type> <system> <name> - show documentation for a given service or agent show host - show host related parameters show hosts - show all available hosts show ports [host] - show all ports used by a host. If no host is given, the host currently connected to is used show installations [ list | current | -n <Name> | -h <Host> | -s <System> | -m <Module> | -t <Type> | -itb <InstallationTime before> | -ita <InstallationTime after> | -utb <UnInstallationTime before> | -uta <UnInstallationTime after> ]* - show all the installations of components that match the given parameters show profile <system> <component> [ -s <size> | -h <host> | -id <initial date DD/MM/YYYY> | -it <initial time hh:mm> | -ed <end date DD/MM/YYYY | -et <end time hh:mm> ]* - show <size> log lines of profiling information for a component in the machine <host> show errors [*|<system> <service|agent>] - show error count for the given component or all the components in the last hour and day """ argss = args.split() if not argss: gLogger.notice( self.do_show.__doc__ ) return option = argss[0] del argss[0] if option == 'software': client = SystemAdministratorClient( self.host, self.port ) result = client.getSoftwareComponents() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'installed': client = SystemAdministratorClient( self.host, self.port ) result = client.getInstalledComponents() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'setup': client = SystemAdministratorClient( self.host, self.port ) result = client.getSetupComponents() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'project': result = SystemAdministratorClient( self.host, self.port ).getProject() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( "Current project is %s" % result[ 'Value' ] ) elif option == 'status': client = SystemAdministratorClient( self.host, self.port ) result = client.getOverallStatus() if not result['OK']: self._errMsg( result['Message'] ) else: fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID'] records = [] rDict = result['Value'] for compType in rDict: for system in rDict[compType]: components = rDict[compType][system].keys() components.sort() for component in components: record = [] if rDict[compType][system][component]['Installed']: module = str( rDict[compType][system][component]['Module'] ) record += [ system,component,module,compType.lower()[:-1]] if rDict[compType][system][component]['Setup']: record += ['Setup'] else: record += ['NotSetup'] if rDict[compType][system][component]['Installed']: record += ['Installed'] else: record += ['NotInstalled'] record += [str( rDict[compType][system][component]['RunitStatus'] )] record += [str( rDict[compType][system][component]['Timeup'] )] record += [str( rDict[compType][system][component]['PID'] )] records.append(record) printTable(fields,records) elif option == 'database' or option == 'databases': client = SystemAdministratorClient( self.host, self.port ) if not gComponentInstaller.mysqlPassword: gComponentInstaller.mysqlPassword = "******" gComponentInstaller.getMySQLPasswords() result = client.getDatabases( gComponentInstaller.mysqlRootPwd ) if not result['OK']: self._errMsg( result['Message'] ) return resultSW = client.getAvailableDatabases() if not resultSW['OK']: self._errMsg( resultSW['Message'] ) return sw = resultSW['Value'] installed = result['Value'] gLogger.notice( '' ) for db in sw: if db in installed: gLogger.notice( db.rjust( 25 ), ': Installed' ) else: gLogger.notice( db.rjust( 25 ), ': Not installed' ) if not sw: gLogger.notice( "No database found" ) elif option == 'mysql': client = SystemAdministratorClient( self.host, self.port ) result = client.getMySQLStatus() if not result['OK']: self._errMsg( result['Message'] ) elif result['Value']: gLogger.notice( '' ) for par, value in result['Value'].items(): gLogger.notice( ( par.rjust( 28 ), ':', value ) ) else: gLogger.notice( "No MySQL database found" ) elif option == "log": self.getLog( argss ) elif option == "info": client = SystemAdministratorClient( self.host, self.port ) result = client.getInfo() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( '' ) gLogger.notice( "Setup:", result['Value']['Setup'] ) gLogger.notice( "DIRAC version:", result['Value']['DIRAC'] ) if result['Value']['Extensions']: for e, v in result['Value']['Extensions'].items(): gLogger.notice( "%s version" % e, v ) gLogger.notice( '' ) elif option == "host": client = SystemAdministratorClient( self.host, self.port ) result = client.getHostInfo() if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( '' ) gLogger.notice( "Host info:" ) gLogger.notice( '' ) fields = ['Parameter','Value'] records = [] for parameter in result['Value'].iteritems(): if parameter[0] == 'Extension': extensions = parameter[1].split( ',' ) for extension in extensions: extensionName, extensionVersion = extension.split( ':' ) records.append( [ '%sVersion' % extensionName, str( extensionVersion ) ] ) else: records.append( [ parameter[0], str( parameter[1] ) ] ) printTable( fields, records ) elif option == "hosts": client = ComponentMonitoringClient() result = client.getHosts( {}, False, False ) if not result[ 'OK' ]: self._errMsg( 'Error retrieving the list of hosts: %s' % ( result[ 'Message' ] ) ) else: hostList = result[ 'Value' ] gLogger.notice( '' ) gLogger.notice( ' ' + 'Host'.center( 32 ) + ' ' + 'CPU'.center( 34 ) + ' ' ) gLogger.notice( ( '-' * 69 ) ) for element in hostList: gLogger.notice( '|' + element[ 'HostName' ].center( 32 ) + '|' + element[ 'CPU' ].center( 34 ) + '|' ) gLogger.notice( ( '-' * 69 ) ) gLogger.notice( '' ) elif option == "ports": if not argss: client = SystemAdministratorClient( self.host ) else: hostname = argss[0] del argss[0] client = ComponentMonitoringClient() result = client.hostExists( { 'HostName': hostname } ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return else: if not result[ 'Value' ]: self._errMsg( 'Given host does not exist' ) return client = SystemAdministratorClient( hostname ) result = client.getUsedPorts() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return pprint.pprint( result[ 'Value' ] ) elif option == "errors": self.getErrors( argss ) elif option == "installations": self.getInstallations( argss ) elif option == "doc": if len( argss ) > 2: if argss[0] in [ 'service', 'agent' ]: compType = argss[0] compSystem = argss[1] compModule = argss[2] client = SystemAdministratorClient( self.host, self.port ) result = client.getComponentDocumentation( compType, compSystem, compModule ) if result[ 'OK' ]: gLogger.notice( result[ 'Value' ] ) else: self._errMsg( result[ 'Message' ] ) else: gLogger.notice( self.do_show.__doc__ ) else: gLogger.notice( self.do_show.__doc__ ) elif option == "profile": if len( argss ) > 1: system = argss[0] del argss[0] component = argss[0] del argss[0] component = '%s_%s' % ( system, component ) argDict = { '-s': None, '-h': self.host, '-id': None, '-it': '00:00', '-ed': None, '-et': '00:00' } key = None for arg in argss: if not key: key = arg else: argDict[ key ] = arg key = None size = None try: if argDict[ '-s' ]: size = int( argDict[ '-s' ] ) except ValueError as _ve: self._errMsg( 'Argument \'size\' must be an integer' ) return host = argDict[ '-h' ] initialDate = argDict[ '-id' ] initialTime = argDict[ '-it' ] endingDate = argDict[ '-ed' ] endingTime = argDict[ '-et' ] if initialDate: initialDate = '%s %s' % ( initialDate, initialTime ) else: initialDate = '' if endingDate: endingDate = '%s %s' % ( endingDate, endingTime ) else: endingDate = '' client = MonitoringClient() if size: result = client.getLimitedData( host, component, size ) else: result = client.getDataForAGivenPeriod( host, component, initialDate, endingDate ) if result[ 'OK' ]: text = '' headers = [result['Value'][0].keys()] for header in headers: text += str( header ).ljust( 15 ) gLogger.notice( text ) for record in result[ 'Value' ]: for metric in record.itervalues(): text += str( metric ).ljust( 15 ) gLogger.notice( text ) else: self._errMsg( result[ 'Message' ] ) else: gLogger.notice( self.do_show.__doc__ ) else: gLogger.notice( "Unknown option:", option )
def setUp( self ): """ Initialize the client on every test """ self.client = ComponentMonitoringClient()
def do_install( self, args ): """ Install various DIRAC components usage: install mysql install db <database> install service <system> <service> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ... install agent <system> <agent> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ... install executor <system> <executor> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ... """ argss = args.split() if not argss: gLogger.notice( self.do_install.__doc__ ) return option = argss[0] del argss[0] if option == "mysql": gLogger.notice( "Installing MySQL database, this can take a while ..." ) client = SystemAdministratorClient( self.host, self.port ) if gComponentInstaller.mysqlPassword == 'LocalConfig': gComponentInstaller.mysqlPassword = '' gComponentInstaller.getMySQLPasswords() result = client.installMySQL( gComponentInstaller.mysqlRootPwd, gComponentInstaller.mysqlPassword ) if not result['OK']: self._errMsg( result['Message'] ) else: gLogger.notice( "MySQL:", result['Value'] ) gLogger.notice( "You might need to restart SystemAdministrator service to take new settings into account" ) elif option == "db": if not argss: gLogger.notice( self.do_install.__doc__ ) return database = argss[0] client = SystemAdministratorClient( self.host, self.port ) result = client.getAvailableDatabases() if not result['OK']: self._errMsg( "Can not get database list: %s" % result['Message'] ) return if not result['Value'].has_key( database ): self._errMsg( "Unknown database %s: " % database ) return system = result['Value'][database]['System'] setup = gConfig.getValue( '/DIRAC/Setup', '' ) if not setup: self._errMsg( "Unknown current setup" ) return instance = gConfig.getValue( '/DIRAC/Setups/%s/%s' % ( setup, system ), '' ) if not instance: self._errMsg( "No instance defined for system %s" % system ) self._errMsg( "\tAdd new instance with 'add instance %s <instance_name>'" % system ) return if not gComponentInstaller.mysqlPassword: gComponentInstaller.mysqlPassword = '******' gComponentInstaller.getMySQLPasswords() result = client.installDatabase( database, gComponentInstaller.mysqlRootPwd ) if not result['OK']: self._errMsg( result['Message'] ) return extension, system = result['Value'] result = client.getHostInfo() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return if database != 'InstalledComponentsDB': result = MonitoringUtilities.monitorInstallation( 'DB', system.replace( 'System', '' ), database, cpu = cpu, hostname = hostname ) if not result['OK']: self._errMsg( result['Message'] ) return # result = client.addDatabaseOptionsToCS( system, database ) gComponentInstaller.mysqlHost = self.host result = client.getInfo() if not result['OK']: self._errMsg( result['Message'] ) hostSetup = result['Value']['Setup'] result = gComponentInstaller.addDatabaseOptionsToCS( gConfig, system, database, hostSetup, overwrite = True ) if not result['OK']: self._errMsg( result['Message'] ) return gLogger.notice( "Database %s from %s/%s installed successfully" % ( database, extension, system ) ) elif option in self.runitComponents: if len( argss ) < 2: gLogger.notice( self.do_install.__doc__ ) return system = argss[0] del argss[0] component = argss[0] del argss[0] specialOptions = {} module = '' for i in range(len(argss)): if argss[i] == "-m": specialOptions['Module'] = argss[i+1] module = argss[i+1] if argss[i] == "-p": opt,value = argss[i+1].split('=') specialOptions[opt] = value if module == component: module = '' client = SystemAdministratorClient( self.host, self.port ) # First need to update the CS # result = client.addDefaultOptionsToCS( option, system, component ) gComponentInstaller.host = self.host result = client.getInfo() if not result['OK']: self._errMsg( result['Message'] ) return hostSetup = result['Value']['Setup'] # Install Module section if not yet there if module: result = gComponentInstaller.addDefaultOptionsToCS( gConfig, option, system, module, getCSExtensions(), hostSetup ) # in case of Error we must stop, this can happen when the module name is wrong... if not result['OK']: self._errMsg( result['Message'] ) return # Add component section with specific parameters only result = gComponentInstaller.addDefaultOptionsToCS( gConfig, option, system, component, getCSExtensions(), hostSetup, specialOptions, addDefaultOptions = True ) else: # Install component section result = gComponentInstaller.addDefaultOptionsToCS( gConfig, option, system, component, getCSExtensions(), hostSetup, specialOptions ) if not result['OK']: self._errMsg( result['Message'] ) return # Then we can install and start the component result = client.setupComponent( option, system, component, module ) if not result['OK']: self._errMsg( result['Message'] ) return compType = result['Value']['ComponentType'] runit = result['Value']['RunitStatus'] gLogger.notice( "%s %s_%s is installed, runit status: %s" % ( compType, system, component, runit ) ) # And register it in the database result = client.getHostInfo() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host if component == 'ComponentMonitoring': # Make sure that the service is running before trying to use it nTries = 0 maxTries = 5 mClient = ComponentMonitoringClient() result = mClient.ping() while not result[ 'OK' ] and nTries < maxTries: time.sleep( 3 ) result = mClient.ping() nTries = nTries + 1 if not result[ 'OK' ]: self._errMsg( 'ComponentMonitoring service taking too long to start. Installation will not be logged into the database' ) return result = MonitoringUtilities.monitorInstallation( 'DB', system, 'InstalledComponentsDB', cpu = cpu, hostname = hostname ) if not result['OK']: self._errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] ) return result = MonitoringUtilities.monitorInstallation( option, system, component, module, cpu = cpu, hostname = hostname ) if not result['OK']: self._errMsg( 'Error registering installation into database: %s' % result[ 'Message' ] ) return else: gLogger.notice( "Unknown option:", option )
def web_getSysInfo(self): userData = self.getSessionData() DN = str(userData["user"]["DN"]) group = str(userData["user"]["group"]) callback = [] # self.finish( { "success" : "false" , "error" : "No system information found" } ) # return """ client = SystemAdministratorIntegrator( delegatedDN = DN , delegatedGroup = group ) resultHosts = yield self.threadTask( client.getHostInfo ) if resultHosts[ "OK" ]: hosts = resultHosts['Value'] for i in hosts: if hosts[i]['OK']: host = hosts[i]['Value'] host['Host'] = i callback.append( host ) else: callback.append( {'Host':i} ) else: self.finish( { "success" : "false" , "error" : resultHosts['Message']} ) """ callback = [] client = ComponentMonitoringClient() result = client.getHosts({}, False, False) if result['OK']: hosts = result['Value'] for obj in hosts: host = obj['HostName'] client = SystemAdministratorClient(host, None, delegatedDN=DN, delegatedGroup=group) resultHost = yield self.threadTask(client.getHostInfo) if resultHost["OK"]: resultHost["Value"]["Host"] = host if "Timestamp" in resultHost["Value"]: resultHost["Value"]["Timestamp"] = str( resultHost["Value"]["Timestamp"]) callback.append(resultHost["Value"]) else: callback.append({"Host": host}) total = len(callback) if not total > 0: self.finish({ "success": "false", "error": "No system information found" }) return callback = sorted(callback, key=lambda i: i['Host']) # Add the information about the extensions' versions, if available, to display along with the DIRAC version for i in range(len(callback)): if 'Extension' in callback[i]: #We have to keep the backward compatibility (this can heppen when we do not update one host to v6r15 ... callback[i]['DIRAC'] = '%s,%s' % (callback[i].get( 'DIRACVersion', callback[i].get('DIRAC')), callback[i]['Extension']) elif 'Extensions' in callback[i]: callback[i]['DIRAC'] = '%s,%s' % (callback[i].get( 'DIRAC', callback[i].get('DIRAC')), callback[i]['Extensions']) self.finish({"success": "true", "result": callback, "total": total})
def do_uninstall( self, args ): """ Uninstall a DIRAC component or host along with all its components usage: uninstall db <database> uninstall host <hostname> uninstall <-f ForceLogUninstall> <system> <component> """ argss = args.split() if not argss: gLogger.notice( self.do_uninstall.__doc__ ) return # Retrieve user uninstalling the component result = getProxyInfo() if not result[ 'OK' ]: self._errMsg( result[ 'Message'] ) user = result[ 'Value' ][ 'username' ] option = argss[0] if option == 'db': del argss[0] if not argss: gLogger.notice( self.do_uninstall.__doc__ ) return component = argss[0] client = SystemAdministratorClient( self.host, self.port ) result = client.getHostInfo() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host result = client.getAvailableDatabases() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return system = result[ 'Value' ][ component ][ 'System' ] result = MonitoringUtilities.monitorUninstallation( system , component, hostname = hostname, cpu = cpu ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return result = client.uninstallDatabase( component ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) else: gLogger.notice( "Successfully uninstalled %s" % ( component ) ) elif option == 'host': del argss[0] if not argss: gLogger.notice( self.do_uninstall.__doc__ ) return hostname = argss[0] client = ComponentMonitoringClient() result = client.hostExists( { 'HostName': hostname } ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) else: if not result[ 'Value' ]: self._errMsg( 'Given host does not exist' ) else: result = client.getHosts( {'HostName': hostname }, True, False ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) else: host = result[ 'Value' ][0] # Remove every installation associated with the host for installation in host[ 'Installations' ]: result = client.removeInstallations( installation, {}, { 'HostName': hostname } ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) break # Finally remove the host result = client.removeHosts( { 'HostName': hostname } ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) else: gLogger.notice( 'Host %s was successfully removed' % hostname ) else: if option == '-f': force = True del argss[0] else: force = False if len( argss ) != 2: gLogger.notice( self.do_uninstall.__doc__ ) return system, component = argss client = SystemAdministratorClient( self.host, self.port ) monitoringClient = ComponentMonitoringClient() result = monitoringClient.getInstallations( { 'Instance': component, 'UnInstallationTime': None }, { 'System': system }, { 'HostName': self.host }, True ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return if len( result[ 'Value' ] ) < 1: self._errMsg( "Given component does not exist" ) return if len( result[ 'Value' ] ) > 1: self._errMsg( "Too many components match" ) return removeLogs = False if force: removeLogs = True else: if result[ 'Value' ][0][ 'Component' ][ 'Type' ] in self.runitComponents: result = promptUser( 'Remove logs?', ['y', 'n'], 'n' ) if result[ 'OK' ]: removeLogs = result[ 'Value' ] == 'y' result = client.uninstallComponent( system, component, removeLogs ) if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) else: gLogger.notice( "Successfully uninstalled %s/%s" % ( system, component ) ) result = client.getHostInfo() if not result[ 'OK' ]: self._errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host result = MonitoringUtilities.monitorUninstallation( system, component, hostname = hostname, cpu = cpu ) if not result[ 'OK' ]: return result
def monitorInstallation(componentType, system, component, module=None, cpu=None, hostname=None): """ Register the installation of a component in the ComponentMonitoringDB """ monitoringClient = ComponentMonitoringClient() if not module: module = component # Retrieve user installing the component user = None result = getProxyInfo() if result["OK"]: proxyInfo = result["Value"] if "username" in proxyInfo: user = proxyInfo["username"] else: return result if not user: user = "******" if not cpu: cpu = "Not available" for line in open("/proc/cpuinfo"): if line.startswith("model name"): cpu = line.split(":")[1][0:64] cpu = cpu.replace("\n", "").lstrip().rstrip() if not hostname: hostname = socket.getfqdn() instance = component[0:32] result = monitoringClient.installationExists( { "Instance": instance, "UnInstallationTime": None }, { "Type": componentType, "DIRACSystem": system, "DIRACModule": module }, { "HostName": hostname, "CPU": cpu }, ) if not result["OK"]: return result if result["Value"]: return S_OK("Monitoring of %s is already enabled" % component) result = monitoringClient.addInstallation( { "InstallationTime": datetime.datetime.utcnow(), "InstalledBy": user, "Instance": instance }, { "Type": componentType, "DIRACSystem": system, "DIRACModule": module }, { "HostName": hostname, "CPU": cpu }, True, ) return result
def do_show( self, args ): """ Show list of components with various related information usage: show software - show components for which software is available show installed - show components installed in the host with runit system show setup - show components set up for automatic running in the host show project - show project to install or upgrade show status - show status of the installed components show database - show status of the databases show mysql - show status of the MySQL server show log <system> <service|agent> [nlines] - show last <nlines> lines in the component log file show info - show version of software and setup show host - show host related parameters show hosts - show all available hosts show installations [ list | current | -n <Name> | -h <Host> | -s <System> | -m <Module> | -t <Type> | -itb <InstallationTime before> | -ita <InstallationTime after> | -utb <UnInstallationTime before> | -uta <UnInstallationTime after> ]* - show all the installations of components that match the given parameters show errors [*|<system> <service|agent>] - show error count for the given component or all the components in the last hour and day """ argss = args.split() if not argss: gLogger.notice( self.do_show.__doc__ ) return option = argss[0] del argss[0] if option == 'software': client = SystemAdministratorClient( self.host, self.port ) result = client.getSoftwareComponents() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'installed': client = SystemAdministratorClient( self.host, self.port ) result = client.getInstalledComponents() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'setup': client = SystemAdministratorClient( self.host, self.port ) result = client.getSetupComponents() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( '' ) pprint.pprint( result['Value'] ) elif option == 'project': result = SystemAdministratorClient( self.host, self.port ).getProject() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( "Current project is %s" % result[ 'Value' ] ) elif option == 'status': client = SystemAdministratorClient( self.host, self.port ) result = client.getOverallStatus() if not result['OK']: self.__errMsg( result['Message'] ) else: fields = ["System",'Name','Module','Type','Setup','Installed','Runit','Uptime','PID'] records = [] rDict = result['Value'] for compType in rDict: for system in rDict[compType]: components = rDict[compType][system].keys() components.sort() for component in components: record = [] if rDict[compType][system][component]['Installed']: module = str( rDict[compType][system][component]['Module'] ) record += [ system,component,module,compType.lower()[:-1]] if rDict[compType][system][component]['Setup']: record += ['Setup'] else: record += ['NotSetup'] if rDict[compType][system][component]['Installed']: record += ['Installed'] else: record += ['NotInstalled'] record += [str( rDict[compType][system][component]['RunitStatus'] )] record += [str( rDict[compType][system][component]['Timeup'] )] record += [str( rDict[compType][system][component]['PID'] )] records.append(record) printTable(fields,records) elif option == 'database' or option == 'databases': client = SystemAdministratorClient( self.host, self.port ) if not InstallTools.mysqlPassword: InstallTools.mysqlPassword = "******" InstallTools.getMySQLPasswords() result = client.getDatabases( InstallTools.mysqlRootPwd ) if not result['OK']: self.__errMsg( result['Message'] ) return resultSW = client.getAvailableDatabases() if not resultSW['OK']: self.__errMsg( resultSW['Message'] ) return sw = resultSW['Value'] installed = result['Value'] gLogger.notice( '' ) for db in sw: if db in installed: gLogger.notice( db.rjust( 25 ), ': Installed' ) else: gLogger.notice( db.rjust( 25 ), ': Not installed' ) if not sw: gLogger.notice( "No database found" ) elif option == 'mysql': client = SystemAdministratorClient( self.host, self.port ) result = client.getMySQLStatus() if not result['OK']: self.__errMsg( result['Message'] ) elif result['Value']: gLogger.notice( '' ) for par, value in result['Value'].items(): gLogger.notice( ( par.rjust( 28 ), ':', value ) ) else: gLogger.notice( "No MySQL database found" ) elif option == "log": self.getLog( argss ) elif option == "info": client = SystemAdministratorClient( self.host, self.port ) result = client.getInfo() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( '' ) gLogger.notice( "Setup:", result['Value']['Setup'] ) gLogger.notice( "DIRAC version:", result['Value']['DIRAC'] ) if result['Value']['Extensions']: for e, v in result['Value']['Extensions'].items(): gLogger.notice( "%s version" % e, v ) gLogger.notice( '' ) elif option == "host": client = SystemAdministratorClient( self.host, self.port ) result = client.getHostInfo() if not result['OK']: self.__errMsg( result['Message'] ) else: gLogger.notice( '' ) gLogger.notice( "Host info:" ) gLogger.notice( '' ) fields = ['Parameter','Value'] records = [] for key, value in result['Value'].items(): records.append( [key, str( value ) ] ) printTable( fields, records ) elif option == "hosts": client = ComponentMonitoringClient() result = client.getHosts( {}, False, False ) if not result[ 'OK' ]: self.__errMsg( 'Error retrieving the list of hosts: %s' % ( result[ 'Message' ] ) ) else: hostList = result[ 'Value' ] gLogger.notice( '' ) gLogger.notice( ' ' + 'Host'.center( 32 ) + ' ' + 'CPU'.center( 34 ) + ' ' ) gLogger.notice( ( '-' * 69 ) ) for element in hostList: gLogger.notice( '|' + element[ 'HostName' ].center( 32 ) + '|' + element[ 'CPU' ].center( 34 ) + '|' ) gLogger.notice( ( '-' * 69 ) ) gLogger.notice( '' ) elif option == "errors": self.getErrors( argss ) elif option == "installations": self.getInstallations( argss ) else: gLogger.notice( "Unknown option:", option )
def main(): global excludedHosts Script.registerSwitch( "e:", "exclude=", "Comma separated list of hosts to be excluded from the scanning process", setExcludedHosts) Script.parseCommandLine(ignoreErrors=False) componentType = '' # Get my setup mySetup = gConfig.getValue('DIRAC/Setup') # Retrieve information from all the hosts client = SystemAdministratorIntegrator(exclude=excludedHosts) resultAll = client.getOverallStatus() # Retrieve user installing the component result = getProxyInfo() if result['OK']: user = result['Value']['username'] else: DIRACexit(-1) if not user: user = '******' notificationClient = NotificationClient() for host in resultAll['Value']: if not resultAll['Value'][host]['OK']: # If the host cannot be contacted, exclude it and send message excludedHosts.append(host) result = notificationClient.sendMail( Operations().getValue('EMail/Production', []), 'Unreachable host', '\ndirac-populate-component-db: Could not fill the database with the components from unreachable host %s\n' % host) if not result['OK']: gLogger.error( 'Can not send unreachable host notification mail: %s' % result['Message']) if not resultAll['OK']: gLogger.error(resultAll['Message']) DIRACexit(-1) resultHosts = client.getHostInfo() if not resultHosts['OK']: gLogger.error(resultHosts['Message']) DIRACexit(-1) resultInfo = client.getInfo() if not resultInfo['OK']: gLogger.error(resultInfo['Message']) DIRACexit(-1) resultMySQL = client.getMySQLStatus() if not resultMySQL['OK']: gLogger.error(resultMySQL['Message']) DIRACexit(-1) resultAllDB = client.getDatabases() if not resultAllDB['OK']: gLogger.error(resultAllDB['Message']) DIRACexit(-1) resultAvailableDB = client.getAvailableDatabases() if not resultAvailableDB['OK']: gLogger.error(resultAvailableDB['Message']) DIRACexit(-1) records = [] finalSet = list(set(resultAll['Value']) - set(excludedHosts)) for host in finalSet: hasMySQL = True result = resultAll['Value'][host] hostResult = resultHosts['Value'][host] infoResult = resultInfo['Value'][host] mySQLResult = resultMySQL['Value'][host] allDBResult = resultAllDB['Value'][host] availableDBResult = resultAvailableDB['Value'][host] if not result['OK']: gLogger.error('Host %s: %s' % (host, result['Message'])) continue if not hostResult['OK']: gLogger.error('Host %s: %s' % (host, hostResult['Message'])) continue if not infoResult['OK']: gLogger.error('Host %s: %s' % (host, infoResult['Message'])) continue if mySQLResult['OK']: if not allDBResult['OK']: gLogger.error('Host %s: %s' % (host, allDBResult['Message'])) continue if not availableDBResult['OK']: gLogger.error('Host %s: %s' % (host, availableDBResult['Message'])) continue else: hasMySQL = False setup = infoResult['Value']['Setup'] if setup != mySetup: continue cpu = hostResult['Value']['CPUModel'].strip() rDict = result['Value'] # Components other than databases for compType in rDict: if componentType and componentType != compType: continue for system in rDict[compType]: components = sorted(rDict[compType][system]) for component in components: record = {'Installation': {}, 'Component': {}, 'Host': {}} if rDict[compType][system][component]['Installed'] and \ component != 'ComponentMonitoring': runitStatus = \ str(rDict[compType][system][component]['RunitStatus']) if runitStatus != 'Unknown': module = \ str(rDict[compType][system][component]['Module']) record['Component']['System'] = system record['Component']['Module'] = module # Transform 'Services' into 'service', 'Agents' into 'agent' ... record['Component']['Type'] = compType.lower()[:-1] record['Host']['HostName'] = host record['Host']['CPU'] = cpu record['Installation']['Instance'] = component record['Installation'][ 'InstallationTime'] = datetime.utcnow() record['Installation']['InstalledBy'] = user records.append(record) # Databases csClient = CSAPI() cfg = csClient.getCurrentCFG()['Value'] if hasMySQL: allDB = allDBResult['Value'] availableDB = availableDBResult['Value'] for db in allDB: # Check for DIRAC only databases if db in availableDB and db != 'InstalledComponentsDB': # Check for 'installed' databases isSection = cfg.isSection( 'Systems/' + availableDB[db]['System'] + '/' + cfg.getOption('DIRAC/Setups/' + setup + '/' + availableDB[db]['System']) + '/Databases/' + db + '/') if isSection: record = { 'Installation': {}, 'Component': {}, 'Host': {} } record['Component']['System'] = availableDB[db][ 'System'] record['Component']['Module'] = db record['Component']['Type'] = 'DB' record['Host']['HostName'] = host record['Host']['CPU'] = cpu record['Installation']['Instance'] = db record['Installation'][ 'InstallationTime'] = datetime.utcnow() record['Installation']['InstalledBy'] = user records.append(record) monitoringClient = ComponentMonitoringClient() # Add the installations to the database for record in records: result = MonitoringUtilities.monitorInstallation( record['Component']['Type'], record['Component']['System'], record['Installation']['Instance'], record['Component']['Module'], record['Host']['CPU'], record['Host']['HostName']) if not result['OK']: gLogger.error(result['Message'])
def getInstallations( self, argss ): """ Get data from the component monitoring database """ display = 'table' installationFilter = {} componentFilter = {} hostFilter = {} key = None for arg in argss: if not key: if arg == 'list': display = 'list' elif arg == 'current': installationFilter[ 'UnInstallationTime' ] = None elif arg == '-t': key = 'Component.Type' elif arg == '-m': key = 'Component.Module' elif arg == '-s': key = 'Component.System' elif arg == '-h': key = 'Host.HostName' elif arg == '-n': key = 'Instance' elif arg == '-itb': key = 'InstallationTime.smaller' elif arg == '-ita': key = 'InstallationTime.bigger' elif arg == '-utb': key = 'UnInstallationTime.smaller' elif arg == '-uta': key = 'UnInstallationTime.bigger' else: if 'Component.' in key: componentFilter[ key.replace( 'Component.', '' ) ] = arg elif 'Host.' in key: hostFilter[ key.replace( 'Host.', '' ) ] = arg else: if 'Time.' in key: arg = datetime.datetime.strptime( arg, '%d-%m-%Y' ) installationFilter[ key ] = arg key = None client = ComponentMonitoringClient() result = client.getInstallations( installationFilter, componentFilter, hostFilter, True ) if not result[ 'OK' ]: self.__errMsg( 'Could not retrieve the installations: %s' % ( result[ 'Message' ] ) ) installations = None else: installations = result[ 'Value' ] if installations: if display == 'table': gLogger.notice( '' ) gLogger.notice( ' ' + 'Num'.center( 5 ) + ' ' \ + 'Host'.center( 20 ) + ' ' \ + 'Name'.center( 24 ) + ' ' \ + 'Module'.center( 24 ) + ' ' \ + 'System'.center( 20 ) + ' ' \ + 'Type'.center( 12 ) + ' ' \ + 'Installed on'.center( 18 ) + ' ' \ + 'Uninstalled on'.center( 18 ) + ' ' ) gLogger.notice( ( '-' ) * 150 ) for i, installation in enumerate( installations ): if installation[ 'UnInstallationTime' ]: uninstalledOn = installation[ 'UnInstallationTime' ].strftime( "%d-%m-%Y %H:%M" ) isInstalled = 'No' else: uninstalledOn = '' isInstalled = 'Yes' if display == 'table': gLogger.notice( '|' + str( i + 1 ).center( 5 ) + '|' \ + installation[ 'Host' ][ 'HostName' ].center( 20 ) + '|' \ + installation[ 'Instance' ].center( 24 ) + '|' \ + installation[ 'Component' ][ 'Module' ].center( 24 ) + '|' \ + installation[ 'Component' ][ 'System' ].center( 20 ) + '|' \ + installation[ 'Component' ][ 'Type' ].center( 12 ) + '|' \ + installation[ 'InstallationTime' ].strftime( "%d-%m-%Y %H:%M" ).center( 18 ) + '|' \ + uninstalledOn.center( 18 ) + '|' ) gLogger.notice( ( '-' ) * 150 ) elif display == 'list': gLogger.notice( '' ) gLogger.notice( 'Installation: '.rjust( 20 ) + str ( i + 1 ) ) gLogger.notice( 'Installed: '.rjust( 20 ) + isInstalled ) gLogger.notice( 'Host: '.rjust( 20 ) + installation[ 'Host' ][ 'HostName' ] ) gLogger.notice( 'Name: '.rjust( 20 ) + installation[ 'Instance' ] ) gLogger.notice( 'Module: '.rjust( 20 ) + installation[ 'Component' ][ 'Module' ] ) gLogger.notice( 'System: '.rjust( 20 ) + installation[ 'Component' ][ 'System' ] ) gLogger.notice( 'Type: '.rjust( 20 ) + installation[ 'Component' ][ 'Type' ] ) gLogger.notice( 'Installed on: '.rjust( 20 ) + installation[ 'InstallationTime' ].strftime( "%d-%m-%Y %H:%M" ) ) if uninstalledOn != '': gLogger.notice( 'Uninstalled on: '.rjust( 20 ) + uninstalledOn ) else: self.__errMsg( 'No display mode was selected' ) gLogger.notice( '' )
def main(): global force from DIRAC.FrameworkSystem.Client.ComponentInstaller import gComponentInstaller gComponentInstaller.exitOnError = True Script.registerSwitch("f", "force", "Forces the removal of the logs", setForce) # Registering arguments will automatically add their description to the help menu Script.registerArgument(( "System/Component: Full component name (ie: WorkloadManagement/Matcher)", "System: Name of the DIRAC system (ie: WorkloadManagement)", )) Script.registerArgument( " Component: Name of the DIRAC service (ie: Matcher)", mandatory=False) _, args = Script.parseCommandLine() if len(args) == 1: args = args[0].split("/") if len(args) < 2: Script.showHelp(exitCode=1) system = args[0] component = args[1] monitoringClient = ComponentMonitoringClient() result = monitoringClient.getInstallations( { "Instance": component, "UnInstallationTime": None }, {"System": system}, {"HostName": socket.getfqdn()}, True) if not result["OK"]: gLogger.error(result["Message"]) DIRACexit(1) if len(result["Value"]) < 1: gLogger.warn("Given component does not exist") DIRACexit(1) if len(result["Value"]) > 1: gLogger.error("Too many components match") DIRACexit(1) removeLogs = False if force: removeLogs = True else: if result["Value"][0]["Component"][ "Type"] in gComponentInstaller.componentTypes: result = promptUser("Remove logs?", ["y", "n"], "n") if result["OK"]: removeLogs = result["Value"] == "y" else: gLogger.error(result["Message"]) DIRACexit(1) result = gComponentInstaller.uninstallComponent(system, component, removeLogs) if not result["OK"]: gLogger.error(result["Message"]) DIRACexit(1) result = MonitoringUtilities.monitorUninstallation(system, component) if not result["OK"]: gLogger.error(result["Message"]) DIRACexit(1) gLogger.notice("Successfully uninstalled component %s/%s" % (system, component)) DIRACexit()
def do_uninstall( self, args ): """ Uninstall DIRAC component usage: uninstall db <database> uninstall <-f ForceLogUninstall> <system> <component> """ argss = args.split() if not argss: gLogger.notice( self.do_uninstall.__doc__ ) return option = argss[0] if option == 'db': component = argss[1] client = SystemAdministratorClient( self.host, self.port ) result = client.uninstallDatabase( component ) if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) else: gLogger.notice( "Successfully uninstalled %s" % ( component ) ) result = client.getHostInfo() if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host result = client.getAvailableDatabases() if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) return system = result[ 'Value' ][ component ][ 'System' ] result = MonitoringUtilities.monitorUninstallation( system , component, hostname = hostname, cpu = cpu ) if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) return else: if option == '-f': force = True del argss[0] else: force = False if len( argss ) != 2: gLogger.notice( self.do_uninstall.__doc__ ) return system, component = argss client = SystemAdministratorClient( self.host, self.port ) monitoringClient = ComponentMonitoringClient() result = monitoringClient.getInstallations( { 'Instance': component, 'UnInstallationTime': None }, { 'System': system }, { 'HostName': self.host }, True ) if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) return if len( result[ 'Value' ] ) < 1: self.__errMsg( "Given component does not exist" ) return if len( result[ 'Value' ] ) > 1: self.__errMsg( "Too many components match" ) return removeLogs = False if force: removeLogs = True else: if result[ 'Value' ][0][ 'Component' ][ 'Type' ] in self.runitComponents: result = promptUser( 'Remove logs?', ['y', 'n'], 'n' ) if result[ 'OK' ]: removeLogs = result[ 'Value' ] == 'y' result = client.uninstallComponent( system, component, removeLogs ) if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) else: gLogger.notice( "Successfully uninstalled %s/%s" % ( system, component ) ) result = client.getHostInfo() if not result[ 'OK' ]: self.__errMsg( result[ 'Message' ] ) return else: cpu = result[ 'Value' ][ 'CPUModel' ] hostname = self.host result = MonitoringUtilities.monitorUninstallation( system, component, hostname = hostname, cpu = cpu ) if not result[ 'OK' ]: return result
def main(): global hostName global hostDN global hostProperties Script.registerSwitch("H:", "HostName:", "Name of the Host (Mandatory)", setHostName) Script.registerSwitch("D:", "HostDN:", "DN of the Host Certificate (Mandatory)", setHostDN) Script.registerSwitch( "P:", "Property:", "Property to be added to the Host (Allow Multiple instances or None)", addProperty) # Registering arguments will automatically add their description to the help menu Script.registerArgument([ "Property=<Value>: Other properties to be added to the Host like (Responsible=XXX)" ], mandatory=False) _, args = Script.parseCommandLine(ignoreErrors=True) if hostName is None or hostDN is None: Script.showHelp(exitCode=1) from DIRAC.Interfaces.API.DiracAdmin import DiracAdmin diracAdmin = DiracAdmin() exitCode = 0 errorList = [] hostProps = {"DN": hostDN} if hostProperties: hostProps["Properties"] = ", ".join(hostProperties) for prop in args: pl = prop.split("=") if len(pl) < 2: errorList.append( ("in arguments", "Property %s has to include a '=' to separate name from value" % prop)) exitCode = 255 else: pName = pl[0] pValue = "=".join(pl[1:]) gLogger.info("Setting property %s to %s" % (pName, pValue)) hostProps[pName] = pValue if not diracAdmin.csModifyHost( hostName, hostProps, createIfNonExistant=True)["OK"]: errorList.append(("add host", "Cannot register host %s" % hostName)) exitCode = 255 else: result = diracAdmin.csCommitChanges() if not result["OK"]: errorList.append(("commit", result["Message"])) exitCode = 255 if exitCode == 0: from DIRAC.FrameworkSystem.Client.ComponentMonitoringClient import ComponentMonitoringClient cmc = ComponentMonitoringClient() ret = cmc.hostExists(dict(HostName=hostName)) if not ret["OK"]: gLogger.error( "Cannot check if host is registered in ComponentMonitoring", ret["Message"]) elif ret["Value"]: gLogger.info("Host already registered in ComponentMonitoring") else: ret = cmc.addHost(dict(HostName=hostName, CPU="TO_COME")) if not ret["OK"]: gLogger.error("Failed to add Host to ComponentMonitoring", ret["Message"]) for error in errorList: gLogger.error("%s: %s" % error) DIRAC.exit(exitCode)
def main(): global hostName global hostDN global hostProperties Script.registerSwitch('H:', 'HostName:', 'Name of the Host (Mandatory)', setHostName) Script.registerSwitch('D:', 'HostDN:', 'DN of the Host Certificate (Mandatory)', setHostDN) Script.registerSwitch( 'P:', 'Property:', 'Property to be added to the Host (Allow Multiple instances or None)', addProperty) Script.parseCommandLine(ignoreErrors=True) if hostName is None or hostDN is None: Script.showHelp(exitCode=1) args = Script.getPositionalArgs() from DIRAC.Interfaces.API.DiracAdmin import DiracAdmin diracAdmin = DiracAdmin() exitCode = 0 errorList = [] hostProps = {'DN': hostDN} if hostProperties: hostProps['Properties'] = ', '.join(hostProperties) for prop in args: pl = prop.split("=") if len(pl) < 2: errorList.append( ("in arguments", "Property %s has to include a '=' to separate name from value" % prop)) exitCode = 255 else: pName = pl[0] pValue = "=".join(pl[1:]) Script.gLogger.info("Setting property %s to %s" % (pName, pValue)) hostProps[pName] = pValue if not diracAdmin.csModifyHost( hostName, hostProps, createIfNonExistant=True)['OK']: errorList.append(("add host", "Cannot register host %s" % hostName)) exitCode = 255 else: result = diracAdmin.csCommitChanges() if not result['OK']: errorList.append(("commit", result['Message'])) exitCode = 255 if exitCode == 0: from DIRAC.FrameworkSystem.Client.ComponentMonitoringClient import ComponentMonitoringClient cmc = ComponentMonitoringClient() ret = cmc.hostExists(dict(HostName=hostName)) if not ret['OK']: Script.gLogger.error( 'Cannot check if host is registered in ComponentMonitoring', ret['Message']) elif ret['Value']: Script.gLogger.info( 'Host already registered in ComponentMonitoring') else: ret = cmc.addHost(dict(HostName=hostName, CPU='TO_COME')) if not ret['OK']: Script.gLogger.error( 'Failed to add Host to ComponentMonitoring', ret['Message']) for error in errorList: Script.gLogger.error("%s: %s" % error) DIRAC.exit(exitCode)