Bläddra i källkod

AMBARI-15767. Ambari should report about slow sudo hosts (aonishuk)

Andrew Onishuk 9 år sedan
förälder
incheckning
73472dc1ce

+ 23 - 0
ambari-agent/src/main/python/ambari_agent/main.py

@@ -125,6 +125,27 @@ def resolve_ambari_config():
   except Exception, err:
     logger.warn(err)
 
+def check_sudo():
+  # don't need to check sudo for root.
+  if os.geteuid() == 0:
+    return
+  
+  runner = shellRunner()
+  test_command = [AMBARI_SUDO_BINARY, '/usr/bin/test', '/']
+  test_command_str = ' '.join(test_command)
+  
+  start_time = time.time()
+  res = runner.run(test_command)
+  end_time = time.time()
+  run_time = end_time - start_time
+  
+  if res['exitCode'] != 0:
+    raise Exception("Please check your sudo configurations.\n" + test_command_str + " failed with " + res['error'] + res['output']) # bad sudo configurations
+  
+  if run_time > 2:
+    logger.warn(("Sudo commands on this host are running slowly ('{0}' took {1} seconds).\n" +
+                "This will create a significant slow down for ambari-agent service tasks.").format(test_command_str, run_time))
+    
 
 def perform_prestart_checks(expected_hostname):
   # Check if current hostname is equal to expected one (got from the server
@@ -158,6 +179,8 @@ def perform_prestart_checks(expected_hostname):
     logger.error(msg)
     print(msg)
     sys.exit(1)
+    
+  check_sudo()
 
 
 def daemonize():

+ 3 - 1
ambari-agent/src/test/python/ambari_agent/TestMain.py

@@ -155,12 +155,14 @@ class TestMain(unittest.TestCase):
 
 
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
+  @patch("ambari_commons.shell.shellRunnerLinux.run")
   @patch("sys.exit")
   @patch("os.path.isfile")
   @patch("os.path.isdir")
   @patch("hostname.hostname")
-  def test_perform_prestart_checks(self, hostname_mock, isdir_mock, isfile_mock, exit_mock):
+  def test_perform_prestart_checks(self, hostname_mock, isdir_mock, isfile_mock, exit_mock, shell_mock):
     main.config = AmbariConfig().getConfig()
+    shell_mock.return_value = {"exitCode": 0}
 
     # Check expected hostname test
     hostname_mock.return_value = "test.hst"