def create_service(): global system_config, system_logger, access_logger, session_service, document # Init configure. system_config = dict(SystemUtility.get_config()) # Avoid pylint error. # Init logger. system_logger = SystemUtility.get_system_log( system_config[ConfigKey.CONF_KEY_SYSTEM]) access_logger = SystemUtility.get_access_log( system_config[ConfigKey.CONF_KEY_SYSTEM]) # Init session service. session_service = SessionUtility.get_session_service( system_config[ConfigKey.CONF_KEY_SYSTEM]) # Init database. Base.metadata.create_all(bind=engine) # Init document. document = DocumentUtility.get_document( system_config[ConfigKey.CONF_KEY_SYSTEM]) system_logger.info(system_config) app = falcon.API(middleware=[CommonMiddleware()]) app.req_options.auto_parse_form_urlencoded = True app.add_static_route('/' + Const.PUBLIC_DIR, '/'.join( [os.path.dirname(os.path.abspath(__file__)), Const.PUBLIC_DIR])) for i in CommonRoute.get_routes() + PageRoute.get_routes() + ApiRoute.get_routes(): system_logger.info('add_route: {0}'.format(i[0])) app.add_route(i[0], i[1]) return app
def on_get(self, req, resp, hostname, resource): resp.status = falcon.HTTP_200 body = SystemUtility.get_response_base_with_body(Version.VERSION_1) doc_db = DocumentUtility.get_document() if resource == 'cpu': data = doc_db.latestCpuLog(hostname) body['data']['data'] = list( map( lambda x: { 'timestamp': x['timestamp'], 'ratio': x['cpu']['ratio'] }, data)) body['data']['key'] = ['ratio'] self.logger.debug(str(data)) elif resource == 'memory': data = doc_db.latestMemoryLog(hostname) body['data']['data'] = list( map( lambda x: { 'timestamp': x['timestamp'], 'ratio': x['memory']['ratio'] }, data)) body['data']['key'] = ['ratio'] self.logger.debug(str(data)) elif resource == 'swap': data = doc_db.latestSwapLog(hostname) body['data']['data'] = list( map( lambda x: { 'timestamp': x['timestamp'], 'swapped': x['memory']['swapped'] }, data)) body['data']['key'] = ['swapped'] self.logger.debug(str(data)) elif resource == 'storage': data = doc_db.latestStorageLog(hostname, 'SYSTEM') body['data']['data'] = data body['data']['key'] = ['ratio'] self.logger.debug(str(data)) elif resource == 'diskio': data = doc_db.latestIoLog(hostname) body['data']['data'] = list( map( lambda x: { 'timestamp': x['timestamp'], 'block_in': x['io']['block_in'], 'block_out': x['io']['block_out'] }, data)) body['data']['key'] = ['block_in', 'block_out'] self.logger.debug(str(data)) elif resource == 'network': pass else: resp.status = falcon.HTTP_400 SystemUtility.set_response_metadata( Version.VERSION_1, body, Message.RESPONSE_NG, Message.RESPONSE_REQUEST_URL_ERROR) resp.body = json.dumps(body, cls=CustomJSONEncoder)
def on_get(self, req, resp, hostname): resp.status = falcon.HTTP_200 body = SystemUtility.get_response_base_with_body(Version.VERSION_1) doc_db = DocumentUtility.get_document() session = Session() try: info = session.query(Server, Specification, Os).join( Specification, Server.id == Specification.server_id).join( Os, Specification.os_id == Os.id).filter( Server.hostname == hostname).first() self.logger.debug(info) body['data']['ip'] = info[0].ip body['data']['hostname'] = info[0].hostname body['data']['key'] = ['category', 'value', 'note'] body['data']['data'] = [{ 'category': 'OS', 'value': info[2].os_version, 'note': '' }, { 'category': 'CPUs', 'value': info[1].cpu_core, 'note': '' }, { 'category': 'Memory', 'value': "{0:.2f}GB".format(info[1].memory / 1024**2), 'note': '' }, { 'category': 'Swap', 'value': "{0:.2f}GB".format(info[1].swap / 1024**2), 'note': '' }, { 'category': 'Storage', 'value': "{0:.2f}GB".format(info[1].system_storage / 1024**2), 'note': '' }] except Exception as e: self.logger.error(e) session.rollback() resp.status = falcon.HTTP_500 SystemUtility.set_response_metadata( Version.VERSION_1, body, Message.RESPONSE_NG, Message.RESPONSE_DATABASE_CONNECTION_ERROR) finally: session.close() resp.body = json.dumps(body, cls=CustomJSONEncoder)
def init(): global system_config, system_logger, document # Init configure. system_config = dict(SystemUtility.get_config()) # Avoid pylint error. # Init logger. system_logger = SystemUtility.get_system_log( system_config[ConfigKey.CONF_KEY_SYSTEM]) # Init database. Base.metadata.create_all(bind=engine) # Init document. document = DocumentUtility.get_document( system_config[ConfigKey.CONF_KEY_SYSTEM]) system_logger.info(system_config)
def on_get(self, req, resp, resource, from_date, to_date): resp.status = falcon.HTTP_200 body = SystemUtility.get_response_base_with_body(Version.VERSION_1) doc_db = DocumentUtility.get_document() hostnames = req.get_param('hostnames').split(',') if resource == 'cpu': data = doc_db.searchMultipleCpuLog(hostnames, from_date, to_date) body['data']['data'] = _get_multiple_result(data, 'cpu', 'ratio') body['data']['key'] = hostnames self.logger.debug(str(data)) elif resource == 'memory': data = doc_db.searchMultipleMemoryLog(hostnames, from_date, to_date) body['data']['data'] = _get_multiple_result( data, 'memory', 'ratio') body['data']['key'] = hostnames self.logger.debug(str(data)) elif resource == 'swap': data = doc_db.searchMultipleSwapLog(hostnames, from_date, to_date) body['data']['data'] = _get_multiple_result( data, 'memory', 'swapped') body['data']['key'] = hostnames self.logger.debug(str(data)) elif resource == 'storage': data = doc_db.searchMultipleStorageLog(hostnames, 'SYSTEM', from_date, to_date) body['data']['data'] = _get_multiple_result(data, None, 'ratio') body['data']['key'] = hostnames self.logger.debug(str(data)) elif resource == 'diskio': io_type = hostnames = req.get_param('io_type') data = doc_db.searchMultipleIoLog(hostnames, io_type, from_date, to_date) body['data']['data'] = _get_multiple_result(data, 'io', io_type) body['data']['key'] = hostnames self.logger.debug(str(data)) elif resource == 'network': pass else: resp.status = falcon.HTTP_400 SystemUtility.set_response_metadata( Version.VERSION_1, body, Message.RESPONSE_NG, Message.RESPONSE_REQUEST_URL_ERROR) resp.body = json.dumps(body, cls=CustomJSONEncoder)
def test_insert_search_memory_log(init_document): doc = DocumentUtility.get_document() doc.addMemoryLog(datetime.now() - timedelta(minutes=15), "iris", "web/ap", "front", 0.33) doc.addMemoryLog(datetime.now() - timedelta(minutes=14), "iris", "web/ap", "front", 0.55) doc.addMemoryLog(datetime.now() - timedelta(minutes=13), "iris", "web/ap", "front", 0.63) doc.addMemoryLog(datetime.now() - timedelta(minutes=12), "iris", "web/ap", "front", 0.64) doc.addMemoryLog(datetime.now() - timedelta(minutes=11), "iris", "web/ap", "front", 0.65) doc.addMemoryLog(datetime.now() - timedelta(minutes=10), "iris", "web/ap", "front", 0.66) doc.addMemoryLog(datetime.now() - timedelta(minutes=9), "iris", "web/ap", "front", 0.67) doc.addMemoryLog(datetime.now() - timedelta(minutes=8), "iris", "web/ap", "front", 0.22) doc.addMemoryLog(datetime.now() - timedelta(minutes=7), "iris", "web/ap", "front", 0.24) doc.addMemoryLog(datetime.now() - timedelta(minutes=6), "iris", "web/ap", "front", 0.24) doc.addMemoryLog(datetime.now() - timedelta(minutes=5), "iris", "web/ap", "front", 0.24) doc.addMemoryLog(datetime.now() - timedelta(minutes=4), "iris", "web/ap", "front", 0.24) doc.addMemoryLog(datetime.now() - timedelta(minutes=3), "iris", "web/ap", "front", 0.33) doc.addMemoryLog(datetime.now() - timedelta(minutes=2), "iris", "web/ap", "front", 0.34) doc.addMemoryLog(datetime.now() - timedelta(minutes=1), "iris", "web/ap", "front", 0.33) res = doc.searchMemoryLog("iris", datetime.now() - timedelta(minutes=3), datetime.now()) print(res) assert True
def test_insert_search_cpu_log(init_document): doc = DocumentUtility.get_document() doc.addCpuLog(datetime.now() - timedelta(minutes=15), "iris", "web/ap", "front", 0.05) doc.addCpuLog(datetime.now() - timedelta(minutes=14), "iris", "web/ap", "front", 0.27) doc.addCpuLog(datetime.now() - timedelta(minutes=13), "iris", "web/ap", "front", 0.42) doc.addCpuLog(datetime.now() - timedelta(minutes=12), "iris", "web/ap", "front", 0.21) doc.addCpuLog(datetime.now() - timedelta(minutes=11), "iris", "web/ap", "front", 0.11) doc.addCpuLog(datetime.now() - timedelta(minutes=10), "iris", "web/ap", "front", 0.86) doc.addCpuLog(datetime.now() - timedelta(minutes=9), "iris", "web/ap", "front", 0.33) doc.addCpuLog(datetime.now() - timedelta(minutes=8), "iris", "web/ap", "front", 0.48) doc.addCpuLog(datetime.now() - timedelta(minutes=7), "iris", "web/ap", "front", 0.34) doc.addCpuLog(datetime.now() - timedelta(minutes=6), "iris", "web/ap", "front", 0.01) doc.addCpuLog(datetime.now() - timedelta(minutes=5), "iris", "web/ap", "front", 0.23) doc.addCpuLog(datetime.now() - timedelta(minutes=4), "iris", "web/ap", "front", 0.11) doc.addCpuLog(datetime.now() - timedelta(minutes=3), "iris", "web/ap", "front", 0.91) doc.addCpuLog(datetime.now() - timedelta(minutes=2), "iris", "web/ap", "front", 0.42) doc.addCpuLog(datetime.now() - timedelta(minutes=1), "iris", "web/ap", "front", 0.65) res = doc.searchCpuLog("iris", datetime.now() - timedelta(minutes=3), datetime.now()) print(res) assert True
def test_insert_search_swap_log(init_document): doc = DocumentUtility.get_document() doc.addSwapLog(datetime.now() - timedelta(minutes=15), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=14), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=13), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=12), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=11), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=10), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=9), "iris", "web/ap", "front", 0.0) doc.addSwapLog(datetime.now() - timedelta(minutes=8), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=7), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=6), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=5), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=4), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=3), "iris", "web/ap", "front", 0.1) doc.addSwapLog(datetime.now() - timedelta(minutes=2), "iris", "web/ap", "front", 0.2) doc.addSwapLog(datetime.now() - timedelta(minutes=1), "iris", "web/ap", "front", 0.2) res = doc.searchSwapLog("iris", datetime.now() - timedelta(minutes=3), datetime.now()) print(res) assert True
def init_document(): conf = dict(SystemUtility.get_config()) SystemUtility.get_system_log(conf[ConfigKey.CONF_KEY_SYSTEM]) SystemUtility.get_access_log(conf[ConfigKey.CONF_KEY_SYSTEM]) DocumentUtility.get_document(conf[ConfigKey.CONF_KEY_SYSTEM])