def stop(self): """ Stops all threads, Daemon and kill CentralReport instance. """ crLog.writeInfo('Stopping CentralReport...') self.isRunning = False if CentralReport.webserver_thread is not None: crLog.writeInfo('Stopping Webserver...') CentralReport.webserver_thread.stop() if CentralReport.checks_thread is not None: crLog.writeInfo('Stopping checks thread...') crThreads.Checks.performChecks = False crLog.writeInfo('Stopping daemon...') try: Daemon.stop(self) except: crLog.writeInfo('PID file not found.') # In test mode, we only return 0 (exit can be personalized by others scripts) # But in production, we kill immediately the process. if not Config.CR_CONFIG_ENABLE_DEBUG_MODE: os.system('kill %d' % os.getpid()) return 0
def writeConfigFile(self): """ Writes the actual configuration into the config file. """ crLog.writeInfo('Writing the config file...') # Generating uuid if empty if '' == Config.getConfigValue('General', 'uuid'): Config.setConfigValue('General', 'uuid', uuid.uuid1()) # Writing conf file. Reading all sections defined in the config... for config_section in Config._CR_CONFIG_VALUES: try: Config.config.add_section(config_section) except ConfigParser.DuplicateSectionError: crLog.writeDebug('Section already exist: ' + config_section) except: crLog.writeError('Error creating new section: ' + config_section + ': ' + Exception.message) # Reading all values in this section config_section_vars = Config._CR_CONFIG_VALUES[config_section] for config_value in config_section_vars: try: Config.config.set(config_section, config_value, Config._CR_CONFIG_VALUES[config_section][config_value]) except: crLog.writeError('Error writing config value: ' + config_section + '/' + config_value) try: Config.config.write(open(Config.CR_CONFIG_FULL_PATH, 'w')) # Writing the config file on filesystem... except IOError: crLog.writeError('/!\ Error writing config file. Using the default config')
def run(self): """ Constructor. """ isError = False # If True, there are one or more errors when CentralReport is trying to start # Preparing Logs crLog.configLog(Config.CR_CONFIG_ENABLE_DEBUG_MODE) crLog.writeInfo('CentralReport is starting...') CentralReport.startingDate = datetime.datetime.now() # Starting date CentralReport.configuration = Config() # Getting config object # Getting current OS... if (Config.HOST_CURRENT == Config.HOST_MAC) | (Config.HOST_CURRENT == Config.HOST_DEBIAN) | ( Config.HOST_CURRENT == Config.HOST_UBUNTU): crLog.writeInfo(Config.HOST_CURRENT + ' detected. Starting ThreadChecks...') CentralReport.checks_thread = crThreads.Checks() # Launching checks thread else: isError = True crLog.writeCritical('Sorry, but your OS is not supported yet...') # Is webserver enabled? if not isError & crUtilsText.textToBool(Config.getConfigValue('Webserver', 'enable')): crLog.writeInfo('Enabling the webserver') CentralReport.webserver_thread = WebServer() else: crLog.writeInfo('Webserver is disabled by configuration file') if not isError: crLog.writeInfo('CentralReport started!') while CentralReport.isRunning: try: if not Config.CR_CONFIG_ENABLE_DEBUG_MODE: # If .pid file is not found, we must stop CR (only in production environment) try: pf = file(self.pidfile, 'r') pf.close() except IOError: crLog.writeError('Pid file is not found. CentralReport must stop itself.') CentralReport.isRunning = False self.stop() time.sleep(1) except KeyboardInterrupt: # Stopping CR crLog.writeFatal('KeyboardInterrupt exception. Stopping CentralReport...') CentralReport.isRunning = False self.stop() else: crLog.writeError('Error launching CentralReport!')
def __init__(self): """ Constructor """ Config.determine_current_host() if Config.HOST_CURRENT == Config.HOST_MAC: crLog.writeDebug('Mac config') else: crLog.writeDebug('Linux config') # Managing config file if os.path.isfile(Config.CR_CONFIG_FULL_PATH): crLog.writeDebug('Configuration file: Found. Reading it.') self.readConfigFile() else: crLog.writeInfo('Configuration file: Not found. Creating it.') self.writeConfigFile()