ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [10/31] ambari git commit: AMBARI-13310. alerts should log into ambari-agent.log rather than to ambari-agent.out (aonishuk)
Date Wed, 07 Oct 2015 13:51:51 GMT
AMBARI-13310. alerts should log into ambari-agent.log rather than to ambari-agent.out (aonishuk)


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

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 33c8681709894495263ebc9f22f7825c08605155
Parents: cad57c5
Author: Andrew Onishuk <aonishuk@hortonworks.com>
Authored: Mon Oct 5 15:12:47 2015 +0300
Committer: Andrew Onishuk <aonishuk@hortonworks.com>
Committed: Mon Oct 5 15:12:47 2015 +0300

----------------------------------------------------------------------
 .../main/python/ambari_agent/AmbariConfig.py    |  7 +++++++
 .../python/ambari_agent/alerts/script_alert.py  |  2 +-
 .../src/main/python/ambari_agent/main.py        | 20 +++++++++-----------
 .../src/test/python/ambari_agent/TestMain.py    |  4 ++--
 .../resource_management/core/environment.py     |  6 ++++--
 5 files changed, 23 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/33c86817/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py b/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
index f8f220b..453d59c 100644
--- a/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
+++ b/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
@@ -202,6 +202,13 @@ class AmbariConfig:
       return os.path.join(os.environ['AMBARI_AGENT_LOG_DIR'], "ambari-agent.log")
     else:
       return os.path.join(os.sep, "var", "log", "ambari-agent", "ambari-agent.log")
+    
+  @staticmethod
+  def getAlertsLogFile():
+    if 'AMBARI_ALERTS_AGENT_LOG_DIR' in os.environ:
+      return os.path.join(os.environ['AMBARI_ALERTS_AGENT_LOG_DIR'], "ambari-agent.log")
+    else:
+      return os.path.join(os.sep, "var", "log", "ambari-agent", "ambari-alerts.log")
 
   @staticmethod
   def getOutFile():

http://git-wip-us.apache.org/repos/asf/ambari/blob/33c86817/ambari-agent/src/main/python/ambari_agent/alerts/script_alert.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/alerts/script_alert.py b/ambari-agent/src/main/python/ambari_agent/alerts/script_alert.py
index e70dc22..97c3b64 100644
--- a/ambari-agent/src/main/python/ambari_agent/alerts/script_alert.py
+++ b/ambari-agent/src/main/python/ambari_agent/alerts/script_alert.py
@@ -100,7 +100,7 @@ class ScriptAlert(BaseAlert):
       matchObj = re.match( r'((.*)services(.*)package)', self.path_to_script)
       if matchObj:
         basedir = matchObj.group(1)
-        with Environment(basedir, tmp_dir=Constants.AGENT_TMP_DIR) as env:
+        with Environment(basedir, tmp_dir=Constants.AGENT_TMP_DIR, logger=logging.getLogger('ambari_alerts'))
as env:
           return cmd_module.execute(configurations, self.parameters, self.host_name)
       else:
         return cmd_module.execute(configurations, self.parameters, self.host_name)

http://git-wip-us.apache.org/repos/asf/ambari/blob/33c86817/ambari-agent/src/main/python/ambari_agent/main.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/main.py b/ambari-agent/src/main/python/ambari_agent/main.py
index 25f7fa0..6ab89fd 100644
--- a/ambari-agent/src/main/python/ambari_agent/main.py
+++ b/ambari-agent/src/main/python/ambari_agent/main.py
@@ -46,6 +46,7 @@ import HeartbeatHandlers
 from HeartbeatHandlers import bind_signal_handlers
 from ambari_commons.constants import AMBARI_SUDO_BINARY
 logger = logging.getLogger()
+alerts_logger = logging.getLogger('ambari_alerts')
 
 formatstr = "%(levelname)s %(asctime)s %(filename)s:%(lineno)d - %(message)s"
 agentPid = os.getpid()
@@ -57,24 +58,20 @@ IS_LINUX = platform.system() == "Linux"
 SYSLOG_FORMAT_STRING = ' ambari_agent - %(filename)s - [%(process)d] - %(name)s - %(levelname)s
- %(message)s'
 SYSLOG_FORMATTER = logging.Formatter(SYSLOG_FORMAT_STRING)
 
