TEST : Adding info command in CORD tester for details of docker container.

Change-Id: I1a9f52e6572f766873fd15b8d4fd5a58ce7c3cd1
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 8b2122b..17dbe4c 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -16,7 +16,7 @@
 #
 from argparse import ArgumentParser
 import os,sys,time,socket,errno
-import shutil, platform
+import shutil, platform, re
 utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
 sys.path.append(utils_dir)
 from OnosCtrl import OnosCtrl
@@ -25,6 +25,9 @@
 from CordContainer import *
 from CordTestServer import cord_test_server_start, cord_test_server_stop, CORD_TEST_HOST, CORD_TEST_PORT
 from TestManifest import TestManifest
+from docker import Client
+from docker.utils import kwargs_from_env
+
 try:
     from Fabric import FabricMAAS
 except:
@@ -690,6 +693,66 @@
     else:
         tests = args.test.split('-')
     CordTester.list_tests(tests)
+
+def getMetrics(args):
+
+    detail = c.inspect_container(args.container)
+    state = detail["State"]
+    if bool(state["Paused"]):
+       print("Container is in Paused State")
+    elif bool(state["Running"]):
+       print("Container is in Running State")
+    elif int(state["ExitCode"]) == 0:
+       print("Container is in Stopped State")
+    else:
+       print("Container is in Crashed State")
+
+    print("Ip Address of a container : " +detail['NetworkSettings']['IPAddress'])
+
+    if bool(detail["State"]["Running"]):
+        container_id = detail['Id']
+        cpu_usage = {}
+        with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
+            for line in f:
+                m = re.search(r"(system|user)\s+(\d+)", line)
+                if m:
+                    cpu_usage[m.group(1)] = int(m.group(2))
+            cpu = cpu_usage["system"] + cpu_usage["user"]
+        user_ticks = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
+        print "CPU Usage : %f"%(float(cpu) / user_ticks)
+    else:
+        print(0)
+
+    if bool(detail["State"]["Running"]):
+        container_id = detail['Id']
+        print "Docker Port Info:"
+        cmd = "sudo docker port {}".format(container_id)
+        os.system(cmd)
+
+    if bool(detail["State"]["Running"]):
+        container_id = detail['Id']
+        with open('/sys/fs/cgroup/memory/docker/' + container_id + '/memory.stat', 'r') as f:
+            for line in f:
+                m = re.search(r"total_rss\s+(\d+)", line)
+                if m:
+                    print "Memory in Bytes : %s "%(m.group(1))
+                o = re.search(r"usage\s+(\d+)", line)
+                if o:
+                    print "Usage : %s "%(o.group(1))
+                p = re.search(r"max_usage\s+(\d+)", line)
+                if p:
+                    print "Max Usage : %s "%(p.group(1))
+
+    if bool(detail["State"]["Running"]):
+        container_id = detail['Id']
+        with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
+            for line in f:
+                m = re.search(r"user\s+(\d+)", line)
+                if m:
+                    print "Time spent running processes since boot: %s "%(m.group(1))
+    print "List Networks :"
+    cmd = "docker network ls"
+    os.system(cmd)
     return 0
 
 def buildImages(args):
@@ -803,6 +866,10 @@
     parser_build.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
     parser_build.set_defaults(func=buildImages)
 
+    parser_metrics = subparser.add_parser('metrics', help='Info of container')
+    parser_metrics.add_argument("container", help="Container name")
+    parser_metrics.set_defaults(func=getMetrics)
+
     parser_start = subparser.add_parser('start', help='Start cord tester containers')
     parser_start.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
     parser_start.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
@@ -814,6 +881,8 @@
     parser_cleanup.add_argument('-l', '--olt', action = 'store_true', help = 'Cleanup OLT config')
     parser_cleanup.set_defaults(func=cleanupTests)
 
+    c = Client(**(kwargs_from_env()))
+
     args = parser.parse_args()
     res = args.func(args)
     sys.exit(res)