python獲取aliyun ECS例項

曲珂發表於2017-05-16
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Description : get ecs from aliyun
# Author      : quke
# Date        : 2017-05-16

import json
import math
import time
import random
import datetime
import sqlite3
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest

def coroutine(func):
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        g.next()
        return g
    return start

@coroutine
def get_aliyun_ecs(key, secret, zone):
    '''
    aliyun顯示每頁最大顯示數量PageSize 100條
    https://help.aliyun.com/document_detail/35112.html
    '''
    Client = client.AcsClient(key, secret, zone)

    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_accept_format('json')
    request.set_PageSize(100)
    response = None

    while True:
        pageNumber = (yield response)
        print pageNumber
        request = DescribeInstancesRequest.DescribeInstancesRequest()
        request.set_accept_format('json')
        request.set_PageSize(100)
        request.set_PageNumber(pageNumber)
        response = json.loads(Client.do_action_with_exception(request))

def format_data(jsonData):
    instances = jsonData.get('Instances').get('Instance')
    result = []

    for line in instances:
        data = (
            line.get('ZoneId'),
            line.get('InstanceId'),
            line.get('HostName'),
            line.get('InnerIpAddress').get('IpAddress')[0] if line.get('InnerIpAddress').get('IpAddress') else 'null',
            line.get('PublicIpAddress').get('IpAddress')[0] if line.get('PublicIpAddress').get('IpAddress') else 'null',
            u'%d核|%dG' %(line.get('Cpu'),int(line.get('Memory'))/1024),
            datetime.datetime.strptime(line.get('CreationTime'), '%Y-%m-%dT%H:%MZ').strftime('%Y-%m-%d %H:%M:%S'),
            datetime.datetime.strptime(line.get('ExpiredTime'), '%Y-%m-%dT%H:%MZ').strftime('%Y-%m-%d %H:%M:%S'),
        )
        result.append(data)

    return result

def insertDB(data):
    conn = sqlite3.connect('/Users/quke/db.sqlite3')
    cursor = conn.cursor()

    for line in data:
        cursor.execute('insert into cmdb_aliyunecs("region","instanceId","hostname","inner_ip","outer_ip","hardware","creationTime","expiredTime") values(?,?,?,?,?,?,?,?)', line)
    conn.commit()

if __name__ == '__main__':
    # region https://help.aliyun.com/document_detail/28908.html
    regions = ('cn-beijing', 'cn-hangzhou', 'cn-hongkong', 'us-east-1')
    for region in regions:
        print region
        aliyun_ecs=get_aliyun_ecs('key','secret', region)
        jsonData = aliyun_ecs.send(1)
        formated_data = format_data(jsonData)
        insertDB(formated_data)

        totalCount = jsonData.get('TotalCount')
        totalPage = int(math.ceil(totalCount/float(100))) + 1

        for pageNumber in range(2, totalPage):
            time.sleep(random.random())
            jsonData = aliyun_ecs.send(pageNumber)
            formated_data = format_data(jsonData)
            insertDB(formated_data)

  

相關文章