-
-def setup_logging(verbose):
+def setup_logging(logger, filename, verbose):
   formatter = logging.Formatter(formatstr)
-  rotateLog = logging.handlers.RotatingFileHandler(AmbariConfig.AmbariConfig.getLogFile(),
"a", 10000000, 25)
+  rotateLog = logging.handlers.RotatingFileHandler(filename, "a", 10000000, 25)
   rotateLog.setFormatter(formatter)
   logger.addHandler(rotateLog)
       
   if verbose:
-    logging.basicConfig(format=formatstr, level=logging.DEBUG, filename=AmbariConfig.AmbariConfig.getLogFile())
+    logging.basicConfig(format=formatstr, level=logging.DEBUG, filename=filename)
     logger.setLevel(logging.DEBUG)
     logger.info("loglevel=logging.DEBUG")
   else:
-    logging.basicConfig(format=formatstr, level=logging.INFO, filename=AmbariConfig.AmbariConfig.getLogFile())
+    logging.basicConfig(format=formatstr, level=logging.INFO, filename=filename)
     logger.setLevel(logging.INFO)
     logger.info("loglevel=logging.INFO")
-    
-  global is_logger_setup
-  is_logger_setup = True
 
 def add_syslog_handler(logger):
     
@@ -236,9 +233,10 @@ def main(heartbeat_stop_callback=None):
 
   expected_hostname = options.expected_hostname
 
-  current_user = getpass.getuser()
-
-  setup_logging(options.verbose)
+  setup_logging(logger, AmbariConfig.AmbariConfig.getLogFile(), options.verbose)
+  global is_logger_setup
+  is_logger_setup = True
+  setup_logging(alerts_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(), options.verbose)
 
   default_cfg = {'agent': {'prefix': '/home/ambari'}}
   config.load(default_cfg)

http://git-wip-us.apache.org/repos/asf/ambari/blob/33c86817/ambari-agent/src/test/python/ambari_agent/TestMain.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestMain.py b/ambari-agent/src/test/python/ambari_agent/TestMain.py
index 995dcca..e837fcf 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestMain.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestMain.py
@@ -80,7 +80,7 @@ class TestMain(unittest.TestCase):
   @patch("logging.basicConfig")
   def test_setup_logging(self, basicConfig_mock, rfh_mock, setLevel_mock, addHandler_mock):
     # Testing silent mode
-    main.setup_logging(False)
+    main.setup_logging(logging.getLogger(), '/var/log/ambari-agent/ambari-agent.log', False)
     self.assertTrue(addHandler_mock.called)
     setLevel_mock.assert_called_with(logging.INFO)
 
@@ -88,7 +88,7 @@ class TestMain(unittest.TestCase):
     setLevel_mock.reset_mock()
 
     # Testing verbose mode
-    main.setup_logging(True)
+    main.setup_logging(logging.getLogger(), '/var/log/ambari-agent/ambari-agent.log', True)
     self.assertTrue(addHandler_mock.called)
     setLevel_mock.assert_called_with(logging.DEBUG)
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/33c86817/ambari-common/src/main/python/resource_management/core/environment.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/core/environment.py b/ambari-common/src/main/python/resource_management/core/environment.py
index 907521f..134f164 100644
--- a/ambari-common/src/main/python/resource_management/core/environment.py
+++ b/ambari-common/src/main/python/resource_management/core/environment.py
@@ -39,7 +39,7 @@ from resource_management.core.logger import Logger
 class Environment(object):
   _instances = []
 
-  def __init__(self, basedir=None, tmp_dir=None, test_mode=False, logging_level=logging.INFO):
+  def __init__(self, basedir=None, tmp_dir=None, test_mode=False, logger=None, logging_level=logging.INFO):
     """
     @param basedir: basedir/files, basedir/templates are the places where templates / static
files
     are looked up
@@ -47,7 +47,9 @@ class Environment(object):
     """   
     self.reset(basedir, test_mode, tmp_dir)
     
-    if not Logger.logger:
+    if logger:
+      Logger.logger = logger
+    else:
       Logger.initialize_logger(__name__, logging_level)
 
   def reset(self, basedir, test_mode, tmp_dir):


Mime
View raw message