def __init__(self): """ Manages the small webserver. """ threading.Thread.__init__(self) # Register events... cherrypy.engine.subscribe('graceful', self.stop) # Update the configuration... cherrypy.config.update({'server.socket_host': Config.getConfigValue('Webserver', 'interface'), 'server.socket_port': int(Config.getConfigValue('Webserver', 'port'))}) cherrypy.config.update({'tools.staticdir.root': WebServer.current_dir}) cherrypy.config.update({'log.screen': False}) # Serving static content confStaticContent = { '/statics': { 'tools.staticdir.dir': 'statics', 'tools.staticdir.on': True }, '/css': { 'tools.staticdir.dir': 'css', 'tools.staticdir.on': True }, '/img': { 'tools.staticdir.dir': 'img', 'tools.staticdir.on': True }, '/js': { 'tools.staticdir.dir': 'js', 'tools.staticdir.on': True }, '/media': { 'tools.staticdir.dir': 'media', 'tools.staticdir.on': True }, '/api/check': { 'request.dispatch': self.setupRoutes() } } # Using Pages class (see below) webApplication = cherrypy.tree.mount(Pages(self.env), '/', confStaticContent) # Disable screen log (standard out) if False == Config.CR_CONFIG_ENABLE_DEBUG_MODE: cherrypy.log.error_log.propagate = False cherrypy.log.access_log.propagate = False self.start()
def run(self): """ Executes checks. """ # Getting informations about the current host Checks.hostEntity = self.MyCollector.get_infos() while Checks.performChecks: if self.tickPerformCheck <= self.tickCount: crLog.writeDebug('---- New check -----') # Checking CPU if crUtilsText.textToBool(Config.getConfigValue('Checks', 'enable_cpu_check')): crLog.writeDebug('Doing a CPU check...') Checks.last_check_cpu = self.MyCollector.get_cpu() # Checking memory if crUtilsText.textToBool(Config.getConfigValue('Checks', 'enable_memory_check')): crLog.writeDebug('Doing a memory check...') Checks.last_check_memory = self.MyCollector.get_memory() # Checking Load Average if crUtilsText.textToBool(Config.getConfigValue('Checks', 'enable_load_check')): crLog.writeDebug('Doing a load average check...') Checks.last_check_loadAverage = self.MyCollector.get_loadaverage() # Checking disks informations if crUtilsText.textToBool(Config.getConfigValue('Checks', 'enable_disks_check')): crLog.writeDebug('Doing a disk check....') Checks.last_check_disk = self.MyCollector.get_disks() # Updating last check date... Checks.last_check_date = datetime.datetime.now() # Update the last check date # Wait 60 seconds before next checks... crLog.writeDebug('All checks are done') crLog.writeDebug('Next checks in ' + str(self.tickPerformCheck) + ' seconds...') self.tickCount = 0 # new tick self.tickCount += 1 time.sleep(1)
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 api_date_check(self): tmpl = self.env.get_template('json/date_check.json') cherrypy.response.headers['Content-Type'] = 'application/json' tmpl_vars = dict() if Checks.last_check_date is None: tmpl_vars['last_timestamp'] = '0' tmpl_vars['last_fulldate'] = 'Never' else: tmpl_vars['last_timestamp'] = crUtilsDate.datetimeToTimestamp(Checks.last_check_date) tmpl_vars['last_fulldate'] = Checks.last_check_date.strftime("%Y-%m-%d %H:%M:%S") tmpl_vars['current_timestamp'] = crUtilsDate.datetimeToTimestamp(datetime.datetime.now()) try: tmpl_vars['checks_interval'] = int(Config.getConfigValue('Checks', 'interval')) except: tmpl_vars['checks_interval'] = int(Config.CR_CONFIG_DEFAULT_CHECKS_INTERVAL) return tmpl.render(tmpl_vars)
def __init__(self): threading.Thread.__init__(self) crLog.writeDebug('ThreadChecks is starting...') # Standard output # What is the current os? if Config.HOST_CURRENT == Config.HOST_MAC: self.MyCollector = crCollectors.MacCollector() elif (Config.HOST_CURRENT == Config.HOST_DEBIAN) \ | (Config.HOST_CURRENT == Config.HOST_UBUNTU): self.MyCollector = crCollectors.DebianCollector() # Perform a check every xx ticks (1 tick = 1 second) try: self.tickPerformCheck = int(Config.getConfigValue('Checks', 'interval')) except: self.tickPerformCheck = 60 crLog.writeDebug('Interval between two checks: ' + str(self.tickPerformCheck) + ' seconds') self.start()
def api_full_check(self): tmpl = self.env.get_template('json/full_check.json') cherrypy.response.headers['Content-Type'] = 'application/json' tmpl_vars = dict() if Checks.last_check_date is None: tmpl_vars['last_timestamp'] = '0' tmpl_vars['last_fulldate'] = 'Never' else: tmpl_vars['last_timestamp'] = crUtilsDate.datetimeToTimestamp(Checks.last_check_date) tmpl_vars['last_fulldate'] = Checks.last_check_date.strftime("%Y-%m-%d %H:%M:%S") tmpl_vars['current_timestamp'] = crUtilsDate.datetimeToTimestamp(datetime.datetime.now()) # CPU Check informations if Checks.last_check_cpu is None: tmpl_vars['cpu_check_enabled'] = 'False' else: tmpl_vars['cpu_check_enabled'] = 'True' tmpl_vars['cpu_percent'] = int(Checks.last_check_cpu.user) + int(Checks.last_check_cpu.system) tmpl_vars['cpu_user'] = Checks.last_check_cpu.user tmpl_vars['cpu_system'] = Checks.last_check_cpu.system if int(Config.getConfigValue('Alerts', 'cpu_alert')) <= int(tmpl_vars['cpu_percent']): tmpl_vars['cpu_state'] = "alert" elif int(Config.getConfigValue('Alerts', 'cpu_warning')) <= int(tmpl_vars['cpu_percent']): tmpl_vars['cpu_state'] = "warning" else: tmpl_vars['cpu_state'] = 'ok' # Memory check informations if Checks.last_check_memory is None: tmpl_vars['memory_check_enabled'] = 'False' else: tmpl_vars['memory_check_enabled'] = "True" tmpl_vars['memory_percent'] = ((int(Checks.last_check_memory.total) - int( Checks.last_check_memory.free)) * 100) / int(Checks.last_check_memory.total) tmpl_vars['memory_free'] = crUtilsText.convertByte(Checks.last_check_memory.free) tmpl_vars['memory_total'] = crUtilsText.convertByte(Checks.last_check_memory.total) tmpl_vars['memory_used'] = crUtilsText.convertByte( float(Checks.last_check_memory.total) - float(Checks.last_check_memory.free)) if int(tmpl_vars['memory_percent']) >= int(Config.getConfigValue('Alerts', 'memory_alert')): tmpl_vars['memory_state'] = "alert" elif int(tmpl_vars['memory_percent']) >= int(Config.getConfigValue('Alerts', 'memory_warning')): tmpl_vars['memory_state'] = "warning" else: tmpl_vars['memory_state'] = 'ok' # Last: swap stats if 0 != int(Checks.last_check_memory.swapSize): tmpl_vars['swap_percent'] = int(Checks.last_check_memory.swapUsed) * 100 / int( Checks.last_check_memory.swapSize) tmpl_vars['swap_used'] = crUtilsText.convertByte(Checks.last_check_memory.swapUsed) tmpl_vars['swap_free'] = crUtilsText.convertByte(Checks.last_check_memory.swapFree) tmpl_vars['swap_size'] = crUtilsText.convertByte(Checks.last_check_memory.swapSize) # On Mac, the swap is unlimited (only limited by the available hard drive size) if Checks.last_check_memory.swapSize == Checks.last_check_memory.total: tmpl_vars['swap_configuration'] = 'unlimited' else: tmpl_vars['swap_configuration'] = 'limited' if isinstance(tmpl_vars['swap_percent'], int): if int(tmpl_vars['swap_percent']) >= int(Config.getConfigValue('Alerts', 'swap_alert')): tmpl_vars['swap_state'] = 'alert' elif int(tmpl_vars['swap_percent']) >= int(Config.getConfigValue('Alerts', 'swap_warning')): tmpl_vars['swap_state'] = 'warning' else: tmpl_vars['swap_state'] = 'ok' else: tmpl_vars['swap_state'] = 'ok' else: # No swap available on this host tmpl_vars['swap_configuration'] = 'undefined' # Load average if Checks.last_check_loadAverage is None: tmpl_vars['load_check_enabled'] = 'False' else: tmpl_vars['load_check_enabled'] = "True" tmpl_vars['load_last_one'] = Checks.last_check_loadAverage.last1m tmpl_vars['load_percent'] = (float(Checks.last_check_loadAverage.last1m) * 100) / int( Checks.hostEntity.cpuCount) if int(tmpl_vars['load_percent']) >= int(Config.getConfigValue('Alerts', 'load_alert')): tmpl_vars['load_state'] = "alert" elif int(tmpl_vars['load_percent']) >= int(Config.getConfigValue('Alerts', 'load_warning')): tmpl_vars['load_state'] = "warning" else: tmpl_vars['load_state'] = 'ok' tmpl_vars['uptime_full_text'] = crUtilsText.secondsToPhraseTime( int(Checks.last_check_loadAverage.uptime)) tmpl_vars['uptime_seconds'] = crUtilsText.numberSeparators(str(Checks.last_check_loadAverage.uptime)) tmpl_vars['start_date'] = datetime.datetime.fromtimestamp( crUtilsDate.datetimeToTimestamp(Checks.last_check_date) - int( Checks.last_check_loadAverage.uptime)).strftime("%Y-%m-%d %H:%M:%S") return tmpl.render(tmpl_vars)
def index(self): tmpl = self.env.get_template('index.tpl') tmpl_vars = dict() # Host informations tmpl_vars['hostname'] = Checks.hostEntity.hostname tmpl_vars['os_name'] = Checks.hostEntity.osName tmpl_vars['os_version'] = Checks.hostEntity.osVersion if Config.HOST_CURRENT == Config.HOST_MAC: tmpl_vars['host_os'] = 'MAC' elif Config.HOST_CURRENT == Config.HOST_UBUNTU: tmpl_vars['host_os'] = 'UBUNTU' elif Config.HOST_CURRENT == Config.HOST_DEBIAN: tmpl_vars['host_os'] = 'DEBIAN' tmpl_vars['CR_version'] = Config.CR_VERSION tmpl_vars['CR_version_name'] = Config.CR_VERSION_NAME if Checks.last_check_date is None: tmpl_vars['last_check'] = 'Never' else: tmpl_vars['last_check'] = Checks.last_check_date.strftime("%Y-%m-%d %H:%M:%S") # CPU stats if Checks.last_check_cpu is not None: tmpl_vars['cpu_percent'] = 100 - int(Checks.last_check_cpu.idle) tmpl_vars['cpu_user'] = Checks.last_check_cpu.user tmpl_vars['cpu_system'] = Checks.last_check_cpu.system tmpl_vars['cpu_count'] = Checks.hostEntity.cpuCount if int(tmpl_vars['cpu_percent']) >= int(Config.getConfigValue('Alerts', 'cpu_alert')): tmpl_vars['cpu_alert'] = True elif int(tmpl_vars['cpu_percent']) >= int(Config.getConfigValue('Alerts', 'cpu_warning')): tmpl_vars['cpu_warning'] = True else: tmpl_vars['cpu_ok'] = True # Memory and swap stats if Checks.last_check_memory is not None: # First: Memory stats tmpl_vars['memory_percent'] = ((int(Checks.last_check_memory.total) - int( Checks.last_check_memory.free)) * 100) / int(Checks.last_check_memory.total) tmpl_vars['memory_free'] = crUtilsText.convertByte(Checks.last_check_memory.free) tmpl_vars['memory_total'] = crUtilsText.convertByte(Checks.last_check_memory.total) tmpl_vars['memory_used'] = crUtilsText.convertByte( float(Checks.last_check_memory.total) - float(Checks.last_check_memory.free)) # Memory status if int(tmpl_vars['memory_percent']) >= int(Config.getConfigValue('Alerts', 'memory_alert')): tmpl_vars['memory_alert'] = True elif int(tmpl_vars['memory_percent']) >= int(Config.getConfigValue('Alerts', 'memory_warning')): tmpl_vars['memory_warning'] = True else: tmpl_vars['memory_ok'] = True # Last: swap stats if 0 != int(Checks.last_check_memory.swapSize): tmpl_vars['swap_percent'] = int(Checks.last_check_memory.swapUsed) * 100 / int( Checks.last_check_memory.swapSize) tmpl_vars['swap_used'] = crUtilsText.convertByte(Checks.last_check_memory.swapUsed) tmpl_vars['swap_free'] = crUtilsText.convertByte(Checks.last_check_memory.swapFree) tmpl_vars['swap_size'] = crUtilsText.convertByte(Checks.last_check_memory.swapSize) # On Mac, the swap is unlimited (only limited by the available hard drive size) if Checks.last_check_memory.swapSize == Checks.last_check_memory.total: tmpl_vars['swap_configuration'] = 'unlimited' else: tmpl_vars['swap_configuration'] = 'limited' if isinstance(tmpl_vars['swap_percent'], int): if int(tmpl_vars['swap_percent']) >= int(Config.getConfigValue('Alerts', 'swap_alert')): tmpl_vars['swap_alert'] = True elif int(tmpl_vars['swap_percent']) >= int(Config.getConfigValue('Alerts', 'swap_warning')): tmpl_vars['swap_warning'] = True else: tmpl_vars['swap_ok'] = True else: tmpl_vars['swap_ok'] = True else: # No swap available on this host tmpl_vars['swap_configuration'] = 'undefined' # Load average stats if Checks.last_check_loadAverage is not None: tmpl_vars['loadaverage'] = Checks.last_check_loadAverage.last1m tmpl_vars['loadaverage_percent'] = (float(Checks.last_check_loadAverage.last1m) * 100) / int( Checks.hostEntity.cpuCount) if int(tmpl_vars['loadaverage_percent']) >= int(Config.getConfigValue('Alerts', 'load_alert')): tmpl_vars['load_alert'] = True elif int(tmpl_vars['loadaverage_percent']) >= int(Config.getConfigValue('Alerts', 'load_warning')): tmpl_vars['load_warning'] = True else: tmpl_vars['load_ok'] = True # Uptime stats (checked in load average collector) if Checks.last_check_loadAverage is not None: tmpl_vars['uptime'] = crUtilsText.secondsToPhraseTime(int(Checks.last_check_loadAverage.uptime)) tmpl_vars['uptime_seconds'] = crUtilsText.numberSeparators(str(Checks.last_check_loadAverage.uptime)) tmpl_vars['start_date'] = datetime.datetime.fromtimestamp( crUtilsDate.datetimeToTimestamp(Checks.last_check_date) - int( Checks.last_check_loadAverage.uptime)).strftime("%Y-%m-%d %H:%M:%S") # DIsks stats if Checks.last_check_disk is not None: allChecksDisk = [] for disk in Checks.last_check_disk.checks: checkDisk = {} # TODO: Find a better solution to decode UTF8 checkDisk['name'] = str.replace(disk.name, '/dev/', '').decode('utf-8') checkDisk['free'] = crUtilsText.convertByte(disk.free) checkDisk['total'] = crUtilsText.convertByte(disk.size) checkDisk['percent'] = int(round(disk.used, 0) * 100 / int(disk.size)) allChecksDisk.append(checkDisk) tmpl_vars['disks'] = allChecksDisk return tmpl.render(tmpl_vars)
resultEnableWebServer = raw_input('Do you want to enable the internal web server? [y/n] ') if('y' == resultEnableWebServer.lower()[:1]): validEnableWebServer = True Config.setConfigValue('Webserver', 'enable', True) elif 'n' == resultEnableWebServer.lower()[:1]: validEnableWebServer = True Config.config_webserver_enable = False Config.setConfigValue('Webserver', 'enable', False) else: print 'We do not understand your answer. Please use "yes" or "no"' # If the webserver is enabled, we can ask the default port for it. if bool(Config.getConfigValue('Webserver', 'enable')): print '\n' print 'Default port is 8080. You can choose a custom port if you want.' validPort = False while not validPort: resultPort = raw_input('Use this port: ') resultPortInt = 0 try: resultPortInt = int(resultPort) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(('127.0.0.1', int(resultPort))) except socket.error, e: