standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_general.py
index 515514c..c6c5a75 100644 (file)
@@ -6,10 +6,11 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+from __future__ import absolute_import
 import logging
 import yardstick.ssh as ssh
 
-import basemonitor as basemonitor
+from yardstick.benchmark.scenarios.availability.monitor import basemonitor
 from yardstick.benchmark.scenarios.availability.util import buildshellparams
 
 
@@ -23,10 +24,8 @@ class GeneralMonitor(basemonitor.BaseMonitor):
 
     def setup(self):
         host = self._context[self._config["host"]]
-        ip = host.get("ip", None)
-        user = host.get("user", "root")
-        key_filename = host.get("key_filename", "~/.ssh/id_rsa")
         self.key = self._config["key"]
+        self.monitor_key = self._config["monitor_key"]
         self.monitor_type = self._config["monitor_type"]
 
         if "parameter" in self._config:
@@ -35,36 +34,39 @@ class GeneralMonitor(basemonitor.BaseMonitor):
             l = list(item for item in parameter.values())
             self.cmd_param = str.format(*l)
 
-        self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(self.key)
+        self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(
+            self.monitor_key)
         self.monitor_script = self.get_script_fullpath(
             self.monitor_cfg['monitor_script'])
-        self.connection = ssh.SSH(user, ip, key_filename=key_filename)
+        self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
         self.connection.wait(timeout=600)
         LOG.debug("ssh host success!")
 
     def monitor_func(self):
         if "parameter" in self._config:
-            exit_status, stdout, stderr = self.connection.execute(
-                self.cmd_param,
-                stdin=open(self.monitor_script, "r"))
+            with open(self.monitor_script, "r") as stdin_file:
+                exit_status, stdout, stderr = self.connection.execute(
+                    self.cmd_param,
+                    stdin=stdin_file)
         else:
-            exit_status, stdout, stderr = self.connection.execute(
-                "/bin/bash -s ",
-                stdin=open(self.monitor_script, "r"))
+            with open(self.monitor_script, "r") as stdin_file:
+                exit_status, stdout, stderr = self.connection.execute(
+                    "/bin/bash -s ",
+                    stdin=stdin_file)
 
         if exit_status:
             return False
         return True
 
     def verify_SLA(self):
-        LOG.debug("the _result:%s" % self._result)
+        LOG.debug("the _result:%s", self._result)
         outage_time = self._result.get('outage_time', None)
-        max_outage_time = self._config["sla"]["max_recover_time"]
+        max_outage_time = self._config["sla"]["max_outage_time"]
         if outage_time is None:
             LOG.error("There is no outage_time in monitor result.")
             return False
         if outage_time > max_outage_time:
-            LOG.error("SLA failure: %f > %f" % (outage_time, max_outage_time))
+            LOG.error("SLA failure: %f > %f", outage_time, max_outage_time)
             return False
         else:
             return True