Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.35 KB | None | 0 0
  1. #!/usr/bin/env python                                                                          
  2. import uuid
  3. import logging
  4. import os
  5. import random
  6. import sys
  7. import datetime
  8. import time
  9.  
  10. from keystoneauth1.identity import v3
  11. from keystoneauth1 import session
  12. from gnocchiclient import client
  13. from gnocchiclient import noauth
  14. from gnocchiclient import exceptions
  15. from six import text_type
  16.  
  17. now = datetime.datetime.utcnow()
  18.  
  19. LOG = logging.getLogger('')
  20. LOG.setLevel(logging.DEBUG)
  21. requests_log = logging.getLogger("requests")
  22. requests_log.setLevel(logging.DEBUG)
  23. console = logging.StreamHandler(sys.stderr)
  24. LOG.addHandler(console)
  25.  
  26. auth = v3.Password(auth_url='http://127.0.0.1:5000/v3',
  27.                    username='gnocchi',
  28.                    password='password',
  29.                    project_name='service',
  30.                    user_domain_id='default',
  31.                    project_domain_id='default')
  32.  
  33. # auth = noauth.GnocchiNoAuthPlugin(                                                          
  34. #     user_id=os.getenv("GNOCCHI_USER_ID"),                                                    
  35. #     project_id=os.getenv("GNOCCHI_PROJECT_ID"),                                              
  36. #     roles="admin",                                                                          
  37. #     endpoint=os.getenv("GNOCCHI_ENDPOINT"),                                                  
  38. # )                                                                                            
  39.  
  40. session = session.Session(auth=auth)
  41.  
  42. from keystoneclient.v3 import client as ks
  43. ks_client = ks.Client(session=session)
  44. # user_list = ks_client.users.list(project="summit")                                          
  45. user_list = ks_client.users.list(project="test-ck")
  46. for user in user_list:
  47.     if user.name == "summit_user":
  48.        user_id=user.id
  49.         break
  50. project_list = ks_client.projects.list()
  51. for project in project_list:
  52.     if project.name == "summit":
  53.         project_id=project.id
  54.         break
  55.  
  56. c = client.Client('1', session=session)
  57.  
  58. try:
  59.     c.archive_policy.create({
  60.         "name": "medium",
  61.         "definition":
  62.         [
  63.             # 1 minute over 1 hour                                                            
  64.             {"granularity": 60, "points": 60},
  65.             # 1 hour over 1 month                                                              
  66.             {"granularity": 3600, "points": 30 * 24},
  67.             # 1 day over a year                                                                
  68.             {"granularity": 3600 * 24, "points": 365},
  69.         ]
  70.     })
  71. except exceptions.ArchivePolicyAlreadyExists:
  72.     pass
  73. images = [
  74.     "foo",
  75.     "bar",
  76. ]
  77.  
  78. flavors = [
  79.     "fooflavor",
  80.     "barflavor",
  81. ]
  82.  
  83. def create_image(i):
  84.     image = c.resource.create("image",
  85.                               {
  86.                                 "id": text_type(uuid.uuid4()),
  87.                                 "user_id": user_id,
  88.                                 "project_id": project_id,
  89.                                 "started_at": "%d-%s-%02dT12:21:00+00:00" % (
  90.                                   now.year,
  91.                                   text_type(now.month).zfill(2),
  92.                                   i),
  93.                                 "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
  94.                                   now.year,
  95.                                   text_type(now.month).zfill(2),
  96.                                   i + 1),
  97.                                 "name": random.choice(images),
  98.                                 "container_format": 'ari',
  99.                                 "disk_format": 'ari',
  100.                                 "metrics":
  101.                                 {
  102.                                   "image.size":
  103.                                   {
  104.                                     "archive_policy_name": "medium",
  105.                                     "unit": "B"
  106.                                   }
  107.                                 }
  108.                               })
  109.     c.metric.add_measures(image['metrics']['image.size'],
  110.                           [{"timestamp": image['ended_at'],
  111.                             "value": 3726000}])
  112.     return image
  113.  
  114. def create_volume(i):
  115.     volume = c.resource.create("volume",
  116.                                {
  117.                                    "id": text_type(uuid.uuid4()),
  118.                                    "user_id": user_id,
  119.                                    "project_id": project_id,
  120.                                    "started_at": "%d-%s-%02dT12:21:00+00:00" % (
  121.                                        now.year,
  122.                                        text_type(now.month).zfill(2),
  123.                                        i),
  124.                                    "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
  125.                                        now.year,
  126.                                        text_type(now.month).zfill(2),
  127.                                        i + 1),
  128.                                    "display_name": "volume%d" % i,
  129.                                    "metrics":
  130.                                    {
  131.                                        "volume.size":
  132.                                        {
  133.                                            "archive_policy_name": "medium",
  134.                                            "unit": "B"
  135.                                        }
  136.                                    }
  137.                                })
  138.     c.metric.add_measures(volume['metrics']['volume.size'],
  139.                           [{"timestamp": volume['ended_at'],
  140.                             "value": 1000000}])
  141.     return volume
  142.  
  143. def create_network_interface(i):
  144.     interface = c.resource.create("instance_network_interface",
  145.                                   {
  146.                                     "instance_id": text_type(uuid.uuid4()),
  147.                                     "user_id": user_id,
  148.                                     "project_id": project_id,
  149.                                     "started_at": "%d-%s-%02dT12:21:00+00:00" % (
  150.                                       now.year,
  151.                                       text_type(now.month).zfill(2),
  152.                                       i),
  153.                                     "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
  154.                                       now.year,
  155.                                       text_type(now.month).zfill(2),
  156.                                       i + 1),
  157.                                     "metrics":
  158.                                     {
  159.                                       "network.outgoing.bytes":
  160.                                       {
  161.                                         "archive_policy_name": "medium",
  162.                                         "unit": "B"
  163.                                       },
  164.                                       "network.incoming.bytes":
  165.                                       {
  166.                                         "archive_policy_name": "medium",
  167.                                         "unit": "B"
  168.                                       }
  169.                                     }
  170.                                   })
  171.     c.metric.add_measures(interface['metrics']['network.outgoing.bytes'],
  172.                           [{"timestamp": interface['ended_at'],
  173.                             "value": random.randint(500, 2000)}])
  174.     c.metric.add_measures(interface['metrics']['network.incoming.bytes'],
  175.                           [{"timestamp": interface['ended_at'],
  176.                             "value": random.randint(500, 2000)}])
  177.     return interface
  178.  
  179. def create_floating(i):
  180.     floating = c.resource.create("network",
  181.                                  {
  182.                                      "id": text_type(uuid.uuid4()),
  183.                                      "user_id": user_id,
  184.                                      "project_id": project_id,
  185.                                      "started_at": "%d-%s-%02dT12:21:00+00:00" % (
  186.                                          now.year,
  187.                                          text_type(now.month).zfill(2),
  188.                                          i),
  189.                                      "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
  190.                                          now.year,
  191.                                          text_type(now.month).zfill(2),
  192.                                          i + 1),
  193.                                      "metrics":
  194.                                      {
  195.                                          "ip.floating":
  196.                                          {
  197.                                              "archive_policy_name": "medium",
  198.                                              "unit": "ip"
  199.                                          }
  200.                                      }
  201.                                  })
  202.     c.metric.add_measures(floating['metrics']['ip.floating'],
  203.                           [{"timestamp": floating['ended_at'],
  204.                             "value": 1}])
  205.     return floating
  206.  
  207. instances = [
  208.     c.resource.create("instance",
  209.                       {
  210.                           "id": text_type(uuid.uuid4()),
  211.                           "user_id": user_id,
  212.                           "project_id": project_id,
  213.                           "started_at": "%d-%s-%02dT12:21:00+00:00" % (
  214.                               now.year,
  215.                               text_type(now.month).zfill(2),
  216.                               i),
  217.               "ended_at": "%d-%s-%02dT12:51:00+00:00" % (
  218.                               now.year,
  219.                               text_type(now.month).zfill(2),
  220.                               i + 1),
  221.                           "flavor_id": random.choice(flavors),
  222.                           "host": "compute1",
  223.                           "image_ref": random.choice(images),
  224.                           "display_name": "instance%d" % i,
  225.                           "metrics":
  226.                           {
  227.                               "memory": {"archive_policy_name": "medium"},
  228.                               "vcpus": {"archive_policy_name": "medium"},
  229.                               "cpu": {"archive_policy_name": "medium"},
  230.                           },
  231.                       })
  232.     for i in range(1, now.day - 1)
  233. ]
  234.  
  235. images = [
  236.     create_image(i)
  237.     for i in range(1, now.day - 1)
  238. ]
  239.  
  240. volumes = [
  241.     create_volume(i)
  242.     for i in range(1, now.day - 1)
  243. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement