forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
billing.py
executable file
·116 lines (105 loc) · 5.08 KB
/
billing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
from himlarcli import tests as tests
tests.is_virtual_env()
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
#from himlarcli.cinder import Cinder
from himlarcli.gnocchi import Gnocchi
from himlarcli.cinder import Cinder
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from datetime import date, timedelta
from collections import OrderedDict
parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)
kc = Keystone(options.config, debug=options.debug)
kc.set_domain(options.domain)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
# Billing will always use all regions
regions = kc.find_regions()
def action_whales():
start = himutils.get_date(options.start, date.today() - timedelta(days=1))
stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
if start > stop:
himutils.sys_error('start %s must be fore stop %s' % (start, stop))
logger.debug('=> start date = %s', start)
logger.debug('=> stop date = %s', stop)
for region in regions:
nc = Nova(options.config, debug=options.debug, log=logger, region=region)
cc = Cinder(options.config, debug=options.debug, log=logger, region=region)
project_usage = nc.get_usage(start=start, end=stop)
logger.debug('=> threshold for whales filter %s', options.threshold)
print_header = True
for usage in project_usage:
project = kc.get_by_id(obj_type='project', obj_id=usage.tenant_id)
if not project:
logger.debug('=> project with id %s not found', usage.tenant_id)
continue
if len(usage.server_usages) < options.threshold:
continue
cinderusage = cc.get_quota(usage.tenant_id, True)
admin = project.admin if hasattr(project, 'admin') else 'unknown!'
output = OrderedDict()
output['instances'] = len(usage.server_usages)
output['volume_gb'] = cinderusage['gigabytes']['in_use']
output['name'] = project.name
output['admin'] = admin
if print_header:
output['header'] = 'project usage %s (instances, volume (GB), name, id)' % region
print_header = False
printer.output_dict(objects=output, sort=False, one_line=True)
def action_flavors():
project = kc.get_project_by_name(options.project)
start = himutils.get_date(options.start, date.today() - timedelta(days=1))
stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
if start > stop:
himutils.sys_error('start %s must be fore stop %s' % (start, stop))
logger.debug('=> start date = %s', start)
logger.debug('=> stop date = %s', stop)
flavors = dict()
for region in regions:
nc = Nova(options.config, debug=options.debug, log=logger, region=region)
usage = nc.get_usage(project_id=project.id, start=start, end=stop)
if not hasattr(usage, 'server_usages'):
continue
for server in usage.server_usages:
flavors[server['flavor']] = flavors.get(server['flavor'], 0) + 1
flavors['header'] = 'flavor usage for %s in all regions' % project.name
printer.output_dict(flavors)
def action_resources():
project = kc.get_project_by_name(options.project)
start = himutils.get_date(options.start, date.today() - timedelta(days=1))
stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
logger.debug('=> start date = %s', start)
logger.debug('=> stop date = %s', stop)
output = dict({'vcpu': 0, 'ram':0})
for region in regions:
# instances
nc = Nova(options.config, debug=options.debug, log=logger, region=region)
gc = Gnocchi(options.config, debug=options.debug, log=logger, region=region)
deleted = nc.get_project_instances(project_id=project.id, deleted=True)
running = nc.get_project_instances(project_id=project.id)
for i in deleted + running:
resource = gc.get_resource(resource_type='instance', resource_id=i.id)
if not resource:
continue
metrics = dict()
metrics['vcpu'] = gc.get_client().metric.get('vcpus', i.id)
metrics['ram'] = gc.get_client().metric.get('memory', i.id)
for key, value in metrics.items():
measurement = gc.get_client().metric.get_measures(metric=value['id'],
aggregation='max',
start=start,
stop=stop)
if measurement:
output[key] += measurement[0][2]
printer.output_dict({'header': 'resources used by %s in all regions' % project.name})
printer.output_dict(output)
# Run local function with the same name as the action
action = locals().get('action_' + options.action)
if not action:
himutils.sys_error("Function action_%s() not implemented" % options.action)
action()