这周有个需求,需要把阿里云上的服务器信息拉下来统计.但是阿里云的文档实在是写的太烂.无奈google+工单.处理ok!

参考资料:

  • SDK安装和示例请参考这里
  • API参数请参考这里
  • 脚本参考这里,自己根据需要有删改.

使用方法:

第一步,需要初始化Client

1
2
3
4
from aliyunsdkcore import client

clt = client.AcsClient('SFAW************','Nc2nZ6dQoiqck0*************',
'cn-hangzhou')

NOTE:DescribeInstancesRequest是调用你对应的API的模块.

第二步, 初始化request。以ECS的DescribeRegionsRequest接口为例:

1
2
3
4
5
6
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest

request = DescribeInstancesRequest.DescribeInstancesRequest()
request.set_PageSize(Number) # 设置返回sizenumber
request.set_PageNumber(Number) # 设置返回pagenumber
request.set_accept_format('json') # 设置返回格式

NOTE:DescribeInstancesRequest是调用你对应的API的模块.除了DescribeInstancesRequest参数,其它参数都需要用set_xxx来设置.

第三步, 发起API调用

1
result = clt.do_action(request) # 默认返回第一页,10条数据,可用set调整

示例脚本

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
#!/usr/bin/env python
# coding: utf-8

'''
功能介绍:
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)
# 将数据格式化成 json,默认为 XML
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 中
'''
# 生成 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) #ip地址是list类型,需转换成str.
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()