ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlun...@apache.org
Subject ambari git commit: AMBARI-8938: Ambari AMS - os.getloadavg not available on windows (Eugene Chekanskiy via jluniya)
Date Mon, 05 Jan 2015 08:48:25 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 64424e6ff -> 6df5648d3


AMBARI-8938: Ambari AMS - os.getloadavg not available on windows (Eugene Chekanskiy via jluniya)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6df5648d
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6df5648d
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6df5648d

Branch: refs/heads/trunk
Commit: 6df5648d3e748b78e1d60ec86df32b09d6caa6a3
Parents: 64424e6
Author: Jayush Luniya <jluniya@hortonworks.com>
Authored: Mon Jan 5 00:48:17 2015 -0800
Committer: Jayush Luniya <jluniya@hortonworks.com>
Committed: Mon Jan 5 00:48:17 2015 -0800

----------------------------------------------------------------------
 .../src/main/python/core/host_info.py           | 17 ++++++++-----
 .../src/test/python/core/TestHostInfo.py        | 26 +++++++++++---------
 .../src/test/python/core/TestMetricCollector.py |  6 ++---
 3 files changed, 27 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/6df5648d/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py
b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py
index a7add91..020b84e 100644
--- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py
+++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py
@@ -48,13 +48,12 @@ class HostInfo():
     Return cpu stats at current time
     """
     cpu_times = psutil.cpu_times_percent()
-    load_avg = os.getloadavg()
     cpu_count = self.__host_static_info.get('cpu_num', 1)
 
     # Divide by number of cpu's on the system
     number2percents = lambda x: ((x / int(cpu_count)) * 100)
 
-    return {
+    result = {
       'cpu_num': int(cpu_count),
       'cpu_user': number2percents(cpu_times.user) if hasattr(cpu_times, 'user') else '',
       'cpu_system': number2percents(cpu_times.system) if hasattr(cpu_times, 'system') else
'',
@@ -62,11 +61,17 @@ class HostInfo():
       'cpu_nice': number2percents(cpu_times.nice) if hasattr(cpu_times, 'nice') else '',
       'cpu_wio': number2percents(cpu_times.iowait) if hasattr(cpu_times, 'iowait') else '',
       'cpu_intr': number2percents(cpu_times.irq) if hasattr(cpu_times, 'irq') else '',
-      'cpu_sintr': number2percents(cpu_times.softirq) if hasattr(cpu_times, 'softirq') else
'',
-      'load_one' : load_avg[0] if len(load_avg) > 0 else '',
-      'load_five' : load_avg[1] if len(load_avg) > 1 else '',
-      'load_fifteen' : load_avg[2] if len(load_avg) > 2 else ''
+      'cpu_sintr': number2percents(cpu_times.softirq) if hasattr(cpu_times, 'softirq') else
''
     }
+    if platform.system() != "Windows":
+      load_avg = os.getloadavg()
+      result.update({
+        'load_one' : load_avg[0] if len(load_avg) > 0 else '',
+        'load_five' : load_avg[1] if len(load_avg) > 1 else '',
+        'load_fifteen' : load_avg[2] if len(load_avg) > 2 else ''
+      })
+    return result
+
   pass
 
   def get_process_info(self):

http://git-wip-us.apache.org/repos/asf/ambari/blob/6df5648d/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py
b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py
index 3338db5..4e63d6a 100644
--- a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py
+++ b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py
@@ -20,7 +20,7 @@ limitations under the License.
 
 import logging
 from host_info import HostInfo
-
+import platform
 from unittest import TestCase
 from mock.mock import patch
 
@@ -29,9 +29,8 @@ logger = logging.getLogger()
 class TestHostInfo(TestCase):
 
   @patch("psutil.cpu_count")
-  @patch("os.getloadavg")
   @patch("psutil.cpu_times_percent")
-  def testCpuTimes(self, cp_mock, avg_mock, count_mock):
+  def testCpuTimes(self, cp_mock, count_mock):
     count_mock.return_value = 1
 
     cp = cp_mock.return_value
@@ -42,13 +41,18 @@ class TestHostInfo(TestCase):
     cp.iowait = 0
     cp.irq = 0
     cp.softirq = 0
-
-    avg_mock.return_value  = [13, 13, 13]
-    
     hostinfo = HostInfo()
-    
-    cpu = hostinfo.get_cpu_times()
-    
+
+    if platform.system() != "Windows":
+      with patch("os.getloadavg") as avg_mock:
+        avg_mock.return_value  = [13, 13, 13]
+        cpu = hostinfo.get_cpu_times()
+        self.assertEqual(cpu['load_one'], 13)
+        self.assertEqual(cpu['load_five'], 13)
+        self.assertEqual(cpu['load_fifteen'], 13)
+    else:
+      cpu = hostinfo.get_cpu_times()
+
     self.assertAlmostEqual(cpu['cpu_user'], 10)
     self.assertAlmostEqual(cpu['cpu_system'], 10)
     self.assertAlmostEqual(cpu['cpu_idle'], 70)
@@ -56,9 +60,7 @@ class TestHostInfo(TestCase):
     self.assertAlmostEqual(cpu['cpu_wio'], 0)
     self.assertAlmostEqual(cpu['cpu_intr'], 0)
     self.assertAlmostEqual(cpu['cpu_sintr'], 0)
-    self.assertEqual(cpu['load_one'], 13)
-    self.assertEqual(cpu['load_five'], 13)
-    self.assertEqual(cpu['load_fifteen'], 13)
+
     
   @patch("psutil.disk_usage")
   @patch("psutil.disk_partitions")

http://git-wip-us.apache.org/repos/asf/ambari/blob/6df5648d/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py
b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py
index bc4fba7..26a3edc 100644
--- a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py
+++ b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py
@@ -30,16 +30,14 @@ logger = logging.getLogger()
 
 class TestMetricCollector(TestCase):
   
-  @patch("os.getloadavg")
   @patch.object(HostInfo, "get_cpu_times")
   @patch.object(ApplicationMetricMap, "__init__")
-  def testCollectEvent(self, amm_mock, host_info_mock, avg_mock):
+  def testCollectEvent(self, amm_mock, host_info_mock):
     amm_mock.return_value = None
     host_info_mock.return_value = {'metric_name' : 'metric_value'}
-    avg_mock.return_value.__getitem__.return_value = 13
 
     metric_collector = MetricsCollector(None, amm_mock)
-    
+
     group_config = {'collect_every' : 1, 'metrics' : 'cpu'}
     
     e = HostMetricCollectEvent(group_config, 'cpu')


Mime
View raw message