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