def insert_agency(url, method, status_code, host, path, content_type, request_headers, response_headers, request_content, response_content): sql = '''INSERT INTO agency(url,method,status_code,host,path,\ content_type,\ request_headers,\ response_headers,\ request_content,\ response_content\ )VALUES( '{url}','{method}',{status_code},'{host}','{path}',\ '{content_type}',\ '{request_headers}',\ '{response_headers}',\ '{request_content}',\ '{response_content}')'''.format( url=url, method=method, status_code=status_code, host=host, path=path, content_type=content_type, request_headers=request_headers.replace("'", "''"), response_headers=response_headers.replace("'", '"'), request_content=request_content.replace("'", '"'), response_content=response_content.replace("'", '"')) try: # print(sql) c.execute(sql) conn.commit() return True except Exception as e: logging.warning("save data error SQL:", sql) print(sql) return e
def try_find(self, element_loc): """尝试定位, 处理偶现元素, 或判断元素是否存在""" logging.info("尝试定位元素: {}".format(element_loc)) try: return self.driver.find_element(*element_loc) except NoSuchElementException: logging.warning("元素未出现: {}".format(element_loc))
def async_cli(self, action, coin): ''' eventually offer async_cli functions :param action: :param coin: :return: ''' try: cmd = f"{self.installed_folder}/{coin.cli} -datadir={self.wallet_directory} {action}" logging.info( f"Attempting to execute command from masternode object: {cmd}") ''' need to have a threadpool and throw this in there and await the result ''' result = self.connection.run(cmd, hide=False) logging.info( f"Executed {result.command} on {result.connection.host}, got stdout:\n{result.stdout}" ) return result.stdout except UnexpectedExit as e: #possibly try to start the daemon again logging.warning(f"{coin.cli} exited unexpectedly", exc_info=e) return '{"status":"restart"}' except Exception as e: logging.error(f"Could not do action on daemon at {self.getIP()}")
def kill_daemon(self, coin): try: kill = f"{self.installed_folder}/{coin.cli} -datadir={self.wallet_directory} stop" result = self.connection.run(kill, hide=False) return result.stdout except UnexpectedExit as e: #possibly try to start the daemon again logging.warning(f"{coin.cli} exited unexpectedly", exc_info=e) return '{"status":"restart"}' except Exception as e: logging.error(f"Could not do action on daemon at {self.getIP()}") return '{"status":"restart"}'
def daemon_action(self, coin, reindex=0): try: cmd = f"{self.installed_folder}/{coin.daemon} -datadir={self.wallet_directory}" if reindex == 1: cmd += " -reindex" result = self.connection.run(cmd, hide=False) logging.info( f"Executed {result.command} on {result.connection.host}, got stdout:\n{result.stdout}" ) return result.stdout except UnexpectedExit as e: logging.warning(f"{coin.daemon} exited unexpectedly", exc_info=e) return '{"status":"restart"}' except Exception as e: logging.error(f"Could not do action on daemon at {self.getIP()}")
def install_sentinel(self, coin): try: self.connection.put(coin.scripts["local_path"] + coin.scripts["sentinel_setup"]) cmd = f"/bin/bash {coin.scripts['sentinel_setup']} {coin.sentinel_git} {self.installed_folder} {coin.coin_name} {self.wallet_directory}" result = self.connection.run(cmd, hide=False) logging.info(f"Uploaded sentinel_setup.sh:\n {result}") return result except UnexpectedExit as e: logging.warning(f"{coin.cli} exited unexpectedly ", exc_info=e) return '{"status":"failed"}' except Exception as e: logging.error( f"Could not do_action {self.masternode['connection_string']} : {e}", exc_info=e) return '{"status":"failed"}'
def init_table(): ''' 创建数据表 ''' create_table_sql_sqlite = ''' create table agency(id INT AUTO_INCREMENT primary key, url varchar, method varchar, status_code smallint, host varchar, path varchar, content_type varchar, request_headers text, response_headers text, request_content text null, response_content text, recodetime datetime DEFAULT CURRENT_TIMESTAMP ) ''' create_table_sql = ''' create table agency( id INT AUTO_INCREMENT primary key, url varchar(255), method varchar(20), status_code smallint, host varchar(255), path varchar(255), content_type varchar(128), request_headers text, response_headers text, request_content text null, response_content text, recodetime datetime DEFAULT NOW() )ENGINE=InnoDB DEFAULT CHARSET=utf8; ''' try: c.execute(create_table_sql_sqlite) conn.commit() except InternalError: pass except OperationalError: pass except Exception as e: logging.warning('数据库创建失败') print("创建数据库失败:", e)
def install_watcher(self, coin): try: self.connection.put(coin.scripts["local_path"] + coin.scripts["watcher_cron"]) logging.info('Uploaded watcher_cron.sh') cmd = f"/bin/bash {coin.scripts['watcher_cron']} {coin.name} {self.installed_folder} {coin.daemon} {self.wallet_directory}" result = self.connection.run(cmd, hide=False) if result.stdout == '' and result.stderr == '': return "{'status':'success'}" return "{'status':'There was a problem installing watcher'}" except UnexpectedExit as e: logging.warning(f"{coin.cli} exited unexpectedly", exc_info=e) return '{"status":"failed"}' except Exception as e: logging.error( f"Could not do_action {self.masternode['connection_string']} : {e}", exc_info=e) return '{"status":"failed"}'
def upgrade(self, coin): try: self.connection.put("Polis/" + coin.version_to_upload) logging.info(f"Uploaded {coin.version_to_upload}") self.connection.put(coin.scripts["local_path"] + coin.scripts["upgrade"]) cmd = f"/bin/bash {coin.scripts['upgrade']} {coin.cli} {self.installed_folder} {coin.daemon} {self.wallet_directory} '{coin.addnode}'" logging.info( f"Uploaded {coin.scripts['local_path']+coin.scripts['upgrade']}" ) result = self.connection.run(cmd, hide=False) logging.info("Done executing: ".format(result.stdout)) success = f"success: result.stdout: {result.stdout}" return success except UnexpectedExit as e: logging.warning("Problem upgrading", exc_info=e) return '{"status":"failed"}' except Exception as e: logging.warning(f"Could not upload daemon bin: {e} ", exc_info=e) return '{"status":"failed"}'
def processDir(dir_proc): """传入文件夹, 从中筛选日志记录, 并进行读取 Args: dir_proc: 需要处理的日志文件夹 Returns: TODO """ for file in os.listdir(dir_proc): if os.path.isdir(os.path.join(dir_proc, file)): logging.warning("%s is a directory" % (file)) processDir(os.path.join(dir_proc, file)) continue matchs = logFilePattern.match(file) if matchs is None: logging.warning("%s is not a log file" % (file)) continue logging.info("process file %s, file date is %s" % (file, matchs.group('date')))
# # datefmt='%a, %d %b %Y %H:%M:%S', # datefmt='%H:%M:%S', # filename='myapp1.log', # filemode='w') from config import logging from logging_t2 import testlog from multiprocessing import Process if __name__ == '__main__': p = Process(target=testlog, args=(10, )) print('开始 Process') p.start() for x in range(20): print('for loop:', x) if x % 3 == 0: logging.error('错误') if x % 4 == 0: logging.fatal('致命错误') else: logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message') sleep(2) p.join() print('结束 Process')