From e79d602e3821955507907a872df6da4cc4d82e2f Mon Sep 17 00:00:00 2001 From: changzhi Date: Fri, 17 Jul 2015 10:43:09 +0800 Subject: [PATCH] Add DNS and DHCP log into dhcp agent Enable set DNS and DHCP log of dnsmasq for dhcp agent Add a new configuration named 'dnsmasq_base_log_dir' in dhcp_agent.ini. This entry should be a path of log file. It should like this: dnsmasq_base_log_dir=/tmp And the DNS and DHCP log will be written into the file "/tmp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/dhcp_dns_log". The dir path will be created if the given path doesn't exists. DocImpact Closes-Bug: #1475636 Change-Id: I87be346ec5059eaa8a29f48fe53933af82d1b155 --- etc/dhcp_agent.ini | 5 +++++ neutron/agent/dhcp/config.py | 5 +++++ neutron/agent/linux/dhcp.py | 16 +++++++++++++++- neutron/tests/unit/agent/linux/test_dhcp.py | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/etc/dhcp_agent.ini b/etc/dhcp_agent.ini index 0f56260f4..115ff86a2 100644 --- a/etc/dhcp_agent.ini +++ b/etc/dhcp_agent.ini @@ -68,6 +68,11 @@ # as forwarders. # dnsmasq_dns_servers = +# Base log dir for dnsmasq logging. The log contains DHCP and DNS log +# information and is useful for debugging issues with either DHCP or DNS. +# If this section is null, disable dnsmasq log. +# dnsmasq_base_log_dir = + # Limit number of leases to prevent a denial-of-service. # dnsmasq_lease_max = 16777216 diff --git a/neutron/agent/dhcp/config.py b/neutron/agent/dhcp/config.py index d00a2fad6..eefac85d4 100644 --- a/neutron/agent/dhcp/config.py +++ b/neutron/agent/dhcp/config.py @@ -54,6 +54,11 @@ DNSMASQ_OPTS = [ "This option is deprecated and " "will be removed in a future release."), deprecated_for_removal=True), + cfg.StrOpt('dnsmasq_base_log_dir', + help=_("Base log dir for dnsmasq logging. " + "The log contains DHCP and DNS log information and " + "is useful for debugging issues with either DHCP or " + "DNS. If this section is null, disable dnsmasq log.")), cfg.IntOpt( 'dnsmasq_lease_max', default=(2 ** 24), diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 0a06259c1..15fab0f59 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -36,7 +36,7 @@ from neutron.common import exceptions from neutron.common import ipv6_utils from neutron.common import utils as commonutils from neutron.extensions import extra_dhcp_opt as edo_ext -from neutron.i18n import _LI, _LW +from neutron.i18n import _LI, _LW, _LE LOG = logging.getLogger(__name__) @@ -379,6 +379,20 @@ class Dnsmasq(DhcpLocalProcess): if self.conf.dhcp_broadcast_reply: cmd.append('--dhcp-broadcast') + if self.conf.dnsmasq_base_log_dir: + try: + if not os.path.exists(self.conf.dnsmasq_base_log_dir): + os.makedirs(self.conf.dnsmasq_base_log_dir) + log_filename = os.path.join( + self.conf.dnsmasq_base_log_dir, + self.network.id, 'dhcp_dns_log') + cmd.append('--log-queries') + cmd.append('--log-dhcp') + cmd.append('--log-facility=%s' % log_filename) + except OSError: + LOG.error(_LE('Error while create dnsmasq base log dir: %s'), + self.conf.dnsmasq_base_log_dir) + return cmd def spawn_process(self): diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index 41a3173d1..ce9587066 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -1001,6 +1001,19 @@ class TestDnsmasq(TestBase): '--server=9.9.9.9', '--domain=openstacklocal']) + def test_spawn_cfg_enable_dnsmasq_log(self): + self.conf.set_override('dnsmasq_base_log_dir', '/tmp') + network = FakeV4Network() + dhcp_dns_log = \ + '/tmp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/dhcp_dns_log' + + self._test_spawn(['--conf-file=', + '--domain=openstacklocal', + '--log-queries', + '--log-dhcp', + ('--log-facility=%s' % dhcp_dns_log)], + network) + def test_spawn_max_leases_is_smaller_than_cap(self): self._test_spawn( ['--conf-file=', '--domain=openstacklocal'], -- 2.45.2