採集容器記憶體並寫到excel

來源:互聯網
上載者:User

標籤:stream   %s   exec   ret   $1   node   os.path   get   bsp   

 1 # coding=utf-8 2 import os 3 import commands 4 import re 5 from pyExcelerator import * 6  7  8 def execute(cmd): 9     status, output = commands.getstatusoutput(cmd)10     if status != 0:11         raise Exception(‘status is %s, output is %s‘ % (status, output))12     return output13 14 15 def get_docker_name():16     infos = execute("docker ps |awk ‘{print $1, $NF}‘").split(‘\n‘)17     regex = re.compile(‘\s+‘)18     id_name = {}19     for info in infos:20         docker_id, docker_name = regex.split(info)21         id_name[docker_id] = docker_name22     return id_name23 24 25 def get_docker_mem():26     regex = re.compile(‘\s+‘)27     ret = execute(‘docker stats --no-stream‘).split(‘\n‘)28     result_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), ‘docker_res.xlsx‘)29     id_name = get_docker_name()30     w = Workbook()31     ws = w.add_sheet(‘node_1_data‘)32     ws.write(0, 0, ‘docker_id‘)33     ws.write(0, 1, ‘docker_name‘)34     ws.write(0, 2, ‘mem(MB)‘)35     index = 136     for docker in ret:37         info = regex.split(docker)38         docker_id = info[0]39         mem = info[2]40         unit = info[3]41         if unit.startswith(‘G‘):42             mem = float(mem) * 102443         if unit.startswith(‘K‘):44             mem = float(mem) / 102445         try:46             mem = float(mem)47         except:48             pass49         name = id_name[docker_id]50         ws.write(index, 0, docker_id)51         ws.write(index, 1, name)52         ws.write(index, 2, mem)53         index += 154     w.save(result_name)55 56 57 if __name__ == ‘__main__‘:58     get_docker_mem()

 

採集容器記憶體並寫到excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.