forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
executable file
·108 lines (94 loc) · 3.86 KB
/
stats.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
#!/usr/bin/env python
from himlarcli import tests
tests.is_virtual_env()
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from himlarcli.nova import Nova
from himlarcli.keystone import Keystone
from himlarcli.statsdclient import StatsdClient
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()
statsd = StatsdClient(options.config, debug=options.debug, log=logger)
statsd.set_dry_run(options.dry_run)
# Region
if hasattr(options, 'region'):
regions = kc.find_regions(region_name=options.region)
else:
regions = kc.find_regions()
def action_compute():
stats = {}
for region in regions:
drop_az = ['iaas-team-only', f'{region}-legacy-1']
metrics = ['vcpus', 'vcpus_used', 'running_vms', 'memory_mb_used',
'memory_mb', 'local_gb']
nc = Nova(options.config, debug=options.debug, region=region, log=logger)
aggregates = nc.get_aggregates(False)
for a in aggregates:
a_name = f'{region}-{a.name}'
stats[a_name] = {}
if a.availability_zone in drop_az:
continue
hosts = nc.get_aggregate_hosts(a.name, True)
for host in hosts:
#print host.to_dict()
for metric in metrics:
logger.debug('=> %s %s=%s', host.hypervisor_hostname,
metric, getattr(host, metric))
stats[a_name][metric] = int(getattr(host, metric) +
stats[a_name].get(metric, 0))
statsd.gauge_dict('compute', stats)
if not options.quiet:
for name, stat in stats.items():
printer.output_dict({'header': name})
printer.output_dict(stat)
def action_legacy():
projects_count = kc.get_project_count('dataporten')
users_count = kc.get_user_count('dataporten')
stats = {}
stats['users'] = {}
stats['projects'] = {}
stats['instances'] = {}
stats['instances']['total'] = {'count': 0, 'error': 0}
for region in regions:
logger.debug(f'=> count region {region}')
# Projects and users (this will be the same for all regions)
stats['projects'][region] = {}
stats['projects']['total'] = {}
stats['projects'][region]['count'] = projects_count
stats['projects']['total']['count'] = projects_count
stats['users'][region] = {}
stats['users']['total'] = {}
stats['users'][region]['count'] = users_count
stats['users']['total']['count'] = users_count
# Instances
novaclient = Nova(options.config, debug=options.debug,
region=region, log=logger)
novastats = novaclient.get_stats()
stats['instances'][region] = {}
stats['instances'][region]['count'] = novastats['count']
stats['instances'][region]['error'] = novastats['error']
stats['instances']['total']['count'] += novastats['count']
stats['instances']['total']['error'] += novastats['error']
for t, s in stats.items():
for r, d in s.items():
name = f'{r}.{t}'
count = d['count']
if not options.quiet:
print(f'{name} = {count}')
statsd.gauge(name, count)
if 'error' in d:
name = f'{r}.instance_errors'
if not options.quiet:
print(f"{name} = {d['error']}")
statsd.gauge(name, d['error'])
# Run local function with the same name as the action (Note: - => _)
action = locals().get('action_' + options.action.replace('-', '_'))
if not action:
himutils.sys_error(f"Function action_{options.action}() not implemented")
action()