Merge "Module to manage pip packages"
[yardstick.git] / yardstick / network_services / nfvi / resource.py
index e3d0e3b..dc5c46a 100644 (file)
 # limitations under the License.
 """ Resource collection definitions """
 
-from __future__ import absolute_import
-from __future__ import print_function
-
-import logging
-from itertools import chain
-
 import errno
-import jinja2
+from itertools import chain
+import logging
+import multiprocessing
 import os
 import os.path
 import re
-import multiprocessing
-import pkg_resources
 
+import jinja2
+import pkg_resources
 from oslo_config import cfg
 from oslo_utils.encodeutils import safe_decode
 
@@ -34,7 +30,7 @@ from yardstick import ssh
 from yardstick.common.task_template import finalize_for_yaml
 from yardstick.common.utils import validate_non_string_sequence
 from yardstick.network_services.nfvi.collectd import AmqpConsumer
-from yardstick.network_services.utils import get_nsb_option
+
 
 LOG = logging.getLogger(__name__)
 
@@ -53,9 +49,9 @@ class ResourceProfile(object):
     AMPQ_PORT = 5672
     DEFAULT_INTERVAL = 25
     DEFAULT_TIMEOUT = 3600
+    OVS_SOCKET_PATH = "/usr/local/var/run/openvswitch/db.sock"
 
-    def __init__(self, mgmt, port_names=None, cores=None, plugins=None,
-                 interval=None, timeout=None):
+    def __init__(self, mgmt, port_names=None, plugins=None, interval=None, timeout=None):
 
         if plugins is None:
             self.plugins = {}
@@ -81,7 +77,18 @@ class ResourceProfile(object):
         self.mgmt = mgmt
         self.connection = ssh.AutoConnectSSH.from_node(mgmt)
 
-    def check_if_sa_running(self, process):
+    @classmethod
+    def make_from_node(cls, node, timeout):
+        # node dict works as mgmt dict
+        # don't need port names, there is no way we can
+        # tell what port is used on the compute node
+        collectd_options = node["collectd"]
+        plugins = collectd_options.get("plugins", {})
+        interval = collectd_options.get("interval")
+
+        return cls(node, plugins=plugins, interval=interval, timeout=timeout)
+
+    def check_if_system_agent_running(self, process):
         """ verify if system agent is running """
         try:
             err, pid, _ = self.connection.execute("pgrep -f %s" % process)
@@ -90,7 +97,7 @@ class ResourceProfile(object):
         except OSError as e:
             if e.errno in {errno.ECONNRESET}:
                 # if we can't connect to check, then we won't be able to connect to stop it
-                LOG.exception("can't connect to host to check collectd status")
+                LOG.exception("Can't connect to host to check %s status", process)
                 return 1, None
             raise
 
@@ -154,7 +161,6 @@ class ResourceProfile(object):
             "dpdkstat": {},
             "virt": {},
             "ovs_stats": {},
-            "intel_pmu": {},
         }
         testcase = ""
 
@@ -166,7 +172,7 @@ class ResourceProfile(object):
             res_key0 = next(res_key_iter)
             res_key1 = next(res_key_iter)
 
-            if "cpu" in res_key0 or "intel_rdt" in res_key0:
+            if "cpu" in res_key0 or "intel_rdt" in res_key0 or "intel_pmu" in res_key0:
                 cpu_key, name, metric, testcase = \
                     self.get_cpu_data(res_key0, res_key1, value)
                 result["cpu"].setdefault(cpu_key, {}).update({name: metric})
@@ -186,9 +192,6 @@ class ResourceProfile(object):
             elif "ovs_stats" in res_key0:
                 result["ovs_stats"].update(self.parse_ovs_stats(key_split, value))
 
-            elif "intel_pmu-all" in res_key0:
-                result["intel_pmu"].update(self.parse_intel_pmu_stats(res_key1, value))
-
         result["timestamp"] = testcase
 
         return result
@@ -236,10 +239,19 @@ class ResourceProfile(object):
         }
         self._provide_config_file(config_file_path, self.COLLECTD_CONF, kwargs)
 
+    def _setup_ovs_stats(self, connection):
+        try:
+            socket_path = self.plugins["ovs_stats"].get("ovs_socket_path", self.OVS_SOCKET_PATH)
+        except KeyError:
+            # ovs_stats is not a dict
+            socket_path = self.OVS_SOCKET_PATH
+        status = connection.execute("test -S {}".format(socket_path))[0]
+        if status != 0:
+            LOG.error("cannot find OVS socket %s", socket_path)
+
     def _start_collectd(self, connection, bin_path):
         LOG.debug("Starting collectd to collect NFVi stats")
         connection.execute('sudo pkill -x -9 collectd')
-        bin_path = get_nsb_option("bin_path")
         collectd_path = os.path.join(bin_path, "collectd", "sbin", "collectd")
         config_file_path = os.path.join(bin_path, "collectd", "etc")
         exit_status = connection.execute("which %s > /dev/null 2>&1" % collectd_path)[0]
@@ -253,6 +265,9 @@ class ResourceProfile(object):
             # connection.execute("sudo %s '%s' '%s'" % (
             #     collectd_installer, http_proxy, https_proxy))
             return
+        if "ovs_stats" in self.plugins:
+            self._setup_ovs_stats(connection)
+
         LOG.debug("Starting collectd to collect NFVi stats")
         # ensure collectd.conf.d exists to avoid error/warning
         connection.execute("sudo mkdir -p /etc/collectd/collectd.conf.d")
@@ -274,7 +289,10 @@ class ResourceProfile(object):
         connection.execute("sudo rabbitmqctl set_permissions -p / admin '.*' '.*' '.*'")
 
         LOG.debug("Start collectd service..... %s second timeout", self.timeout)
-        connection.execute("sudo %s" % collectd_path, timeout=self.timeout)
+        # intel_pmu plug requires large numbers of files open, so try to set
+        # ulimit -n to a large value
+        connection.execute("sudo bash -c 'ulimit -n 1000000 ; %s'" % collectd_path,
+                           timeout=self.timeout)
         LOG.debug("Done")
 
     def initiate_systemagent(self, bin_path):
@@ -305,7 +323,7 @@ class ResourceProfile(object):
             self.amqp_client.terminate()
 
         LOG.debug("Check if %s is running", agent)
-        status, pid = self.check_if_sa_running(agent)
+        status, pid = self.check_if_system_agent_running(agent)
         LOG.debug("status %s  pid %s", status, pid)
         if status != 0:
             return