Re: [ndctl PATCH v12 2/5] ndctl, monitor: add main ndctl monitor configuration file

2018-07-13 Thread Verma, Vishal L


On Sat, 2018-07-14 at 00:54 +0900, QI Fuli wrote:
> This patch adds the main configuration file(/etc/ndctl/monitor.conf)
> of ndctl monitor. It contains the configuration directives that give
> ndctl monitor instructions. Users can change the configuration by
> editing this file or using [--config-file] option to override this
> file. The changed value will work after restart ndctl monitor service.
> 
> Signed-off-by: QI Fuli 
> ---
>  ndctl.spec.in  |   1 +
>  ndctl/Makefile.am  |   5 +++
>  ndctl/monitor.c| 110 +
>  ndctl/monitor.conf |  41 +
>  4 files changed, 157 insertions(+)
>  create mode 100644 ndctl/monitor.conf
> 
> diff --git a/ndctl.spec.in b/ndctl.spec.in
> index e2c879c..42760fa 100644
> --- a/ndctl.spec.in
> +++ b/ndctl.spec.in
> @@ -141,6 +141,7 @@ make check
>  %{_includedir}/ndctl/
>  %{_libdir}/libndctl.so
>  %{_libdir}/pkgconfig/libndctl.pc
> +%{_sysconfdir}/ndctl/monitor.conf
>  
>  %files -n DAX_DNAME
>  %defattr(-,root,root)
> diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
> index 083609a..4d0 100644
> --- a/ndctl/Makefile.am
> +++ b/ndctl/Makefile.am
> @@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
>../test/core.c \
>test.c
>  endif
> +
> +monitor_config_file = monitor.conf
> +monitor_configdir = /etc/ndctl/

This path can't be hard coded - we need to pass it in from autoconf via
a variable. Also see below in monitor.c.

