From 157342589f5b2a9c9e2a1dc528fc6fe81a460a3f Mon Sep 17 00:00:00 2001
From: Ross Williams <ross@ross-williams.net>
Date: Tue, 15 Aug 2017 10:57:02 -0400
Subject: [PATCH] Switch to matching cgroup mounts with zbx_regexp_sub()

The string functions were failing to catch some variations in
/proc/mounts formatting.
---
 .../zabbix_module_docker/zabbix_module_docker.c    | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/modules/zabbix_module_docker/zabbix_module_docker.c b/src/modules/zabbix_module_docker/zabbix_module_docker.c
index 1435df5..e1dfe29 100644
--- a/src/modules/zabbix_module_docker/zabbix_module_docker.c
+++ b/src/modules/zabbix_module_docker/zabbix_module_docker.c
@@ -23,6 +23,7 @@
 #include "module.h"
 #include "sysinc.h"
 #include "zbxjson.h"
+#include "zbxregexp.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -588,7 +589,7 @@ int     zbx_docker_dir_detect()
             ""
         }, **tdriver;
         char path[512];
-        char *temp1, *temp2;
+        const char *mounts_regex = "^[^[:blank:]]+[[:blank:]]+(/[^[:blank:]]+/)[^[:blank:]]+[[:blank:]]+cgroup[[:blank:]]+.*$";
         FILE *fp;
         DIR  *dir;
 
@@ -602,12 +603,11 @@ int     zbx_docker_dir_detect()
         {
             if ((strstr(path, "cpuset cgroup")) != NULL)
             {
-                temp1 = string_replace(path, "cgroup ", "");
-                temp2 = string_replace(temp1, strstr(temp1, " "), "");
-                free(temp1);
-                if (stat_dir != NULL) free(stat_dir);
-                stat_dir = string_replace(temp2, "cpuset", "");
-                free(temp2);
+                if (SUCCEED != zbx_regexp_sub(path, mounts_regex, "\\1", &stat_dir) || NULL == stat_dir)
+                {
+                    zabbix_log(LOG_LEVEL_WARNING, "Cannot detect docker stat directory in /proc/mounts");
+                    return SYSINFO_RET_FAIL;
+                }
                 zabbix_log(LOG_LEVEL_DEBUG, "Detected docker stat directory: %s", stat_dir);
 
                 pclose(fp);