• 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)
    

      

  • 相关阅读:
    贺铿:中国多年来楼市调控不合理又不合法
    Ubuntu12.04下arm交叉编译环境的建立
    Android2.3.7源码结构分析
    【开源推荐】AllJoyn:打造全球物联网的通用开源框架
    Ubuntu12.04安装JDK6
    ubuntu12.04升级后找不到共享目录
    Windows Embedded Compact 7新特性
    Windows Embedded Compact 2013 安装体验
    巴登的故事
    h5调用底层接口的一些知识
  • 原文地址:https://www.cnblogs.com/txwsqk/p/6863263.html
Copyright © 2020-2023  润新知