簡體   English   中英

AWS EBS Volume-Python-查找所有字段信息,如AWS EC2 EBS Volume Console中所示

[英]AWS EBS Volume - Python - Find all fields info as shown in AWS EC2 EBS Volume Console

我正在嘗試創建一個Python腳本,以針對所有可用的AWS EBS卷生成一個csv格式文件,並顯示所有字段值,正如我在AWS EC2 EBS Volume Console中看到的那樣。

[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ cat ~/aws-vol-info.py 
import boto3

#define the connection
ec2 = boto3.resource('ec2', region_name="us-west-2")

volumes = ec2.volumes.all()

for vol in volumes:
    print "Created(" + str(vol.create_time) + "),VolumeState(" + str(vol.state) + "),VolumeID(" + str(vol.id) + "),VolumeSize(" + str(vol.size) + ") " + vol.type
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ 

該腳本給我以下錯誤消息。 原因:如果我不使用vol.type字段,則上述腳本有效。 它沒有工作,隨着產量變量從來沒有認為到它的價值,當它跑ec2.volumes.all()

[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ python ~/aws-vol-info.py 
Traceback (most recent call last):
  File "/Users/arun/aws-vol-info.py", line 9, in <module>
    print "Created(" + str(vol.create_time) + "),VolumeState(" + str(vol.state) + "),VolumeID(" + str(vol.id) + "),VolumeSize(" + str(vol.size) + ") " + vol.type
AttributeError: 'ec2.Volume' object has no attribute 'type'
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ 

我應該在上面的腳本中使用/更改什么庫/函數,通過它們可以顯示EBS卷的所有字段或一些更有意義的字段(在AWS EC2 EBS Volume控制台中看到的內容),有關以下內容的可用字段,請參見下圖: AWS控制台。

在此處輸入圖片說明

我在Github上在線找到了這個其他腳本(#2),看來它可以打印更多字段,但是下面給出了另一個錯誤。 我成功地運行了python -m pip install --user awspython -m pip install awspip install aws ,或運行了腳本(僅在#Import classes from aws package 行中的 #Import classes from aws package之后,才在他的存儲庫的aws文件夾內包含這些行(克隆后),但仍然出現錯誤。

import boto.ec2
class Volumes:
    def __init__(self):
        ''' Volumes Constructor '''

    def list_volumes(conn):
        ''' List Volumes '''
        # get all volumes
        vols = conn.get_all_volumes()

        # if volumes found
        if vols:
            #loop through volumes
            for v in vols:
                print 'Volume Id:', v.id
                print 'Volume Status:', v.status
                print 'Volume Size:', v.size
                print 'Zone:', v.zone
                print 'Volume Type:', v.type
                print 'Encrypted:', v.encrypted

                #print attachment set object
                attachmentData = v.attach_data
                print 'Instance Id:', attachmentData.instance_id
                print 'Attached Time:', attachmentData.attach_time
                print 'Device:', attachmentData.device
                print '**********************************'

#Import classes from aws package
from aws import Connection
from aws import EC2Instance
from aws import Volumes
#import aws

connInst = Connection()
conn = connInst.ec2Connection()

#instantiate Volumes and list volumes
volumeInst = Volumes()
volumeInst.list_volumes(conn)

腳本2錯誤為:

Traceback (most recent call last):
  File "/Users/arun/aws-vol-info2.py", line 30, in <module>
    from aws import Connection
ImportError: cannot import name Connection

如果我在腳本#2中注釋掉from aws ... ..而只是使用/取消注釋import aws ,那么我得到的是:

Traceback (most recent call last):
  File "/Users/arun/aws-vol-info2.py", line 35, in <module>
    connInst = Connection()
NameError: name 'Connection' is not defined

我認為您正在尋找vol.volume_type 您可以在ec2.Volume看到完整的屬性列表。請在此處查看卷: http : ec2.Volume

可以增強該腳本以顯示更多有意義的信息,但是使用jpavs的提示,我想到了此腳本~/aws-vol-info.py

import boto3

# Define the connection
ec2 = boto3.resource('ec2', region_name="us-west-2")

# Find all volumes
volumes = ec2.volumes.all()

# Loop through all volumes and pass it to ec2.Volume('xxx')
for vol in volumes:
    iv = ec2.Volume(str(vol.id))
    print "Created(" + str(iv.create_time) + "),AZ(" + str(iv.availability_zone) + "),VolumeID(" + str(iv.volume_id) + "),VolumeType(" + str(iv.volume_type) + "),State(" + str(iv.state) + "),Size(" + str(iv.size) + "),IOPS(" + str(iv.iops) + "),IsEncrypted(" + str(iv.encrypted) + "),SnapshotID(" + str(iv.snapshot_id) + "),KMS_KEYID(" + str(iv.kms_key_id) + ")",

    # The following next 2 print statements variables apply only in my case.
    print ",InstanceID(" + str(iv.attachments[0]['InstanceId']) + "),InstanceVolumeState(" + str(iv.attachments[0]['State']) + "),DeleteOnTerminationProtection(" + str(iv.attachments[0]['DeleteOnTermination']) + "),Device(" + str(iv.attachments[0]['Device']) + ")",
    if iv.tags:
        print ",Name(" + str(iv.tags[0]['Name']) + "),Mirror(" + str(iv.tags[0]['mirror']) + "),Role(" + str(iv.tags[0]['role']) + "),Cluster(" + str(iv.tags[0]['cluster']) + "),Hostname(" + str(iv.tags[0]['hostname']) + "),Generation(" + str(iv.tags[0]['generation']) + "),Index(" + str(iv.tags[0]['index']) + ")"
    print ""

冉: python ~/aws-vol-info.py ,它為python腳本中提到的所有字段提供了CSV格式值。 缺少(2-3)個AWS Console字段,因為該庫未提供這些字段,但是我可以從上述內容中獲取任何內容,或者如果我深入研究iv.attachments[0]['<somekey>']iv.tags[0]['<somekey>']對我來說就足夠了。

PSiv.attachmentsiv.tags都是iv對象中的List / dictionary類型變量,因此您可以通過顯示確切要從中獲取的內容來增強腳本。 因此,如果您想要InstanceID則可以使用以下命令: str(iv.attachments[0]['InstanceId'])進行打印。

為了獲得更好的版本:請在此處檢查.python腳本: telegraf-exec插件-aws ec2 ebs volumen信息-度量解析錯誤,原因:[缺少字段]或遇到的錯誤:[無效數字]

在這里也找到了這個有用的腳本: http : //www.n2ws.com/blog/ebs-report.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM