forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.py
executable file
·160 lines (151 loc) · 5.97 KB
/
instance.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
import sys
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
himutils.is_virtual_env()
# Load parser config from config/parser/*
parser = Parser()
options = parser.parse_args()
ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_domain('Dataporten')
logger = ksclient.get_logger()
printer = Printer(options.format)
# Regions
regions = himutils.load_region_config('config/stats',
region=ksclient.region,
log=logger)
""" ACTIONS """
def project():
stats = {'demo': 0,
'personal': 0,
'research': 0,
'education': 0,
'admin': 0,
'test': 0,
'hpc': 0,
'total': 0}
for name in sorted(regions['regions'].keys()):
logger.debug('=> count region %s' % name)
novaclient = Nova(options.config, debug=options.debug, log=logger, region=name)
instances = novaclient.get_instances()
stats['total'] += len(instances)
for i in instances:
project = ksclient.get_by_id('project', i.tenant_id)
if not project:
sys.stderr.write("MISSING PROJECT! id=%s for instance %s\n" % (i.tenant_id, i.name))
continue
if hasattr(project, 'course'):
stats['education'] += 1
elif '@' in project.name:
stats['personal'] += 1
else:
if hasattr(project, 'type'):
if project.type not in stats:
print("unknown project type %s for %s" % (project.type, project.name))
else:
stats[project.type] += 1
else:
stats['admin'] += 1
if options.output == 'count':
stats['header'] = 'Number of instances grouped by instance type:'
printer.output_dict(stats)
else:
percent = dict()
if stats['total'] > 0:
for s in sorted(stats):
percent[s] = (float(stats[s])/float(stats['total']))*100
percent['header'] = 'Percent of instances grouped by instance type:'
printer.output_dict(percent)
def users():
stats = dict()
stats['total'] = 0
for name in sorted(regions['regions'].keys()):
logger.debug('=> count region %s' % name)
novaclient = Nova(options.config, debug=options.debug, log=logger, region=name)
instances = novaclient.get_instances()
stats['total'] += len(instances)
for i in instances:
user = ksclient.get_by_id('user', i.user_id)
if not user:
org = 'unknown'
logger.debug('=> unknown user for %s (id=%s)' % (i.name, i.id))
elif '@' not in user.name:
org = 'sysuser'
else:
org = user.name.split("@")[1]
if org in stats:
stats[org] += 1
else:
stats[org] = 1
if options.output == 'count':
stats['header'] = 'Usage grouped by user email domain:'
printer.output_dict(stats)
else:
percent = dict()
if stats['total'] > 0:
for s in sorted(stats):
percent[s] = (float(stats[s])/float(stats['total']))*100
percent['header'] = 'Percentage of instances grouped by users email domain:'
printer.output_dict(percent)
def org():
stats = dict()
stats['total'] = 0
for name in sorted(regions['regions'].keys()):
logger.debug('=> count region %s' % name)
novaclient = Nova(options.config, debug=options.debug, log=logger, region=name)
instances = novaclient.get_instances()
stats['total'] += len(instances)
for i in instances:
user = ksclient.get_by_id('user', i.user_id)
if not user:
org = 'unknown'
logger.debug('=> unknown user for %s (id=%s)' % (i.name, i.id))
elif '@' not in user.name:
org = 'sysuser'
else:
domain = user.name.split("@")[1]
if len(domain.split(".")) > 1:
org = domain.split(".")[-2]
else:
org = 'unknown'
if org in stats:
stats[org] += 1
else:
stats[org] = 1
if options.output == 'count':
stats['header'] = 'Usage grouped by user organization:'
printer.output_dict(stats)
else:
percent = dict()
if stats['total'] > 0:
for s in sorted(stats):
percent[s] = (float(stats[s])/float(stats['total']))*100
percent['header'] = 'Percentage of instances grouped by users organization:'
printer.output_dict(percent)
def user():
if not ksclient.is_valid_user(email=options.email):
print("%s is not a valid user. Please check your spelling or case." % options.email)
sys.exit(1)
obj = ksclient.get_user_objects(email=options.email, domain='Dataporten')
projects = obj['projects']
total = 0
for project in projects:
project_type = project.type if hasattr(project, 'type') else 'unknown'
print("\n%s (type=%s):" % (project.name, project_type))
for name in sorted(regions['regions'].keys()):
logger.debug('=> count region %s' % name)
novaclient = Nova(options.config, debug=options.debug, log=logger, region=name)
instances = novaclient.get_project_instances(project.id)
total += len(instances)
for i in instances:
print("* %s" % i.name)
print("\nTotal number of instances for %s: %s" % (options.email, total))
# Run local function with the same name as the action
action = locals().get(options.action)
if not action:
logger.error("Action %s not implemented" % options.action)
sys.exit(1)
action()