文件夹服务(Folder service)提供操作 vCenter Server 文件夹的操作。
关键方法:Folder.list()
方法说明:返回有关 vCenter 中最多 1000 个可见文件夹的信息。
方法返回值:
Floder ID
:文件夹ID,文件夹的标识符。
Name
:vCenter Server 文件夹的名称。
Type
:类型。
DATACENTER
:可以包含数据中心的文件夹。DATASTORE
:可以包含数据存储的文件夹。HOST
:可以包含计算资源(主机和集群)的文件夹。NETWORK
:可以包含网络的文件夹。VIRTUAL_MACHINE
:可以包含虚拟机的文件夹。可能的值有: DATACENTER
、 DATASTORE
、 HOST
、 NETWORK
、 VIRTUAL_MACHINE
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
list_folder = vsphere_client.vcenter.Folder.list()
print("Floder ID".ljust(40),"Type".ljust(50),"Name")
for i in list_folder:
print(i.folder.ljust(40),
i.name.ljust(50),
i.type,
)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m" ,
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果图:
数据存储服务(Datastore service)提供操作数据存储的操作。
关键方法:Datastore.list()
方法说明:列出数据存储 返回有关 vCenter 中最多 2500 个可见数据存储的信息。
方法返回值:
datastore
:数据存储的标识符。
name
:数据存储名
type
:枚举类型定义了vCenter数据存储的支持类型。
VMFS
: VMware文件系统(仅ESX服务器)。NFS
: 网络文件系统v3(仅Linux和ESX服务器)。NFS41
:网络文件系统v4.1(仅Linux和ESX服务器)。CIFS
: 通用互联网文件系统。VSAN
: 虚拟SAN(仅ESX服务器)。VFFS
:Flash Read Cache(仅 ESX 服务器)。VVOL
: vSphere虚拟卷(仅ESX服务器)。枚举: vmfs
, nfs
, nfs41
, cifs
, vsan
, vffs
, vvol
capacity
:数据存储的容量,以字节为单位。服务器会定期更新该值。
free_space
:数据存储的可用空间,以字节为单位。服务器会定期更新该值。
脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client,byte_to_gb
start_time = time.time()
try:
ds = vsphere_client.vcenter.Datastore.list()
if ds == []:
print("---------Empty---------")
else:
for i in ds:
print("Datastore ID:".ljust(30),i.datastore,
"\nDatastore Name:".ljust(28),i.name,
"\nDatastore Type:".ljust(29),i.type,
"\nDatastore Free Space:".ljust(25),byte_to_gb(i.free_space), #Byte
"\nDatastore Capacity".ljust(26), byte_to_gb(i.capacity), #Byte
"\n=======================")
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m" ,
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:", run_time)
脚本效果图:
关键方法:vcenter.Datastore.get('datastore-ID')
方法说明:检索指定数据存储的信息。
方法返回值:
accessible
:数据存储是否可访问。布尔值。
name
:数据存储名。
thin_provisioning_supported
:数据存储是否支持基于每个文件的精简制备。使用自动精简制备时,会延迟分配后备存储。布尔值。
type
:枚举类型定义了vCenter数据存储的支持类型。
VMFS
: VMware文件系统(仅ESX服务器)。NFS
: 网络文件系统v3(仅Linux和ESX服务器)。NFS41
:网络文件系统v4.1(仅Linux和ESX服务器)。CIFS
: 通用互联网文件系统。VSAN
: 虚拟SAN(仅ESX服务器)。VFFS
:Flash Read Cache(仅 ESX 服务器)。VVOL
: vSphere虚拟卷(仅ESX服务器)。枚举: vmfs
, nfs
, nfs41
, cifs
, vsan
, vffs
, vvol
free_space
:数据存储的可用空间,以字节为单位。服务器会定期更新该值。
脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client,byte_to_gb
start_time = time.time()
try:
get_ds = vsphere_client.vcenter.Datastore.get('datastore-68')
print("Datastore Name:".ljust(38),get_ds.name,
"\nDatastore Type:".ljust(40),get_ds.type,
"\nAccessible:".ljust(44),get_ds.accessible,
"\nDatastore Free Space:".ljust(37),byte_to_gb(get_ds.free_space) ,
"\nMultiple Host Access:".ljust(37),get_ds.multiple_host_access,
"\nThin Provisioning Supported:".ljust(33),get_ds.thin_provisioning_supported
)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m" ,
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果图:
集群服务(Cluster service)提供了在 vCenter Server 中管理集群的操作。
关键方法:Cluster.list()
方法说明:列出集群 返回有关 vCenter 中的最多 1000 个可见集群的信息。
方法返回值:
cluster
:集群的标识符。drs_enabled
:指示是否为群集启用 vSphere DRS 服务的标志。布尔值。ha_enabled
:指示是否为群集启用 vSphere HA 功能的标志。布尔值。name
:集群名import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
print("Cluster ID".ljust(33),"HA Enabled".ljust(20),"DRS Enabled".ljust(17),"Cluster Name")
start_time = time.time()
try:
list_cluster = vsphere_client.vcenter.Cluster.list()
for i in list_cluster:
if i.ha_enabled == True:
i.ha_enabled = "True"
else:
i.ha_enabled = "False"
if i.drs_enabled == True:
i.drs_enabled = "True"
else:
i.drs_enabled = "False"
print(i.cluster.ljust(25),
i.ha_enabled.ljust(25),
i.drs_enabled.ljust(25),
i.name)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(30), run_time)
脚本效果图:
关键方法:Cluster.get(cluster="domain-ID")
方法说明:检索指定集群对应的集群的信息。
方法返回值:
name
:集群名resource_pool
:集群的根资源池的标识符。脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
get_cluster = vsphere_client.vcenter.Cluster.get(cluster="domain-c13239852")
print("Name:".ljust(30),get_cluster.name,
"\nResource Pool:".ljust(27),get_cluster.resource_pool)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(30), run_time)
脚本效果图:
vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts/v1/
关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化