Yang Xu | 5c53ab2 | 2018-04-15 18:07:13 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | import subprocess |
| 3 | import sys |
| 4 | import json |
| 5 | import datetime |
| 6 | import collections |
| 7 | import re |
| 8 | import tzlocal |
| 9 | from decimal import Decimal |
| 10 | |
| 11 | sys.path.append('../util') |
| 12 | import docker_util |
| 13 | |
| 14 | AAI1_NAME = "AAI1" |
| 15 | AAI2_NAME = "AAI2" |
| 16 | SO_NAME = "SO" |
| 17 | SDNC_NAME = "SDNC" |
| 18 | AAI1_IP = "10.0.1.1" |
| 19 | AAI2_IP = "10.0.1.2" |
| 20 | SO_IP = "10.0.5.1" |
| 21 | SDNC_IP = "10.0.7.1" |
| 22 | |
| 23 | def aai1(): |
| 24 | containers = docker_util.get_container_list(AAI1_IP) |
| 25 | run(AAI1_NAME, AAI1_IP, containers) |
| 26 | |
| 27 | def aai2(): |
| 28 | containers = docker_util.get_container_list(AAI2_IP) |
| 29 | run(AAI2_NAME, AAI2_IP, containers) |
| 30 | |
| 31 | def so(): |
| 32 | containers = docker_util.get_container_list(SO_IP) |
| 33 | run(SO_NAME, SO_IP, containers) |
| 34 | |
| 35 | def sdnc(): |
| 36 | containers = docker_util.get_container_list(SDNC_IP) |
| 37 | run(SDNC_NAME, SDNC_IP, containers) |
| 38 | |
| 39 | def 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 | |
| 69 | def myconverter(o): |
| 70 | if isinstance(o, datetime.datetime): |
| 71 | return o.__str__() |
| 72 | |
| 73 | def get_percent_number(s): |
| 74 | return float(re.sub('[^0-9\.]', '', s)) |
| 75 | |
| 76 | def 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 | |
| 82 | file = open("resource.log", "w+") |
| 83 | while True: |
| 84 | so() |
| 85 | sdnc() |
| 86 | aai1() |
| 87 | aai2() |