Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import uuid
- import logging
- import os
- import random
- import sys
- import datetime
- import time
- from keystoneauth1.identity import v3
- from keystoneauth1 import session
- from gnocchiclient import client
- from gnocchiclient import noauth
- from gnocchiclient import exceptions
- from six import text_type
- now = datetime.datetime.utcnow()
- LOG = logging.getLogger('')
- LOG.setLevel(logging.DEBUG)
- requests_log = logging.getLogger("requests")
- requests_log.setLevel(logging.DEBUG)
- console = logging.StreamHandler(sys.stderr)
- LOG.addHandler(console)
- auth = v3.Password(auth_url='http://127.0.0.1:5000/v3',
- username='gnocchi',
- password='password',
- project_name='service',
- user_domain_id='default',
- project_domain_id='default')
- # auth = noauth.GnocchiNoAuthPlugin(
- # user_id=os.getenv("GNOCCHI_USER_ID"),
- # project_id=os.getenv("GNOCCHI_PROJECT_ID"),
- # roles="admin",
- # endpoint=os.getenv("GNOCCHI_ENDPOINT"),
- # )
- session = session.Session(auth=auth)
- from keystoneclient.v3 import client as ks
- ks_client = ks.Client(session=session)
- # user_list = ks_client.users.list(project="summit")
- user_list = ks_client.users.list(project="test-ck")
- for user in user_list:
- if user.name == "summit_user":
- user_id=user.id
- break
- project_list = ks_client.projects.list()
- for project in project_list:
- if project.name == "summit":
- project_id=project.id
- break
- c = client.Client('1', session=session)
- try:
- c.archive_policy.create({
- "name": "medium",
- "definition":
- [
- # 1 minute over 1 hour
- {"granularity": 60, "points": 60},
- # 1 hour over 1 month
- {"granularity": 3600, "points": 30 * 24},
- # 1 day over a year
- {"granularity": 3600 * 24, "points": 365},
- ]
- })
- except exceptions.ArchivePolicyAlreadyExists:
- pass
- images = [
- "foo",
- "bar",
- ]
- flavors = [
- "fooflavor",
- "barflavor",
- ]
- def create_image(i):
- image = c.resource.create("image",
- {
- "id": text_type(uuid.uuid4()),
- "user_id": user_id,
- "project_id": project_id,
- "started_at": "%d-%s-%02dT12:21:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i),
- "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i + 1),
- "name": random.choice(images),
- "container_format": 'ari',
- "disk_format": 'ari',
- "metrics":
- {
- "image.size":
- {
- "archive_policy_name": "medium",
- "unit": "B"
- }
- }
- })
- c.metric.add_measures(image['metrics']['image.size'],
- [{"timestamp": image['ended_at'],
- "value": 3726000}])
- return image
- def create_volume(i):
- volume = c.resource.create("volume",
- {
- "id": text_type(uuid.uuid4()),
- "user_id": user_id,
- "project_id": project_id,
- "started_at": "%d-%s-%02dT12:21:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i),
- "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i + 1),
- "display_name": "volume%d" % i,
- "metrics":
- {
- "volume.size":
- {
- "archive_policy_name": "medium",
- "unit": "B"
- }
- }
- })
- c.metric.add_measures(volume['metrics']['volume.size'],
- [{"timestamp": volume['ended_at'],
- "value": 1000000}])
- return volume
- def create_network_interface(i):
- interface = c.resource.create("instance_network_interface",
- {
- "instance_id": text_type(uuid.uuid4()),
- "user_id": user_id,
- "project_id": project_id,
- "started_at": "%d-%s-%02dT12:21:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i),
- "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i + 1),
- "metrics":
- {
- "network.outgoing.bytes":
- {
- "archive_policy_name": "medium",
- "unit": "B"
- },
- "network.incoming.bytes":
- {
- "archive_policy_name": "medium",
- "unit": "B"
- }
- }
- })
- c.metric.add_measures(interface['metrics']['network.outgoing.bytes'],
- [{"timestamp": interface['ended_at'],
- "value": random.randint(500, 2000)}])
- c.metric.add_measures(interface['metrics']['network.incoming.bytes'],
- [{"timestamp": interface['ended_at'],
- "value": random.randint(500, 2000)}])
- return interface
- def create_floating(i):
- floating = c.resource.create("network",
- {
- "id": text_type(uuid.uuid4()),
- "user_id": user_id,
- "project_id": project_id,
- "started_at": "%d-%s-%02dT12:21:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i),
- "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i + 1),
- "metrics":
- {
- "ip.floating":
- {
- "archive_policy_name": "medium",
- "unit": "ip"
- }
- }
- })
- c.metric.add_measures(floating['metrics']['ip.floating'],
- [{"timestamp": floating['ended_at'],
- "value": 1}])
- return floating
- instances = [
- c.resource.create("instance",
- {
- "id": text_type(uuid.uuid4()),
- "user_id": user_id,
- "project_id": project_id,
- "started_at": "%d-%s-%02dT12:21:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i),
- "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
- now.year,
- text_type(now.month).zfill(2),
- i + 1),
- "flavor_id": random.choice(flavors),
- "host": "compute1",
- "image_ref": random.choice(images),
- "display_name": "instance%d" % i,
- "metrics":
- {
- "memory": {"archive_policy_name": "medium"},
- "vcpus": {"archive_policy_name": "medium"},
- "cpu": {"archive_policy_name": "medium"},
- },
- })
- for i in range(1, now.day - 1)
- ]
- images = [
- create_image(i)
- for i in range(1, now.day - 1)
- ]
- volumes = [
- create_volume(i)
- for i in range(1, now.day - 1)
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement