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 datetimeToTimestamp(datetime): """ Converts a datetime to Unix timestamp. """ timestamp = 0 try: # Uses the local timestamp timestamp = int(time.mktime(datetime.timetuple())) except: crLog.writeError('Error convert datetime to timestamp') return timestamp
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 readConfigFile(self): """ Reads the configuration file. """ crLog.writeDebug('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 crLog.writeError('Config section does not exist in the file: ' + config_section) except ConfigParser.NoOptionError: config_must_be_updated = True crLog.writeError('Config value does not exist in the file: ' + config_value) except: config_must_be_updated = True crLog.writeError('Error getting a config value: ' + 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.writeConfigFile()
# if '__main__' == __name__: daemon = CentralReport(Config.CR_PID_FILE) if 2 == len(sys.argv): if 'start' == sys.argv[1]: daemon.start() elif 'stop' == sys.argv[1]: daemon.stop() elif 'restart' == sys.argv[1]: daemon.restart() elif 'status' == sys.argv[1]: pid = daemon.status() if 0 == pid: print 'CentralReport is not running' else: print 'CentralReport is running with pid ' + str(pid) else: crLog.writeError('Unknown command') sys.exit(2) sys.exit(0) else: crLog.writeError("usage: %s start|stop|restart|status" % sys.argv[0]) sys.exit(2)