作为一个项目,我需要在 OpenStack 上使用 Ceilometer 进行一些遥测。我得到了一段旧代码作为新版本应该是什么样子的参考。
使用我对 Python 和 OpenStack 的一点了解并花费数小时阅读 OpenStack 文档,我编写了以下代码:
import os
import datetime
import argparse
from os import environ as env
from ceilometerclient import client
from keystoneclient.v2_0 import client
from keystoneauth1.identity import v3
from keystoneauth1 import session
from glanceclient import Client
auth = v3.Password(user_domain_name=env['OS_USER_DOMAIN_NAME'],
username=env['OS_USERNAME'],
password=env['OS_PASSWORD'],
project_domain_id=env['OS_PROJECT_DOMAIN_ID'],
project_name=env['OS_PROJECT_NAME'],
auth_url=env['OS_AUTH_URL'])
sess = session.Session(auth=auth, verify='/usr/local/share/ca-certificates/openstack.crt')
keystone = client.Client(session = sess)
final_data = []
def list_all_projects():
projects = {}
projects = keystone.projects.list()
return projects
def get_datetime(midnight):
dtime = datetime.datetime.now()
if midnight is True:
current_dtime = dtime.strftime('%d-%m-%YT00:00')
else:
current_dtime = dtime.strftime('%d-%m-%YT%H:%M')
return current_dtime
timestamp_start = get_datetime(True)
timestamp_end = get_datetime(False)
projects = list_all_projects()
for project in projects:
project_name = project['name']
project_id = project['id']
ceilometer_client = client.get_client("2", **ks_creds)
query = [dict(field='project', op='eq', value=project_id),
dict(field='source', op='eq', value='openstack')]
resources = ceilometer_client.resources.list(q=query)
for resource in resources:
rid = resource.resource_id
query = [dict(field='resource_id', op='eq', value=rid),
dict(field='timestamp', op='gt', value=timestamp_start),
dict(field='timestamp', op='lt', value=timestamp_end)]
# average CPU usage
cpu_util = []
cpus = client.statistics.list(meter_name='cpu_util', q=query)
cpu_util = cpus['avg']
# average number of VCPUs
vcpu_average = []
vcpus = client.statistics.list(meter_name='vcpus', q=query)
vcpu_average = vcpus['avg']
# memory usage
memory_usage = []
memories = client.statistics.list(meter_name='memory.usage', q=query)
memory_usage = memories['avg']
# disc usage
disk_root_average = []
root_disk_size = client.statistics.list(
meter_name='disk.root.size',q=query)
disk_root_average = root_disk_size['avg']
resource_data = resource['project_id']
+ ";" + project_name
+ ";" + resource['resource_id']
+ ";" + timestamp_start
+ ";" + timestamp_end
+ ";" + str(cpu_util)
+ ";" + str(vcpu_average)
+ ";" + str(memory_usage)
+ ";" + str(disk_root_average)
+ ";\n"
final_data.append(resource_data)
parser = argparse.ArgumentParser(description='Script for statistics')
parser.add_argument('--fname', default="stats.txt", metavar='FILE', type=str, help='output file names')
args = parser.parse_args()
filename = args.fname
wfile = open(filename, 'a')
wfile.write(final_data)
wfile.close()
出于某种原因,当我尝试运行此代码时,运行它的虚拟机说项目是一个未知属性。
在 OpenStack 中显示所有项目的新方法是什么?代码还有什么问题吗?