Logs are saved without formatting with a bunch of special characters, line breaks, etc. Plus, the log is written to UTF8, and when we pull it from the buffer of the remote machine to our local car it is filled in in unreadable form. How to bypass the problem with the encoding? Who can help? Each server has 10+ machines in which logs are written. We create lists of servers. In turn we go around each machine we create a list of logs and perform grep log (for example, by date). After we save the output to a separate file with the name of the machine with which the log was generated.

import paramiko import select import re lst=['192.168.0.1','192.168.0.2','192.168.0.3'] com = 'ls /home/servers/logs/ | grep Application' def SSH(server_address): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=server_address, username='user', password='pass', port=22) return ssh def run_commands_remote(command, server_address): session = SSH(server_address).get_transport().open_session() session.set_combine_stderr(True) session.get_pty() session.exec_command(command) stdin = session.makefile('wb', -1) stdout = session.makefile('rb', -1) return (stdout.read().decode()) #Функция греп лога def do_grep(log_file, grep_pattern, server_address ): remote_command = 'grep --line-buffered "%s" %s ' % (grep_pattern, log_file) print (remote_command) ssh = SSH(server_address) transport = ssh.get_transport() channel = transport.open_session() channel.exec_command(remote_command) while 1: try: buffer_string = channel.recv(256) File_log(str(log)+'.log', buffer_string) done = False while not done: more = channel.recv(256) if not more: done = True else: File_log(str(log)+'.log', more) except (KeyboardInterrupt, SystemExit): print ('got ctrl+c') break ssh.close() print ('client closed') #Удаляем пустые элементы в списке def clear(l): for e in l: if isinstance(e, list): clear(e) if isinstance(e, str) and len(e) == 0: l.remove(e) #Пишем в файл для создания списка def files(file, out): with open(file, 'w') as f: f.write(str(out)) f.close() f = open(file, 'r') f.seek(0) l = f.readlines() f.close() l=[spisok.rstrip("\n") for spisok in l] clear(l) return(l) #Создаем файл с отфильтрованным логом. def File_log(file_name, log): with open(file_name, 'a') as f: f.write(str(log)+"\n") f.close() for i in lst: servers = run_commands_remote(com, server_address=i) ls = files('servers.txt', servers) for log in ls: out1 = 'ls -lohtr /home/servers/logs/'+str(log) output = run_commands_remote(out1, server_address=i) ls2 = files('log.txt', output) pattern = 'Mar 02' out2 = '/home/servers/logs/'+str(log)+'/'+str(ls2) do_grep(out2,pattern,i) 
  • type code: with open(file, 'w') as f: ... f.close() hints that you should split the task into smaller steps and test them separately. By testing separately, with greater confidence and understanding of what the code does, you can try to integrate the individual steps into larger blocks and test them. - jfs
  • one
    Of course, such a solution has the right to exist, but why not simplify your life by merging logs from servers to one host (syslog) and stuff everything you need on that host? - de_frag
  • The architecture of the project has developed to me) And this is not a large cluster of servers. If you manage to collect logs from this cluster will be more intricate puzzles. - ksm638

0