def run(self): """ Manages the execution of new checks periodically. """ while Checks.performChecks: if self.tickPerformCheck <= self.tickCount: try: self.perform_check() except Exception as e: log.log_error('Error performing a new check: %s' % e) log.log_debug('Next checks in %s seconds...' % self.tickPerformCheck) self.tickCount = 0 self.tickCount += 1 time.sleep(1)
def write_config_file(self): """ Writes the actual configuration into the config file. """ log.log_info('Writing the config file...') # Generating uuid if empty if '' == Config.get_config_value('General', 'uuid'): Config.set_config_value('General', 'uuid', str(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: log.log_debug('Section already exist: %s' % config_section) except: log.log_error('Error creating new section: %s:%s' % (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: log.log_error('Error writing config value: %s/%s' % (config_section, config_value)) try: Config.config.write(open(Config.CR_CONFIG_FULL_PATH, 'w')) # Writing the config file on filesystem... except IOError: log.log_error('/!\ Error writing config file. Using the default config')
def read_config_file(self): """ Reads the configuration file. """ log.log_debug('Reading the config file...') # Using Python ConfigParser module to read the file Config.config.read(Config.CR_CONFIG_FULL_PATH) # False = all sections and options are found. # True = config must be updated: some options or sections are mising (outdated config file?) config_must_be_updated = False # Getting all values... for config_section in Config._CR_CONFIG_VALUES: for config_value in Config._CR_CONFIG_VALUES[config_section]: try: Config._CR_CONFIG_VALUES[config_section][config_value] = \ Config.config.get(config_section, config_value) except ConfigParser.NoSectionError: config_must_be_updated = True log.log_error('Config section does not exist in the file: %s' % config_section) except ConfigParser.NoOptionError: config_must_be_updated = True log.log_error('Config value does not exist in the file: %s' % config_value) except: config_must_be_updated = True log.log_error('Error getting a config value: %s/%s' % (config_section, config_value)) # In this case, config file have been written by a last version of CR. # We must update it to include new sections or options. if config_must_be_updated: self.write_config_file()
def run(self): """ Constructor. """ is_error = False # If True, there are one or more errors when CentralReport is trying to start log.log_info("------------------------------------------------") log.log_info("CentralReport is starting...") log.log_info("Current user: "******"Debug", "log_level") except: log_level = "INFO" log.change_log_level(log_level) # Starting the check thread... if host.get_current_host().os != host.OS_UNKNOWN: log.log_info("%s detected. Starting ThreadChecks..." % host.get_current_host().os) CentralReport.checks_thread = threads.Checks() # Launching checks thread else: is_error = True log.log_critical("Sorry, but your OS is not supported yet...") # Starting the internal webserver... if not is_error and text.convert_text_to_bool(Config.get_config_value("Webserver", "enable")): local_web_port = int(Config.get_config_value("Webserver", "port")) if not utils_web.check_port("127.0.0.1", local_web_port): log.log_info("Starting the webserver...") # Importing the module here improve the memory usage import cr.web CentralReport.webserver_thread = cr.web.server.WebServer() else: log.log_error("Error launching the webserver: port %s is already in use on this host!" % local_web_port) else: log.log_info("Webserver is disabled by configuration file!") if not is_error: log.log_info("CentralReport started!") while CentralReport.is_running: 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: log.log_error("Pid file is not found. CentralReport must stop itself.") CentralReport.is_running = False self.stop() time.sleep(1) else: log.log_error("Error launching CentralReport!")
def run(self): """ Constructor. """ is_error = False # If True, there are one or more errors when CentralReport is trying to start log.log_info('------------------------------------------------') log.log_info('CentralReport is starting...') log.log_info('Current user: '******'Debug', 'log_level') except: log_level = 'INFO' log.change_log_level(log_level) # Starting the check thread... if host.get_current_host().os != host.OS_UNKNOWN: log.log_info('%s detected. Starting ThreadChecks...' % host.get_current_host().os) CentralReport.checks_thread = threads.Checks( ) # Launching checks thread else: is_error = True log.log_critical('Sorry, but your OS is not supported yet...') # Starting the internal webserver... if not is_error and text.convert_text_to_bool( Config.get_config_value('Webserver', 'enable')): local_web_port = int(Config.get_config_value('Webserver', 'port')) if not utils_web.check_port('127.0.0.1', local_web_port): log.log_info('Starting the webserver...') # Importing the module here improve the memory usage import cr.web CentralReport.webserver_thread = cr.web.server.WebServer() else: log.log_error( 'Error launching the webserver: port %s is already in use on this host!' % local_web_port) else: log.log_info('Webserver is disabled by configuration file!') if not is_error: log.log_info('CentralReport started!') while CentralReport.is_running: 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: log.log_error( 'Pid file is not found. CentralReport must stop itself.' ) CentralReport.is_running = False self.stop() time.sleep(1) else: log.log_error('Error launching CentralReport!')