def get_logs(_from, to, sp_path, file=True): # _from = "2019-04-25 17:11:01.201" # Object | Timerange start. See description for date format # to = "2019-04-25 17:26:01.201" # Object | Timerange end. See description for date format # sp_path = configuration = graylog.Configuration() configuration.username = env.graylog_username configuration.password = env.graylog_password configuration.host = env.graylog_host try: source = sp_path.split(".")[0].replace("http://","") except: source = sp_path query = "source:{} AND type:E".format(source) try: # Message search with absolute timerange. api_instance = graylog.SearchuniversalabsoluteApi() api_response = api_instance.search_absolute(query, _from, to) reply = api_response.to_dict() except ApiException as e: print(e) if file: with open('graylogs.log', 'w') as logs: for message in reply["messages"]: logs.write(message["message"]["timestamp"] + " " + message["message"]["container_name"] + ": " + message["message"].get("message") + "\n") else: for message in reply["messages"]: print(message["message"]["timestamp"] + " " + message["message"]["container_name"] + ": " + message["message"].get("message") + "\n")
counter += 1 if counter == 12: print("termination took longer than 2 minutes, aborting") time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] get_logging(time_start, time_finish, query="source:{} AND type:E".format(sp_path)) _exit += 1 # clean all packages tnglib.remove_all_packages() # Configuring the logging connection configuration = graylog.Configuration() configuration.username = "******" configuration.password = "******" configuration.host = "logs.sonata-nfv.eu:12900" tnglib.set_sp_path(os.environ["SP_PATH"]) tnglib.set_timeout(60) LOG = logging.getLogger(__name__) level = logging.DEBUG logging.getLogger("tnglib").setLevel(level) logging.getLogger(__name__).setLevel(level) logging.basicConfig(level=level) # Prering the timers test_start = current_time = int(time.time()) test_end = int(time.time()) + 86400 counter = 0