1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
''' 功能介绍: 1、调用阿里云API,收集所有区域 ECS 信息 2、将需要的数据整理、生成 Excel 文档 3、关于阿里 sdk 的安装,api 的调用请参考阿里云官网 4、xlsxwriter 请参考这里:http://xlsxwriter.readthedocs.org/ '''
import json, sys
try: from termcolor import colored from xlsxwriter import workbook from aliyunsdkcore import client from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest except ImportError as e: print(colored('%s : %s' % ('Error', e), 'red')) exit(9)
reload(sys)
sys.setdefaultencoding('utf8')
def get_sys_info(key, secret, zone, page): ''' 1、获取该区域全部主机详细信息 2、参数:cn-qingdao、cn-hangzhou、cn-beijing 等 ''' clt = client.AcsClient(key, secret, zone) request = DescribeInstancesRequest.DescribeInstancesRequest() request.set_PageSize(100) request.set_PageNumber(page) request.set_accept_format('json') result = json.loads(clt.do_action(request)).get('Instances').get('Instance')
return result
def format_data(data_info): ''' 从全部数据中整理出需要的数据 ''' result = []
for line in data_info: data = ( line.get('InstanceId'), line.get('ZoneId'), line.get('HostName'), line.get('InstanceName'), line.get('PublicIpAddress').get('IpAddress'), line.get('InnerIpAddress').get('IpAddress'), line.get('Cpu'), line.get('Memory'), line.get('InternetMaxBandwidthOut'), line.get('Status'), line.get('CreationTime'), line.get('ExpiredTime') ) result.append(data)
return result
def write_excel(file, data): ''' 1、设置 Excel 样式 2、将数据写入到 Excel 中 ''' work = workbook.Workbook(file) worksheet = work.add_worksheet() format_title = work.add_format({'bold': True, 'font_size': 16}) format_title.set_align('center') format_title.set_align('vcenter')
format_body = work.add_format({'font_size': 14}) worksheet.set_row(0, 25) worksheet.set_column(0, 0, 30) worksheet.set_column(1, 1, 20) worksheet.set_column(2, 3, 28) worksheet.set_column(4, 5, 25) worksheet.set_column(6, 6, 12) worksheet.set_column(7, 9, 16) worksheet.set_column(10, 11, 25) title = ( '实例 ID', '所在区域', '主机名称', '主机别名', '公网地址', '私网地址', 'CPU 核数', '内存大小 MB', '网络带宽 MB', '运行状态', '创建时间', '过期时间' )
row = 0 col = 0 for item in title: worksheet.write(row, col, item, format_title) col += 1 for line in data: row += 1 col = 0 for key in line: worksheet.write(row, col, str(key), format_body) col += 1
work.close()
def main(): key = 'key' secret = 'secret' zones = ['cn-qingdao', 'cn-hangzhou', 'cn-beijing', 'cn-shanghai] #根据需要修改
filename = './aliyunSystemToExcel.xlsx'
result = []
for zone in zones: for page in range(1, 2): # 根据主机数量,设置page数 info = get_sys_info(key, secret, zone, page) data = format_data(info)
[result.append(line) for line in data]
write_excel(filename, result)
if __name__ == '__main__': main()
|