-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_cadvisor.py
More file actions
executable file
·79 lines (58 loc) · 2.01 KB
/
api_cadvisor.py
File metadata and controls
executable file
·79 lines (58 loc) · 2.01 KB
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
#!/usr/bin/python
import requests
import json
import dateutil.parser
import pprint
class CadvisorContainer:
data = {}
mesos_id = ""
cpu_usage = 0.0
def req_data(self, url):
r = requests.get(url)
#todo if r.status_code
self.data = json.loads(r.text)
if 'aliases' in self.data:
#self.mesos_id = self.data['aliases'][0]
self.mesos_id = self.data['aliases'][1]
#todo if not substring mesos in mesos_id
def get_subcontainers(self):
return self.data['subcontainers']
def get_memory_usage(self):
return self.data['stats'][-1]['memory']['usage']
def get_cpu_usage(self):
## somethin wrong :) not work properly
a = self.data['stats'][1]['cpu']['usage']['total']
b = self.data['stats'][0]['cpu']['usage']['total']
c = float(t0 - t1)
tdelta = dateutil.parser.parse(
self.data['stats'][1]['timestamp']) - dateutil.parser.parse(
self.data['stats'][0]['timestamp'])
interval = tdelta.seconds * 1000000000.0 + tdelta.microseconds * 1000.0
u = float(a - b) / float(interval)
return (u)
# def __init__(self, url):
# self.req_data(url)
class Cadvisor:
url = ""
machine = {}
host = CadvisorContainer()
def req_machine(self):
r = requests.get(self.url + "machine/")
#todo if r.status_code
self.machine = json.loads(r.text)
def req_host(self):
self.host.req_data(self.url + 'containers/docker/')
def get_containers(self):
ret = {}
c_ids = self.host.get_subcontainers()
for c_id in c_ids:
c = CadvisorContainer()
c.req_data(self.url + "containers" + c_id['name'])
ret[c.mesos_id] = c
return (ret)
def get_num_cores(self):
return self.machine['num_cores']
def __init__(self, addr, port):
self.url = "http://" + addr + ":" + str(port) + "/api/v1.2/"
self.req_machine()
self.req_host()