def dispatch(self): #新建DispatchLog用来记录当前产生的日志 dispatchlog = DispatchLog(self.logname) for (host, connconfig) in self.dispatch_config.items(): #遍历HOST hostlogconfig = HostLogConfig(host, "SUCCESS", "") #初始化HOST日志 for dbconfig in connconfig.dbconfigs: #遍历HOST里面的DB dblogconfig = DbLogConfig(dbconfig.dbname, "SUCCESS", "") #初始化DB日志 if (self.is_connect(host, dbconfig.dbname)): #是否需要连接数据库 result = self.connect(connconfig, dbconfig) #连接数据库 if ("SUCCESS" == result[1]): #如果连接成功 cur = result[0].cursor() #获取数据库游标 cur.connection.autocommit(True) #设置游标为主动提交 for (filename, sqlfile) in self.search_sql.items(): #遍历文件 filelogconfig = FileLogConfig(filename, time.time(), "SUCCESS", "") #初始化文件日志 if (self.file_is_dispatch(host, dbconfig.dbname, sqlfile)): #该文件是否需要派发 for sql in sqlfile.sqls: sqllogconfig = SqlLogConfig(sql, "SUCCESS", "") if (self.sql_is_dispatch(host, dbconfig.dbname, filename, sql)): res = self.executesql(cur, sql); if ("ERROR" == res[0]): #执行SQL语句失败 #修改文件日志信息 sqllogconfig.error_tag = "ERROR" sqllogconfig.error_msg = ("Mysql Error: %d %s" %(res[1], res[2])) else: #不需要派发的错误sql,需要保存到新的日志中 sqllogconfig = self.dispatch_log[host][dbconfig.dbname][sqlfile.filename][sql] #即使不需要派发sql,如果文件有误,还是需要显示的 if (sqllogconfig.error_tag == "ERROR"): filelogconfig.error_tag = sqllogconfig.error_tag filelogconfig.error_msg = sqllogconfig.error_msg print "host:%s dbname:%s filename:%s [%s] %s\n" %(host, dbconfig.dbname, filename, sqllogconfig.error_tag, sqllogconfig.error_msg) filelogconfig[sql] = sqllogconfig if (filelogconfig.error_tag == "SUCCESS"): print "host:%s dbname:%s filename:%s [%s] %s\n" %(host, dbconfig.dbname, filename, filelogconfig.error_tag, filelogconfig.error_msg) else: #不需要派发的错误文件,需要保存到新的日志中 filelogconfig = self.dispatch_log[host][dbconfig.dbname][filename] if ("ERROR" == filelogconfig.error_tag): print "host:%s dbname:%s %s\n" %(host, dbconfig.dbname, filelogconfig.__str__()) #保存FILE日志到上一级DB日志 dblogconfig[filelogconfig.filename] = filelogconfig cur.close() #关闭游标 result[0].close() #关闭数据库连接 else: if (result[2] == self.HOST_ERROR or result[2] == self.PASS_ERROR): #连接错误 hostlogconfig.error_tag = "ERROR" hostlogconfig.error_msg = "Mysql Error: (%d) %s" %(result[2], result[3]) print hostlogconfig break; elif (result[2] == self.DB_ERROR or result[2] == self.CHARSET_ERROR): #DB错误 dblogconfig.error_tag = "ERROR" dblogconfig.error_msg = "Mysql Error: (%d) %s" %(result[2], result[3]) print "host:%s %s\n" %(host, dblogconfig.__str__()) else: #原有日志中的连接有错误,需要更新到最新的日志中来 if ("ERROR" == self.dispatch_log[host].error_tag): hostlogconfig.error_tag = self.dispatch_log[host].error_tag hostlogconfig.error_msg = self.dispatch_log[host].error_msg print hostlogconfig break; #原有数据库信息有错误,需要更新到最新的日志中来 elif ("ERROR" == self.dispatch_log[host][dbconfig.dbname].error_tag): dblogconfig.error_tag = self.dispatch_log[host][dbconfig.dbname].error_tag dblogconfig.error_msg = self.dispatch_log[host][dbconfig.dbname].error_msg print "host:%s %s\n" %(host, dblogconfig.__str__()) #保存DB日志到上一级的HOST日志 hostlogconfig[dbconfig.dbname] = dblogconfig #保存HOST日志到上一级的Dispatch日志 dispatchlog[host] = hostlogconfig #写日志 dispatchlog.write_log()