> +monitor_config_DATA = $(monitor_config_file)
> +EXTRA_DIST += $(monitor_config_file)
> diff --git a/ndctl/monitor.c b/ndctl/monitor.c
> index caf8c3d..0b0f8f8 100644
> --- a/ndctl/monitor.c
> +++ b/ndctl/monitor.c
> @@ -19,6 +19,7 @@
>  
>  static struct monitor {
>   const char *log;
> + const char *config_file;
>   const char *dimm_event;
>   bool daemon;
>   bool human;
> @@ -454,6 +455,109 @@ dimm_event_all:
>   return 0;
>  }
>  
> +static void parse_config(const char **arg, char *key, char *val, char *ident)
> +{
> + struct strbuf value = STRBUF_INIT;
> + size_t arg_len = *arg ? strlen(*arg) : 0;
> +
> + if (!ident || !key || (strcmp(ident, key) != 0))
> + return;
> +
> + if (arg_len) {
> + strbuf_add(, *arg, arg_len);
> + strbuf_addstr(, " ");
> + }
> + strbuf_addstr(, val);
> + *arg = strbuf_detach(, NULL);
> +}
> +
> +static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
> + struct util_filter_params *_param)
> +{
> + FILE *f;
> + int line = 0;
> + size_t len = 0;
> + char *buf, *value, *config_file;
> + const char *def_config_file = "/etc/ndctl/monitor.conf";

Here too, the path needs to come via the same autoconf define as above.

> +
> + if (_monitor->config_file)
> + config_file = strdup(_monitor->config_file);
> + else
> + config_file = strdup(def_config_file);
> + if (!config_file) {
> + fail("strdup default config file failed\n");
> + goto out;
> + }
> +
> + buf = malloc(BUF_SIZE);
> + if (!buf) {
> + fail("malloc read config-file buf error\n");
> + goto out;
> + }
> +
> + f = fopen(config_file, "r");
> + if (!f) {
> + fail("config-file: %s cannot be opened\n", config_file);
> + goto out;
> + }
> +
> + while (fgets(buf, BUF_SIZE, f)) {
> + value = NULL;
> + line++;
> +
> + while (isspace(*buf))
> + buf++;
> +
> + if (*buf == '#' || *buf == '\0')
> + continue;
> +
> + value = strchr(buf, '=');
> + if (!value) {
> + fail("config-file syntax error, skip line[%i]\n", line);
> + continue;
> + }
> +
> + value[0] = '\0';
> + value++;
> +
> + while (isspace(value[0]))
> + value++;
> +
> + len = strlen(buf);
> + if (len == 0)
> + continue;
> + while (isspace(buf[len-1]))
> + len--;
> + buf[len] = '\0';
> +
> + len = strlen(value);
> + if (len == 0)
> + continue;
> + while (isspace(value[len-1]))
> + len--;
> + value[len] = '\0';
> +
> + if (len == 0)
> + continue;
> +
> + parse_config(&_param->bus, "bus", value, buf);
> + parse_config(&_param->dimm, "dimm", value, buf);
> + parse_config(&_param->region, "region", value, buf);
> + parse_config(&_param->namespace, "namespace", value, buf);
> + parse_config(&_monitor->dimm_event, "dimm-event", value, buf);
> +
> + if (!_monitor->log)
> + parse_config(&_monitor->log, "log", value, buf);
> + }
> + 

[ndctl PATCH v12 2/5] ndctl, monitor: add main ndctl monitor configuration file

2018-07-13 Thread QI Fuli
This patch adds the main configuration file(/etc/ndctl/monitor.conf)
of ndctl monitor. It contains the configuration directives that give
ndctl monitor instructions. Users can change the configuration by
editing this file or using [--config-file] option to override this
file. The changed value will work after restart ndctl monitor service.

Signed-off-by: QI Fuli 
---
 ndctl.spec.in  |   1 +
 ndctl/Makefile.am  |   5 +++
 ndctl/monitor.c| 110 +
 ndctl/monitor.conf |  41 +
 4 files changed, 157 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl.spec.in b/ndctl.spec.in
index e2c879c..42760fa 100644
--- a/ndctl.spec.in
+++ b/ndctl.spec.in
@@ -141,6 +141,7 @@ make check
 %{_includedir}/ndctl/
 %{_libdir}/libndctl.so
 %{_libdir}/pkgconfig/libndctl.pc
+%{_sysconfdir}/ndctl/monitor.conf
 
 %files -n DAX_DNAME
 %defattr(-,root,root)
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 083609a..4d0 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/core.c \
 test.c
 endif
+
+monitor_config_file = monitor.conf
+monitor_configdir = /etc/ndctl/
+monitor_config_DATA = $(monitor_config_file)
+EXTRA_DIST += $(monitor_config_file)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index caf8c3d..0b0f8f8 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -19,6 +19,7 @@
 
 static struct monitor {
const char *log;
+   const char *config_file;
const char *dimm_event;
bool daemon;
bool human;
@@ -454,6 +455,109 @@ dimm_event_all:
return 0;
 }
 
+static void parse_config(const char **arg, char *key, char *val, char *ident)
+{
+   struct strbuf value = STRBUF_INIT;
+   size_t arg_len = *arg ? strlen(*arg) : 0;
+
+   if (!ident || !key || (strcmp(ident, key) != 0))
+   return;
+
+   if (arg_len) {
+   strbuf_add(, *arg, arg_len);
+   strbuf_addstr(, " ");
+   }
+   strbuf_addstr(, val);
+   *arg = strbuf_detach(, NULL);
+}
+
+static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
+   struct util_filter_params *_param)
+{
+   FILE *f;
+   int line = 0;
+   size_t len = 0;
+   char *buf, *value, *config_file;
+   const char *def_config_file = "/etc/ndctl/monitor.conf";
+
+   if (_monitor->config_file)
+   config_file = strdup(_monitor->config_file);
+   else
+   config_file = strdup(def_config_file);
+   if (!config_file) {
+   fail("strdup default config file failed\n");
+   goto out;
+   }
+
+   buf = malloc(BUF_SIZE);
+   if (!buf) {
+   fail("malloc read config-file buf error\n");
+   goto out;
+   }
+
+   f = fopen(config_file, "r");
+   if (!f) {
+   fail("config-file: %s cannot be opened\n", config_file);
+   goto out;
+   }
+
+   while (fgets(buf, BUF_SIZE, f)) {
+   value = NULL;
+   line++;
+
+   while (isspace(*buf))
+   buf++;
+
+   if (*buf == '#' || *buf == '\0')
+   continue;
+
+   value = strchr(buf, '=');
+   if (!value) {
+   fail("config-file syntax error, skip line[%i]\n", line);
+   continue;
+   }
+
+   value[0] = '\0';
+   value++;
+
+   while (isspace(value[0]))
+   value++;
+
+   len = strlen(buf);
+   if (len == 0)
+   continue;
+   while (isspace(buf[len-1]))
+   len--;
+   buf[len] = '\0';
+
+   len = strlen(value);
+   if (len == 0)
+   continue;
+   while (isspace(value[len-1]))
+   len--;
+   value[len] = '\0';
+
+   if (len == 0)
+   continue;
+
+   parse_config(&_param->bus, "bus", value, buf);
+   parse_config(&_param->dimm, "dimm", value, buf);
+   parse_config(&_param->region, "region", value, buf);
+   parse_config(&_param->namespace, "namespace", value, buf);
+   parse_config(&_monitor->dimm_event, "dimm-event", value, buf);
+
+   if (!_monitor->log)
+   parse_config(&_monitor->log, "log", value, buf);
+   }
+   fclose(f);
+   free(config_file);
+   return 0;
+out:
+   if (config_file)
+   free(config_file);
+   return 1;
+}
+
 int cmd_monitor(int argc, const char **argv, void *ctx)
 {
const struct option options[] = {
@@ -469,6 +573,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
OPT_FILENAME('l', "log", ,