def check_cpu_utilization(log_file):
    pattern = re.compile(('.*%s.*' % peer))
    pattern1 = re.compile(('.*%s.*' % order))
    cpus = []
    mems = []
    with os.popen('sudo docker stats --no-stream') as f:
        for s in f.readlines():
            ss = s.split()
            if ((len(ss) >= 3) and (pattern.match(ss[1]) or pattern1.match(ss[1]))):
                cu = float(ss[2].replace('%', ''))
                cpus.append(cu)
                name = ss[1]
                mem = float(ss[6].replace('%', ''))
                mems.append(mem)
                print(('INFO: container %s: cpu %.2f%%, mem %.2f%%' % (ss[1], cu, mem)))
    num = len(cpus)
    avg_cpu = ((sum(cpus) / num) if (num > 0) else (- 1))
    avg_mem = ((sum(mems) / num) if (num > 0) else (- 1))
    log_file.write(('%s,%d,%.2f,%.2f,%s\n' % (datetime.now().strftime('%H:%M:%S'), num, avg_cpu, avg_mem, ','.join((('%.2f,%.2f' % (cpus[i], mems[i])) for i in range(num))))))
    log_file.flush()
    return avg_cpu
def check_cpu_utilization(log_file):
    pattern = re.compile(('.*%s.*' % peer))
    pattern1 = re.compile(('.*%s.*' % order))
    cpus = []
    mems = []
    with os.popen('sudo docker stats --no-stream') as f:
        for s in f.readlines():
            ss = s.split()
            if ((len(ss) >= 3) and (pattern.match(ss[1]) or pattern1.match(ss[1]))):
                cu = float(ss[2].replace('%', ))
                cpus.append(cu)
                name = ss[1]
                mem = float(ss[6].replace('%', ))
                mems.append(mem)
                print(('INFO: container %s: cpu %.2f%%, mem %.2f%%' % (ss[1], cu, mem)))
    num = len(cpus)
    avg_cpu = ((sum(cpus) / num) if (num > 0) else (- 1))
    avg_mem = ((sum(mems) / num) if (num > 0) else (- 1))
    log_file.write(('%s,%d,%.2f,%.2f,%s\n' % (datetime.now().strftime('%H:%M:%S'), num, avg_cpu, avg_mem, ','.join((('%.2f,%.2f' % (cpus[i], mems[i])) for i in range(num))))))
    log_file.flush()
    return avg_cpu