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 signal_handler(self, signum, frame): """ Receives signals from the OS. """ if signum == signal.SIGTERM: # In this case, we must stop CentralReport immediatly! if not self.SIGTERM_SENT: self.SIGTERM_SENT = True # Prevents if SIGTERM is received twice. log.log_info('SIGTERM signal received (%s). Shutting Down...' % signum) log.log_info('Shutting down sub-processes...') os.killpg(0, signal.SIGTERM) self.stop() elif signum == signal.SIGINT: # Keyboard interruption (CTRL + C) log.log_info( 'SIGINT signal received (%s). Stopping CentralReport...' % signum) self.stop() else: log.log_debug('Unknown signal number: %s' % signum)
def __init__(self): """ Constructor """ # Managing config file if os.path.isfile(Config.CR_CONFIG_FULL_PATH): log.log_debug('Configuration file: Found. Reading it.') self.read_config_file() else: log.log_info('Configuration file: Not found. Creating it.') self.write_config_file()
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 __init__(self): threading.Thread.__init__(self) log.log_debug('ThreadChecks is starting...') # Standard output if cr.host.get_current_host().os == cr.host.OS_MAC: self.MyCollector = collectors.MacCollector() elif cr.host.get_current_host().family == cr.host.FAMILY_LINUX: self.MyCollector = collectors.DebianCollector() else: raise TypeError # Perform a check every xx ticks (1 tick = 1 second) try: self.tickPerformCheck = int(Config.get_config_value('Checks', 'interval')) except: self.tickPerformCheck = 60 log.log_debug('Interval between two checks: %s seconds' % self.tickPerformCheck) self.start()
def signal_handler(self, signum, frame): """ Receives signals from the OS. """ if signum == signal.SIGTERM: # In this case, we must stop CentralReport immediatly! if not self.SIGTERM_SENT: self.SIGTERM_SENT = True # Prevents if SIGTERM is received twice. log.log_info("SIGTERM signal received (%s). Shutting Down..." % signum) log.log_info("Shutting down sub-processes...") os.killpg(0, signal.SIGTERM) self.stop() elif signum == signal.SIGINT: # Keyboard interruption (CTRL + C) log.log_info("SIGINT signal received (%s). Stopping CentralReport..." % signum) self.stop() else: log.log_debug("Unknown signal number: %s" % signum)
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 perform_check(self): """ Performs a new global check. Can be raise a error if a check fail. """ log.log_debug('---- New check -----') check_entity = checks.Check() # Checking CPU log.log_debug('Performing a CPU check...') check_entity.cpu = self.MyCollector.get_cpu() # Checking memory log.log_debug('Performing a memory check...') check_entity.memory = self.MyCollector.get_memory() # Checking Load Average log.log_debug('Performing a load average check...') check_entity.load = self.MyCollector.get_loadaverage() # Checking disks information log.log_debug('Performing a disk check....') check_entity.disks = self.MyCollector.get_disks() # Updating last check date... check_entity.date = datetime.now() data.last_check = check_entity log.log_debug('All checks are done')