aboutsummaryrefslogtreecommitdiffstats
path: root/test/s3p/collector/get_resource_stats.py
blob: db377a7669cf12d5db7482ef445e90a07b25e3fa (plain)
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
#!/usr/bin/python
import subprocess
import sys
import json
import datetime
import collections
import re
import tzlocal

sys.path.append('../util')
import docker_util

AAI1_NAME = "AAI1"
AAI2_NAME = "AAI2"
SO_NAME = "SO"
SDNC_NAME = "SDNC"
AAI1_IP = "10.0.1.1"
AAI2_IP = "10.0.1.2"
SO_IP = "10.0.5.1"
SDNC_IP = "10.0.7.1"

def aai1():
    containers = docker_util.get_container_list(AAI1_IP)
    run(AAI1_NAME, AAI1_IP, containers)

def aai2():
    containers = docker_util.get_container_list(AAI2_IP)
    run(AAI2_NAME, AAI2_IP, containers)

def so():
    containers = docker_util.get_container_list(SO_IP)
    run(SO_NAME, SO_IP, containers)

def sdnc():
    containers = docker_util.get_container_list(SDNC_IP)
    run(SDNC_NAME, SDNC_IP, containers)

def run(component, ip, containers):
    cmd = ["ssh", "-i", "onap_dev"]
    cmd.append("ubuntu@" + ip)
    cmd.append("sudo docker stats --no-stream")
    for c in containers:
        cmd.append(c)
    ssh = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    result = ssh.stdout.readlines()
    if result == []:
        error = ssh.stderr.readlines()
        print(error)
    else:
        result.pop(0)
        for line in result:
            token = line.decode('ascii').strip().split()
            data = collections.OrderedDict()
            data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
            data['component'] = component
            data['container'] = token[0]
            data['cpu'] = get_percent_number(token[1])
            data['memory'] = get_memory_number(token[2])
            data['physical'] = get_memory_number(token[4])
            data['mem_percent'] = get_percent_number(token[5])
            size = docker_util.get_container_volume_size(ip, data['container'])
            if size is not None:
                data['volume'] = size
            file.write(json.dumps(data, default = myconverter) + "\n")
            file.flush()

def myconverter(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()

def get_percent_number(s):
    return float(re.sub('[^0-9\.]', '', s))

def get_memory_number(s):
    f = float(re.sub('[^0-9\.]', '', s))
    if s.endswith("GiB"):
        f = f*1000
    return f

file = open("resource.log", "w+")
while True:
    so()
    sdnc()
    aai1()
    aai2()