blob: db377a7669cf12d5db7482ef445e90a07b25e3fa [file] [log] [blame]
Yang Xu5c53ab22018-04-15 18:07:13 +00001#!/usr/bin/python
2import subprocess
3import sys
4import json
5import datetime
6import collections
7import re
8import tzlocal
Yang Xu5c53ab22018-04-15 18:07:13 +00009
10sys.path.append('../util')
11import docker_util
12
13AAI1_NAME = "AAI1"
14AAI2_NAME = "AAI2"
15SO_NAME = "SO"
16SDNC_NAME = "SDNC"
17AAI1_IP = "10.0.1.1"
18AAI2_IP = "10.0.1.2"
19SO_IP = "10.0.5.1"
20SDNC_IP = "10.0.7.1"
21
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010022def aai1():
Yang Xu5c53ab22018-04-15 18:07:13 +000023 containers = docker_util.get_container_list(AAI1_IP)
24 run(AAI1_NAME, AAI1_IP, containers)
25
26def aai2():
27 containers = docker_util.get_container_list(AAI2_IP)
28 run(AAI2_NAME, AAI2_IP, containers)
29
30def so():
31 containers = docker_util.get_container_list(SO_IP)
32 run(SO_NAME, SO_IP, containers)
33
34def sdnc():
35 containers = docker_util.get_container_list(SDNC_IP)
36 run(SDNC_NAME, SDNC_IP, containers)
37
38def run(component, ip, containers):
39 cmd = ["ssh", "-i", "onap_dev"]
40 cmd.append("ubuntu@" + ip)
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010041 cmd.append("sudo docker stats --no-stream")
Yang Xu5c53ab22018-04-15 18:07:13 +000042 for c in containers:
43 cmd.append(c)
44 ssh = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45
46 result = ssh.stdout.readlines()
47 if result == []:
48 error = ssh.stderr.readlines()
49 print(error)
50 else:
51 result.pop(0)
52 for line in result:
53 token = line.decode('ascii').strip().split()
54 data = collections.OrderedDict()
55 data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
56 data['component'] = component
57 data['container'] = token[0]
58 data['cpu'] = get_percent_number(token[1])
59 data['memory'] = get_memory_number(token[2])
60 data['physical'] = get_memory_number(token[4])
61 data['mem_percent'] = get_percent_number(token[5])
62 size = docker_util.get_container_volume_size(ip, data['container'])
63 if size is not None:
64 data['volume'] = size
65 file.write(json.dumps(data, default = myconverter) + "\n")
66 file.flush()
67
68def myconverter(o):
69 if isinstance(o, datetime.datetime):
70 return o.__str__()
71
72def get_percent_number(s):
73 return float(re.sub('[^0-9\.]', '', s))
74
75def get_memory_number(s):
76 f = float(re.sub('[^0-9\.]', '', s))
77 if s.endswith("GiB"):
78 f = f*1000
79 return f
80
81file = open("resource.log", "w+")
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010082while True:
Yang Xu5c53ab22018-04-15 18:07:13 +000083 so()
84 sdnc()
85 aai1()
86 aai2()