-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdomain-stats.py
executable file
·223 lines (182 loc) · 7.24 KB
/
domain-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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
import json
import logging
import os
import re
import time
import urllib
import urlparse
import requests
from influxdb import InfluxDBClient
logger = logging.getLogger("domain_stats")
class MetaverseAuth:
METAVERSE_URL = "https://metaverse.highfidelity.com/oauth"
TOKEN_URL = "%s/token" % METAVERSE_URL
AUTHORIZE_URL = "%s/authorize" % METAVERSE_URL
def __init__(self, username, password):
self._cookies = {}
self._tokens = self._get_access_token(username=username,
password=password)
# Call once when initialized
def _get_access_token(self, username=None, password=None):
if username and password:
resp = requests.post(
self.TOKEN_URL,
data={
"grant_type": "password",
"scope": "owner",
"username": username,
"password": password,
},
)
tokens = resp.json()
assert "access_token" in tokens
return tokens
def _get_cookies(self, domain, port):
k = (domain, port)
if k in self._cookies:
return self._cookies[k]
# This request is meant to redirect through the metaverse and
# back to the domain server. If we followed the redirect it
# would fail because it's missing the access token. A valid
# request needs the client_id, state, and access_token. We have
# to make an intial request to grab the state and client_id, but
# we don't follow the redirect because it will fail until we add
# the access token.
domain_tls_url = "https://%s:%d" % (domain, int(port) + 1)
domain_url = "http://%s:%d" % (domain, port)
redirect_resp = requests.get(domain_url, allow_redirects=False)
location = urlparse.urlparse(redirect_resp.headers["Location"])
query = urlparse.parse_qs(location.query)
params = {
"client_id": query["client_id"][0],
"response_type": "code",
"state": query["state"][0],
"redirect_uri": "%s/oauth" % domain_tls_url,
}
oauth_url = "%s?%s" % (self.AUTHORIZE_URL, urllib.urlencode(params))
# Make the initial authorization request and steal its cookies.
session = requests.Session()
headers = {"Authorization": "Bearer %s" % self._tokens["access_token"]}
session.get(oauth_url, headers=headers, verify=False)
cookies = session.cookies.get_dict()
self._cookies[k] = cookies
return cookies
def __call__(self, request):
url = urlparse.urlparse(request.url)
cookies = self._get_cookies(url.hostname, url.port)
cookies_str = "; ".join(("%s=%s" % (k, v) for k, v in cookies.items()))
request.headers.update({"Cookie": cookies_str})
return request
class DomainRequester:
def __init__(self, hostname, auth):
self.hostname = hostname
self.auth = auth
def get(self, path):
base_url = 'http://%s:40100' % self.hostname
path = re.sub(r'^/+', '', path)
url = '/'.join((base_url, path))
response = requests.get(url, auth=self.auth)
return response.json()
def __call__(self, path):
return self.get(path)
def clean_measurement(measurement):
measurement = measurement.replace(' ', '-')
if not measurement.startswith('z_'):
return {}, measurement
val = measurement.split('.')
if val[0] not in ('z_avatars', 'z_listeners'):
return {}, measurement
return {'uuid': val.pop(1)}, '.'.join(val)
def clean_val(val):
try:
return float(val)
except ValueError:
m = re.match(r'^(\d+) \w+$', val) # like: 0 usecs
if m:
val = m.groups()[0]
return float(m.groups()[0])
raise
def flatten(key, val):
if isinstance(val, dict):
for k, v in val.items():
k = '.'.join((key, k)) if key else k
for _ in flatten(key=k, val=v):
yield _
else:
if val is not None:
try:
# InfluxDB is strongly typed and tries to guess types.
# If you add a new measure measurement as 0 it will
# guess it's an int, but when you later add 0.1234 it
# will explode because it can't handle a float. So, just
# assume everything is a float. This means string won't
# work, but InfluxDB doesn't like strings anyway.
val = clean_val(val)
except (TypeError, ValueError) as exc:
logger.warn("couldn't clean value for %s: %s", key, val)
else:
yield (key, val)
def get_stats(request, domain_name):
nodes = {n['type']: n for n in request('nodes.json')['nodes']}
for k in ('audio-mixer', 'avatar-mixer'):
d = request('nodes/%s.json' % nodes[k]['uuid'])
for measurement, value in flatten('', d):
yield measurement, value, {'domain_name': domain_name,
'assignment': k}
def write_stats(request, client_kwargs):
client = InfluxDBClient(**client_kwargs)
client.create_database(client_kwargs['database'])
stats = get_stats(request, domain_name)
body = []
for measurement, value, tags in stats:
_tags, measurement = clean_measurement(measurement)
tags.update(_tags)
point = {
'measurement': measurement,
'tags': tags,
'fields': {
'value': value,
}
}
logger.debug(point)
body.append(point)
try:
client.write_points(body)
except Exception as exc:
logger.exception("couldn't write points")
else:
logger.info("wrote %d points" % len(body))
if __name__ == '__main__':
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger.info("starting")
domain_name = os.environ.get('HIFI_DOMAIN_NAME')
sleep_interval = int(os.environ.get('HIFI_SLEEP_INTERVAL', 3))
client_kwargs = {
'host': os.environ.get('HIFI_INFLUX_HOST', 'localhost'),
'port': int(os.environ.get('HIFI_INFLUX_PORT', '8086')),
'username': os.environ.get('HIFI_INFLUX_USERNAME'),
'password': os.environ.get('HIFI_INFLUX_PASSWORD'),
'database': os.environ.get('HIFI_INFLUX_DATABASE', 'domain_stats'),
}
if client_kwargs:
if not client_kwargs['username']:
del client_kwargs['username']
if not client_kwargs['password']:
del client_kwargs['password']
logger.debug("creating request")
request = DomainRequester(
'%s.highfidelity.io' % domain_name,
auth=MetaverseAuth(os.environ.get('HIFI_META_USERNAME'),
os.environ.get('HIFI_META_PASSWORD'))
)
logger.debug("created request")
while 1:
logger.debug("starting loop")
ts = time.time()
logger.debug("write stats")
write_stats(request, client_kwargs)
sleep_for = sleep_interval - (time.time() - ts)
logger.info("sleeping for %.02f secs" % sleep_for)
time.sleep(max(sleep_for, 0))