python获取aliyun ECS实例

#!/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)

  

转载于:https://www.cnblogs.com/txwsqk/p/6863263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值