Double check what the output from dns_response we've got

Related-Bug: PROD-18984

Change-Id: Ic94504b9bf985e602e0ebd3d7a7754a5cde967a9
diff --git a/docker_entrypoint/monitoring/__init__.py b/docker_entrypoint/monitoring/__init__.py
index e5c0e3e..a2a8067 100644
--- a/docker_entrypoint/monitoring/__init__.py
+++ b/docker_entrypoint/monitoring/__init__.py
@@ -14,6 +14,7 @@
 try:
     import dns.resolver
     import dns.reversename
+    import re
 except ImportError:
     pass
 
@@ -73,15 +74,17 @@
 def get_data_dir(node_ip, retries=10, interval=5):
     address = dns.reversename.from_address(node_ip)
     ptr = dns_query(address, 'PTR', retries, interval)
+    ptr_len = len(str(ptr[0]).rstrip('.').split('.'))
 
-    if ptr:
+    if ptr_len == 4:
         # monitoring_server.2.c81t242vtv4qobpv75mckqwob.monitoring_monitoring.
-        ptr_str = str(ptr[0]).split('.')
+        ptr_str = str(ptr[0]).rstrip('.').split('.')
         # ptr_str[0] = service name, monitoring_server
-        # ptr_str[1] = replica number, 2
-        # ptr_str[3] = docker id, c81t242vtv4qobpv75mckqwob
-        # ptr_str[4] = network name, monitoring_monitoring
-        if ptr_str[1]:
+        # ptr_str[1] = swarm node ID, which is running the container
+        # ptr_str[2] = docker task id, c81t242vtv4qobpv75mckqwob
+        # ptr_str[3] = network name, monitoring_monitoring
+        ptr_str_regex = re.compile("^[a-zA-Z0-9]{1,1024}$")
+        if ptr_str_regex.match(ptr_str[1]):
             return 'data/{}/'.format(ptr_str[1])
 
     LOG.error('Fail to discover data dir')