[RFC ndctl PATCH 1/3] ndctl, ccan: import ciniparser

2021-05-16 Thread QI Fuli
From: QI Fuli 

Import ciniparser from ccan[1].
[1] https://ccodearchive.net/info/ciniparser.html

Signed-off-by: QI Fuli 
---
 Makefile.am  |   6 +-
 ccan/ciniparser/ciniparser.c | 480 +++
 ccan/ciniparser/ciniparser.h | 262 +++
 ccan/ciniparser/dictionary.c | 266 +++
 ccan/ciniparser/dictionary.h | 166 
 5 files changed, 1179 insertions(+), 1 deletion(-)
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h

diff --git a/Makefile.am b/Makefile.am
index 60a1998..960b5e9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -64,7 +64,11 @@ libccan_a_SOURCES = \
ccan/array_size/array_size.h \
ccan/minmax/minmax.h \
ccan/short_types/short_types.h \
-   ccan/endian/endian.h
+   ccan/endian/endian.h \
+   ccan/ciniparser/ciniparser.h \
+   ccan/ciniparser/ciniparser.c \
+   ccan/ciniparser/dictionary.h \
+   ccan/ciniparser/dictionary.c
 
 noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
diff --git a/ccan/ciniparser/ciniparser.c b/ccan/ciniparser/ciniparser.c
new file mode 100644
index 000..527f837
--- /dev/null
+++ b/ccan/ciniparser/ciniparser.c
@@ -0,0 +1,480 @@
+/* Copyright (c) 2000-2007 by Nicolas Devillard.
+ * Copyright (x) 2009 by Tim Post 
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/** @addtogroup ciniparser
+ * @{
+ */
+/**
+ *  @file ciniparser.c
+ *  @author N. Devillard
+ *  @date Sep 2007
+ *  @version 3.0
+ *  @brief Parser for ini files.
+ */
+
+#include 
+#include 
+
+#define ASCIILINESZ  (1024)
+#define INI_INVALID_KEY  ((char*) NULL)
+
+/**
+ * This enum stores the status for each parsed line (internal use only).
+ */
+typedef enum _line_status_ {
+   LINE_UNPROCESSED,
+   LINE_ERROR,
+   LINE_EMPTY,
+   LINE_COMMENT,
+   LINE_SECTION,
+   LINE_VALUE
+} line_status;
+
+
+/**
+ * @brief Convert a string to lowercase.
+ * @param s String to convert.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string
+ * containing a lowercased version of the input string. Do not free
+ * or modify the returned string! Since the returned string is statically
+ * allocated, it will be modified at each function call (not re-entrant).
+ */
+static char *strlwc(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   int i;
+
+   if (s == NULL)
+   return NULL;
+
+   for (i = 0; s[i] && i < ASCIILINESZ; i++)
+   l[i] = tolower(s[i]);
+   l[i] = '\0';
+   return l;
+}
+
+/**
+ * @brief Remove blanks at the beginning and the end of a string.
+ * @param s String to parse.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string,
+ * which is identical to the input string, except that all blank
+ * characters at the end and the beg. of the string have been removed.
+ * Do not free or modify the returned string! Since the returned string
+ * is statically allocated, it will be modified at each function call
+ * (not re-entrant).
+ */
+static char *strstrip(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   unsigned int i, numspc;
+
+   if (s == NULL)
+   return NULL;
+
+   while (isspace(*s))
+   s++;
+
+   for (i = numspc = 0; s[i] && i < ASCIILINESZ; i++) {
+   l[i] = s[i];
+   if (isspace(l[i]))
+   numspc++;
+   else
+   numspc = 0;
+   }
+   l[i - numspc] = '\0';
+   return l;
+}
+
+/**
+ * @brief Load a single line from an INI file
+ * @param input_line Input line, may be concatenated multi-line inpu

[RFC ndctl PATCH 3/3] ndctl, rename monitor.conf to ndctl.conf

2021-05-16 Thread QI Fuli
From: QI Fuli 

Rename monitor.conf to ndctl.conf, and make it a ndclt global
configuration file that all commands can refer to.
Refactor monitor to make it work with ndctl.conf.

Signed-off-by: QI Fuli 
---
 configure.ac   |   8 +-
 ndctl/Makefile.am  |   9 +-
 ndctl/monitor.c| 127 +
 ndctl/{monitor.conf => ndctl.conf} |  16 +++-
 4 files changed, 40 insertions(+), 120 deletions(-)
 rename ndctl/{monitor.conf => ndctl.conf} (82%)

diff --git a/configure.ac b/configure.ac
index 5ec8d2f..ab2d8a3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -171,10 +171,10 @@ fi
 AC_SUBST([systemd_unitdir])
 AM_CONDITIONAL([ENABLE_SYSTEMD_UNITS], [test "x$with_systemd" = "xyes"])
 
-ndctl_monitorconfdir=${sysconfdir}/ndctl
-ndctl_monitorconf=monitor.conf
-AC_SUBST([ndctl_monitorconfdir])
-AC_SUBST([ndctl_monitorconf])
+ndctl_confdir=${sysconfdir}/ndctl
+ndctl_conf=ndctl.conf
+AC_SUBST([ndctl_confdir])
+AC_SUBST([ndctl_conf])
 
 daxctl_modprobe_datadir=${datadir}/daxctl
 daxctl_modprobe_data=daxctl.conf
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index a63b1e0..b107b3d 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -7,7 +7,7 @@ BUILT_SOURCES = config.h
 config.h: $(srcdir)/Makefile.am
$(AM_V_GEN) echo "/* Autogenerated by ndctl/Makefile.am */" >$@ && \
echo '#define NDCTL_CONF_FILE \
-   "$(ndctl_monitorconfdir)/$(ndctl_monitorconf)"' >>$@
+   "$(ndctl_confdir)/$(ndctl_conf)"' >>$@
$(AM_V_GEN) echo '#define NDCTL_KEYS_DIR  "$(ndctl_keysdir)"' >>$@
 
 ndctl_SOURCES = ndctl.c \
@@ -42,7 +42,7 @@ keys_configdir = $(ndctl_keysdir)
 keys_config_DATA = $(ndctl_keysreadme)
 endif
 
-EXTRA_DIST += keys.readme monitor.conf ndctl-monitor.service
+EXTRA_DIST += keys.readme ndctl.conf ndctl-monitor.service
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
@@ -54,6 +54,7 @@ ndctl_LDADD =\
lib/libndctl.la \
../daxctl/lib/libdaxctl.la \
../libutil.a \
+   ../libccan.a \
$(UUID_LIBS) \
$(KMOD_LIBS) \
$(JSON_LIBS)
@@ -73,8 +74,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 test.c
 endif
 
-monitor_configdir = $(ndctl_monitorconfdir)
-monitor_config_DATA = $(ndctl_monitorconf)
+ndctl_configdir = $(ndctl_confdir)
+ndctl_config_DATA = $(ndctl_conf)
 
 if ENABLE_SYSTEMD_UNITS
 systemd_unit_DATA = ndctl-monitor.service
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index ca36179..ea707d2 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -10,11 +10,13 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #define BUF_SIZE 2048
 
 /* reuse the core log helpers for the monitor logger */
@@ -463,113 +465,6 @@ out:
return rc;
 }
 
-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;
-   size_t len = 0;
-   int line = 0, rc = 0;
-   char *buf = NULL, *seek, *value, *config_file;
-
-   if (_monitor->config_file)
-   config_file = strdup(_monitor->config_file);
-   else
-   config_file = strdup(NDCTL_CONF_FILE);
-   if (!config_file) {
-   fail("strdup default config file failed\n");
-   rc = -ENOMEM;
-   goto out;
-   }
-
-   buf = malloc(BUF_SIZE);
-   if (!buf) {
-   fail("malloc read config-file buf error\n");
-   rc = -ENOMEM;
-   goto out;
-   }
-   seek = buf;
-
-   f = fopen(config_file, "r");
-   if (!f) {
-   if (_monitor->config_file) {
-   err(, "config-file: %s cannot be opened\n",
-   config_file);
-   rc = -errno;
-   }
-   goto out;
-   }
-
-   while (fgets(seek, BUF_SIZE, f)) {
-   value = NULL;
-   line++;
-
-   while (isspace(*seek))
-   seek++;
-
-   if (*seek == '#' || *seek == '\0')
-   continue;
-
-   value = strchr(seek, '=');
-   if (!value) {
-   fail("config-file syntax error, skip line[%i]\n", line);
-

[RFC ndctl PATCH 2/3] ndctl, util: add parse-configs helper

2021-05-16 Thread QI Fuli
From: QI Fuli 

Add parse-config helper to help ndctl commands read the ndctl global
configuration file.

Signed-off-by: QI Fuli 
---
 Makefile.am  |  2 ++
 util/parse-configs.c | 47 
 util/parse-configs.h | 26 
 3 files changed, 75 insertions(+)
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h

diff --git a/Makefile.am b/Makefile.am
index 960b5e9..6e50741 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -74,6 +74,8 @@ noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
util/parse-options.c \
util/parse-options.h \
+   util/parse-configs.c \
+   util/parse-configs.h \
util/usage.c \
util/size.c \
util/main.c \
diff --git a/util/parse-configs.c b/util/parse-configs.c
new file mode 100644
index 000..d404786
--- /dev/null
+++ b/util/parse-configs.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include 
+#include 
+#include 
+#include 
+
+static void set_value(const char **value, char *val)
+{
+   struct strbuf buf = STRBUF_INIT;
+   size_t len = *value ? strlen(*value) : 0;
+
+   if (!val)
+   return;
+
+   if (len) {
+   strbuf_add(, *value, len);
+   strbuf_addstr(, " ");
+   }
+   strbuf_addstr(, val);
+   *value = strbuf_detach(, NULL);
+}
+
+int parse_configs(const char *config_file, const struct config *configs)
+{
+   dictionary *dic;
+
+   dic = ciniparser_load(config_file);
+   if (!dic)
+   return -errno;
+
+   for (; configs->type != CONFIG_END; configs++) {
+   switch (configs->type) {
+   case CONFIG_STRING:
+   set_value((const char **)configs->value,
+   ciniparser_getstring(dic,
+   configs->key, configs->defval));
+   break;
+   case CONFIG_END:
+   break;
+   }
+   }
+
+   ciniparser_freedict(dic);
+   return 0;
+}
diff --git a/util/parse-configs.h b/util/parse-configs.h
new file mode 100644
index 000..31886f7
--- /dev/null
+++ b/util/parse-configs.h
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include 
+#include 
+#include 
+
+enum parse_conf_type {
+   CONFIG_STRING,
+   CONFIG_END,
+};
+
+struct config {
+   enum parse_conf_type type;
+   const char *key;
+   void *value;
+   void *defval;
+};
+
+#define check_vtype(v, type) ( 
BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
+
+#define CONF_END() { .type = CONFIG_END }
+#define CONF_STR(k,v,d) \
+   { .type = CONFIG_STRING, .key = (k), .value = check_vtype(v, const char 
**), .defval = (d) }
+
+int parse_configs(const char *config_file, const struct config *configs);
-- 
2.30.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[RFC ndctl PATCH 0/3] Rename monitor.conf to ndctl.conf as a ndctl global config file

2021-05-16 Thread QI Fuli
From: QI Fuli 

This patch set is to rename monitor.conf to ndctl.conf, and make it a
global ndctl configuration file that all ndctl commands can refer to.

As this patch set has been pending until now, I would like to know if
current idea works or not. If yes, I will finish the documents and test.

Signed-off-by: QI Fuli 

QI Fuli (3):
  ndctl, ccan: import ciniparser
  ndctl, util: add parse-configs helper
  ndctl, rename monitor.conf to ndctl.conf

 Makefile.am|   8 +-
 ccan/ciniparser/ciniparser.c   | 480 +
 ccan/ciniparser/ciniparser.h   | 262 
 ccan/ciniparser/dictionary.c   | 266 
 ccan/ciniparser/dictionary.h   | 166 ++
 configure.ac   |   8 +-
 ndctl/Makefile.am  |   9 +-
 ndctl/monitor.c| 127 ++--
 ndctl/{monitor.conf => ndctl.conf} |  16 +-
 util/parse-configs.c   |  47 +++
 util/parse-configs.h   |  26 ++
 11 files changed, 1294 insertions(+), 121 deletions(-)
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h
 rename ndctl/{monitor.conf => ndctl.conf} (82%)
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h

-- 
2.30.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[ndctl PATCH 2/2] ndctl/test: add checking the presence of jq command ahead

2021-03-01 Thread QI Fuli
Due to the lack of jq command, the result of the test will be 'fail'.
This patch adds checking the presence of jq commmand ahead.
If there is no jq command in the system, the test will be marked as 'skip'.

Signed-off-by: QI Fuli 
Link: https://github.com/pmem/ndctl/issues/141
---
 test/daxdev-errors.sh   | 1 +
 test/inject-error.sh| 2 ++
 test/inject-smart.sh| 1 +
 test/label-compat.sh| 1 +
 test/max_available_extent_ns.sh | 1 +
 test/monitor.sh | 2 ++
 test/multi-dax.sh   | 1 +
 test/sector-mode.sh | 2 ++
 8 files changed, 11 insertions(+)

diff --git a/test/daxdev-errors.sh b/test/daxdev-errors.sh
index 6281f32..9547d78 100755
--- a/test/daxdev-errors.sh
+++ b/test/daxdev-errors.sh
@@ -9,6 +9,7 @@ rc=77
 . $(dirname $0)/common

 check_min_kver "4.12" || do_skip "lacks dax dev error handling"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/inject-error.sh b/test/inject-error.sh
index c636033..7d0b826 100755
--- a/test/inject-error.sh
+++ b/test/inject-error.sh
@@ -11,6 +11,8 @@ err_count=8

 . $(dirname $0)/common

+check_prereq "jq"
+
 trap 'err $LINENO' ERR

 # sample json:
diff --git a/test/inject-smart.sh b/test/inject-smart.sh
index 94705df..4ca83b8 100755
--- a/test/inject-smart.sh
+++ b/test/inject-smart.sh
@@ -166,6 +166,7 @@ do_tests()
 }

 check_min_kver "4.19" || do_skip "kernel $KVER may not support smart 
(un)injection"
+check_prereq "jq"
 modprobe nfit_test
 rc=1

diff --git a/test/label-compat.sh b/test/label-compat.sh
index 340b93d..8ab2858 100755
--- a/test/label-compat.sh
+++ b/test/label-compat.sh
@@ -10,6 +10,7 @@ BASE=$(dirname $0)
 . $BASE/common

 check_min_kver "4.11" || do_skip "may not provide reliable isetcookie values"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/max_available_extent_ns.sh b/test/max_available_extent_ns.sh
index 14d741d..343f3c9 100755
--- a/test/max_available_extent_ns.sh
+++ b/test/max_available_extent_ns.sh
@@ -9,6 +9,7 @@ rc=77
 trap 'err $LINENO' ERR

 check_min_kver "4.19" || do_skip "kernel $KVER may not support 
max_available_size"
+check_prereq "jq"

 init()
 {
diff --git a/test/monitor.sh b/test/monitor.sh
index cdab5e1..28c5541 100755
--- a/test/monitor.sh
+++ b/test/monitor.sh
@@ -13,6 +13,8 @@ smart_supported_bus=""

 . $(dirname $0)/common

+check_prereq "jq"
+
 trap 'err $LINENO' ERR

 check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
diff --git a/test/multi-dax.sh b/test/multi-dax.sh
index e932569..8496619 100755
--- a/test/multi-dax.sh
+++ b/test/multi-dax.sh
@@ -9,6 +9,7 @@ rc=77
 . $(dirname $0)/common

 check_min_kver "4.13" || do_skip "may lack multi-dax support"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/sector-mode.sh b/test/sector-mode.sh
index dd7013e..54fa806 100755
--- a/test/sector-mode.sh
+++ b/test/sector-mode.sh
@@ -6,6 +6,8 @@ rc=77

 . $(dirname $0)/common

+check_prereq "jq"
+
 set -e
 trap 'err $LINENO' ERR

--
2.29.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[ndctl PATCH 1/2] configure: add checking jq command

2021-03-01 Thread QI Fuli
Add checking jq command since it is needed to validate tests

Cc: Santosh Sivaraj 
Signed-off-by: QI Fuli 
Link: https://github.com/pmem/ndctl/issues/141
---
 configure.ac | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/configure.ac b/configure.ac
index 5ec8d2f..839836b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,6 +65,12 @@ fi
 AC_SUBST([XMLTO])
 fi

+AC_CHECK_PROG(JQ, [jq], [$(which jq)], [missing])
+if test "x$JQ" = xmissing; then
+   AC_MSG_ERROR([jq command needed to validate tests])
+fi
+AC_SUBST([JQ])
+
 AC_C_TYPEOF
 AC_DEFINE([HAVE_STATEMENT_EXPR], 1, [Define to 1 if you have statement 
expressions.])

--
2.29.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[ndctl PATCH] ndctl/test: add checking the presence of jq command ahead

2021-02-03 Thread QI Fuli
Due to the lack of jq command, the result of the test will be 'fail'.
This patch adds checking the presence of jq commmand ahead.
If there is no jq command in the system, the test will be marked as 'skip'.

Signed-off-by: QI Fuli 
Link: https://github.com/pmem/ndctl/issues/141
---
 test/daxdev-errors.sh   | 1 +
 test/inject-error.sh| 2 ++
 test/inject-smart.sh| 1 +
 test/label-compat.sh| 1 +
 test/max_available_extent_ns.sh | 1 +
 test/monitor.sh | 2 ++
 test/multi-dax.sh   | 1 +
 test/sector-mode.sh | 2 ++
 8 files changed, 11 insertions(+)

diff --git a/test/daxdev-errors.sh b/test/daxdev-errors.sh
index 6281f32..9547d78 100755
--- a/test/daxdev-errors.sh
+++ b/test/daxdev-errors.sh
@@ -9,6 +9,7 @@ rc=77
 . $(dirname $0)/common

 check_min_kver "4.12" || do_skip "lacks dax dev error handling"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/inject-error.sh b/test/inject-error.sh
index c636033..7d0b826 100755
--- a/test/inject-error.sh
+++ b/test/inject-error.sh
@@ -11,6 +11,8 @@ err_count=8

 . $(dirname $0)/common

+check_prereq "jq"
+
 trap 'err $LINENO' ERR

 # sample json:
diff --git a/test/inject-smart.sh b/test/inject-smart.sh
index 94705df..4ca83b8 100755
--- a/test/inject-smart.sh
+++ b/test/inject-smart.sh
@@ -166,6 +166,7 @@ do_tests()
 }

 check_min_kver "4.19" || do_skip "kernel $KVER may not support smart 
(un)injection"
+check_prereq "jq"
 modprobe nfit_test
 rc=1

diff --git a/test/label-compat.sh b/test/label-compat.sh
index 340b93d..8ab2858 100755
--- a/test/label-compat.sh
+++ b/test/label-compat.sh
@@ -10,6 +10,7 @@ BASE=$(dirname $0)
 . $BASE/common

 check_min_kver "4.11" || do_skip "may not provide reliable isetcookie values"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/max_available_extent_ns.sh b/test/max_available_extent_ns.sh
index 14d741d..343f3c9 100755
--- a/test/max_available_extent_ns.sh
+++ b/test/max_available_extent_ns.sh
@@ -9,6 +9,7 @@ rc=77
 trap 'err $LINENO' ERR

 check_min_kver "4.19" || do_skip "kernel $KVER may not support 
max_available_size"
+check_prereq "jq"

 init()
 {
diff --git a/test/monitor.sh b/test/monitor.sh
index cdab5e1..28c5541 100755
--- a/test/monitor.sh
+++ b/test/monitor.sh
@@ -13,6 +13,8 @@ smart_supported_bus=""

 . $(dirname $0)/common

+check_prereq "jq"
+
 trap 'err $LINENO' ERR

 check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
diff --git a/test/multi-dax.sh b/test/multi-dax.sh
index e932569..8496619 100755
--- a/test/multi-dax.sh
+++ b/test/multi-dax.sh
@@ -9,6 +9,7 @@ rc=77
 . $(dirname $0)/common

 check_min_kver "4.13" || do_skip "may lack multi-dax support"
+check_prereq "jq"

 trap 'err $LINENO' ERR

diff --git a/test/sector-mode.sh b/test/sector-mode.sh
index dd7013e..54fa806 100755
--- a/test/sector-mode.sh
+++ b/test/sector-mode.sh
@@ -6,6 +6,8 @@ rc=77

 . $(dirname $0)/common

+check_prereq "jq"
+
 set -e
 trap 'err $LINENO' ERR

--
2.29.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[ndctl PATCH] ndctl: update .gitignore

2021-02-02 Thread QI Fuli
Add Documentation/ndctl/attrs.adoc and *.lo to .gitignore.

Signed-off-by: QI Fuli 
---
 .gitignore | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3ef9ff7..53512b2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *.o
+*.lo
 *.xml
 .deps/
 .libs/
@@ -15,13 +16,13 @@ Makefile.in
 *.1
 Documentation/daxctl/asciidoc.conf
 Documentation/ndctl/asciidoc.conf
+Documentation/ndctl/attrs.adoc
 Documentation/daxctl/asciidoctor-extensions.rb
 Documentation/ndctl/asciidoctor-extensions.rb
 .dirstamp
 daxctl/config.h
 daxctl/daxctl
 daxctl/lib/libdaxctl.la
-daxctl/lib/libdaxctl.lo
 daxctl/lib/libdaxctl.pc
 *.a
 ndctl/config.h
@@ -29,8 +30,6 @@ ndctl/lib/libndctl.pc
 ndctl/ndctl
 rhel/
 sles/ndctl.spec
-util/log.lo
-util/sysfs.lo
 version.m4
 *.swp
 cscope.files
--
2.29.2
___
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org


[ndctl PATCH] monitor: removing the requirement of default config

2019-03-29 Thread QI Fuli
Removing the requirement of default configuration file.
If "-c, --config-file" option is not specified, default configuration
file should be dispensable.

Link: https://github.com/pmem/ndctl/issues/83
Signed-off-by: QI Fuli 

---
 ndctl/monitor.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 346a6df..6829a6b 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -484,8 +484,11 @@ static int read_config_file(struct ndctl_ctx *ctx, struct 
monitor *_monitor,
 
f = fopen(config_file, "r");
if (!f) {
-   err(, "config-file: %s cannot be opened\n", 
config_file);
-   rc = -errno;
+   if (_monitor->config_file) {
+   err(, "config-file: %s cannot be opened\n",
+   config_file);
+   rc = -errno;
+   }
goto out;
}
 
-- 
2.21.0.rc1

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v2 0/2] ndctl, monitor: refactor read_config_file

2019-01-07 Thread QI Fuli
This patch set is used for refactoring read_config_file by using
ccan/ciniparser library.

Signed-off-by: QI Fuli 
--
Change log since v1:
- Fix the conflicts with the lastest cleanup patch of monitor

QI Fuli (2):
  ndctl: add the ciniparser tool from ccan
  ndctl, monitor: refactor read_config_file

 Makefile.am  |   6 +-
 ccan/ciniparser/LICENSE  |   1 +
 ccan/ciniparser/ciniparser.c | 480 +++
 ccan/ciniparser/ciniparser.h | 262 +++
 ccan/ciniparser/dictionary.c | 266 +++
 ccan/ciniparser/dictionary.h | 166 
 ndctl/Makefile.am|   1 +
 ndctl/monitor.c  | 115 +++--
 ndctl/monitor.conf   |   2 +
 9 files changed, 1212 insertions(+), 87 deletions(-)
 create mode 12 ccan/ciniparser/LICENSE
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h

-- 
2.20.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v2 1/2] ndctl: add the ciniparser tool from ccan

2019-01-07 Thread QI Fuli
Import ciniparser tool from ccan [1], therefore, the ndctl monitor can
read the configuration file by using this library.

[1]: https://ccodearchive.net/info/ciniparser.html

Signed-off-by: QI Fuli 
---
 Makefile.am  |   6 +-
 ccan/ciniparser/LICENSE  |   1 +
 ccan/ciniparser/ciniparser.c | 480 +++
 ccan/ciniparser/ciniparser.h | 262 +++
 ccan/ciniparser/dictionary.c | 266 +++
 ccan/ciniparser/dictionary.h | 166 
 6 files changed, 1180 insertions(+), 1 deletion(-)
 create mode 12 ccan/ciniparser/LICENSE
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h

diff --git a/Makefile.am b/Makefile.am
index e0c463a..2e4b033 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -56,7 +56,11 @@ libccan_a_SOURCES = \
ccan/array_size/array_size.h \
ccan/minmax/minmax.h \
ccan/short_types/short_types.h \
-   ccan/endian/endian.h
+   ccan/endian/endian.h \
+   ccan/ciniparser/ciniparser.h \
+   ccan/ciniparser/ciniparser.c \
+   ccan/ciniparser/dictionary.h \
+   ccan/ciniparser/dictionary.c
 
 noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
diff --git a/ccan/ciniparser/LICENSE b/ccan/ciniparser/LICENSE
new file mode 12
index 000..2354d12
--- /dev/null
+++ b/ccan/ciniparser/LICENSE
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/ciniparser/ciniparser.c b/ccan/ciniparser/ciniparser.c
new file mode 100644
index 000..527f837
--- /dev/null
+++ b/ccan/ciniparser/ciniparser.c
@@ -0,0 +1,480 @@
+/* Copyright (c) 2000-2007 by Nicolas Devillard.
+ * Copyright (x) 2009 by Tim Post 
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/** @addtogroup ciniparser
+ * @{
+ */
+/**
+ *  @file ciniparser.c
+ *  @author N. Devillard
+ *  @date Sep 2007
+ *  @version 3.0
+ *  @brief Parser for ini files.
+ */
+
+#include 
+#include 
+
+#define ASCIILINESZ  (1024)
+#define INI_INVALID_KEY  ((char*) NULL)
+
+/**
+ * This enum stores the status for each parsed line (internal use only).
+ */
+typedef enum _line_status_ {
+   LINE_UNPROCESSED,
+   LINE_ERROR,
+   LINE_EMPTY,
+   LINE_COMMENT,
+   LINE_SECTION,
+   LINE_VALUE
+} line_status;
+
+
+/**
+ * @brief Convert a string to lowercase.
+ * @param s String to convert.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string
+ * containing a lowercased version of the input string. Do not free
+ * or modify the returned string! Since the returned string is statically
+ * allocated, it will be modified at each function call (not re-entrant).
+ */
+static char *strlwc(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   int i;
+
+   if (s == NULL)
+   return NULL;
+
+   for (i = 0; s[i] && i < ASCIILINESZ; i++)
+   l[i] = tolower(s[i]);
+   l[i] = '\0';
+   return l;
+}
+
+/**
+ * @brief Remove blanks at the beginning and the end of a string.
+ * @param s String to parse.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string,
+ * which is identical to the input string, except that all blank
+ * characters at the end and the beg. of the string have been removed.
+ * Do not free or modify the returned string! Since the returned string
+ * is statically allocated, it will be modified at each function call
+ * (not re-entrant).
+ */
+static char *strstrip(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   unsigned int i, numspc;
+
+   if (s == NULL)
+   return NULL;
+
+   while (isspace(*s))
+   s++;
+
+   for (i = numspc 

[PATCH 2/2] ndctl, monitor: refactor parse_config_file

2018-12-19 Thread QI Fuli
This patch is used for refactoring parse_config_file by replacing the
open coded implementation with ccan/ciniparser library.

Signed-off-by: QI Fuli 
---
 ndctl/Makefile.am  |   1 +
 ndctl/monitor.c| 114 -
 ndctl/monitor.conf |   2 +
 3 files changed, 32 insertions(+), 85 deletions(-)

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 8a5e5f8..3da1c0e 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -28,6 +28,7 @@ ndctl_LDADD =\
lib/libndctl.la \
../daxctl/lib/libdaxctl.la \
../libutil.a \
+   ../libccan.a \
$(UUID_LIBS) \
$(KMOD_LIBS) \
$(JSON_LIBS)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index b92a133..a4e8048 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -14,6 +14,7 @@
 #include 
 #include 
 #include 
+#include 
 #define BUF_SIZE 2048
 
 static struct monitor {
@@ -476,12 +477,12 @@ out:
return rc;
 }
 
-static void parse_config(const char **arg, char *key, char *val, char *ident)
+static void parse_config(const char **arg, char *val)
 {
struct strbuf value = STRBUF_INIT;
size_t arg_len = *arg ? strlen(*arg) : 0;
 
-   if (!ident || !key || (strcmp(ident, key) != 0))
+   if (!val)
return;
 
if (arg_len) {
@@ -492,92 +493,32 @@ static void parse_config(const char **arg, char *key, 
char *val, char *ident)
*arg = strbuf_detach(, NULL);
 }
 
-static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
-   struct util_filter_params *_param)
+static int parse_config_file(const char *config_file)
 {
-   FILE *f;
-   size_t len = 0;
-   int line = 0, rc = 0;
-   char *buf = NULL, *seek, *value, *config_file;
-
-   if (_monitor->config_file)
-   config_file = strdup(_monitor->config_file);
-   else
-   config_file = strdup(DEF_CONF_FILE);
-   if (!config_file) {
-   fail("strdup default config file failed\n");
-   rc = -ENOMEM;
-   goto out;
-   }
-
-   buf = malloc(BUF_SIZE);
-   if (!buf) {
-   fail("malloc read config-file buf error\n");
-   rc = -ENOMEM;
+   dictionary *dic;
+
+   dic = ciniparser_load(config_file);
+   if (!dic)
+   return -errno;
+
+   parse_config(,
+   ciniparser_getstring(dic, "monitor:bus", NULL));
+   parse_config(,
+   ciniparser_getstring(dic, "monitor:dimm", NULL));
+   parse_config(,
+   ciniparser_getstring(dic, "monitor:region", NULL));
+   parse_config(,
+   ciniparser_getstring(dic, "monitor:namespace", NULL));
+   parse_config(_event,
+   ciniparser_getstring(dic, "monitor:dimm-event", NULL));
+   if (monitor.log)
goto out;
-   }
-   seek = buf;
-
-   f = fopen(config_file, "r");
-   if (!f) {
-   err(ctx, "config-file: %s cannot be opened\n", config_file);
-   rc = -errno;
-   goto out;
-   }
-
-   while (fgets(seek, BUF_SIZE, f)) {
-   value = NULL;
-   line++;
-
-   while (isspace(*seek))
-   seek++;
-
-   if (*seek == '#' || *seek == '\0')
-   continue;
+   parse_config(,
+   ciniparser_getstring(dic, "monitor:log", NULL));
 
-   value = strchr(seek, '=');
-   if (!value) {
-   fail("config-file syntax error, skip line[%i]\n", line);
-   continue;
-   }
-
-   value[0] = '\0';
-   value++;
-
-   while (isspace(value[0]))
-   value++;
-
-   len = strlen(seek);
-   if (len == 0)
-   continue;
-   while (isspace(seek[len-1]))
-   len--;
-   seek[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, seek);
-   parse_config(&_param->dimm, "dimm", value, seek);
-   parse_config(&_param->region, "region", value, seek);
-   parse_config(&_param->namespace, "namespace", value, seek);
-   parse_config(&_monitor->dimm_event, "dimm-event", value, seek);
-
-   if (!_monitor->log)
-   parse_config(&_monitor->log, &quo

[PATCH 1/2] ndctl: add the ciniparser tool from ccan

2018-12-19 Thread QI Fuli
Import ciniparser tool from ccan [1], therefore, the ndctl monitor can
read the configuration file by using this library.

[1]: https://ccodearchive.net/info/ciniparser.html

Signed-off-by: QI Fuli 
---
 Makefile.am  |   6 +-
 ccan/ciniparser/LICENSE  |   1 +
 ccan/ciniparser/ciniparser.c | 480 +++
 ccan/ciniparser/ciniparser.h | 262 +++
 ccan/ciniparser/dictionary.c | 266 +++
 ccan/ciniparser/dictionary.h | 166 
 6 files changed, 1180 insertions(+), 1 deletion(-)
 create mode 12 ccan/ciniparser/LICENSE
 create mode 100644 ccan/ciniparser/ciniparser.c
 create mode 100644 ccan/ciniparser/ciniparser.h
 create mode 100644 ccan/ciniparser/dictionary.c
 create mode 100644 ccan/ciniparser/dictionary.h

diff --git a/Makefile.am b/Makefile.am
index e0c463a..2e4b033 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -56,7 +56,11 @@ libccan_a_SOURCES = \
ccan/array_size/array_size.h \
ccan/minmax/minmax.h \
ccan/short_types/short_types.h \
-   ccan/endian/endian.h
+   ccan/endian/endian.h \
+   ccan/ciniparser/ciniparser.h \
+   ccan/ciniparser/ciniparser.c \
+   ccan/ciniparser/dictionary.h \
+   ccan/ciniparser/dictionary.c
 
 noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
diff --git a/ccan/ciniparser/LICENSE b/ccan/ciniparser/LICENSE
new file mode 12
index 000..2354d12
--- /dev/null
+++ b/ccan/ciniparser/LICENSE
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/ciniparser/ciniparser.c b/ccan/ciniparser/ciniparser.c
new file mode 100644
index 000..527f837
--- /dev/null
+++ b/ccan/ciniparser/ciniparser.c
@@ -0,0 +1,480 @@
+/* Copyright (c) 2000-2007 by Nicolas Devillard.
+ * Copyright (x) 2009 by Tim Post 
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/** @addtogroup ciniparser
+ * @{
+ */
+/**
+ *  @file ciniparser.c
+ *  @author N. Devillard
+ *  @date Sep 2007
+ *  @version 3.0
+ *  @brief Parser for ini files.
+ */
+
+#include 
+#include 
+
+#define ASCIILINESZ  (1024)
+#define INI_INVALID_KEY  ((char*) NULL)
+
+/**
+ * This enum stores the status for each parsed line (internal use only).
+ */
+typedef enum _line_status_ {
+   LINE_UNPROCESSED,
+   LINE_ERROR,
+   LINE_EMPTY,
+   LINE_COMMENT,
+   LINE_SECTION,
+   LINE_VALUE
+} line_status;
+
+
+/**
+ * @brief Convert a string to lowercase.
+ * @param s String to convert.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string
+ * containing a lowercased version of the input string. Do not free
+ * or modify the returned string! Since the returned string is statically
+ * allocated, it will be modified at each function call (not re-entrant).
+ */
+static char *strlwc(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   int i;
+
+   if (s == NULL)
+   return NULL;
+
+   for (i = 0; s[i] && i < ASCIILINESZ; i++)
+   l[i] = tolower(s[i]);
+   l[i] = '\0';
+   return l;
+}
+
+/**
+ * @brief Remove blanks at the beginning and the end of a string.
+ * @param s String to parse.
+ * @return ptr to statically allocated string.
+ *
+ * This function returns a pointer to a statically allocated string,
+ * which is identical to the input string, except that all blank
+ * characters at the end and the beg. of the string have been removed.
+ * Do not free or modify the returned string! Since the returned string
+ * is statically allocated, it will be modified at each function call
+ * (not re-entrant).
+ */
+static char *strstrip(const char *s)
+{
+   static char l[ASCIILINESZ+1];
+   unsigned int i, numspc;
+
+   if (s == NULL)
+   return NULL;
+
+   while (isspace(*s))
+   s++;
+
+   for (i = numspc 

[PATCH] ndctl, monitor: fix the sevrity of "daemon started" message

2018-10-05 Thread QI Fuli
The "daemon started" message was printed as an error and this is improper.
This patch is used to chang it to info() meanwhile to chang the default log
level of monitor to "LOG_INFO".

Signed-off-by: QI Fuli 
---
 ndctl/monitor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index d29e378..17465bb 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -628,7 +628,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
if (monitor.verbose)
ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_DEBUG);
else
-   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
+   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_INFO);
 
rc = read_config_file((struct ndctl_ctx *)ctx, , );
if (rc)
@@ -660,7 +660,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
err((struct ndctl_ctx *)ctx, "daemon start failed\n");
goto out;
}
-   err((struct ndctl_ctx *)ctx, "ndctl monitor daemon started\n");
+   info((struct ndctl_ctx *)ctx, "ndctl monitor daemon started\n");
}
 
if (parse_monitor_event(, (struct ndctl_ctx *)ctx))
-- 
2.19.0.rc0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 1/2] ndctl, monitor: fix the severity of the "daemon started" message

2018-10-05 Thread Qi, Fuli
> > > >
> > >
> > > Sounds good to me.
> >
> > In my original idea, I just wanted to log the "daemon started" message.
> > After thinking more about it, I agree that the message is not at "error" 
> > level.
> > However, considering this message will be helpful for some users like system
> operators,
> > how about changing this message level to "info" and meanwhile changing the
> default log level to "LOG_INFO" as following?
> >
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c
> > index d29e378..10866b6 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -628,7 +628,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > if (monitor.verbose)
> > ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_DEBUG);
> > else
> > -   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
> > +   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_INFO);
> >
> > rc = read_config_file((struct ndctl_ctx *)ctx, , );
> > if (rc)
> > @@ -657,7 +657,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > if (!monitor.log || strncmp(monitor.log, "./", 2) == 0)
> > ndctl_set_log_fn((struct ndctl_ctx *)ctx, 
> > log_syslog);
> > if (daemon(0, 0) != 0) {
> > -   err((struct ndctl_ctx *)ctx, "daemon start 
> > failed\n");
> > +   info((struct ndctl_ctx *)ctx, "daemon start 
> > failed\n");
> > goto out;
> > }
> > err((struct ndctl_ctx *)ctx, "ndctl monitor daemon 
> > started\n");
> 
> Yes, this looks good to me. Will you send a proper patch for this, and I
> can replace my patch with yours.
> 
> Thanks,
>   -Vishal
> 

Ok, I will send a patch.

> >
> > Thank you very much,
> > QI Fuli
> > > ___
> > > Linux-nvdimm mailing list
> > > Linux-nvdimm@lists.01.org
> > > https://lists.01.org/mailman/listinfo/linux-nvdimm
> >
> >
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 2/2] ndctl, monitor: in daemon mode, exit successfully if no DIMMs are found

2018-10-04 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Friday, October 5, 2018 9:01 AM
> To: linux-nvdimm@lists.01.org
> Cc: Qi, Fuli/斉 福利 ; Andreas Hasenack
> ; Vishal Verma ; Dan
> Williams 
> Subject: [ndctl PATCH 2/2] ndctl, monitor: in daemon mode, exit successfully 
> if no
> DIMMs are found
> 
> When we are running as a daemon, it is preferred to exit successfully when no 
> DIMMs
> are found. When running in the foreground, retain an error return so that the 
> user can
> be notified that nothing was found to monitor.
> 
> In the longer term, replace this with a libudev uevent monitor that will look 
> for DIMM
> addition/removal events, and update the running monitor(s) if the DIMMs match 
> the
> active filter spec.
> 
> Link: https://bugs.launchpad.net/ubuntu/+source/ndctl/+bug/1781268
> Reported-by: Andreas Hasenack 
> Cc: Dan Williams 
> Cc: QI Fuli 
> Signed-off-by: Vishal Verma 
> ---
>  ndctl/monitor.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/ndctl/monitor.c b/ndctl/monitor.c index b44f946..7bf2ba7 100644
> --- a/ndctl/monitor.c
> +++ b/ndctl/monitor.c
> @@ -681,8 +681,9 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
>   goto out;
> 
>   if (!mfa.num_dimm) {
> - err((struct ndctl_ctx *)ctx, "no dimms to monitor\n");
> - rc = -ENXIO;
> + dbg((struct ndctl_ctx *)ctx, "no dimms to monitor\n");
> + if (!monitor.daemon)
> + rc = -ENXIO;
>   goto out;
>   }
> 
> --
> 2.17.1
> 

Looks good to me.

Thank you very much,
QI Fuli

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 1/2] ndctl, monitor: fix the severity of the "daemon started" message

2018-10-04 Thread Qi, Fuli
> -Original Message-
> From: Linux-nvdimm [mailto:linux-nvdimm-boun...@lists.01.org] On Behalf Of Dan
> Williams
> Sent: Friday, October 5, 2018 1:24 PM
> To: Gotou, Yasunori/五島 康文 
> Cc: andr...@canonical.com; linux-nvdimm 
> Subject: Re: [ndctl PATCH 1/2] ndctl, monitor: fix the severity of the "daemon
> started" message
> 
> On Thu, Oct 4, 2018 at 9:09 PM Yasunori Goto  wrote:
> >
> > Hi, Vishal-san,
> >
> > > The above message was printed as an error, but it is just an
> > > informational message. Change it to dbg().
> >
> > Hmmm.
> >
> > When I was a engineer for trouble-shooting of customer's Linux system,
> > the starting time and the ending time of any daemon was very helpful
> > for investigating their trouble.
> >
> > So, I think it is not only for developer, but also it is essential for
> > trouble-shooter for custmer support.
> > (It may be also same with system operator)
> >
> >
> > >
> > > Cc: QI Fuli 
> > > Cc: Dan Williams 
> > > Signed-off-by: Vishal Verma 
> > > ---
> > >  ndctl/monitor.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/ndctl/monitor.c b/ndctl/monitor.c index
> > > d29e378..b44f946 100644
> > > --- a/ndctl/monitor.c
> > > +++ b/ndctl/monitor.c
> > > @@ -660,7 +660,7 @@ int cmd_monitor(int argc, const char **argv, void 
> > > *ctx)
> > >   err((struct ndctl_ctx *)ctx, "daemon start 
> > > failed\n");
> > >   goto out;
> > >   }
> > > - err((struct ndctl_ctx *)ctx, "ndctl monitor daemon 
> > > started\n");
> > > + dbg((struct ndctl_ctx *)ctx, "ndctl monitor daemon
> > > + started\n");
> >
> > Though I agree its message is not "error" certainly, I would like to
> > keep it as normal status level message.
> >
> 
> Sounds good to me.

In my original idea, I just wanted to log the "daemon started" message.
After thinking more about it, I agree that the message is not at "error" level.
However, considering this message will be helpful for some users like system 
operators,
how about changing this message level to "info" and meanwhile changing the 
default log level to "LOG_INFO" as following?

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index d29e378..10866b6 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -628,7 +628,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
if (monitor.verbose)
ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_DEBUG);
else
-   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
+   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_INFO);

rc = read_config_file((struct ndctl_ctx *)ctx, , );
if (rc)
@@ -657,7 +657,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
if (!monitor.log || strncmp(monitor.log, "./", 2) == 0)
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
if (daemon(0, 0) != 0) {
-   err((struct ndctl_ctx *)ctx, "daemon start failed\n");
+   info((struct ndctl_ctx *)ctx, "daemon start failed\n");
goto out;
}
err((struct ndctl_ctx *)ctx, "ndctl monitor daemon started\n");

Thank you very much,
QI Fuli
> ___
> Linux-nvdimm mailing list
> Linux-nvdimm@lists.01.org
> https://lists.01.org/mailman/listinfo/linux-nvdimm


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl, test: add UUID_LIBS for list_smart_dimm

2018-08-20 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Tuesday, August 21, 2018 5:21 AM
> To: linux-nvdimm@lists.01.org
> Cc: Vishal Verma ; Qi, Fuli/斉 福利
> 
> Subject: [ndctl PATCH] ndctl, test: add UUID_LIBS for list_smart_dimm
> 
> Some environments automatically include the missing library dependency, and 
> others
> need it to be explicitly included.
> 
> Link: https://github.com/pmem/ndctl/issues/66
> Cc: QI Fuli 
> Fixes: d1941474111a ("ndctl, test: add a new unit test for monitor")
> Signed-off-by: Vishal Verma 
> ---
>  test/Makefile.am | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/test/Makefile.am b/test/Makefile.am index c224eb7..50bb2e4 100644
> --- a/test/Makefile.am
> +++ b/test/Makefile.am
> @@ -162,4 +162,5 @@ list_smart_dimm_SOURCES = \  list_smart_dimm_LDADD = \
>   $(LIBNDCTL_LIB) \
>   $(JSON_LIBS) \
> + $(UUID_LIBS) \
>   ../libutil.a

Looks good to me.
Reviewed-by: QI Fuli 

Thank you very much.
 Qi

> --
> 2.14.4
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 1/3] ndctl, monitor: Fix formatting for --log in the man page

2018-08-14 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Wednesday, August 15, 2018 8:06 AM
> To: linux-nvdimm@lists.01.org
> Cc: Vishal Verma ; Qi, Fuli/斉 福利
> 
> Subject: [ndctl PATCH 1/3] ndctl, monitor: Fix formatting for --log in the 
> man page
> 
> Change the --log option to be similar to other options-with-arguments, such 
> as --map.
> 
> Cc: QI Fuli 
> Fixes: 8d2d75f76f1e ("ndctl, monitor: set default log destination to syslog 
> if...")
> Signed-off-by: Vishal Verma 
> ---
>  Documentation/ndctl/ndctl-monitor.txt | 23 ++-
>  1 file changed, 10 insertions(+), 13 deletions(-)
> 
> diff --git a/Documentation/ndctl/ndctl-monitor.txt
> b/Documentation/ndctl/ndctl-monitor.txt
> index 9a8d76b..363c398 100644
> --- a/Documentation/ndctl/ndctl-monitor.txt
> +++ b/Documentation/ndctl/ndctl-monitor.txt
> @@ -65,20 +65,17 @@ OPTIONS
>   A 'namespaceX.Y' device name, or namespace region plus id tuple
>   'X.Y'.
> 
> --l ::
> ---log=::
> +-l::
> +--log=::
>   Send log messages to the specified destination.
> -+
> ---
> -::
> - Send log messages to specified . When fopen() is not able
> - to open , log messages will be forwarded to syslog.
> -syslog::
> - Send messages to syslog.
> -standard::
> - Send messages to standard output.
> ---
> -+
> + - "":
> +   Send log messages to specified . When fopen() is not able
> +   to open , log messages will be forwarded to syslog.
> + - "syslog":
> +   Send messages to syslog.
> + - "standard":
> +   Send messages to standard output.
> +

Looks good to me.
Please feel free to add: Reviewed-by: QI Fuli 

Thanks,
QI

>  The default log destination is 'syslog' if "--daemon" is specified,  
> otherwise 'standard'.
> Note that standard and relative path for   will not work if "--daemon" 
> is specified.
> --
> 2.14.4
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 2/4] ndctl, monitor: set default log destination to syslog if "--daemon" is specified

2018-08-08 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, August 8, 2018 10:35 PM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH 2/4] ndctl, monitor: set default log destination to 
> syslog if
> "--daemon" is specified
> 
> HiQi,
> 
> On 08/07/2018 09:31 PM, Qi, Fuli wrote:
> ...
> >> On 08/07/2018 09:17 AM, QI Fuli wrote:
> >>> When running monitor as a daemon, if the log destination is "standard"
> >>> or a relative path for log file, the messages will not be able to be 
> >>> logged.
> >>> Sometimes, users may not notice that the default log destination is 
> >>> "standard"
> >>> when they start monitor daemon by systemctl, so they will lose messages.
> >>> This patch is used to fix the unfriendly interface. When running
> >>> monitor as a daemon, the default log destination will be changed to
> >>> syslog. Also, the messages will be forwarded to syslog if the log
> >>> destination is
> >> a relative path for log file.
> >>>
> >>> Signed-off-by: QI Fuli 
> >>> ---
> >>>  Documentation/ndctl/ndctl-monitor.txt | 16 +++-
> >>>  ndctl/monitor.c   |  5 -
> >>>  ndctl/monitor.conf|  2 ++
> >>>  3 files changed, 21 insertions(+), 2 deletions(-)
> >>>
> >>> diff --git a/Documentation/ndctl/ndctl-monitor.txt
> >>> b/Documentation/ndctl/ndctl-monitor.txt
> >>> index 1cba9ea..9a8d76b 100644
> >>> --- a/Documentation/ndctl/ndctl-monitor.txt
> >>> +++ b/Documentation/ndctl/ndctl-monitor.txt
> >>> @@ -67,7 +67,21 @@ OPTIONS
> >>>
> >>>  -l ::
> >>>  --log=::
> >>> - Output notifications to , syslog or standard output.
> >>> + Send log messages to the specified destination.
> >>> ++
> >>> +--
> >>> +::
> >>> + Send log messages to specified . When fopen() is not able
> >>> + to open , log messages will be forwarded to syslog.
> >>> +syslog::
> >>> + Send messages to syslog.
> >>> +standard::
> >>> + Send messages to standard output.
> >>> +--
> >>> ++
> >>> +The default log destination is 'syslog' if "--daemon" is specified,
> >>> +otherwise 'standard'. Note that standard and relative path for
> >>> + will not work if "--daemon" is specified.
> >>>
> >>>  -c::
> >>>  --config-file=::
> >>> diff --git a/ndctl/monitor.c b/ndctl/monitor.c index
> >>> bf1f1d3..2f3d751
> >>> 100644
> >>> --- a/ndctl/monitor.c
> >>> +++ b/ndctl/monitor.c
> >>> @@ -93,7 +93,8 @@ static void log_file(struct ndctl_ctx *ctx, int
> >>> priority, const
> >> char *file,
> >>>   f = fopen(monitor.log, "a+");
> >>>   if (!f) {
> >>>   ndctl_set_log_fn(ctx, log_syslog);
> >>> - err(ctx, "open logfile %s failed\n", monitor.log);
> >>> + err(ctx, "open logfile %s failed, forward messages to syslog\n",
> >>> + monitor.log);
> >>>   did_fail = 1;
> >>>   notice(ctx, "%s\n", buf);
> >>>   goto end;
> >>> @@ -644,6 +645,8 @@ int cmd_monitor(int argc, const char **argv, void 
> >>> *ctx)
> >>>   }
> >>>
> >>>   if (monitor.daemon) {
> >>
> >> Why don't you add './standard' check? Like as:
> >>
> >> if (strncmp(monitor.log, "./standard", 10) == 0)
> >> error("daemon doesn't work for 'standard' log 
> >> option");
> >> goto out;
> >>
> > Hi Masa,
> >
> > Thank you for your comment.
> >
> > When running monitor as a daemon, the messages will not be able to be 
> > logged in
> following cases.
> > a) Users set the log destination to standard by using [--log] option or 
> > setting value
> of "log" in config file.
> 
> > b) The log destination is standard by default.
> 
> Ummm... is this right behavior...? If the monitor running as a daemon, 
> shouldn't the
> default log destination be standard...?

No, If the monitor runs as a daemon, the default log destination should not be

[ndctl PATCH v2] ndctl, list: add alarm_enable_ to list

2018-08-07 Thread QI Fuli
This patch adds alarm_enable_ to list, so that users could check
if the "ndctl inject-smart ---alarm=on/off" works well or not.

Signed-off-by: QI Fuli 
---
v1 -> v2:
 remove the renaming for list items.

 ndctl/util/json-smart.c | 27 +++
 1 file changed, 27 insertions(+)

diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c
index 6a1f294..e25483d 100644
--- a/ndctl/util/json-smart.c
+++ b/ndctl/util/json-smart.c
@@ -39,34 +39,61 @@ static void smart_threshold_to_json(struct ndctl_dimm *dimm,
unsigned int temp;
double t;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_media_temperature", jobj);
temp = ndctl_cmd_smart_threshold_get_temperature(cmd);
t = ndctl_decode_smart_temperature(temp);
jobj = json_object_new_double(t);
if (jobj)
json_object_object_add(jhealth,
"temperature_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_media_temperature", jobj);
}
 
if (alarm_control & ND_SMART_CTEMP_TRIP) {
unsigned int temp;
double t;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_ctrl_temperature", jobj);
temp = ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
t = ndctl_decode_smart_temperature(temp);
jobj = json_object_new_double(t);
if (jobj)
json_object_object_add(jhealth,
"controller_temperature_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_ctrl_temperature", jobj);
}
 
if (alarm_control & ND_SMART_SPARE_TRIP) {
unsigned int spares;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_spares", jobj);
spares = ndctl_cmd_smart_threshold_get_spares(cmd);
jobj = json_object_new_int(spares);
if (jobj)
json_object_object_add(jhealth,
"spares_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_spares", jobj);
}
 
  out:
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 2/4] ndctl, monitor: set default log destination to syslog if "--daemon" is specified

2018-08-07 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, August 8, 2018 4:30 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH 2/4] ndctl, monitor: set default log destination to 
> syslog
> if "--daemon" is specified
> 
> Hi Qi,
> 
> On 08/07/2018 09:17 AM, QI Fuli wrote:
> > When running monitor as a daemon, if the log destination is "standard"
> > or a relative path for log file, the messages will not be able to be logged.
> > Sometimes, users may not notice that the default log destination is 
> > "standard"
> > when they start monitor daemon by systemctl, so they will lose messages.
> > This patch is used to fix the unfriendly interface. When running
> > monitor as a daemon, the default log destination will be changed to
> > syslog. Also, the messages will be forwarded to syslog if the log 
> > destination is
> a relative path for log file.
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  Documentation/ndctl/ndctl-monitor.txt | 16 +++-
> >  ndctl/monitor.c   |  5 -
> >  ndctl/monitor.conf|  2 ++
> >  3 files changed, 21 insertions(+), 2 deletions(-)
> >
> > diff --git a/Documentation/ndctl/ndctl-monitor.txt
> > b/Documentation/ndctl/ndctl-monitor.txt
> > index 1cba9ea..9a8d76b 100644
> > --- a/Documentation/ndctl/ndctl-monitor.txt
> > +++ b/Documentation/ndctl/ndctl-monitor.txt
> > @@ -67,7 +67,21 @@ OPTIONS
> >
> >  -l ::
> >  --log=::
> > -   Output notifications to , syslog or standard output.
> > +   Send log messages to the specified destination.
> > ++
> > +--
> > +::
> > +   Send log messages to specified . When fopen() is not able
> > +   to open , log messages will be forwarded to syslog.
> > +syslog::
> > +   Send messages to syslog.
> > +standard::
> > +   Send messages to standard output.
> > +--
> > ++
> > +The default log destination is 'syslog' if "--daemon" is specified,
> > +otherwise 'standard'. Note that standard and relative path for 
> > +will not work if "--daemon" is specified.
> >
> >  -c::
> >  --config-file=::
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c index bf1f1d3..2f3d751
> > 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -93,7 +93,8 @@ static void log_file(struct ndctl_ctx *ctx, int priority, 
> > const
> char *file,
> > f = fopen(monitor.log, "a+");
> > if (!f) {
> > ndctl_set_log_fn(ctx, log_syslog);
> > -   err(ctx, "open logfile %s failed\n", monitor.log);
> > +   err(ctx, "open logfile %s failed, forward messages to syslog\n",
> > +   monitor.log);
> > did_fail = 1;
> > notice(ctx, "%s\n", buf);
> > goto end;
> > @@ -644,6 +645,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > }
> >
> > if (monitor.daemon) {
> 
> Why don't you add './standard' check? Like as:
> 
> if (strncmp(monitor.log, "./standard", 10) == 0)
> error("daemon doesn't work for 'standard' log 
> option");
> goto out;
> 
Hi Masa,

Thank you for your comment.

When running monitor as a daemon, the messages will not be able to be logged in 
following cases.
a) Users set the log destination to standard by using [--log] option or setting 
value of "log" in config file.
b) The log destination is standard by default.
c) Users set the log destination to a relative path of log file by using 
[--log] option or setting value of "log" in config file.

The './standard' check will only works for case a).

Also, it would be more friendly to set a default log destination to monitor 
daemon.

Thanks,
QI

> Thanks,
> Masa
> 
> > +   if (!monitor.log || strncmp(monitor.log, "./", 2) == 0)
> > +   ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
> 
> > if (daemon(0, 0) != 0) {
> > err((struct ndctl_ctx *)ctx, "daemon start failed\n");
> > goto out;
> > diff --git a/ndctl/monitor.conf b/ndctl/monitor.conf index
> > 857aadf..934e2c0 100644
> > --- a/ndctl/monitor.conf
> > +++ b/ndctl/monitor.conf
> > @@ -38,4 +38,6 @@
> >  # to standard output (log=standard) or to write into a special file
> > (log=)  # by setting key "log". If this value is in conflict
> > with the value of  # [--log=] option, this value will be ignored.
> > +# Note: Setting value to "standard" or relative path for  will
> > +not work # when running moniotr as a daemon.
> >  # log = /var/log/ndctl/monitor.log
> >
> 

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 1/4] ndctl, monitor: fix the lack of detection of invalid path of log file

2018-08-07 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, August 8, 2018 4:06 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH 1/4] ndctl, monitor: fix the lack of detection of 
> invalid
> path of log file
> 
> Hi Qi,
> 
> On 08/07/2018 09:17 AM, QI Fuli wrote:
> > Currently the monitor can be started even with an invalid path of log file.
> > This patch adds a detection of invalid path of log file when starting 
> > monitor.
> > If the path of log file is invalid, the monitor will refuse to be started.
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/monitor.c | 11 ++-
> >  1 file changed, 10 insertions(+), 1 deletion(-)
> >
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c index f10384b..bf1f1d3
> > 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -603,6 +603,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > struct util_filter_ctx fctx = { 0 };
> > struct monitor_filter_arg mfa = { 0 };
> > int i, rc;
> > +   FILE *f;
> >
> > argc = parse_options_prefix(argc, argv, prefix, options, u, 0);
> > for (i = 0; i < argc; i++) {
> > @@ -630,8 +631,16 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
> > else if (strncmp(monitor.log, "./standard", 10) == 0)
> > ; /*default, already set */
> > -   else
> > +   else {
> > +   f = fopen(monitor.log, "a+");
> > +   if (!f) {
> > +   error("open %s failed\n", monitor.log);
> > +   rc = -errno;
> > +   goto out;
> > +   }
> > +   fclose(f);
> > ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_file);
> > +   }
> 

Masa,

Thanks for your comments.

> In log_file(), the log file does fallback to syslog if the fopen() fails.
> In my understanding here is that the fallback is needed to save in case of the
> monitor.log in trouble for example, the parent directory is removed.
> And, the new fopen() check, you have added by this patch, to inform the 
> invalid log
> path for users.
> 
Yes, this is what I wanted to implement.

> Is my understanding correct? If so, make sense to me.
> Please feel free to add:
> 
> Reviewed-by: Masayoshi Mizuma 
> 

Thanks,
QI

> Thanks,
> Masa
> 
> > }
> >
> > if (monitor.daemon) {
> >
> 

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl, list: emit alarm_enable_ and clarify misc list items

2018-08-07 Thread Qi, Fuli
Hi Vishal,

Thanks for your comments.

> -Original Message-
> From: Verma, Vishal L [mailto:vishal.l.ve...@intel.com]
> Sent: Wednesday, August 8, 2018 5:15 AM
> To: linux-nvdimm@lists.01.org; Qi, Fuli/斉 福利 
> Subject: Re: [ndctl PATCH] ndctl, list: emit alarm_enable_ and clarify 
> misc
> list items
> 
> 
> On Wed, 2018-08-08 at 00:35 +0900, QI Fuli wrote:
> > This patch adds alarm_enable_ to list. Therefore, users can
> > know
> 
> Hi Qi,
> 
> Thanks, I was meaning to do this work but you beat me to it :) I just have a 
> few
> nits, see below. But otherwise this looks good.
> 
> > if the "ndclt inject-smart ---alarm=on/off" works or not.
> 
> s/ndclt/ndctl/
> 
> >
> > Users may confuse "alarm_enable" with "alarm_flag" and "media_temperature"
> > with "ctrl_temperature", this patch also used to clarify these items.
> 
> I'm not sure it is a good idea to change these names since they've been 
> released
> for quite some time, and users/scripts might be depending on them, which this 
> change
> will break. Let's just add a new field for
> alarm_enabled_* for each smart field, but not change any of the existing ones 
> for
> now.
> 
Ok, I will make a v2 patch which only includes adding alarm_enable_.

Thanks,
QI

> >
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/util/json-smart.c | 45
> > ++---
> >  1 file changed, 38 insertions(+), 7 deletions(-)
> >
> > diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c index
> > 6a1f294..a0677a6 100644
> > --- a/ndctl/util/json-smart.c
> > +++ b/ndctl/util/json-smart.c
> > @@ -39,34 +39,61 @@ static void smart_threshold_to_json(struct ndctl_dimm 
> > *dimm,
> > unsigned int temp;
> > double t;
> >
> > +   jobj = json_object_new_boolean(true);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "alarm_enable_media_temperature", jobj);
> 
> Lets reword all instances to alarm_enabled_* Since it is showing the status 
> of the
> alarm.
> 
> > temp = ndctl_cmd_smart_threshold_get_temperature(cmd);
> > t = ndctl_decode_smart_temperature(temp);
> > jobj = json_object_new_double(t);
> > if (jobj)
> > json_object_object_add(jhealth,
> > -   "temperature_threshold", jobj);
> > +   "media_temperature_threshold", jobj);
> > +   } else {
> > +   jobj = json_object_new_boolean(false);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "alarm_enable_media_temperature", jobj);
> > }
> >
> > if (alarm_control & ND_SMART_CTEMP_TRIP) {
> > unsigned int temp;
> > double t;
> >
> > +   jobj = json_object_new_boolean(true);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "alarm_enable_ctrl_temperature", jobj);
> > temp = ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
> > t = ndctl_decode_smart_temperature(temp);
> > jobj = json_object_new_double(t);
> > if (jobj)
> > json_object_object_add(jhealth,
> > -   "controller_temperature_threshold", jobj);
> > +   "ctrl_temperature_threshold", jobj);
> > +   } else {
> > +   jobj = json_object_new_boolean(false);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "alarm_enable_ctrl_temperature", jobj);
> > }
> >
> > if (alarm_control & ND_SMART_SPARE_TRIP) {
> > unsigned int spares;
> >
> > +   jobj = json_object_new_boolean(true);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "alarm_enable_spares", jobj);
> > spares = ndctl_cmd_smart_threshold_get_spares(cmd);
> > jobj = json_object_new_int(spares);
> > if (jobj)
> > json_object_object_add(jhealth,
> > "spares_threshold", jobj);
> > +

RE: [ndctl PATCH 3/4] ndctl, monitor: add timestamp and pid to log messages in log_file()

2018-08-07 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, August 8, 2018 3:40 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH 3/4] ndctl, monitor: add timestamp and pid to log 
> messages
> in log_file()
> 
> Hi Qi,
> 
> On 08/07/2018 09:17 AM, QI Fuli wrote:
> > This patch is used to add timestamp and process id to log messages
> > when logging messages to a file.
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/monitor.c | 11 ++-
> >  1 file changed, 10 insertions(+), 1 deletion(-)
> >
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c index 2f3d751..d29e378
> > 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -84,6 +84,8 @@ static void log_file(struct ndctl_ctx *ctx, int
> > priority, const char *file,  {
> > FILE *f;
> > char *buf;
> > +   struct timespec ts;
> > +   char timestamp[32];
> >
> > if (vasprintf(, format, args) < 0) {
> > fail("vasprintf error\n");
> > @@ -99,7 +101,14 @@ static void log_file(struct ndctl_ctx *ctx, int 
> > priority, const
> char *file,
> > notice(ctx, "%s\n", buf);
> > goto end;
> > }
> > -   fprintf(f, "%s", buf);
> > +
> > +   if (priority != LOG_NOTICE) {
> 
Hi Masa,

Thanks for your comments.

> Why is the timestamp not needed in case of LOG_NOTICE...?

Because LOG_NOTICE level is only used for smart event notification in monitor.
Since the timestamp and pid are already added to json format messages by 
notify_dimm_event(),
so there is no need to add them again.

> 
> > +   clock_gettime(CLOCK_REALTIME, );
> > +   sprintf(timestamp, "%10ld.%09ld", ts.tv_sec, ts.tv_nsec);
> > +   fprintf(f, "[%s] [%d] %s", timestamp, getpid(), buf);
> 
> What is the pid for...?
> 
Multiple monitor can be run at the same time and they may send messages to a 
common log file.
In this case, the pid could help users to know the monitor where each message 
came from.

Thanks,
QI

> Thanks,
> Masa
> 
> > +   } else
> > +   fprintf(f, "%s", buf);
> > +>  fflush(f);
> > fclose(f);
> >  end:
> >
> 

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH] ndctl, list: emit alarm_enable_ and clarify misc list items

2018-08-07 Thread QI Fuli
This patch adds alarm_enable_ to list. Therefore, users can know
if the "ndclt inject-smart ---alarm=on/off" works or not.

Users may confuse "alarm_enable" with "alarm_flag" and "media_temperature"
with "ctrl_temperature", this patch also used to clarify these items.

Signed-off-by: QI Fuli 
---
 ndctl/util/json-smart.c | 45 ++---
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c
index 6a1f294..a0677a6 100644
--- a/ndctl/util/json-smart.c
+++ b/ndctl/util/json-smart.c
@@ -39,34 +39,61 @@ static void smart_threshold_to_json(struct ndctl_dimm *dimm,
unsigned int temp;
double t;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_media_temperature", jobj);
temp = ndctl_cmd_smart_threshold_get_temperature(cmd);
t = ndctl_decode_smart_temperature(temp);
jobj = json_object_new_double(t);
if (jobj)
json_object_object_add(jhealth,
-   "temperature_threshold", jobj);
+   "media_temperature_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_media_temperature", jobj);
}
 
if (alarm_control & ND_SMART_CTEMP_TRIP) {
unsigned int temp;
double t;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_ctrl_temperature", jobj);
temp = ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
t = ndctl_decode_smart_temperature(temp);
jobj = json_object_new_double(t);
if (jobj)
json_object_object_add(jhealth,
-   "controller_temperature_threshold", jobj);
+   "ctrl_temperature_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_ctrl_temperature", jobj);
}
 
if (alarm_control & ND_SMART_SPARE_TRIP) {
unsigned int spares;
 
+   jobj = json_object_new_boolean(true);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_spares", jobj);
spares = ndctl_cmd_smart_threshold_get_spares(cmd);
jobj = json_object_new_int(spares);
if (jobj)
json_object_object_add(jhealth,
"spares_threshold", jobj);
+   } else {
+   jobj = json_object_new_boolean(false);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "alarm_enable_spares", jobj);
}
 
  out:
@@ -118,7 +145,8 @@ struct json_object *util_dimm_health_to_json(struct 
ndctl_dimm *dimm)
 
jobj = json_object_new_double(t);
if (jobj)
-   json_object_object_add(jhealth, "temperature_celsius", 
jobj);
+   json_object_object_add(jhealth,
+   "media_temperature_celsius", jobj);
}
 
if (flags & ND_SMART_CTEMP_VALID) {
@@ -128,7 +156,7 @@ struct json_object *util_dimm_health_to_json(struct 
ndctl_dimm *dimm)
jobj = json_object_new_double(t);
if (jobj)
json_object_object_add(jhealth,
-   "controller_temperature_celsius", jobj);
+   "ctrl_temperature_celsius", jobj);
}
 
if (flags & ND_SMART_SPARES_VALID) {
@@ -147,15 +175,18 @@ struct json_object *util_dimm_health_to_json(struct 
ndctl_dimm *dimm)
 
jobj = json_object_new_boolean(temp_flag);
if (jobj)
-   json_object_object_add(jhealth, "alarm_temperature", 
jobj);
+   json_object_object_add(jhealth,
+   "alarm_flag_media_temperature", jobj);
 
jobj = json_object_new_boolean(ctrl_temp_flag);
i

[ndctl PATCH 4/4] ndctl, monitor: add [Install] Section to systemd unit file of ndctl-monitor

2018-08-07 Thread QI Fuli
This patch is used to add "[Install]" section to systemd unit file of
ndctl-monitor. Therefore, users can set ndctl-monitor unit enable.

Signed-off-by: QI Fuli 
---
 ndctl/ndctl-monitor.service | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
index 44f9326..342a1b1 100644
--- a/ndctl/ndctl-monitor.service
+++ b/ndctl/ndctl-monitor.service
@@ -5,3 +5,6 @@ Description=Ndctl Monitor Daemon
 Type=forking
 ExecStart=/usr/bin/ndctl monitor --daemon
 ExecStop=/bin/kill ${MAINPID}
+
+[Install]
+WantedBy=multi-user.target
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH 2/4] ndctl, monitor: set default log destination to syslog if "--daemon" is specified

2018-08-07 Thread QI Fuli
When running monitor as a daemon, if the log destination is "standard" or
a relative path for log file, the messages will not be able to be logged.
Sometimes, users may not notice that the default log destination is "standard"
when they start monitor daemon by systemctl, so they will lose messages.
This patch is used to fix the unfriendly interface. When running monitor as a
daemon, the default log destination will be changed to syslog. Also, the 
messages
will be forwarded to syslog if the log destination is a relative path for log 
file.

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/ndctl-monitor.txt | 16 +++-
 ndctl/monitor.c   |  5 -
 ndctl/monitor.conf|  2 ++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
index 1cba9ea..9a8d76b 100644
--- a/Documentation/ndctl/ndctl-monitor.txt
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -67,7 +67,21 @@ OPTIONS
 
 -l ::
 --log=::
-   Output notifications to , syslog or standard output.
+   Send log messages to the specified destination.
++
+--
+::
+   Send log messages to specified . When fopen() is not able
+   to open , log messages will be forwarded to syslog.
+syslog::
+   Send messages to syslog.
+standard::
+   Send messages to standard output.
+--
++
+The default log destination is 'syslog' if "--daemon" is specified,
+otherwise 'standard'. Note that standard and relative path for 
+will not work if "--daemon" is specified.
 
 -c::
 --config-file=::
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index bf1f1d3..2f3d751 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -93,7 +93,8 @@ static void log_file(struct ndctl_ctx *ctx, int priority, 
const char *file,
f = fopen(monitor.log, "a+");
if (!f) {
ndctl_set_log_fn(ctx, log_syslog);
-   err(ctx, "open logfile %s failed\n", monitor.log);
+   err(ctx, "open logfile %s failed, forward messages to syslog\n",
+   monitor.log);
did_fail = 1;
notice(ctx, "%s\n", buf);
goto end;
@@ -644,6 +645,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
}
 
if (monitor.daemon) {
+   if (!monitor.log || strncmp(monitor.log, "./", 2) == 0)
+   ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
if (daemon(0, 0) != 0) {
err((struct ndctl_ctx *)ctx, "daemon start failed\n");
goto out;
diff --git a/ndctl/monitor.conf b/ndctl/monitor.conf
index 857aadf..934e2c0 100644
--- a/ndctl/monitor.conf
+++ b/ndctl/monitor.conf
@@ -38,4 +38,6 @@
 # to standard output (log=standard) or to write into a special file 
(log=)
 # by setting key "log". If this value is in conflict with the value of
 # [--log=] option, this value will be ignored.
+# Note: Setting value to "standard" or relative path for  will not work
+# when running moniotr as a daemon.
 # log = /var/log/ndctl/monitor.log
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH 3/4] ndctl, monitor: add timestamp and pid to log messages in log_file()

2018-08-07 Thread QI Fuli
This patch is used to add timestamp and process id to log messages when logging
messages to a file.

Signed-off-by: QI Fuli 
---
 ndctl/monitor.c | 11 ++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 2f3d751..d29e378 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -84,6 +84,8 @@ static void log_file(struct ndctl_ctx *ctx, int priority, 
const char *file,
 {
FILE *f;
char *buf;
+   struct timespec ts;
+   char timestamp[32];
 
if (vasprintf(, format, args) < 0) {
fail("vasprintf error\n");
@@ -99,7 +101,14 @@ static void log_file(struct ndctl_ctx *ctx, int priority, 
const char *file,
notice(ctx, "%s\n", buf);
goto end;
}
-   fprintf(f, "%s", buf);
+
+   if (priority != LOG_NOTICE) {
+   clock_gettime(CLOCK_REALTIME, );
+   sprintf(timestamp, "%10ld.%09ld", ts.tv_sec, ts.tv_nsec);
+   fprintf(f, "[%s] [%d] %s", timestamp, getpid(), buf);
+   } else
+   fprintf(f, "%s", buf);
+
fflush(f);
fclose(f);
 end:
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH 1/4] ndctl, monitor: fix the lack of detection of invalid path of log file

2018-08-07 Thread QI Fuli
Currently the monitor can be started even with an invalid path of log file.
This patch adds a detection of invalid path of log file when starting monitor.
If the path of log file is invalid, the monitor will refuse to be started.

Signed-off-by: QI Fuli 
---
 ndctl/monitor.c | 11 ++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index f10384b..bf1f1d3 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -603,6 +603,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
struct util_filter_ctx fctx = { 0 };
struct monitor_filter_arg mfa = { 0 };
int i, rc;
+   FILE *f;
 
argc = parse_options_prefix(argc, argv, prefix, options, u, 0);
for (i = 0; i < argc; i++) {
@@ -630,8 +631,16 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
else if (strncmp(monitor.log, "./standard", 10) == 0)
; /*default, already set */
-   else
+   else {
+   f = fopen(monitor.log, "a+");
+   if (!f) {
+   error("open %s failed\n", monitor.log);
+   rc = -errno;
+   goto out;
+   }
+   fclose(f);
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_file);
+   }
}
 
if (monitor.daemon) {
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH 0/4] Some fixups and improvements ndctl monitor

2018-08-07 Thread QI Fuli
Fix a couple of issues about log collection, and add [Install] section
to systemd unit file of ndclt-monitor.

Signed-off-by: QI Fuli 
---
QI Fuli (4):
  ndctl, monitor: fix the lack of detection of invalid path of log file
  ndctl, monitor: set default log destination to syslog if "--daemon" is 
specified
  ndctl, monitor: add timestamp and pid to log messages in log_file()
  ndctl, monitor: add [Install] Section to systemd unit file of ndctl-monitor

 Documentation/ndctl/ndctl-monitor.txt | 16 +++-
 ndctl/monitor.c   | 27 ---
 ndctl/monitor.conf|  2 ++
 ndctl/ndctl-monitor.service   |  3 +++
 4 files changed, 44 insertions(+), 4 deletions(-)

-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH] ndctl, monitor: add [--verbose] option to emit extra debug messages

2018-08-02 Thread QI Fuli
In the ndctl monitor, currently the debug messages cannot be outputted to log.
This patch is used for adding [--verbose] option to ndctl monitor to emit
debug messages. Also, the log level of some messages are changed by this patch.

Cc: Masayoshi Mizuma 
Signed-off-by: QI Fuli 
---
 Documentation/ndctl/ndctl-monitor.txt |  4 
 ndctl/monitor.c   | 20 +++-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
index 762073e..1cba9ea 100644
--- a/Documentation/ndctl/ndctl-monitor.txt
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -98,6 +98,10 @@ specified events.
Output monitor notification as human friendly json format instead
of the default machine friendly json format.
 
+-v::
+--verbose::
+   Emit extra debug messages to log.
+
 COPYRIGHT
 -
 Copyright (c) 2018, FUJITSU LIMITED. License GPLv2: GNU GPL version 2
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 4e5daf5..f10384b 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -22,6 +22,7 @@ static struct monitor {
const char *dimm_event;
bool daemon;
bool human;
+   bool verbose;
unsigned int event_flags;
 } monitor;
 
@@ -92,7 +93,9 @@ static void log_file(struct ndctl_ctx *ctx, int priority, 
const char *file,
f = fopen(monitor.log, "a+");
if (!f) {
ndctl_set_log_fn(ctx, log_syslog);
-   fail("open logfile %s failed\n%s", monitor.log, buf);
+   err(ctx, "open logfile %s failed\n", monitor.log);
+   did_fail = 1;
+   notice(ctx, "%s\n", buf);
goto end;
}
fprintf(f, "%s", buf);
@@ -390,8 +393,9 @@ static int monitor_event(struct ndctl_ctx *ctx,
if (util_dimm_event_filter(mdimm, monitor.event_flags)) 
{
rc = notify_dimm_event(mdimm);
if (rc) {
-   fail("%s: notify dimm event failed\n",
+   err(ctx, "%s: notify dimm event 
failed\n",

ndctl_dimm_get_devname(mdimm->dimm));
+   did_fail = 1;
goto out;
}
}
@@ -506,7 +510,7 @@ static int read_config_file(struct ndctl_ctx *ctx, struct 
monitor *_monitor,
 
f = fopen(config_file, "r");
if (!f) {
-   fail("config-file: %s cannot be opened\n", config_file);
+   err(ctx, "config-file: %s cannot be opened\n", config_file);
rc = -errno;
goto out;
}
@@ -587,6 +591,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
"run ndctl monitor as a daemon"),
OPT_BOOLEAN('u', "human", ,
"use human friendly output formats"),
+   OPT_BOOLEAN('v', "verbose", ,
+   "emit extra debug messages to log"),
OPT_END(),
};
const char * const u[] = {
@@ -607,7 +613,11 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
 
/* default to log_standard */
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_standard);
-   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
+
+   if (monitor.verbose)
+   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_DEBUG);
+   else
+   ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
 
rc = read_config_file((struct ndctl_ctx *)ctx, , );
if (rc)
@@ -629,7 +639,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
err((struct ndctl_ctx *)ctx, "daemon start failed\n");
goto out;
}
-   notice((struct ndctl_ctx *)ctx, "ndctl monitor daemon 
started\n");
+   err((struct ndctl_ctx *)ctx, "ndctl monitor daemon started\n");
}
 
if (parse_monitor_event(, (struct ndctl_ctx *)ctx))
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [PATCH] ndctl, monitor: Change the fail log priority to err.

2018-08-02 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Thursday, August 2, 2018 3:56 AM
> To: linux-nvdimm@lists.01.org
> Cc: Masayoshi Mizuma ; Qi, Fuli/斉 福利
> ; Mizuma, Masayoshi/水間 理仁 
> Subject: [PATCH] ndctl, monitor: Change the fail log priority to err.
> 
> From: Masayoshi Mizuma 
> 
> fail() is called if an error happens, but the log priority is debug, so user 
> may
> not notice the error.
> Let's change the priority to err.
> 
> Cc: QI Fuli 
> Signed-off-by: Masayoshi Mizuma 
> ---
>  ndctl/monitor.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/ndctl/monitor.c b/ndctl/monitor.c index 84d51c4..68bbf65 100644
> --- a/ndctl/monitor.c
> +++ b/ndctl/monitor.c
> @@ -40,7 +40,7 @@ static int did_fail;
>  #define fail(fmt, ...) \
>  do { \
>   did_fail = 1; \
> - dbg(ctx, "ndctl-%s:%s:%d: " fmt, \
> + err(ctx, "ndctl-%s:%s:%d: " fmt, \
>   VERSION, __func__, __LINE__, ##__VA_ARGS__); \  } while 
> (0)
> 

Hi Masa,

The debug messages which are from fail() include source code line numbers and 
function name,
these are useful for developers not typical users. Therefore, we made it a 
debug level print.
Currently the debug level cannot be outputted is a bug.
I will make a patch for adding [--verbose] option to emit debug messages and 
changing the log level of some messages.

Thanks,
QI

> --
> 2.18.0
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndclt PATCH] ndctl, monitor: Fix duplicate prefix in monitor.log

2018-08-02 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Thursday, August 2, 2018 3:15 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndclt PATCH] ndctl, monitor: Fix duplicate prefix in monitor.log
> 
> Hi QI,
> 
> On 07/31/2018 01:15 AM, QI Fuli wrote:
> > When a monitor runs with [--log] option, the prefix will be dually
> > added to monitor.log. Therefore, the monitor cannot log the smart
> > notification to syslog. This patch is used to prevent prefix from
> > being dually added to monitor.log.
> >
> > Fixes: fdf6b6844ccf ("ndctl, monitor: add a new command - monitor")
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/monitor.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c index c6419ad..4e5daf5
> > 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -614,7 +614,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> > goto out;
> >
> > if (monitor.log) {
> > -   fix_filename(prefix, (const char **));
> > +   if (strncmp(monitor.log, "./", 2) != 0)
> > +   fix_filename(prefix, (const char **));
> 
> prefix is not needed to 'syslog' and 'standard', so why don't you move the 
> strncmp()
> before fix_filename(), like as:
> 
> @@ -614,13 +619,14 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
> goto out;
> 
> if (monitor.log) {
> -   fix_filename(prefix, (const char **));
> if (strncmp(monitor.log, "./syslog", 8) == 0)
> ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
> else if (strncmp(monitor.log, "./standard", 10) == 0)
> ; /*default, already set */
> -   else
> +   else {
> +   fix_filename(prefix, (const char
> + **));
> ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_file);
> +   }
> }
> 
> if (monitor.daemon) {
> 
> Thanks,
> Masa

Hi Masa,

Thank you very much for your comments.

There are two ways to set monitor.log.
a) setting the argument of [--log] option
b) setting the value of [log] key in configuration file

When users set monitor.log by b, the prefix will not be added to monitor.log.
Therefore, we should do fix_filename() before strncmp().

Thanks,
QI

> 
> > if (strncmp(monitor.log, "./syslog", 8) == 0)
> > ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
> > else if (strncmp(monitor.log, "./standard", 10) == 0)
> >
> 

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndclt PATCH] ndctl, monitor: Fix duplicate prefix in monitor.log

2018-07-30 Thread QI Fuli
When a monitor runs with [--log] option, the prefix will be dually
added to monitor.log. Therefore, the monitor cannot log the smart
notification to syslog. This patch is used to prevent prefix from
being dually added to monitor.log.

Fixes: fdf6b6844ccf ("ndctl, monitor: add a new command - monitor")
Signed-off-by: QI Fuli 
---
 ndctl/monitor.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index c6419ad..4e5daf5 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -614,7 +614,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
goto out;
 
if (monitor.log) {
-   fix_filename(prefix, (const char **));
+   if (strncmp(monitor.log, "./", 2) != 0)
+   fix_filename(prefix, (const char **));
if (strncmp(monitor.log, "./syslog", 8) == 0)
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog);
else if (strncmp(monitor.log, "./standard", 10) == 0)
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl, test/monitor: fix inject-smart field in test_filter_dimmevent

2018-07-30 Thread Qi, Fuli



> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Tuesday, July 31, 2018 4:35 AM
> To: linux-nvdimm@lists.01.org
> Cc: Vishal Verma ; Qi, Fuli/斉 福利
> 
> Subject: [ndctl PATCH] ndctl, test/monitor: fix inject-smart field in
> test_filter_dimmevent
> 
> Change the inject-smart field from -s to -m when testing the 
> dimm-media-temperature
> event. The -s seems to have been a copy-paste error.
> 
> Cc: QI Fuli 
> Signed-off-by: Vishal Verma 
> ---
>  test/monitor.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/test/monitor.sh b/test/monitor.sh index 1bbfd14..29d4ea1 100755
> --- a/test/monitor.sh
> +++ b/test/monitor.sh
> @@ -152,7 +152,7 @@ test_filter_dimmevent()
>   inject_value=$($NDCTL list -H -d $monitor_dimms | jq
> -r .[]."health"."temperature_threshold")
>   inject_value=$((inject_value + 1))
>   start_monitor "-d $monitor_dimms -D dimm-media-temperature"
> - inject_smart "-s $inject_value"
> + inject_smart "-m $inject_value"
>   check_result "$monitor_dimms"
>   stop_monitor
>  }

Hi Vishal,

Yes, this is a copy-paste error and I am sorry for this.

Thank you very much.
QI

> --
> 2.14.4
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl: deprecate undocumented short-options

2018-07-29 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Saturday, July 28, 2018 3:31 AM
> To: linux-nvdimm@lists.01.org
> Cc: Vishal Verma ; Qi, Fuli/斉 福利
> 
> Subject: [ndctl PATCH] ndctl: deprecate undocumented short-options
> 
> The inject-smart and monitor man pages refrained from displaying short 
> options for
> various arguments (alarms, daemon) due to a lack of a coherent letter that 
> could
> be made to relate to the event name. It was expected that the user will 
> always use
> the long option for these. Since the OPT_STRING helper refused to take an 
> empty string
> for the short option, we used bogus characters for each of them.
> 
> However there is a better way to provide no short options, and that is by 
> using '\0'
> for the short option field to OPT_STRING. Replace the bogus characters with 
> '\0'
> so that the 'short help' also becomes consistent with the man pages.
> 
> Cc: Cc: QI Fuli 
> Signed-off-by: Vishal Verma 
> ---
>  ndctl/inject-smart.c | 7 ---
>  ndctl/monitor.c  | 2 +-
>  2 files changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/ndctl/inject-smart.c b/ndctl/inject-smart.c index 
> 006ea2a..edb87e1
> 100644
> --- a/ndctl/inject-smart.c
> +++ b/ndctl/inject-smart.c
> @@ -73,7 +73,7 @@ OPT_STRING('M', "media-temperature-threshold", \
>   _temperature_threshold, \
>   "set smart media temperature threshold", \
>   "set threshold value for smart media temperature"), \ -OPT_STRING('x',
> "media-temperature-alarm", _temperature_alarm, \
> +OPT_STRING('\0', "media-temperature-alarm",
> +_temperature_alarm, \
>   "smart media temperature alarm", \
>   "enable or disable the smart media temperature alarm"), \  
> OPT_STRING('c',
> "ctrl-temperature", _temperature, \ @@ -83,7 +83,7 @@ 
> OPT_STRING('C',
> "ctrl-temperature-threshold", \
>   _temperature_threshold, \
>   "set smart controller temperature threshold", \
>   "set threshold value for smart controller temperature"), \ 
> -OPT_STRING('y',
> "ctrl-temperature-alarm", _temperature_alarm, \
> +OPT_STRING('\0', "ctrl-temperature-alarm",
> +_temperature_alarm, \
>   "smart controller temperature alarm", \
>   "enable or disable the smart controller temperature alarm"),
> \  OPT_STRING('s', "spares", , \ @@ -92,13 +92,14 @@ 
> OPT_STRING('s',
> "spares", , \  OPT_STRING('S', "spares-threshold",
> _threshold, \
>   "set smart spares threshold", \
>   "set a threshold value for smart spares"), \ -OPT_STRING('z', 
> "spares-alarm",
> _alarm, \
> +OPT_STRING('\0', "spares-alarm", _alarm, \
>   "smart spares alarm", \
>   "enable or disable the smart spares alarm"), \  OPT_BOOLEAN('f', 
> "fatal",
> , "inject fatal smart health status"), \  OPT_BOOLEAN('U',
> "unsafe-shutdown", _shutdown, \
>   "inject smart unsafe shutdown status")
> 
> +
>  static const struct option smart_opts[] = {
>   SMART_OPTIONS(),
>   OPT_END(),
> diff --git a/ndctl/monitor.c b/ndctl/monitor.c index b97d1ea..c6419ad 100644
> --- a/ndctl/monitor.c
> +++ b/ndctl/monitor.c
> @@ -583,7 +583,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
>   "where to output the monitor's notification"),
>   OPT_FILENAME('c', "config-file", _file,
>   "config-file", "override the default config"),
> - OPT_BOOLEAN('x', "daemon", ,
> + OPT_BOOLEAN('\0', "daemon", ,
>   "run ndctl monitor as a daemon"),
>   OPT_BOOLEAN('u', "human", ,
>   "use human friendly output formats"),

Looks good to me.
Please feel free to add: Reviewed-by: QI Fuli 

Thanks,
QI

> --
> 2.14.4
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl, monitor: fix a resource leak in parse_monitor_event

2018-07-24 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Wednesday, July 25, 2018 9:09 AM
> To: linux-nvdimm@lists.01.org
> Cc: Qi, Fuli/斉 福利 ; Vishal Verma
> 
> Subject: [ndctl PATCH] ndctl, monitor: fix a resource leak in 
> parse_monitor_event
> 
> Static analysis reports we leak dimm_event in the above function.
> Fix it by adding an 'out' label for all exit paths, and refactoring out the 
> 'all
> events' cases.
> 
> Fixes: fdf6b6844ccf ("ndctl, monitor: add a new command - monitor")
> Cc: QI Fuli 
> Signed-off-by: Vishal Verma 
> ---
>  ndctl/monitor.c | 37 ++---
>  1 file changed, 22 insertions(+), 15 deletions(-)
> 
> diff --git a/ndctl/monitor.c b/ndctl/monitor.c index dbad7aa..b97d1ea 100644
> --- a/ndctl/monitor.c
> +++ b/ndctl/monitor.c
> @@ -410,22 +410,35 @@ static int monitor_event(struct ndctl_ctx *ctx,
>   return rc;
>  }
> 
> +static void monitor_enable_all_events(struct monitor *_monitor) {
> + _monitor->event_flags = ND_EVENT_SPARES_REMAINING
> + | ND_EVENT_MEDIA_TEMPERATURE
> + | ND_EVENT_CTRL_TEMPERATURE
> + | ND_EVENT_HEALTH_STATE
> + | ND_EVENT_UNCLEAN_SHUTDOWN;
> +}
> +
>  static int parse_monitor_event(struct monitor *_monitor, struct ndctl_ctx 
> *ctx)
> {
>   char *dimm_event, *save;
>   const char *event;
> + int rc = 0;
> +
> + if (!_monitor->dimm_event) {
> + monitor_enable_all_events(_monitor);
> + return 0;;
> + }
> 
> - if (!_monitor->dimm_event)
> - goto dimm_event_all;
>   dimm_event = strdup(_monitor->dimm_event);
>   if (!dimm_event)
> - return 1;
> + return -ENOMEM;
> 
>   for (event = strtok_r(dimm_event, " ", ); event;
>   event = strtok_r(NULL, " ", )) {
>   if (strcmp(event, "all") == 0) {
> - free(dimm_event);
> - goto dimm_event_all;
> + monitor_enable_all_events(_monitor);
> + goto out;
>   }
>   if (strcmp(event, "dimm-spares-remaining") == 0)
>   _monitor->event_flags |= ND_EVENT_SPARES_REMAINING; @@
> -439,20 +452,14 @@ static int parse_monitor_event(struct monitor *_monitor, 
> struct
> ndctl_ctx *ctx)
>   _monitor->event_flags |= ND_EVENT_UNCLEAN_SHUTDOWN;
>   else {
>   err(ctx, "no dimm-event named %s\n", event);
> - return 1;
> + rc = -EINVAL;
> + goto out;
>   }
>   }
> 
> +out:
>   free(dimm_event);
> - return 0;
> -
> -dimm_event_all:
> - _monitor->event_flags = ND_EVENT_SPARES_REMAINING
> - | ND_EVENT_MEDIA_TEMPERATURE
> - | ND_EVENT_CTRL_TEMPERATURE
> - | ND_EVENT_HEALTH_STATE
> - | ND_EVENT_UNCLEAN_SHUTDOWN;
> - return 0;
> + return rc;
>  }
> 

Looks good to me.
Please feel free to add: Reviewed-by: QI Fuli 

Thanks,
QI

>  static void parse_config(const char **arg, char *key, char *val, char *ident)
> --
> 2.14.4
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 0/4] misc fixups for ndctl-monitor

2018-07-19 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vis...@kernel.org]
> Sent: Friday, July 20, 2018 12:26 PM
> To: Qi, Fuli/斉 福利 ; 'Vishal Verma'
> ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH 0/4] misc fixups for ndctl-monitor
> 
> On Thu, 2018-07-19 at 23:59 +, Qi, Fuli wrote:
> > > -Original Message-
> > > From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> > > Sent: Friday, July 20, 2018 8:00 AM
> > > To: linux-nvdimm@lists.01.org
> > > Cc: Qi, Fuli/斉 福利 ; Vishal Verma
> > > 
> > > Subject: [ndctl PATCH 0/4] misc fixups for ndctl-monitor
> > >
> > > Fix a couple of issues reported by static analysis, improve the
> > > error reporting, and add a missing option to the monitor man page
> > >
> > > Vishal Verma (4):
> > >   ndctl, monitor: Add a config-file section to the man page
> > >   ndctl, monitor: fix memory leak in read_config_file
> > >   ndctl, monitor: Fix memory leak in monitor_event
> > >   ndctl, monitor: improve error reporting throughout monitor.c
> > >
> >
> > Hi Vishal,
> >
> > Looks good to me.
> > Thank you very much.
> 
> Hi Qi,
> 
> Can I add your Reviewed-by tags for the series then?
> 
> Thanks,
>   -Vishal
> 

Yes, please.

Thanks,
Qi

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH 0/4] misc fixups for ndctl-monitor

2018-07-19 Thread Qi, Fuli
> -Original Message-
> From: Vishal Verma [mailto:vishal.l.ve...@intel.com]
> Sent: Friday, July 20, 2018 8:00 AM
> To: linux-nvdimm@lists.01.org
> Cc: Qi, Fuli/斉 福利 ; Vishal Verma
> 
> Subject: [ndctl PATCH 0/4] misc fixups for ndctl-monitor
> 
> Fix a couple of issues reported by static analysis, improve the error 
> reporting,
> and add a missing option to the monitor man page
> 
> Vishal Verma (4):
>   ndctl, monitor: Add a config-file section to the man page
>   ndctl, monitor: fix memory leak in read_config_file
>   ndctl, monitor: Fix memory leak in monitor_event
>   ndctl, monitor: improve error reporting throughout monitor.c
> 

Hi Vishal,

Looks good to me.
Thank you very much.
Qi

>  Documentation/ndctl/ndctl-monitor.txt |   5 ++
>  ndctl/monitor.c   | 129 
> +-
>  2 files changed, 69 insertions(+), 65 deletions(-)
> 
> --
> 2.14.4
> 
> 


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH] ndctl, monitor: fix monitor can be started though no dimm event to monitor

2018-07-19 Thread QI Fuli
Currently, the monitor still can be started though there is no dimm event
to be monitored, which is caused by the typo of uses. In this case,
no smart event message can be logged.
This patch is used to fix this bug. When user starts the monitor with
invalid dimm event in [--dimm-event] option, the monitor will stop and
output error message.

Signed-off-by: QI Fuli 
---
 ndctl/monitor.c | 16 ++--
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 8300250..abab45f 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -413,7 +413,7 @@ static int monitor_event(struct ndctl_ctx *ctx,
return 0;
 }
 
-static int parse_monitor_event(struct monitor *_monitor)
+static int parse_monitor_event(struct monitor *_monitor, struct ndctl_ctx *ctx)
 {
char *dimm_event, *save;
const char *event;
@@ -432,14 +432,18 @@ static int parse_monitor_event(struct monitor *_monitor)
}
if (strcmp(event, "dimm-spares-remaining") == 0)
_monitor->event_flags |= ND_EVENT_SPARES_REMAINING;
-   if (strcmp(event, "dimm-media-temperature") == 0)
+   else if (strcmp(event, "dimm-media-temperature") == 0)
_monitor->event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
-   if (strcmp(event, "dimm-controller-temperature") == 0)
+   else if (strcmp(event, "dimm-controller-temperature") == 0)
_monitor->event_flags |= ND_EVENT_CTRL_TEMPERATURE;
-   if (strcmp(event, "dimm-health-state") == 0)
+   else if (strcmp(event, "dimm-health-state") == 0)
_monitor->event_flags |= ND_EVENT_HEALTH_STATE;
-   if (strcmp(event, "dimm-unclean-shutdown") == 0)
+   else if (strcmp(event, "dimm-unclean-shutdown") == 0)
_monitor->event_flags |= ND_EVENT_UNCLEAN_SHUTDOWN;
+   else {
+   err(ctx, "no dimm-event named %s\n", event);
+   return 1;
+   }
}
 
free(dimm_event);
@@ -620,7 +624,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
notice((struct ndctl_ctx *)ctx, "ndctl monitor daemon 
started\n");
}
 
-   if (parse_monitor_event())
+   if (parse_monitor_event(, (struct ndctl_ctx *)ctx))
goto out;
 
fctx.filter_bus = filter_bus;
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH v13 0/5] ndctl, monitor: add ndctl monitor daemon

2018-07-18 Thread Qi, Fuli
> -Original Message-
> From: Verma, Vishal L [mailto:vishal.l.ve...@intel.com]
> Sent: Thursday, July 19, 2018 9:43 AM
> To: linux-nvdimm@lists.01.org; Qi, Fuli/斉 福利 
> Cc: Williams, Dan J ; Tokunaga, Keiichirou/徳永 圭一郎
> 
> Subject: Re: [ndctl PATCH v13 0/5] ndctl, monitor: add ndctl monitor daemon
> 
> 
> On Sat, 2018-07-14 at 08:33 +0900, QI Fuli wrote:
> > This is the v13 patch for ndctl monitor, a tiny daemon to monitor
> > the smart events of nvdimm DIMMs. Since NVDIMM does not have a
> > feature like mirroring, if it breaks down, the data will be
> > impossible to restore. Ndctl monitor daemon will catch the smart
> > events notify from firmware and outputs notification to logfile,
> > therefore users can replace NVDIMM before it is completely broken.
> >
> > Signed-off-by: QI Fuli 
> 
> Hi Qi,
> 
> This looks great, thank you for all the updates!
> 
> I have applied the series to my local tree, and expect to push it out
> to the pending branch soon. I will also add reviewed-by tags for Dan
> (with his permission) since he has been integral to the review of this
> series.
> 
> I'm also separately sending out the bash completion updates for the
> monitor command.
> 
> Thank you,
> 
>   -Vishal

Hi Vishal,

Thank you very much for all comments and advices.
I would appreciate your continued support.

Qi
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v13 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 
---
 configure.ac   |   1 +
 ndctl.spec.in  |   1 +
 ndctl/Makefile.am  |   5 +++
 ndctl/monitor.c| 110 -
 ndctl/monitor.conf |  41 +
 5 files changed, 157 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.conf

diff --git a/configure.ac b/configure.ac
index cf44260..ff0096d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -144,6 +144,7 @@ AC_CHECK_FUNCS([ \
 ])
 
 my_CFLAGS="\
+-D DEF_CONF_FILE='\"${sysconfdir}/ndctl/monitor.conf\"' \
 -Wall \
 -Wchar-subscripts \
 -Wformat-security \
diff --git a/ndctl.spec.in b/ndctl.spec.in
index e2c879c..41a1d59 100644
--- a/ndctl.spec.in
+++ b/ndctl.spec.in
@@ -116,6 +116,7 @@ make check
 %{_bindir}/ndctl
 %{_mandir}/man1/ndctl*
 %{bashcompdir}/
+%{_sysconfdir}/ndctl/monitor.conf
 
 %files -n daxctl
 %defattr(-,root,root)
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 083609a..58b747a 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 = $(sysconfdir)/ndctl/
+monitor_config_DATA = $(monitor_config_file)
+EXTRA_DIST += $(monitor_config_file)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 12317d0..8300250 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -16,9 +16,9 @@
 #include 
 #define BUF_SIZE 2048
 
-
 static struct monitor {
const char *log;
+   const char *config_file;
const char *dimm_event;
bool daemon;
bool human;
@@ -454,6 +454,108 @@ 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;
+
+   if (_monitor->config_file)
+   config_file = strdup(_monitor->config_file);
+   else
+   config_file = strdup(DEF_CONF_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)
+  

[ndctl PATCH v13 4/5] ndctl, documentation: add man page for monitor

2018-07-13 Thread QI Fuli
This patch is used to add man page for ndctl monitor command.

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt | 108 ++
 2 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt

diff --git a/Documentation/ndctl/Makefile.am b/Documentation/ndctl/Makefile.am
index 4fd9636..a30b139 100644
--- a/Documentation/ndctl/Makefile.am
+++ b/Documentation/ndctl/Makefile.am
@@ -46,7 +46,8 @@ man1_MANS = \
ndctl-inject-error.1 \
ndctl-inject-smart.1 \
ndctl-update-firmware.1 \
-   ndctl-list.1
+   ndctl-list.1 \
+   ndctl-monitor.1
 
 CLEANFILES = $(man1_MANS)
 
diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
new file mode 100644
index 000..304b6e7
--- /dev/null
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0
+
+ndctl-monitor(1)
+
+
+NAME
+
+ndctl-monitor - Monitor the smart events of nvdimm objects
+
+SYNOPSIS
+
+[verse]
+'ndctl monitor' []
+
+DESCRIPTION
+---
+Ndctl monitor is used for monitoring the smart events of nvdimm
+objects and dumping the json format notifications to syslog, standard
+output or a logfile.
+
+The objects to monitor and smart events to notify can be selected by
+setting options and/or the default configuration file
+(/etc/ndctl/monitor.conf). Both of the values in configuration file
+and in options will work. If there is a conflict, the values in
+options will override the values in configuration file. The changed
+values in configuration file will work after the monitor is restarted.
+
+The smart threshold evnet alarm of dimm will be turned on when the
+monitor gets started, if it is set off.
+
+EXAMPLES
+
+
+Run a monitor as a daemon to monitor DIMMs on bus "nfit_test.1"
+[verse]
+ndctl monitor --bus nfit_test.1 --daemon
+
+Run a monitor as a one-shot command and output the notifications to
+/var/log/ndctl.log
+[verse]
+ndctl monitor -log /var/log/ndctl.log
+
+Run a monitor daemon as a system service
+[verse]
+systemctl start ndctl-monitor.service
+
+OPTIONS
+---
+-b::
+--bus=::
+   Enforce that the operation only be carried on devices that are
+   attached to the given bus. Where 'bus' can be a provider name
+   or a bus id number.
+
+-d::
+--dimm=::
+   A 'nmemX' device name, or dimm id number. Select the devices to
+   monitor reference the given dimm.
+
+-r::
+--region=::
+   A 'regionX' device name, or a region id number. The keyword 'all'
+   can be specified to carry out the operation on every region in
+   the system, optionally filtered by bus id (see --bus= option).
+
+-n::
+--namespace=::
+   A 'namespaceX.Y' device name, or namespace region plus id tuple
+   'X.Y'.
+
+-l ::
+--log=::
+   Output notifications to , syslog or standard output.
+
+--daemon::
+   Run a monitor as a daemon.
+
+-D::
+--dimm-event=::
+   Name of an smart health event from the following:
+   - "dimm-spares-remaining": Spare Blocks Remaining value has gone
+  below the pre-programmed threshold.
+   - "dimm-media-temperature": NVDIMM Media temperature value has
+  gone above the pre-programmed threshold.
+   - "dimm-controller-temperature": NVDIMM Controller temperature
+  value has gone above the pre-programmed threshold.
+   - "dimm-health-state": NVDIMM Normal Health Status has changed
+   - "dimm-unclean-shutdown": NVDIMM Last Shutdown Status was a
+  unclean shutdown.
+
+The monitor will attempt to enable the alarm control bits for all
+specified events.
+
+-u::
+--human::
+   Output monitor notification as human friendly json format instead
+   of the default machine friendly json format.
+
+COPYRIGHT
+-
+Copyright (c) 2018, FUJITSU LIMITED. License GPLv2: GNU GPL version 2
+<http://gnu.org/licenses/gpl.html>. This is free software: you are
+free to change and redistribute it. There is NO WARRANTY, to the
+extent permitted by law.
+
+SEE ALSO
+
+linkndctl:ndctl-list[1], linkndctl:ndctl-inject-smart[1]
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v13 0/5] ndctl, monitor: add ndctl monitor daemon

2018-07-13 Thread QI Fuli
This is the v13 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v12:
 - Fixing log_fn() for removing output new line
 - Fixing hard code default configuration file path
 - Fixing RPM spec file for configuration file and systemd unit file
 - Fixing man page

Change log since v11:
 - Adding log_standard()
 - Adding [-u | --human] option
 - Fixing man page
 - Refactoring unit test
 - Updating configuration file and systemd unit file to RPM spec file

Change log since v10:
 - Adding unit test
 - Adding fflush to log_file()

Change log since v9:
 - Replacing ndctl_cmd_smart_get_event_flags() with
   ndctl_dimm_get_event_flags()
 - Adding ndctl_dimm_get_health() api
 - Adding ndctl_dimm_get_flags() api
 - Adding ndctl_dimm_is_flag_supported api
 - Adding manpage

Change log since v8:
 - Adding ndctl_cmd_smart_get_event_flags() api
 - Adding monitor_filter_arg to the union in util_filter_ctx
 - Removing is_dir()
 - Replacing malloc + vsprintf with vasprintf() in log_file() and log_syslog()
 - Adding parse_monitor_event()
 - Refactoring util_dimm_event_filter()
 - Adding event_flags to monitor
 - Refactoring dimm_event_to_json()
 - Adding check_dimm_supported_threshold_alarms()
 - Fixing fail token

Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (5):
  ndctl, monitor: add a new command - monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service
  ndctl, documentation: add man page for monitor
  ndctl, test: add a new unit test for monitor

 .gitignore|   1 +
 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt | 108 +
 autogen.sh|   3 +-
 builtin.h |   1 +
 configure.ac  |  23 +
 ndctl.spec.in |   3 +
 ndctl/Makefile.am |  12 +-
 ndctl/lib/libndctl.c  |  82 
 ndctl/lib/libndctl.sym|   4 +
 ndctl/libndctl.h  |  10 +
 ndctl/monitor.c   | 650 ++
 ndctl/monitor.conf|  41 ++
 ndctl/ndctl-monitor.service   |   7 +
 ndctl/ndctl.c |   1 +
 test/Makefile.am  |  14 +-
 test/list-smart-dimm.c| 117 +
 test/monitor.sh   | 176 +++
 util/filter.h  

[ndctl PATCH v13 1/5] ndctl, monitor: add a new command - monitor

2018-07-13 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of NVDIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and event information to syslog, standard
output or a file by setting [--log] option. The notifications follow
json format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --log /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h  |   1 +
 ndctl/Makefile.am  |   3 +-
 ndctl/lib/libndctl.c   |  82 +++
 ndctl/lib/libndctl.sym |   4 +
 ndctl/libndctl.h   |  10 +
 ndctl/monitor.c| 542 +
 ndctl/ndctl.c  |   1 +
 util/filter.h  |   9 +
 8 files changed, 651 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 0f56871..083609a 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -15,7 +15,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index 47e005e..969e4aa 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -1635,6 +1635,88 @@ NDCTL_EXPORT int ndctl_dimm_get_health_eventfd(struct 
ndctl_dimm *dimm)
return dimm->health_eventfd;
 }
 
+NDCTL_EXPORT unsigned int ndctl_dimm_get_health(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int health;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   health = ndctl_cmd_smart_get_health(cmd);
+   ndctl_cmd_unref(cmd);
+   return health;
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int flags;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   dbg(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   dbg(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   flags = ndctl_cmd_smart_get_flags(cmd);
+   ndctl_cmd_unref(cmd);
+   return flags;
+}
+
+NDCTL_EXPORT int ndctl_dimm_is_flag_supported(struct ndctl_dimm *dimm,
+   unsigned int flag)
+{
+   unsigned int flags = ndctl_dimm_get_flags(dimm);
+   return (flags ==  UINT_MAX) ? 0 : !!(flags & flag);
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_event_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int alarm_flags, event_flags = 0;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
+   if (alarm_flags & ND_SMART_SPARE_TRIP)
+   event_flags |= ND_EVENT_SPARES_REMAINING;
+   if (alarm_flags & ND_SMART_MTEMP_TRIP)
+   event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
+   if (alarm_flags &

[ndctl PATCH v13 5/5] ndctl, test: add a new unit test for monitor

2018-07-13 Thread QI Fuli
Add a new unit test to test all options of the monitor command.

Based-on-patch-by: Yasunori Goto 
Acked-by: Masayoshi Mizuma 
Signed-off-by: QI Fuli 
---
 .gitignore |   1 +
 test/Makefile.am   |  14 +++-
 test/list-smart-dimm.c | 117 +++
 test/monitor.sh| 176 +
 4 files changed, 306 insertions(+), 2 deletions(-)
 create mode 100644 test/list-smart-dimm.c
 create mode 100755 test/monitor.sh

diff --git a/.gitignore b/.gitignore
index 1016b3b..0baace4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,3 +56,4 @@ test/smart-notify
 test/fio.job
 test/local-write-0-verify.state
 test/ack-shutdown-count-set
+test/list-smart-dimm
diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c55056 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
@@ -34,7 +35,8 @@ check_PROGRAMS =\
smart-listen \
hugetlb \
daxdev-errors \
-   ack-shutdown-count-set
+   ack-shutdown-count-set \
+   list-smart-dimm
 
 if ENABLE_DESTRUCTIVE
 TESTS +=\
@@ -151,3 +153,11 @@ multi_pmem_LDADD = \
$(UUID_LIBS) \
$(KMOD_LIBS) \
../libutil.a
+
+list_smart_dimm_SOURCES = \
+   list-smart-dimm.c \
+   ../util/json.c
+list_smart_dimm_LDADD = \
+   $(LIBNDCTL_LIB) \
+   $(JSON_LIBS) \
+   ../libutil.a
diff --git a/test/list-smart-dimm.c b/test/list-smart-dimm.c
new file mode 100644
index 000..c9982d5
--- /dev/null
+++ b/test/list-smart-dimm.c
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct util_filter_params param;
+static int did_fail;
+static int jflag = JSON_C_TO_STRING_PRETTY;
+
+#define fail(fmt, ...) \
+do { \
+   did_fail = 1; \
+   fprintf(stderr, "ndctl-%s:%s:%d: " fmt, \
+   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
+} while (0)
+
+static bool filter_region(struct ndctl_region *region,
+   struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static void filter_dimm(struct ndctl_dimm *dimm, struct util_filter_ctx *ctx)
+{
+   struct list_filter_arg *lfa = ctx->list;
+   struct json_object *jdimm;
+
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART))
+   return;
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART_THRESHOLD))
+   return;
+   if (!ndctl_dimm_is_flag_supported(dimm, ND_SMART_ALARM_VALID))
+   return;
+
+   if (!lfa->jdimms) {
+   lfa->jdimms = json_object_new_array();
+   if (!lfa->jdimms) {
+   fail("\n");
+   return;
+   }
+   }
+
+   jdimm = util_dimm_to_json(dimm, lfa->flags);
+   if (!jdimm) {
+   fail("\n");
+   return;
+   }
+
+   json_object_array_add(lfa->jdimms, jdimm);
+}
+
+static bool filter_bus(struct ndctl_bus *bus, struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static int list_display(struct list_filter_arg *lfa)
+{
+   struct json_object *jdimms = lfa->jdimms;
+
+   if (jdimms)
+   util_display_json_array(stdout, jdimms, jflag);
+   return 0;
+}
+
+int main(int argc, const char *argv[])
+{
+   struct ndctl_ctx *ctx;
+   int i, rc;
+   const struct option options[] = {
+   OPT_STRING('b', "bus", , "bus-id", "filter by bus"),
+   OPT_STRING('r', "region", , "region-id",
+   "filter by region"),
+   OPT_STRING('d', "dimm", , "dimm-id",
+   "filter by dimm"),
+   OPT_STRING('n', "namespace", , "namespace-id",
+   "filter by namespace id"),
+   OPT_END(),
+   };
+   const char * const u[] = {
+   "list-smart-dimm []",
+   NULL
+   };
+   struct util_filter_ctx fctx = { 0 };
+   struct list_filter_arg lfa = { 0 };
+
+   rc = ndctl_new();
+   if (rc < 0)
+   return EXIT_FAILURE;
+argc = parse_options(argc, argv, options, u, 0);
+   for (i = 0; i < argc; i++)
+   error("unknown parameter \"%s\"\n", argv[i]);
+   if (argc)
+   usage_with_options(u, options);
+
+   fctx.filter_bus = filter_bus;
+   fctx.filter_dimm = filter_dimm;
+ 

[ndctl PATCH v13 3/5] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-07-13 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl.spec.in   |  2 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 5 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index ff0096d..e242334 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -D DEF_CONF_FILE='\"${sysconfdir}/ndctl/monitor.conf\"' \
 -Wall \
@@ -185,6 +206,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl.spec.in b/ndctl.spec.in
index 41a1d59..17152c1 100644
--- a/ndctl.spec.in
+++ b/ndctl.spec.in
@@ -20,6 +20,7 @@ BuildRequires:pkgconfig(libudev)
 BuildRequires: pkgconfig(uuid)
 BuildRequires: pkgconfig(json-c)
 BuildRequires: pkgconfig(bash-completion)
+BuildRequires: systemd
 
 %description
 Utility library for managing the "libnvdimm" subsystem.  The "libnvdimm"
@@ -117,6 +118,7 @@ make check
 %{_mandir}/man1/ndctl*
 %{bashcompdir}/
 %{_sysconfdir}/ndctl/monitor.conf
+%{_unitdir}/ndctl-monitor.service
 
 %files -n daxctl
 %defattr(-,root,root)
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 58b747a..0f9bb43 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = $(sysconfdir)/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v12 0/5] ndctl, monitor: add ndctl monitor daemon

2018-07-13 Thread QI Fuli
This is the v12 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v11:
 - Adding log_standard()
 - Adding [-u | --human] option
 - Fixing man page
 - Refactoring unit test
 - Updating configuration file and systemd unit file to RPM spec file

Change log since v10:
 - Adding unit test
 - Adding fflush to log_file()

Change log since v9:
 - Replacing ndctl_cmd_smart_get_event_flags() with
   ndctl_dimm_get_event_flags()
 - Adding ndctl_dimm_get_health() api
 - Adding ndctl_dimm_get_flags() api
 - Adding ndctl_dimm_is_flag_supported api
 - Adding manpage

Change log since v8:
 - Adding ndctl_cmd_smart_get_event_flags() api
 - Adding monitor_filter_arg to the union in util_filter_ctx
 - Removing is_dir()
 - Replacing malloc + vsprintf with vasprintf() in log_file() and log_syslog()
 - Adding parse_monitor_event()
 - Refactoring util_dimm_event_filter()
 - Adding event_flags to monitor
 - Refactoring dimm_event_to_json()
 - Adding check_dimm_supported_threshold_alarms()
 - Fixing fail token

Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (5):
  ndctl, monitor: add a new command - monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service
  ndctl, documentation: add man page for monitor
  ndctl, test: add a new unit test for monitor

 .gitignore|   1 +
 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt | 104 +
 autogen.sh|   3 +-
 builtin.h |   1 +
 configure.ac  |  22 +
 ndctl.spec.in |   2 +
 ndctl/Makefile.am |  12 +-
 ndctl/lib/libndctl.c  |  82 
 ndctl/lib/libndctl.sym|   4 +
 ndctl/libndctl.h  |  10 +
 ndctl/monitor.c   | 649 ++
 ndctl/monitor.conf|  41 ++
 ndctl/ndctl-monitor.service   |   7 +
 ndctl/ndctl.c |   1 +
 test/Makefile.am  |  14 +-
 test/list-smart-dimm.c| 117 +
 test/monitor.sh   | 176 +++
 util/filter.h |   9 +
 19 files changed, 1253 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt
 create mode 100644 ndctl/monitor.c
 create mode 100644 ndctl/monitor.conf

[ndctl PATCH v12 1/5] ndctl, monitor: add a new command - monitor

2018-07-13 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of NVDIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and event information to syslog, standard
output or a file by setting [--log] option. The notifications follow
json format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --log /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h  |   1 +
 ndctl/Makefile.am  |   3 +-
 ndctl/lib/libndctl.c   |  82 +++
 ndctl/lib/libndctl.sym |   4 +
 ndctl/libndctl.h   |  10 +
 ndctl/monitor.c| 539 +
 ndctl/ndctl.c  |   1 +
 util/filter.h  |   9 +
 8 files changed, 648 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 0f56871..083609a 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -15,7 +15,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index 47e005e..969e4aa 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -1635,6 +1635,88 @@ NDCTL_EXPORT int ndctl_dimm_get_health_eventfd(struct 
ndctl_dimm *dimm)
return dimm->health_eventfd;
 }
 
+NDCTL_EXPORT unsigned int ndctl_dimm_get_health(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int health;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   health = ndctl_cmd_smart_get_health(cmd);
+   ndctl_cmd_unref(cmd);
+   return health;
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int flags;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   dbg(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   dbg(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   flags = ndctl_cmd_smart_get_flags(cmd);
+   ndctl_cmd_unref(cmd);
+   return flags;
+}
+
+NDCTL_EXPORT int ndctl_dimm_is_flag_supported(struct ndctl_dimm *dimm,
+   unsigned int flag)
+{
+   unsigned int flags = ndctl_dimm_get_flags(dimm);
+   return (flags ==  UINT_MAX) ? 0 : !!(flags & flag);
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_event_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int alarm_flags, event_flags = 0;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
+   if (alarm_flags & ND_SMART_SPARE_TRIP)
+   event_flags |= ND_EVENT_SPARES_REMAINING;
+   if (alarm_flags & ND_SMART_MTEMP_TRIP)
+   event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
+   if (alarm_flags &

[ndctl PATCH v12 4/5] ndctl, documentation: add man page for monitor

2018-07-13 Thread QI Fuli
This patch is used to add man page for ndctl monitor command.

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt | 104 ++
 2 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt

diff --git a/Documentation/ndctl/Makefile.am b/Documentation/ndctl/Makefile.am
index 4fd9636..a30b139 100644
--- a/Documentation/ndctl/Makefile.am
+++ b/Documentation/ndctl/Makefile.am
@@ -46,7 +46,8 @@ man1_MANS = \
ndctl-inject-error.1 \
ndctl-inject-smart.1 \
ndctl-update-firmware.1 \
-   ndctl-list.1
+   ndctl-list.1 \
+   ndctl-monitor.1
 
 CLEANFILES = $(man1_MANS)
 
diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
new file mode 100644
index 000..4f1803e
--- /dev/null
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+
+ndctl-monitor(1)
+
+
+NAME
+
+ndctl-monitor - Monitor the smart events of nvdimm objects
+
+SYNOPSIS
+
+[verse]
+'ndctl monitor' []
+
+DESCRIPTION
+---
+Ndctl monitor is used for monitoring the smart events of nvdimm
+objects and dumping the json format notifications to syslog, standard
+output or a logfile.
+
+The objects to monitor and smart events to notify can be selected by
+setting options and/or the default configuration file
+(/etc/ndctl/monitor.conf). Both of the values in configuration file
+and in options will work. If there is a conflict, the values in
+options will override the values in configuration file. The changed
+values in configuration file will work after the monitor is restarted.
+
+The smart threshold evnet alarm of dimm will be turned on when the
+monitor gets started, if it is set off.
+
+EXAMPLES
+
+
+Run a monitor as a daemon to monitor DIMMs on bus "nfit_test.1"
+[verse]
+ndctl monitor --bus nfit_test.1 --daemon
+
+Run a monitor as a one-shot command and output the notifications to
+/var/log/ndctl.log
+[verse]
+ndctl monitor -log /var/log/ndctl.log
+
+Run a monitor daemon as a system service
+[verse]
+systemctl start ndctl-monitor.service
+
+OPTIONS
+---
+-b::
+--bus=::
+   Enforce that the operation only be carried on devices that are
+   attached to the given bus. Where 'bus' can be a provider name
+   or a bus id number.
+
+-d::
+--dimm=::
+   A 'nmemX' device name, or dimm id number. Select the devices to
+   monitor reference the given dimm.
+
+-r::
+--region=::
+   A 'regionX' device name, or a region id number. The keyword 'all'
+   can be specified to carry out the operation on every region in
+   the system, optionally filtered by bus id (see --bus= option).
+
+-n::
+--namespace=::
+   A 'namespaceX.Y' device name, or namespace region plus id tuple
+   'X.Y'.
+
+-l ::
+--log=::
+   Output notifications to , syslog or standard output.
+
+--daemon::
+   Run a monitor as a daemon.
+
+-D::
+--dimm-event=::
+   Name of an smart health event from the following:
+   - "dimm-spares-remaining": Spare Blocks Remaining value has gone
+  below the pre-programmed threshold.
+   - "dimm-media-temperature": NVDIMM Media temperature value has
+  gone above the pre-programmed threshold.
+   - "dimm-controller-temperature": NVDIMM Controller temperature
+  value has gone above the pre-programmed threshold.
+   - "dimm-health-state": NVDIMM Normal Health Status has changed
+   - "dimm-unclean-shutdown": NVDIMM Last Shutdown Status was a
+  unclean shutdown.
+-u::
+--human::
+   Output monitor notification as human friendly json format instead
+   of the default machine friendly json format.
+
+COPYRIGHT
+-
+Copyright (c) 2018, FUJITSU LIMITED. License GPLv2: GNU GPL version 2
+<http://gnu.org/licenses/gpl.html>. This is free software: you are
+free to change and redistribute it. There is NO WARRANTY, to the
+extent permitted by law.
+
+SEE ALSO
+
+linkndctl:ndctl-list[1], linkndctl:ndctl-inject-smart[1]
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[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 ch

[ndctl PATCH v12 3/5] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-07-13 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl.spec.in   |  1 +
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 5 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index cf44260..a5ba9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -184,6 +205,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl.spec.in b/ndctl.spec.in
index 42760fa..6f7d299 100644
--- a/ndctl.spec.in
+++ b/ndctl.spec.in
@@ -142,6 +142,7 @@ make check
 %{_libdir}/libndctl.so
 %{_libdir}/pkgconfig/libndctl.pc
 %{_sysconfdir}/ndctl/monitor.conf
+%{_unitdir}/ndctl-monitor.service
 
 %files -n DAX_DNAME
 %defattr(-,root,root)
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 4d0..087ddc5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v12 5/5] ndctl, test: add a new unit test for monitor

2018-07-13 Thread QI Fuli
Add a new unit test to test all options of the monitor command.

Based-on-patch-by: Yasunori Goto 
Acked-by: Masayoshi Mizuma 
Signed-off-by: QI Fuli 
---
 .gitignore |   1 +
 test/Makefile.am   |  14 +++-
 test/list-smart-dimm.c | 117 +++
 test/monitor.sh| 176 +
 4 files changed, 306 insertions(+), 2 deletions(-)
 create mode 100644 test/list-smart-dimm.c
 create mode 100755 test/monitor.sh

diff --git a/.gitignore b/.gitignore
index 1016b3b..0baace4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,3 +56,4 @@ test/smart-notify
 test/fio.job
 test/local-write-0-verify.state
 test/ack-shutdown-count-set
+test/list-smart-dimm
diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c55056 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
@@ -34,7 +35,8 @@ check_PROGRAMS =\
smart-listen \
hugetlb \
daxdev-errors \
-   ack-shutdown-count-set
+   ack-shutdown-count-set \
+   list-smart-dimm
 
 if ENABLE_DESTRUCTIVE
 TESTS +=\
@@ -151,3 +153,11 @@ multi_pmem_LDADD = \
$(UUID_LIBS) \
$(KMOD_LIBS) \
../libutil.a
+
+list_smart_dimm_SOURCES = \
+   list-smart-dimm.c \
+   ../util/json.c
+list_smart_dimm_LDADD = \
+   $(LIBNDCTL_LIB) \
+   $(JSON_LIBS) \
+   ../libutil.a
diff --git a/test/list-smart-dimm.c b/test/list-smart-dimm.c
new file mode 100644
index 000..c9982d5
--- /dev/null
+++ b/test/list-smart-dimm.c
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct util_filter_params param;
+static int did_fail;
+static int jflag = JSON_C_TO_STRING_PRETTY;
+
+#define fail(fmt, ...) \
+do { \
+   did_fail = 1; \
+   fprintf(stderr, "ndctl-%s:%s:%d: " fmt, \
+   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
+} while (0)
+
+static bool filter_region(struct ndctl_region *region,
+   struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static void filter_dimm(struct ndctl_dimm *dimm, struct util_filter_ctx *ctx)
+{
+   struct list_filter_arg *lfa = ctx->list;
+   struct json_object *jdimm;
+
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART))
+   return;
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART_THRESHOLD))
+   return;
+   if (!ndctl_dimm_is_flag_supported(dimm, ND_SMART_ALARM_VALID))
+   return;
+
+   if (!lfa->jdimms) {
+   lfa->jdimms = json_object_new_array();
+   if (!lfa->jdimms) {
+   fail("\n");
+   return;
+   }
+   }
+
+   jdimm = util_dimm_to_json(dimm, lfa->flags);
+   if (!jdimm) {
+   fail("\n");
+   return;
+   }
+
+   json_object_array_add(lfa->jdimms, jdimm);
+}
+
+static bool filter_bus(struct ndctl_bus *bus, struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static int list_display(struct list_filter_arg *lfa)
+{
+   struct json_object *jdimms = lfa->jdimms;
+
+   if (jdimms)
+   util_display_json_array(stdout, jdimms, jflag);
+   return 0;
+}
+
+int main(int argc, const char *argv[])
+{
+   struct ndctl_ctx *ctx;
+   int i, rc;
+   const struct option options[] = {
+   OPT_STRING('b', "bus", , "bus-id", "filter by bus"),
+   OPT_STRING('r', "region", , "region-id",
+   "filter by region"),
+   OPT_STRING('d', "dimm", , "dimm-id",
+   "filter by dimm"),
+   OPT_STRING('n', "namespace", , "namespace-id",
+   "filter by namespace id"),
+   OPT_END(),
+   };
+   const char * const u[] = {
+   "list-smart-dimm []",
+   NULL
+   };
+   struct util_filter_ctx fctx = { 0 };
+   struct list_filter_arg lfa = { 0 };
+
+   rc = ndctl_new();
+   if (rc < 0)
+   return EXIT_FAILURE;
+argc = parse_options(argc, argv, options, u, 0);
+   for (i = 0; i < argc; i++)
+   error("unknown parameter \"%s\"\n", argv[i]);
+   if (argc)
+   usage_with_options(u, options);
+
+   fctx.filter_bus = filter_bus;
+   fctx.filter_dimm = filter_dimm;
+ 

RE: [ndctl PATCH v11 3/5] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-07-13 Thread Qi, Fuli
> -Original Message-
> From: Verma, Vishal L [mailto:vishal.l.ve...@intel.com]
> Sent: Friday, July 13, 2018 11:47 AM
> To: linux-nvdimm@lists.01.org; Qi, Fuli/斉 福利 
> Subject: Re: [ndctl PATCH v11 3/5] ndctl, monitor: add the unit file of 
> systemd for
> ndctl-monitor service
> 
> 
> On Wed, 2018-07-11 at 12:00 +0900, QI Fuli wrote:
> > This patch adds the systemd unit file for ndctl-monitor service.
> > The systemd unit directory can be configured by setting environment
> > variable "--with-systemd-unit-dir[=DIR]".
> >
> > A monitor daemon can be started as a system service:
> ># systemctl start ndctl-monitor.service
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  autogen.sh  |  3 ++-
> >  configure.ac| 22 ++
> >  ndctl/Makefile.am   |  4 
> >  ndctl/ndctl-monitor.service |  7 +++
> >  4 files changed, 35 insertions(+), 1 deletion(-)  create mode 100644
> > ndctl/ndctl-monitor.service
> 
> We install the service unit file when doing a 'make install', but I think 
> this also
> needs an update to the RPM spec file so that RPM knows about this too.
> 

Yes, I think both the configuration file and the service unit file are need to 
update the
RPM spec file. I knew that hard code the path of the files is not right, and I 
tried to
add %{_monitor_configdir}/monitor.conf and 
%{_systemd_unitdir}/ndctl-monitor.service in ndctl.spec.in,
but I got failed. Would you please tell me how to update the RPM spec file?

Thank you very much.
QI

> >
> > diff --git a/autogen.sh b/autogen.sh
> > index 2a52688..21b0e25 100755
> > --- a/autogen.sh
> > +++ b/autogen.sh
> > @@ -17,7 +17,8 @@ libdir() {
> >
> >  args="--prefix=/usr \
> >  --sysconfdir=/etc \
> > ---libdir=$(libdir /usr/lib)"
> > +--libdir=$(libdir /usr/lib) \
> > +--with-systemd-unit-dir"
> >
> >  echo
> >  echo "
> > "
> > diff --git a/configure.ac b/configure.ac index cf44260..a5ba9a1 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
> > secure_getenv\
> >  ])
> >
> > +PKG_PROG_PKG_CONFIG
> > +AC_ARG_WITH([systemd-unit-dir],
> > +   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
> > +   [Directory for systemd service files]),
> > +   [],
> > +   [with_systemd_unit_dir=yes])
> > +
> > +if test "x$with_systemd_unit_dir" = "xyes"; then
> > +   def_systemd_unit_dir=$($PKG_CONFIG --
> > variable=systemdsystemunitdir systemd)
> > +   if test "x$def_systemd_unit_dir" = "x"; then
> > +   AC_MSG_ERROR([systemd support requested but pkg-
> > config unable to query systemd package])
> > +   with_systemd_unit_dir=no
> > +   else
> > +   with_systemd_unit_dir="$def_systemd_unit_dir"
> > +   fi
> > +fi
> > +
> > +AS_IF([test "x$with_systemd_unit_dir" != "xno"],
> > +   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
> > +AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test
> > "x$with_systemd_unit_dir" != "xno"])
> > +
> >  my_CFLAGS="\
> >  -Wall \
> >  -Wchar-subscripts \
> > @@ -184,6 +205,7 @@ AC_MSG_RESULT([
> >  sysconfdir: ${sysconfdir}
> >  libdir: ${libdir}
> >  includedir: ${includedir}
> > +   systemd-unit-dir:   ${systemd_unitdir}
> >
> >  compiler:   ${CC}
> >  cflags: ${CFLAGS}
> > diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am index
> > ae3d894..9d008d5 100644
> > --- a/ndctl/Makefile.am
> > +++ b/ndctl/Makefile.am
> > @@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
> > monitor_configdir = /etc/ndctl/  monitor_config_DATA =
> > $(monitor_config_file)  EXTRA_DIST += $(monitor_config_file)
> > +
> > +if ENABLE_SYSTEMD_UNIT_DIR
> > +systemd_unit_DATA = ndctl-monitor.service endif
> > diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-
> > monitor.service new file mode 100644 index 000..44f9326
> > --- /dev/null
> > +++ b/ndctl/ndctl-monitor.service
> > @@ -0,0 +1,7 @@
> > +[Unit]
> > +Description=Ndctl Monitor Daemon
> > +
> > +[Service]
> > +Type=forking
> > +ExecStart=/usr/bin/ndctl monitor --daemon ExecStop=/bin/kill
> > +${MAINPID}
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH v11 5/5] ndctl, test: add a new unit test for monitor

2018-07-12 Thread Qi, Fuli
> On Thu, 2018-07-12 at 15:51 -0400, Masayoshi Mizuma wrote:
> > Hi Qi,
> >
> > Nice work! Let me ask some comments.
> >
> > On 07/10/2018 11:00 PM, QI Fuli wrote:
> > [...]
> > > diff --git a/test/monitor.sh b/test/monitor.sh new file mode 100755
> > > index 000..43cb11f
> > > --- /dev/null
> > > +++ b/test/monitor.sh
> > > @@ -0,0 +1,177 @@
> > > +#!/bin/bash -Ex
> > > +
> > > +# SPDX-License-Identifier: GPL-2.0
> > > +# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
> > > +
> > > +rc=77
> > > +logfile=""
> > > +conf_file=""
> > > +monitor_dimms=""
> > > +monitor_regions=""
> > > +monitor_namespace=""
> > > +monitor_pid=65536
> > > +
> > > +. ./common
> > > +
> > > +trap 'err $LINENO' ERR
> > > +
> > > +check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor 
> > > service"
> > > +
> > > +init()
> > > +{
> > > + $NDCTL disable-region -b $NFIT_TEST_BUS0 all
> > > + $NDCTL zero-labels -b $NFIT_TEST_BUS0 all
> > > + $NDCTL enable-region -b $NFIT_TEST_BUS0 all }
> > > +
> > > +start_monitor()
> > > +{
> > > + logfile=$(mktemp)
> > > + $NDCTL monitor -l $logfile $1 &
> > > + monitor_pid=$!
> > > + truncate --size 0 $logfile #remove startup log
> > > + sync; sleep 3
> >
> > Should this sync be moved to before the truncate?
> >
> > > +}
> > > +
> > > +get_monitor_dimm()
> > > +{
> > > + jlist=$(./list-smart-dimm -b $smart_supported_bus $1)
> > > + monitor_dimms=$(jq '.[]."dev"?, ."dev"?' <<<$jlist | sort | uniq |
> > > +xargs) }
> > > +
> > > +call_notify()
> > > +{
> > > + ./smart-notify $smart_supported_bus
> > > + sync; sleep 3
> > > +}
> > > +
> > > +inject_smart()
> > > +{
> > > + $NDCTL inject-smart $monitor_dimms $1
> > > + sync; sleep 3
> > > +}
> > > +
> > > +check_result()
> > > +{
> > > + jlog=$(cat $logfile)
> > > + notify_dimms=$(jq ."dimm"."dev" <<<$jlog | sort | uniq | xargs)
> > > + [[ $monitor_dimms == $notify_dimms ]] }
> > > +
> > > +stop_monitor()
> > > +{
> > > + kill $monitor_pid
> > > + rm $logfile
> > > +}
> > > +
> > > +create_conf_file()
> > > +{
> > > + conf_file=$(mktemp)
> > > + echo "dimm = $1" > $conf_file
> > > +}
> > > +
> > > +test_filter_dimm()
> > > +{
> > > + smart_supported_bus=$NFIT_TEST_BUS0
> > > + monitor_dimms=$(./list-smart-dimm -b $smart_supported_bus | jq -r 
> > > .[0].dev)
> > > + if [ -z $monitor_dimms ]; then
> > > + smart_supported_bus=$NFIT_TEST_BUS1
> > > + monitor_dimms=$(./list-smart-dimm -b $smart_supported_bus | jq
> -r .[0].dev)
> > > + fi
> > > + start_monitor "-d $monitor_dimms"
> > > + call_notify
> > > + check_result
> > > + stop_monitor
> > > +}
> >
> > I think the global variable "smart_supported_bus" configuration should
> > be separated from this function.
> > Like as:
> >
> > set_smart_supported_bus()
> > {
> > smart_supported_bus=$NFIT_TEST_BUS0
> > monitor_dimms=$(./list-smart-dimm -b $smart_supported_bus | jq
> -r .[0].dev)
> > if [ -z $monitor_dimms ]; then
> > smart_supported_bus=$NFIT_TEST_BUS1
> > fi
> > }
> >
> > > +
> > > +test_filter_bus()
> > > +{
> > > + monitor_dimms=""
> > > + get_monitor_dimm
> > > + start_monitor "-b $smart_supported_bus"
> > > + call_notify
> > > + check_result
> > > + stop_monitor
> > > +}
> > > +
> > > +test_filter_region()
> > > +{
> > > + monitor_dimms=""
> > > + monitor_region=""
> > > + count=$($NDCTL list -R -b $smart_supported_bus | jq -r .[].dev | wc -l)
> > > + i=0
> > > + while [ $i -lt $count ]; do
> > > + monitor_region=$($NDCTL list -R -b $smart_supported_bus | jq
> -r .[$i].dev)
> > > + get_monitor_dimm "-r $monitor_region"
> > > + [ ! -z $m

[ndctl PATCH] ndctl, documentation: fix the manpage of inject-smart

2018-07-11 Thread QI Fuli
The ndctl inject-smart doesn't have a [-H | --health] option.
This patch replaces [-H | --health] in the manpage of inject-smart
with [-f | --fatal].

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/ndctl-inject-smart.txt | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/Documentation/ndctl/ndctl-inject-smart.txt 
b/Documentation/ndctl/ndctl-inject-smart.txt
index 0dc6481..06710ec 100644
--- a/Documentation/ndctl/ndctl-inject-smart.txt
+++ b/Documentation/ndctl/ndctl-inject-smart.txt
@@ -50,6 +50,7 @@ OPTIONS
 --media-temperature-threshold=::
Set  for the smart media temperature threshold.
 
+-x::
 --media-temperature-alarm=::
Enable or disable the smart media temperature alarm. Options are
'on' or 'off'.
@@ -62,6 +63,7 @@ OPTIONS
 --ctrl-temperature-threshold=::
Set  for the smart controller temperature threshold.
 
+-y::
 --ctrl-temperature-alarm=::
Enable or disable the smart controller temperature alarm. Options are
'on' or 'off'.
@@ -74,16 +76,16 @@ OPTIONS
 --spares-threshold=::
Set  for the smart spares threshold.
 
+-z::
 --spares-alarm=::
Enable or disable the smart spares alarm. Options are 'on' or 'off'.
 
--H::
---health=::
-   Smart attribute for health status. Provide either 'fatal' or 'nominal'
-   to set the state of the attribute.
+-f::
+--fatal::
+   Inject fatal for the health state attribute.
 
 -U::
---unsafe-shutdown=::
+--unsafe-shutdown::
Set the flag to spoof an unsafe shutdown on the next power down.
 
 -v::
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v11 3/5] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-07-10 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index cf44260..a5ba9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -184,6 +205,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index ae3d894..9d008d5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


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

2018-07-10 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/Makefile.am  |   5 ++
 ndctl/monitor.c| 115 +
 ndctl/monitor.conf |  41 
 3 files changed, 161 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 7dbf223..ae3d894 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/multi-pmem.c \
 ../test/core.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 8127bbc..90582a2 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -19,6 +19,7 @@
 
 static struct monitor {
const char *logfile;
+   const char *config_file;
const char *dimm_event;
bool daemon;
unsigned int event_flags;
@@ -430,6 +431,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->logfile)
+   parse_config(&_monitor->logfile, "logfile", 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[] = {
@@ -442,6 +546,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
"namespace-id", "filter by namespace id"),
OPT_FILENAME('l', "logfile", , "file | sys

[ndctl PATCH v11 1/5] ndctl, monitor: add a new command - monitor

2018-07-10 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and event informations to syslog or a
logfile by setting [--logfile] option. The notifications follow json
format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h  |   1 +
 ndctl/Makefile.am  |   3 +-
 ndctl/lib/libndctl.c   |  82 +++
 ndctl/lib/libndctl.sym |   4 +
 ndctl/libndctl.h   |  10 +
 ndctl/monitor.c| 509 +
 ndctl/ndctl.c  |   1 +
 util/filter.h  |   9 +
 8 files changed, 618 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index d22a379..7dbf223 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index 47e005e..969e4aa 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -1635,6 +1635,88 @@ NDCTL_EXPORT int ndctl_dimm_get_health_eventfd(struct 
ndctl_dimm *dimm)
return dimm->health_eventfd;
 }
 
+NDCTL_EXPORT unsigned int ndctl_dimm_get_health(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int health;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   health = ndctl_cmd_smart_get_health(cmd);
+   ndctl_cmd_unref(cmd);
+   return health;
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int flags;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   dbg(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   dbg(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   flags = ndctl_cmd_smart_get_flags(cmd);
+   ndctl_cmd_unref(cmd);
+   return flags;
+}
+
+NDCTL_EXPORT int ndctl_dimm_is_flag_supported(struct ndctl_dimm *dimm,
+   unsigned int flag)
+{
+   unsigned int flags = ndctl_dimm_get_flags(dimm);
+   return (flags ==  UINT_MAX) ? 0 : !!(flags & flag);
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_event_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int alarm_flags, event_flags = 0;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
+   if (alarm_flags & ND_SMART_SPARE_TRIP)
+   event_flags |= ND_EVENT_SPARES_REMAINING;
+   if (alarm_flags & ND_SMART_MTEMP_TRIP)
+   event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
+   if (alarm_flags &

[ndctl PATCH v11 5/5] ndctl, test: add a new unit test for monitor

2018-07-10 Thread QI Fuli
Add a new unit test to test all options of the monitor command.

Based-on-patch-by: Yasunori Goto 
Signed-off-by: QI Fuli 
---
 .gitignore |   1 +
 test/Makefile.am   |  14 +++-
 test/list-smart-dimm.c | 117 +++
 test/monitor.sh| 177 +
 4 files changed, 307 insertions(+), 2 deletions(-)
 create mode 100644 test/list-smart-dimm.c
 create mode 100755 test/monitor.sh

diff --git a/.gitignore b/.gitignore
index 1016b3b..0baace4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,3 +56,4 @@ test/smart-notify
 test/fio.job
 test/local-write-0-verify.state
 test/ack-shutdown-count-set
+test/list-smart-dimm
diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c55056 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
@@ -34,7 +35,8 @@ check_PROGRAMS =\
smart-listen \
hugetlb \
daxdev-errors \
-   ack-shutdown-count-set
+   ack-shutdown-count-set \
+   list-smart-dimm
 
 if ENABLE_DESTRUCTIVE
 TESTS +=\
@@ -151,3 +153,11 @@ multi_pmem_LDADD = \
$(UUID_LIBS) \
$(KMOD_LIBS) \
../libutil.a
+
+list_smart_dimm_SOURCES = \
+   list-smart-dimm.c \
+   ../util/json.c
+list_smart_dimm_LDADD = \
+   $(LIBNDCTL_LIB) \
+   $(JSON_LIBS) \
+   ../libutil.a
diff --git a/test/list-smart-dimm.c b/test/list-smart-dimm.c
new file mode 100644
index 000..c9982d5
--- /dev/null
+++ b/test/list-smart-dimm.c
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct util_filter_params param;
+static int did_fail;
+static int jflag = JSON_C_TO_STRING_PRETTY;
+
+#define fail(fmt, ...) \
+do { \
+   did_fail = 1; \
+   fprintf(stderr, "ndctl-%s:%s:%d: " fmt, \
+   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
+} while (0)
+
+static bool filter_region(struct ndctl_region *region,
+   struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static void filter_dimm(struct ndctl_dimm *dimm, struct util_filter_ctx *ctx)
+{
+   struct list_filter_arg *lfa = ctx->list;
+   struct json_object *jdimm;
+
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART))
+   return;
+   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART_THRESHOLD))
+   return;
+   if (!ndctl_dimm_is_flag_supported(dimm, ND_SMART_ALARM_VALID))
+   return;
+
+   if (!lfa->jdimms) {
+   lfa->jdimms = json_object_new_array();
+   if (!lfa->jdimms) {
+   fail("\n");
+   return;
+   }
+   }
+
+   jdimm = util_dimm_to_json(dimm, lfa->flags);
+   if (!jdimm) {
+   fail("\n");
+   return;
+   }
+
+   json_object_array_add(lfa->jdimms, jdimm);
+}
+
+static bool filter_bus(struct ndctl_bus *bus, struct util_filter_ctx *ctx)
+{
+   return true;
+}
+
+static int list_display(struct list_filter_arg *lfa)
+{
+   struct json_object *jdimms = lfa->jdimms;
+
+   if (jdimms)
+   util_display_json_array(stdout, jdimms, jflag);
+   return 0;
+}
+
+int main(int argc, const char *argv[])
+{
+   struct ndctl_ctx *ctx;
+   int i, rc;
+   const struct option options[] = {
+   OPT_STRING('b', "bus", , "bus-id", "filter by bus"),
+   OPT_STRING('r', "region", , "region-id",
+   "filter by region"),
+   OPT_STRING('d', "dimm", , "dimm-id",
+   "filter by dimm"),
+   OPT_STRING('n', "namespace", , "namespace-id",
+   "filter by namespace id"),
+   OPT_END(),
+   };
+   const char * const u[] = {
+   "list-smart-dimm []",
+   NULL
+   };
+   struct util_filter_ctx fctx = { 0 };
+   struct list_filter_arg lfa = { 0 };
+
+   rc = ndctl_new();
+   if (rc < 0)
+   return EXIT_FAILURE;
+argc = parse_options(argc, argv, options, u, 0);
+   for (i = 0; i < argc; i++)
+   error("unknown parameter \"%s\"\n", argv[i]);
+   if (argc)
+   usage_with_options(u, options);
+
+   fctx.filter_bus = filter_bus;
+   fctx.filter_dimm = filter_dimm;
+   fctx.filter_region = f

[ndctl PATCH v11 0/5] ndctl, monitor: add ndctl monitor daemon

2018-07-10 Thread QI Fuli
This is the v11 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v10:
 - Adding unit test
 - Adding fflush to log_file()

Change log since v9:
 - Replacing ndctl_cmd_smart_get_event_flags() with
   ndctl_dimm_get_event_flags()
 - Adding ndctl_dimm_get_health() api
 - Adding ndctl_dimm_get_flags() api
 - Adding ndctl_dimm_is_flag_supported api
 - Adding manpage

Change log since v8:
 - Adding ndctl_cmd_smart_get_event_flags() api
 - Adding monitor_filter_arg to the union in util_filter_ctx
 - Removing is_dir()
 - Replacing malloc + vsprintf with vasprintf() in log_file() and log_syslog()
 - Adding parse_monitor_event()
 - Refactoring util_dimm_event_filter()
 - Adding event_flags to monitor
 - Refactoring dimm_event_to_json()
 - Adding check_dimm_supported_threshold_alarms()
 - Fixing fail token

Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (5):
  ndctl, monitor: add a new command - monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service
  ndctl, documentation: add manpage for monitor
  ndctl, test: add a new unit test for monitor

 .gitignore|   1 +
 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt |  96 
 autogen.sh|   3 +-
 builtin.h |   1 +
 configure.ac  |  22 +
 ndctl/Makefile.am |  12 +-
 ndctl/lib/libndctl.c  |  82 
 ndctl/lib/libndctl.sym|   4 +
 ndctl/libndctl.h  |  10 +
 ndctl/monitor.c   | 624 ++
 ndctl/monitor.conf|  41 ++
 ndctl/ndctl-monitor.service   |   7 +
 ndctl/ndctl.c |   1 +
 test/Makefile.am  |  14 +-
 test/list-smart-dimm.c| 117 +
 test/monitor.sh   | 177 
 util/filter.h |   9 +
 18 files changed, 1219 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt
 create mode 100644 ndctl/monitor.c
 create mode 100644 ndctl/monitor.conf
 create mode 100644 ndctl/ndctl-monitor.service
 create mode 100644 test/list-smart-dimm.c
 create mode 100755 test/monitor.sh

-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org

[ndctl PATCH v11 4/5] ndctl, documentation: add manpage for monitor

2018-07-10 Thread QI Fuli
This patch is used to add manpage for ndctl monitor command.

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/Makefile.am   |  3 +-
 Documentation/ndctl/ndctl-monitor.txt | 96 +++
 2 files changed, 98 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt

diff --git a/Documentation/ndctl/Makefile.am b/Documentation/ndctl/Makefile.am
index 4fd9636..a30b139 100644
--- a/Documentation/ndctl/Makefile.am
+++ b/Documentation/ndctl/Makefile.am
@@ -46,7 +46,8 @@ man1_MANS = \
ndctl-inject-error.1 \
ndctl-inject-smart.1 \
ndctl-update-firmware.1 \
-   ndctl-list.1
+   ndctl-list.1 \
+   ndctl-monitor.1
 
 CLEANFILES = $(man1_MANS)
 
diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
new file mode 100644
index 000..037fdc7
--- /dev/null
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -0,0 +1,96 @@
+ndctl-monitor(1)
+
+
+NAME
+
+ndctl-monitor - Monitor the smart events of nvdimm objects
+
+SYNOPSIS
+
+[verse]
+'ndctl monitor' []
+
+DESCRIPTION
+---
+Ndctl monitor is used for monitoring the smart events of nvdimm
+objects and dumping the json format notifications to syslog or
+a logfile.
+
+The objects to monitor and smart evnets to notify can be selected
+by setting options and/or the default configuration file
+(/etc/ndctl/monitor.conf). Both of the values in configuration file
+and in options will work. If conflict, the values in options will
+override the values in configuration file. The changed values in
+configuration file will work after restart ndctl monitor.
+
+EXAMPLES
+
+
+Run a monitor as a daemon to monitor DIMMs on a provider named ndbus1
+[verse]
+ndctl monitor -b ndbus1 -f
+
+Run a monitor as a one-shot command and output the notifications to
+/var/log/ndctl.log
+[verse]
+ndctl monitor -l /var/log/ndctl.log
+
+Run a monitor daemon as a system service
+[verse]
+systemctl start ndctl-monitor.service
+
+OPTIONS
+---
+-b::
+--bus=::
+   Enforce that the operation only be carried on devices that are
+   attached to the given bus. Where 'bus' can be a provider name
+   or a bus id number.
+
+-d::
+--dimm=::
+   A 'nmemX' device name, or dimm id number. Select the devices to
+   monitor refrerence the given dimm.
+
+-r::
+--region=::
+   A 'regionX' device name, or a region id number. The keyword 'all'
+   can be specified to carry out the operation on every region in
+   the system, optionally filtered by bus id (see --bus= option).
+
+-n::
+--namespace=::
+   A 'namespaceX.Y' device name, or namespace region plus id tuple
+   'X.Y'.
+
+-l ::
+--logfile=::
+   Output notifications to  or syslog.
+
+-f::
+--daemon::
+   Run a monitor as a daemon.
+
+-D::
+--dimm-event=::
+   A name of smart events come NVDIMM DIMMs.
+   - "dimm-spares-remaining": Spare Blocks Remaining value has gone
+  below the pre-programmed threshold limit
+   - "dimm-media-temperature": NVDIMM Media temperature value has
+  gone above the pre-programmed threshold limit
+   - "dimm-controller-temperature": NVDIMM Controller temperature
+  value has gone above the pre-programmed threshold limit
+   - "dimm-health-state": NVDIMM Normal Health Status has changed
+   - "dimm-unclean-shutdown": NVDIMM Last Shutdown Status was a dirty
+  shutdown.
+
+COPYRIGHT
+-
+Copyright (c) 2018, FUJITSU LIMITED. License GPLv2: GNU GPL version 2
+<http://gnu.org/licenses/gpl.html>. This is free software: you are
+free to change and redistribute it. There is NO WARRANTY, to the
+extent permitted by law.
+
+SEE ALSO
+
+linkndctl:ndctl-list[1]
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH v10 1/4] ndctl, monitor: add a new command - monitor

2018-07-10 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, July 11, 2018 5:19 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH v10 1/4] ndctl, monitor: add a new command - monitor
> 
> 
> On 07/09/2018 10:17 AM, QI Fuli wrote:
> [...]
> > +static void log_file(struct ndctl_ctx *ctx, int priority, const char *file,
> > +   int line, const char *fn, const char *format, va_list args) {
> > +   FILE *f;
> > +   char *buf;
> > +
> > +   if (vasprintf(, format, args) < 0) {
> > +   fail("vasprintf error\n");
> > +   return;
> > +   }
> > +
> > +   f = fopen(monitor.logfile, "a+");
> > +   if (!f) {
> > +   ndctl_set_log_fn(ctx, log_syslog);
> > +   fail("open logfile %s failed\n%s", monitor.logfile, buf);
> > +   goto end;
> > +   }
> > +   fprintf(f, "%s\n", buf);
> 
> The unit test sometimes failed because the log file is empty.
> I think fflush(f) should be needed here to complete the write.
> Otherwise, if the monitor daemon stops accidentally, the all log in buffer 
> are gone...
> 
Ok, I will fix it.

Thank you very much.
Qi

> Thanks,
> Masa
> 
> > +   fclose(f);
> > +end:
> > +   free(buf);
> > +   return;
> > +}
> > +
> > +static struct json_object *dimm_event_to_json(struct monitor_dimm
> > +*mdimm) {
> > +   struct json_object *jevent, *jobj;
> > +   bool spares_flag, media_temp_flag, ctrl_temp_flag,
> > +   health_state_flag, unclean_shutdown_flag;
> > +   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(mdimm->dimm);
> > +
> > +   jevent = json_object_new_object();
> > +   if (!jevent) {
> > +   fail("\n");
> > +   return NULL;
> > +   }
> > +
> > +   if (monitor.event_flags & ND_EVENT_SPARES_REMAINING) {
> > +   spares_flag = !!(mdimm->event_flags
> > +   & ND_EVENT_SPARES_REMAINING);
> > +   jobj = json_object_new_boolean(spares_flag);
> > +   if (jobj)
> > +   json_object_object_add(jevent,
> > +   "dimm-spares-remaining", jobj);
> > +   }
> > +
> > +   if (monitor.event_flags & ND_EVENT_MEDIA_TEMPERATURE) {
> > +   media_temp_flag = !!(mdimm->event_flags
> > +   & ND_EVENT_MEDIA_TEMPERATURE);
> > +   jobj = json_object_new_boolean(media_temp_flag);
> > +   if (jobj)
> > +   json_object_object_add(jevent,
> > +   "dimm-media-temperature", jobj);
> > +   }
> > +
> > +   if (monitor.event_flags & ND_EVENT_CTRL_TEMPERATURE) {
> > +   ctrl_temp_flag = !!(mdimm->event_flags
> > +   & ND_EVENT_CTRL_TEMPERATURE);
> > +   jobj = json_object_new_boolean(ctrl_temp_flag);
> > +   if (jobj)
> > +   json_object_object_add(jevent,
> > +   "dimm-controller-temperature", jobj);
> > +   }
> > +
> > +   if (monitor.event_flags & ND_EVENT_HEALTH_STATE) {
> > +   health_state_flag = !!(mdimm->event_flags
> > +   & ND_EVENT_HEALTH_STATE);
> > +   jobj = json_object_new_boolean(health_state_flag);
> > +   if (jobj)
> > +   json_object_object_add(jevent,
> > +   "dimm-health-state", jobj);
> > +   }
> > +
> > +   if (monitor.event_flags & ND_EVENT_UNCLEAN_SHUTDOWN) {
> > +   unclean_shutdown_flag = !!(mdimm->event_flags
> > +   & ND_EVENT_UNCLEAN_SHUTDOWN);
> > +   jobj = json_object_new_boolean(unclean_shutdown_flag);
> > +   if (jobj)
> > +   json_object_object_add(jevent,
> > +   "dimm-unclean-shutdown", jobj);
> > +   }
> > +
> > +   return jevent;
> > +}
> > +
> > +static int notify_dimm_event(struct monitor_dimm *mdimm) {
> > +   struct json_object *jmsg, *jdimm, *jobj;
> > +   struct timespec ts;
> > +   char timestamp[32];
> > +   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(mdimm->dimm);
> > +
> > +   jmsg = json_object_new_object();
> > +   if (!jmsg) {
> > +   fail("\n");
> > +   return -1;
> > +   }
> > +
>

RE: [ndctl PATCH v4] ndctl, test: add a new unit test for monitor

2018-07-10 Thread Qi, Fuli
> -Original Message-
> From: Masayoshi Mizuma [mailto:msys.miz...@gmail.com]
> Sent: Wednesday, July 11, 2018 12:11 AM
> To: Qi, Fuli/斉 福利 ; linux-nvdimm@lists.01.org
> Subject: Re: [ndctl PATCH v4] ndctl, test: add a new unit test for monitor
> 
> Hi Qi
> 
> Thank you for creating the v4 test, but unfortunately the test has failed...
> 
> On 07/10/2018 06:04 AM, QI Fuli wrote:
> > Add a new unit test to test the following options of the monitor command.
> >--dimm
> >--bus
> >--region
> >--namespace
> >--logfile
> >--config-file
> >
> > Based-on-patch-by: Yasunori Goto 
> > Signed-off-by: QI Fuli 
> > ---
> > v3 -> v4:
> >Add list-smart-dimm() helper to list and filter smart supported
> > dimms
> > v2 -> v3:
> >  - Add filter_obj instead of hard-coded values
> > v1 -> v2:
> >  - Add init()
> >  - Add get_filter_dimm() to get the filter dimms by ndctl list command
> >instead of hard-cording
> >  - Add sleep to call_notify()
> >
> >  .gitignore |   1 +
> >  test/Makefile.am   |  14 +++-
> >  test/list-smart-dimm.c | 117 +
> >  test/monitor.sh| 146 +
> >  4 files changed, 276 insertions(+), 2 deletions(-)  create mode
> > 100644 test/list-smart-dimm.c  create mode 100755 test/monitor.sh
> >
> > diff --git a/.gitignore b/.gitignore
> > index 1016b3b..0baace4 100644
> > --- a/.gitignore
> > +++ b/.gitignore
> > @@ -56,3 +56,4 @@ test/smart-notify
> >  test/fio.job
> >  test/local-write-0-verify.state
> >  test/ack-shutdown-count-set
> > +test/list-smart-dimm
> > diff --git a/test/Makefile.am b/test/Makefile.am index
> > cd451e9..8c55056 100644
> > --- a/test/Makefile.am
> > +++ b/test/Makefile.am
> > @@ -21,7 +21,8 @@ TESTS =\
> > btt-pad-compat.sh \
> > firmware-update.sh \
> > ack-shutdown-count-set \
> > -   rescan-partitions.sh
> > +   rescan-partitions.sh \
> > +   monitor.sh
> >
> >  check_PROGRAMS =\
> > libndctl \
> > @@ -34,7 +35,8 @@ check_PROGRAMS =\
> > smart-listen \
> > hugetlb \
> > daxdev-errors \
> > -   ack-shutdown-count-set
> > +   ack-shutdown-count-set \
> > +   list-smart-dimm
> >
> >  if ENABLE_DESTRUCTIVE
> >  TESTS +=\
> > @@ -151,3 +153,11 @@ multi_pmem_LDADD = \
> > $(UUID_LIBS) \
> > $(KMOD_LIBS) \
> > ../libutil.a
> > +
> > +list_smart_dimm_SOURCES = \
> > +   list-smart-dimm.c \
> > +   ../util/json.c
> > +list_smart_dimm_LDADD = \
> > +   $(LIBNDCTL_LIB) \
> > +   $(JSON_LIBS) \
> > +   ../libutil.a
> > diff --git a/test/list-smart-dimm.c b/test/list-smart-dimm.c new file
> > mode 100644 index 000..c9982d5
> > --- /dev/null
> > +++ b/test/list-smart-dimm.c
> > @@ -0,0 +1,117 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +struct util_filter_params param;
> > +static int did_fail;
> > +static int jflag = JSON_C_TO_STRING_PRETTY;
> > +
> > +#define fail(fmt, ...) \
> > +do { \
> > +   did_fail = 1; \
> > +   fprintf(stderr, "ndctl-%s:%s:%d: " fmt, \
> > +   VERSION, __func__, __LINE__, ##__VA_ARGS__); \ } while 
> > (0)
> > +
> > +static bool filter_region(struct ndctl_region *region,
> > +   struct util_filter_ctx *ctx)
> > +{
> > +   return true;
> > +}
> > +
> > +static void filter_dimm(struct ndctl_dimm *dimm, struct
> > +util_filter_ctx *ctx) {
> > +   struct list_filter_arg *lfa = ctx->list;
> > +   struct json_object *jdimm;
> > +
> > +   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART))
> > +   return;
> > +   if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SMART_THRESHOLD))
> > +   return;
> > +   if (!ndctl_dimm_is_flag_supported(dimm, ND_SMART_ALARM_VALID))
> > +   return;
> > +
> > +   if (!lfa->jdimms) {
> > +   lfa->jdimms = json_object_new_array();
> > +   if (!lfa->jdimms) {
> > +   fail("\n");
> > +   return;
> > 

RE: [ndctl PATCH v3] ndctl, test: add a new unit test for monitor

2018-07-10 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Tuesday, July 10, 2018 12:46 AM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [ndctl PATCH v3] ndctl, test: add a new unit test for monitor
> 
> On Mon, Jul 9, 2018 at 4:38 AM, Qi, Fuli  wrote:
> > Hi Dan,
> > Thanks for your comments.
> >
> >> -Original Message-
> >> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> >> Sent: Sunday, July 8, 2018 3:56 AM
> >> To: Qi, Fuli/斉 福利 
> >> Cc: linux-nvdimm 
> >> Subject: Re: [ndctl PATCH v3] ndctl, test: add a new unit test for
> >> monitor
> >>
> >> On Fri, Jul 6, 2018 at 10:35 PM, QI Fuli  wrote:
> >> > Add a new unit test to test the following options of the monitor command.
> >> >--dimm
> >> >--bus
> >> >--region
> >> >--namespace
> >> >--logfile
> >> >--config-file
> >> >
> >> > Based-on-patch-by: Yasunori Goto 
> >> > Signed-off-by: QI Fuli 
> >> > ---
> >> > v2 -> v3:
> >> >  - Add filter_obj instead of hard-coded values
> >> > v1 -> v2:
> >> >  - Add init()
> >> >  - Add get_filter_dimm() to get the filter dimms by ndctl list command
> >> >instead of hard-cording
> >> >  - Add sleep to call_notify()
> >> >
> >> >  test/Makefile.am |   3 +-
> >> >  test/monitor.sh  | 134
> >> > +++
> >> >  2 files changed, 136 insertions(+), 1 deletion(-)  create mode
> >> > 100755 test/monitor.sh
> >>
> >> This test is still failing for me, here is the tail of the 
> >> test/test-suite.log
> output:
> >>
> >> + sync
> >> + sleep 3
> >> + check_result
> >> ++ cat /tmp/tmp.dT3TJ4l5IE
> >> + jlog='nmem0: no smart support
> >>
> >
> > My idea of [--dimm option] unit test is try to get the first dimm on
> > bus nfit_test.0, then let monitor to run [-d] option with it, the same as:
> > ndctl monitor -b nfit_test.0 -d nmem0 but in your environment,
> > since the "nmem0" didn't support smart, then the monitor outputted the 
> > following
> error message and stopped.
> 
> Right, the test is fragile if it tries to guess nmemX device names.
> 
> >
> >> no dimms to monitor'
> >
> >> ++ jq .dimm.dev
> >> ++ sort
> >> ++ uniq
> >> ++ sed -e ':loop; N; $!b loop; s/\n/:/g'
> >> ++ sed 's/\"//g'
> >> parse error: Invalid literal at line 1, column 6
> >> + notify_dimms=
> >> + [[ '' == '' ]]
> >> + stop_monitor
> >> + kill 39414
> >> ./monitor.sh: line 48: kill: (39414) - No such process
> >> ++ err 48
> >> +++ basename ./monitor.sh
> >> ++ echo test/monitor.sh: failed at line 48
> >> test/monitor.sh: failed at line 48
> >> ++ '[' -n '' ']'
> >> ++ exit 1
> >> FAIL monitor.sh (exit status: 1)
> >>
> >> I notice errors around get_filter_dimm(). Why is that function
> >> needed, it seems redundant now that $filter_obj is being used?
> >>
> >
> > I am sorry that the "filter_dimm" is not a suitable name, maybe changing it 
> > to
> "monitor_dimm" will be better to understand.
> > This function is used to get dimms to be monitored from "ndctl list -D -b 
> > nfit_test.0
> $1".
> > After "./smart-notify nfit_test.0", get "notify dimms" from output 
> > notifications.
> > Then compare the "monitor dimms" with the "notify dimms", if same that 
> > means the
> unit test option works well.
> > I will add the a filter to make sure that the "monitor dimms" support smart 
> > event
> in next version.
> 
> I'm confused, how does checking for smart event support get around the fact 
> that
> the test is looking for the wrong DIMM names in the first place?
> 

I want to add a helper function "list-smart-dimm", which can list and filter 
all of the smart supported dimms.
The "list-smart-dimm" could have the [-d] [-b] [-n] [-r] options the same as 
list.
For example to test the [-r] option of monitor:
First, we can get the "monitor_dimms = list-smart-dimm -b nfit_test.0 -r 
regionX".
If "monitor_dimms" is NULL, then get the "monitor_dimms" form "list-smart-dimm 
-b nfit_test.1 -r regionX&

[ndctl PATCH v10 1/4] ndctl, monitor: add a new command - monitor

2018-07-09 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and event informations to syslog or a
logfile by setting [--logfile] option. The notifications follow json
format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h  |   1 +
 ndctl/Makefile.am  |   3 +-
 ndctl/lib/libndctl.c   |  82 +++
 ndctl/lib/libndctl.sym |   4 +
 ndctl/libndctl.h   |  10 +
 ndctl/monitor.c| 508 +
 ndctl/ndctl.c  |   1 +
 util/filter.h  |   9 +
 8 files changed, 617 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index d22a379..7dbf223 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index 47e005e..969e4aa 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -1635,6 +1635,88 @@ NDCTL_EXPORT int ndctl_dimm_get_health_eventfd(struct 
ndctl_dimm *dimm)
return dimm->health_eventfd;
 }
 
+NDCTL_EXPORT unsigned int ndctl_dimm_get_health(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int health;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   health = ndctl_cmd_smart_get_health(cmd);
+   ndctl_cmd_unref(cmd);
+   return health;
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int flags;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   dbg(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   dbg(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   flags = ndctl_cmd_smart_get_flags(cmd);
+   ndctl_cmd_unref(cmd);
+   return flags;
+}
+
+NDCTL_EXPORT int ndctl_dimm_is_flag_supported(struct ndctl_dimm *dimm,
+   unsigned int flag)
+{
+   unsigned int flags = ndctl_dimm_get_flags(dimm);
+   return (flags ==  UINT_MAX) ? 0 : !!(flags & flag);
+}
+
+NDCTL_EXPORT unsigned int ndctl_dimm_get_event_flags(struct ndctl_dimm *dimm)
+{
+   struct ndctl_cmd *cmd = NULL;
+   unsigned int alarm_flags, event_flags = 0;
+   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
+   const char *devname = ndctl_dimm_get_devname(dimm);
+
+   cmd = ndctl_dimm_cmd_new_smart(dimm);
+   if (!cmd) {
+   err(ctx, "%s: no smart command support\n", devname);
+   return UINT_MAX;
+   }
+   if (ndctl_cmd_submit(cmd)) {
+   err(ctx, "%s: smart command failed\n", devname);
+   return UINT_MAX;
+   }
+
+   alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
+   if (alarm_flags & ND_SMART_SPARE_TRIP)
+   event_flags |= ND_EVENT_SPARES_REMAINING;
+   if (alarm_flags & ND_SMART_MTEMP_TRIP)
+   event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
+   if (alarm_flags &

[ndctl PATCH v10 3/4] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-07-09 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index cf44260..a5ba9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -184,6 +205,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index ae3d894..9d008d5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v10 2/4] ndctl, monitor: add main ndctl monitor configuration file

2018-07-09 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/Makefile.am  |   5 ++
 ndctl/monitor.c| 115 +
 ndctl/monitor.conf |  41 
 3 files changed, 161 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 7dbf223..ae3d894 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/multi-pmem.c \
 ../test/core.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 700bd22..83f22af 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -19,6 +19,7 @@
 
 static struct monitor {
const char *logfile;
+   const char *config_file;
const char *dimm_event;
bool daemon;
unsigned int event_flags;
@@ -429,6 +430,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->logfile)
+   parse_config(&_monitor->logfile, "logfile", 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[] = {
@@ -441,6 +545,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
"namespace-id", "filter by namespace id"),
OPT_FILENAME('l', "logfile", , "file | sys

[ndctl PATCH v10 4/4] ndctl, documentation: add manpage for monitor

2018-07-09 Thread QI Fuli
This patch is used to add manpage for ndctl monitor command.

Signed-off-by: QI Fuli 
---
 Documentation/ndctl/Makefile.am   |  3 +-
 Documentation/ndctl/ndctl-monitor.txt | 96 +++
 2 files changed, 98 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt

diff --git a/Documentation/ndctl/Makefile.am b/Documentation/ndctl/Makefile.am
index 4fd9636..a30b139 100644
--- a/Documentation/ndctl/Makefile.am
+++ b/Documentation/ndctl/Makefile.am
@@ -46,7 +46,8 @@ man1_MANS = \
ndctl-inject-error.1 \
ndctl-inject-smart.1 \
ndctl-update-firmware.1 \
-   ndctl-list.1
+   ndctl-list.1 \
+   ndctl-monitor.1
 
 CLEANFILES = $(man1_MANS)
 
diff --git a/Documentation/ndctl/ndctl-monitor.txt 
b/Documentation/ndctl/ndctl-monitor.txt
new file mode 100644
index 000..037fdc7
--- /dev/null
+++ b/Documentation/ndctl/ndctl-monitor.txt
@@ -0,0 +1,96 @@
+ndctl-monitor(1)
+
+
+NAME
+
+ndctl-monitor - Monitor the smart events of nvdimm objects
+
+SYNOPSIS
+
+[verse]
+'ndctl monitor' []
+
+DESCRIPTION
+---
+Ndctl monitor is used for monitoring the smart events of nvdimm
+objects and dumping the json format notifications to syslog or
+a logfile.
+
+The objects to monitor and smart evnets to notify can be selected
+by setting options and/or the default configuration file
+(/etc/ndctl/monitor.conf). Both of the values in configuration file
+and in options will work. If conflict, the values in options will
+override the values in configuration file. The changed values in
+configuration file will work after restart ndctl monitor.
+
+EXAMPLES
+
+
+Run a monitor as a daemon to monitor DIMMs on a provider named ndbus1
+[verse]
+ndctl monitor -b ndbus1 -f
+
+Run a monitor as a one-shot command and output the notifications to
+/var/log/ndctl.log
+[verse]
+ndctl monitor -l /var/log/ndctl.log
+
+Run a monitor daemon as a system service
+[verse]
+systemctl start ndctl-monitor.service
+
+OPTIONS
+---
+-b::
+--bus=::
+   Enforce that the operation only be carried on devices that are
+   attached to the given bus. Where 'bus' can be a provider name
+   or a bus id number.
+
+-d::
+--dimm=::
+   A 'nmemX' device name, or dimm id number. Select the devices to
+   monitor refrerence the given dimm.
+
+-r::
+--region=::
+   A 'regionX' device name, or a region id number. The keyword 'all'
+   can be specified to carry out the operation on every region in
+   the system, optionally filtered by bus id (see --bus= option).
+
+-n::
+--namespace=::
+   A 'namespaceX.Y' device name, or namespace region plus id tuple
+   'X.Y'.
+
+-l ::
+--logfile=::
+   Output notifications to  or syslog.
+
+-f::
+--daemon::
+   Run a monitor as a daemon.
+
+-D::
+--dimm-event=::
+   A name of smart events come NVDIMM DIMMs.
+   - "dimm-spares-remaining": Spare Blocks Remaining value has gone
+  below the pre-programmed threshold limit
+   - "dimm-media-temperature": NVDIMM Media temperature value has
+  gone above the pre-programmed threshold limit
+   - "dimm-controller-temperature": NVDIMM Controller temperature
+  value has gone above the pre-programmed threshold limit
+   - "dimm-health-state": NVDIMM Normal Health Status has changed
+   - "dimm-unclean-shutdown": NVDIMM Last Shutdown Status was a dirty
+  shutdown.
+
+COPYRIGHT
+-
+Copyright (c) 2018, FUJITSU LIMITED. License GPLv2: GNU GPL version 2
+<http://gnu.org/licenses/gpl.html>. This is free software: you are
+free to change and redistribute it. There is NO WARRANTY, to the
+extent permitted by law.
+
+SEE ALSO
+
+linkndctl:ndctl-list[1]
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v10 0/4] ndctl, monitor: add ndctl monitor daemon

2018-07-09 Thread QI Fuli
This is the v10 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v9:
 - Replacing ndctl_cmd_smart_get_event_flags() with
   ndctl_dimm_get_event_flags()
 - Adding ndctl_dimm_get_health() api
 - Adding ndctl_dimm_get_flags() api
 - Adding ndctl_dimm_is_flag_supported api
 - Adding manpage

Change log since v8:
 - Adding ndctl_cmd_smart_get_event_flags() api
 - Adding monitor_filter_arg to the union in util_filter_ctx
 - Removing is_dir()
 - Replacing malloc + vsprintf with vasprintf() in log_file() and log_syslog()
 - Adding parse_monitor_event()
 - Refactoring util_dimm_event_filter()
 - Adding event_flags to monitor
 - Refactoring dimm_event_to_json()
 - Adding check_dimm_supported_threshold_alarms()
 - Fixing fail token

Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (4):
  ndctl, monitor: add a new command - monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service
  ndctl, documentation: add manpage for monitor

 Documentation/ndctl/Makefile.am   |   3 +-
 Documentation/ndctl/ndctl-monitor.txt |  96 
 autogen.sh|   3 +-
 builtin.h |   1 +
 configure.ac  |  22 +
 ndctl/Makefile.am |  12 +-
 ndctl/lib/libndctl.c  |  82 
 ndctl/lib/libndctl.sym|   4 +
 ndctl/libndctl.h  |  10 +
 ndctl/monitor.c   | 623 ++
 ndctl/monitor.conf|  41 ++
 ndctl/ndctl-monitor.service   |   7 +
 ndctl/ndctl.c |   1 +
 util/filter.h |   9 +
 14 files changed, 911 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/ndctl/ndctl-monitor.txt
 create mode 100644 ndctl/monitor.c
 create mode 100644 ndctl/monitor.conf
 create mode 100644 ndctl/ndctl-monitor.service

-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH v3] ndctl, test: add a new unit test for monitor

2018-07-09 Thread Qi, Fuli
Hi Dan,
Thanks for your comments.

> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Sunday, July 8, 2018 3:56 AM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [ndctl PATCH v3] ndctl, test: add a new unit test for monitor
> 
> On Fri, Jul 6, 2018 at 10:35 PM, QI Fuli  wrote:
> > Add a new unit test to test the following options of the monitor command.
> >--dimm
> >--bus
> >--region
> >--namespace
> >--logfile
> >--config-file
> >
> > Based-on-patch-by: Yasunori Goto 
> > Signed-off-by: QI Fuli 
> > ---
> > v2 -> v3:
> >  - Add filter_obj instead of hard-coded values
> > v1 -> v2:
> >  - Add init()
> >  - Add get_filter_dimm() to get the filter dimms by ndctl list command
> >instead of hard-cording
> >  - Add sleep to call_notify()
> >
> >  test/Makefile.am |   3 +-
> >  test/monitor.sh  | 134
> > +++
> >  2 files changed, 136 insertions(+), 1 deletion(-)  create mode 100755
> > test/monitor.sh
> 
> This test is still failing for me, here is the tail of the 
> test/test-suite.log output:
> 
> + sync
> + sleep 3
> + check_result
> ++ cat /tmp/tmp.dT3TJ4l5IE
> + jlog='nmem0: no smart support
> 

My idea of [--dimm option] unit test is try to get the first dimm on bus 
nfit_test.0, then let
monitor to run [-d] option with it, the same as:
ndctl monitor -b nfit_test.0 -d nmem0
but in your environment, since the "nmem0" didn't support smart, then the 
monitor outputted the following error message and stopped.

> no dimms to monitor'

> ++ jq .dimm.dev
> ++ sort
> ++ uniq
> ++ sed -e ':loop; N; $!b loop; s/\n/:/g'
> ++ sed 's/\"//g'
> parse error: Invalid literal at line 1, column 6
> + notify_dimms=
> + [[ '' == '' ]]
> + stop_monitor
> + kill 39414
> ./monitor.sh: line 48: kill: (39414) - No such process
> ++ err 48
> +++ basename ./monitor.sh
> ++ echo test/monitor.sh: failed at line 48
> test/monitor.sh: failed at line 48
> ++ '[' -n '' ']'
> ++ exit 1
> FAIL monitor.sh (exit status: 1)
> 
> I notice errors around get_filter_dimm(). Why is that function needed, it 
> seems
> redundant now that $filter_obj is being used?
> 

I am sorry that the "filter_dimm" is not a suitable name, maybe changing it to 
"monitor_dimm" will be better to understand.
This function is used to get dimms to be monitored from "ndctl list -D -b 
nfit_test.0 $1".
After "./smart-notify nfit_test.0", get "notify dimms" from output 
notifications.
Then compare the "monitor dimms" with the "notify dimms", if same that means 
the unit test option works well.
I will add the a filter to make sure that the "monitor dimms" support smart 
event in next version.

> Hmm, if I just delete get_filter_dimm() I get this;
> 
> ++ jq .dimm.dev
> ++ sort
> ++ uniq
> ++ sed -e ':loop; N; $!b loop; s/\n/:/g'
> ++ sed 's/\"//g'
> + notify_dimms=nmem3
> + [[ '' == nmem3 ]]
> ++ err 34
> +++ basename ./monitor.sh
> ++ echo test/monitor.sh: failed at line 34
> test/monitor.sh: failed at line 34
> ++ '[' -n '' ']'
> ++ exit 1
> FAIL monitor.sh (exit status: 1)
> 
> I assume this test is passing for you? Can you try running it inside a 
> virtual machine
> that has a virtual NVDIMM so you can debug the collisions between the 
> nfit_test.0
> bus objects and the ACPI.NFIT ones?
> 

I passed this unit test on a virtual machine, which has a virtual NVDIMM.
Here is the buses and dimms list of my test environment.

$ sudo ndctl list -BD
[
  {
"provider":"nfit_test.0",
"dev":"ndbus1",
"scrub_state":"idle",
"dimms":[
  {
"dev":"nmem1",
"id":"cdab-0a-07e0-feff",
"handle":1,
"phys_id":1
  },
  {
"dev":"nmem3",
"id":"cdab-0a-07e0-fefe",
"handle":257,
"phys_id":3
  },
  {
"dev":"nmem0",
"id":"cdab-0a-07e0-",
"handle":0,
"phys_id":0
  },
  {
"dev":"nmem2",
"id":"cdab-0a-07e0-fffe",
"handle":256,
"phys_id":2
  }
]
  },
  {
"provider":"e820",
"dev":"ndbus0"
  },
  {
"provider":"nfit_test.1",
"dev":"ndbus2",

RE: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor

2018-07-09 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Monday, July 9, 2018 3:27 PM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> 
> On Sun, Jul 8, 2018 at 11:22 PM, Qi, Fuli  wrote:
> >> -Original Message-
> >> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> >> Sent: Monday, July 9, 2018 3:04 PM
> >> To: Qi, Fuli/斉 福利 
> >> Cc: linux-nvdimm 
> >> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> >>
> >> On Sun, Jul 8, 2018 at 9:59 PM, Qi, Fuli  wrote:
> >> >> -Original Message-
> >> >> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> >> >> Sent: Sunday, July 8, 2018 5:06 AM
> >> >> To: Qi, Fuli/斉 福利 
> >> >> Cc: linux-nvdimm 
> >> >> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> >> >>
> >> >> On Thu, Jun 28, 2018 at 7:30 PM, QI Fuli  wrote:
> >> >> > Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
> >> >> > When a smart event fires, monitor will output the notifications
> >> >> > which include dimm health status and evnet informations to
> >> >> > syslog or a logfile by setting [--logfile] option. The
> >> >> > notifications follow json format and can be consumed by log 
> >> >> > collectors like
> Fluentd.
> >> >> >
> >> >> > The objects to monitor can be selected by setting [--dimm]
> >> >> > [--region] [--namespace] [--bus] options and the event type can
> >> >> > be filtered by setting [--dimm-event] option. These options
> >> >> > support multiple space-separated arguments.
> >> >> >
> >> >> > Ndctl monitor can be forked as a daemon by using [--daemon]
> >> >> > option, such as:
> >> >> ># ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log
> >> >> >
> >> >> > Signed-off-by: QI Fuli 
> >> >> > ---
> >> >> >  builtin.h  |   1 +
> >> >> >  ndctl/Makefile.am  |   3 +-
> >> >> >  ndctl/lib/libndctl.sym |   1 +
> >> >> >  ndctl/lib/smart.c  |  17 ++
> >> >> >  ndctl/libndctl.h   |   6 +
> >> >> >  ndctl/monitor.c| 531 
> >> >> > +
> >> >> >  ndctl/ndctl.c  |   1 +
> >> >> >  util/filter.h  |   9 +
> >> >> >  8 files changed, 568 insertions(+), 1 deletion(-)  create mode
> >> >> > 100644 ndctl/monitor.c
> >> >> >
> >> >> > diff --git a/builtin.h b/builtin.h index d3cc723..675a6ce 100644
> >> >> > --- a/builtin.h
> >> >> > +++ b/builtin.h
> >> >> > @@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char
> >> >> > **argv, void *ctx);  int cmd_wait_scrub(int argc, const char
> >> >> > **argv, void *ctx);  int cmd_start_scrub(int argc, const char
> >> >> > **argv, void *ctx); int cmd_list(int argc, const char **argv,
> >> >> > void *ctx);
> >> >> > +int cmd_monitor(int argc, const char **argv, void *ctx);
> >> >> >  #ifdef ENABLE_TEST
> >> >> >  int cmd_test(int argc, const char **argv, void *ctx);  #endif
> >> >> > diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am index
> >> >> > d22a379..7dbf223
> >> >> > 100644
> >> >> > --- a/ndctl/Makefile.am
> >> >> > +++ b/ndctl/Makefile.am
> >> >> > @@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
> >> >> > util/json-smart.c \
> >> >> > util/json-firmware.c \
> >> >> > inject-error.c \
> >> >> > -   inject-smart.c
> >> >> > +   inject-smart.c \
> >> >> > +   monitor.c
> >> >> >
> >> >> >  if ENABLE_DESTRUCTIVE
> >> >> >  ndctl_SOURCES += ../test/blk_namespaces.c \ diff --git
> >> >> > a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym index
> >> >> > e939993..f64df56 100644
> >> >> > --- a/ndctl/lib/libndctl.sym
> >> >> > +++ b/ndctl/li

RE: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor

2018-07-09 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Monday, July 9, 2018 3:04 PM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> 
> On Sun, Jul 8, 2018 at 9:59 PM, Qi, Fuli  wrote:
> >> -Original Message-
> >> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> >> Sent: Sunday, July 8, 2018 5:06 AM
> >> To: Qi, Fuli/斉 福利 
> >> Cc: linux-nvdimm 
> >> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> >>
> >> On Thu, Jun 28, 2018 at 7:30 PM, QI Fuli  wrote:
> >> > Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
> >> > When a smart event fires, monitor will output the notifications
> >> > which include dimm health status and evnet informations to syslog
> >> > or a logfile by setting [--logfile] option. The notifications
> >> > follow json format and can be consumed by log collectors like Fluentd.
> >> >
> >> > The objects to monitor can be selected by setting [--dimm]
> >> > [--region] [--namespace] [--bus] options and the event type can be
> >> > filtered by setting [--dimm-event] option. These options support
> >> > multiple space-separated arguments.
> >> >
> >> > Ndctl monitor can be forked as a daemon by using [--daemon] option,
> >> > such as:
> >> ># ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log
> >> >
> >> > Signed-off-by: QI Fuli 
> >> > ---
> >> >  builtin.h  |   1 +
> >> >  ndctl/Makefile.am  |   3 +-
> >> >  ndctl/lib/libndctl.sym |   1 +
> >> >  ndctl/lib/smart.c  |  17 ++
> >> >  ndctl/libndctl.h   |   6 +
> >> >  ndctl/monitor.c| 531 +
> >> >  ndctl/ndctl.c  |   1 +
> >> >  util/filter.h  |   9 +
> >> >  8 files changed, 568 insertions(+), 1 deletion(-)  create mode
> >> > 100644 ndctl/monitor.c
> >> >
> >> > diff --git a/builtin.h b/builtin.h
> >> > index d3cc723..675a6ce 100644
> >> > --- a/builtin.h
> >> > +++ b/builtin.h
> >> > @@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv,
> >> > void *ctx);  int cmd_wait_scrub(int argc, const char **argv, void
> >> > *ctx);  int cmd_start_scrub(int argc, const char **argv, void
> >> > *ctx); int cmd_list(int argc, const char **argv, void *ctx);
> >> > +int cmd_monitor(int argc, const char **argv, void *ctx);
> >> >  #ifdef ENABLE_TEST
> >> >  int cmd_test(int argc, const char **argv, void *ctx);  #endif diff
> >> > --git a/ndctl/Makefile.am b/ndctl/Makefile.am index
> >> > d22a379..7dbf223
> >> > 100644
> >> > --- a/ndctl/Makefile.am
> >> > +++ b/ndctl/Makefile.am
> >> > @@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
> >> > util/json-smart.c \
> >> > util/json-firmware.c \
> >> > inject-error.c \
> >> > -   inject-smart.c
> >> > +   inject-smart.c \
> >> > +   monitor.c
> >> >
> >> >  if ENABLE_DESTRUCTIVE
> >> >  ndctl_SOURCES += ../test/blk_namespaces.c \ diff --git
> >> > a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym index
> >> > e939993..f64df56 100644
> >> > --- a/ndctl/lib/libndctl.sym
> >> > +++ b/ndctl/lib/libndctl.sym
> >> > @@ -366,4 +366,5 @@ global:
> >> > ndctl_namespace_inject_error2;
> >> > ndctl_namespace_uninject_error2;
> >> > ndctl_cmd_ars_stat_get_flag_overflow;
> >> > +   ndctl_cmd_smart_get_event_flags;
> >> >  } LIBNDCTL_15;
> >> > diff --git a/ndctl/lib/smart.c b/ndctl/lib/smart.c index
> >> > 0455252..90a65d0 100644
> >> > --- a/ndctl/lib/smart.c
> >> > +++ b/ndctl/lib/smart.c
> >> > @@ -101,6 +101,23 @@ NDCTL_EXPORT unsigned int
> >> > ndctl_cmd_smart_threshold_get_temperature(
> >> >
> >> >  smart_cmd_op(smart_threshold_get_supported_alarms, unsigned int,
> >> > 0);
> >> >
> >> > +NDCTL_EXPORT unsigned int ndctl_cmd_smart_get_event_flags(struct
> >> > +ndctl_cmd *cmd)
> >>
> >> My expectation for this ndctl_*_get_event_flags() api was to

RE: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor

2018-07-08 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Sunday, July 8, 2018 5:06 AM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v9 1/3] ndctl, monitor: add ndctl monitor
> 
> On Thu, Jun 28, 2018 at 7:30 PM, QI Fuli  wrote:
> > Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
> > When a smart event fires, monitor will output the notifications which
> > include dimm health status and evnet informations to syslog or a
> > logfile by setting [--logfile] option. The notifications follow json
> > format and can be consumed by log collectors like Fluentd.
> >
> > The objects to monitor can be selected by setting [--dimm] [--region]
> > [--namespace] [--bus] options and the event type can be filtered by
> > setting [--dimm-event] option. These options support multiple
> > space-separated arguments.
> >
> > Ndctl monitor can be forked as a daemon by using [--daemon] option,
> > such as:
> ># ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  builtin.h  |   1 +
> >  ndctl/Makefile.am  |   3 +-
> >  ndctl/lib/libndctl.sym |   1 +
> >  ndctl/lib/smart.c  |  17 ++
> >  ndctl/libndctl.h   |   6 +
> >  ndctl/monitor.c| 531 +
> >  ndctl/ndctl.c  |   1 +
> >  util/filter.h  |   9 +
> >  8 files changed, 568 insertions(+), 1 deletion(-)  create mode 100644
> > ndctl/monitor.c
> >
> > diff --git a/builtin.h b/builtin.h
> > index d3cc723..675a6ce 100644
> > --- a/builtin.h
> > +++ b/builtin.h
> > @@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv,
> > void *ctx);  int cmd_wait_scrub(int argc, const char **argv, void
> > *ctx);  int cmd_start_scrub(int argc, const char **argv, void *ctx);
> > int cmd_list(int argc, const char **argv, void *ctx);
> > +int cmd_monitor(int argc, const char **argv, void *ctx);
> >  #ifdef ENABLE_TEST
> >  int cmd_test(int argc, const char **argv, void *ctx);  #endif diff
> > --git a/ndctl/Makefile.am b/ndctl/Makefile.am index d22a379..7dbf223
> > 100644
> > --- a/ndctl/Makefile.am
> > +++ b/ndctl/Makefile.am
> > @@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
> > util/json-smart.c \
> > util/json-firmware.c \
> > inject-error.c \
> > -   inject-smart.c
> > +   inject-smart.c \
> > +   monitor.c
> >
> >  if ENABLE_DESTRUCTIVE
> >  ndctl_SOURCES += ../test/blk_namespaces.c \ diff --git
> > a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym index
> > e939993..f64df56 100644
> > --- a/ndctl/lib/libndctl.sym
> > +++ b/ndctl/lib/libndctl.sym
> > @@ -366,4 +366,5 @@ global:
> > ndctl_namespace_inject_error2;
> > ndctl_namespace_uninject_error2;
> > ndctl_cmd_ars_stat_get_flag_overflow;
> > +   ndctl_cmd_smart_get_event_flags;
> >  } LIBNDCTL_15;
> > diff --git a/ndctl/lib/smart.c b/ndctl/lib/smart.c index
> > 0455252..90a65d0 100644
> > --- a/ndctl/lib/smart.c
> > +++ b/ndctl/lib/smart.c
> > @@ -101,6 +101,23 @@ NDCTL_EXPORT unsigned int
> > ndctl_cmd_smart_threshold_get_temperature(
> >
> >  smart_cmd_op(smart_threshold_get_supported_alarms, unsigned int, 0);
> >
> > +NDCTL_EXPORT unsigned int ndctl_cmd_smart_get_event_flags(struct
> > +ndctl_cmd *cmd)
> 
> My expectation for this ndctl_*_get_event_flags() api was to have it be:
> 
> ndctl_dimm_get_event_flags()
> 
> ...and with that in place get rid of the 'struct monitor_dimm' object.
> Push everything to be retrieved through api calls against a 'struct 
> ndctl_dimm' object.
> In other words, the usage of 'struct ndctl_cmd'
> should be hidden and all monitor operations should be done in terms of 'struct
> ndctl_dimm' helper calls.
> 
Hi Dan,
Thanks for your comments.

In the v9 of monitor, I use the 'struct ndctl_cmd' object in the following 
places:
ndctl_cmd_smart_get_flags(struct ndctl_cmd *cmd)
ndctl_cmd_smart_get_health(struct ndctl_cmd *cmd)
ndctl_cmd_smart_get_event_flags(struct ndctl_cmd *cmd)
Is it that you want to hide all of the 'struct ndctl_cmd' objects and add the 
following 'struct ndctl_dimm' helper calls?
ndctl_dimm_get_flags(struct ndctl_dimm *dimm)
ndctl_dimm_get_health(struct ndctl_dimm *dimm)
ndctl_dimm_get_event_flags(struct ndctl_dimm *dimm)


> This allows for other objects like regions and namespace to grow an event 
> flags api
> in the future:
> 
>ndctl_namespace_get_event_flags()
>ndctl_region_get_event_flags()
>ndctl_bus_get_event_flags()
> 
> ...where those objects don't have any relationship with a 'struct ndctl_cmd', 
> so
> neither should dimm event monitoring, at least not for the library api that
> ndctl-monitor is using.
> 

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v3] ndctl, test: add a new unit test for monitor

2018-07-06 Thread QI Fuli
Add a new unit test to test the following options of the monitor command.
   --dimm
   --bus
   --region
   --namespace
   --logfile
   --config-file

Based-on-patch-by: Yasunori Goto 
Signed-off-by: QI Fuli 
---
v2 -> v3:
 - Add filter_obj instead of hard-coded values
v1 -> v2:
 - Add init()
 - Add get_filter_dimm() to get the filter dimms by ndctl list command
   instead of hard-cording
 - Add sleep to call_notify()

 test/Makefile.am |   3 +-
 test/monitor.sh  | 134 +++
 2 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100755 test/monitor.sh

diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c76462 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
diff --git a/test/monitor.sh b/test/monitor.sh
new file mode 100755
index 000..4a7d733
--- /dev/null
+++ b/test/monitor.sh
@@ -0,0 +1,134 @@
+#!/bin/bash -Ex
+
+# SPDX-License-Identifier: GPL-2.0
+# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
+
+rc=77
+logfile=""
+conf_file=""
+filter_dimms=""
+filter_obj=""
+monitor_pid=65536
+
+. ./common
+
+trap 'err $LINENO' ERR
+
+check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
+
+start_monitor()
+{
+   logfile=$(mktemp)
+   $NDCTL monitor -l $logfile $1 &
+   monitor_pid=$!
+   truncate --size 0 $logfile #remove startup log
+}
+
+get_filter_dimm()
+{
+   jlist=$($NDCTL list -D -b $NFIT_TEST_BUS0 $1)
+   filter_dimms=$(jq '.[]."dev"?, ."dev"?' <<<$jlist | sort | uniq | sed 
-e ':loop; N; $!b loop; s/\n/:/g' | sed 's/\"//g')
+}
+
+call_notify()
+{
+   ./smart-notify $NFIT_TEST_BUS0
+   sync; sleep 3
+}
+
+check_result()
+{
+   jlog=$(cat $logfile)
+   notify_dimms=$(jq ."dimm"."dev" <<<$jlog | sort | uniq | sed -e ':loop; 
N; $!b loop; s/\n/:/g' | sed 's/\"//g')
+   [[ $filter_dimms == $notify_dimms ]]
+}
+
+stop_monitor()
+{
+   kill $monitor_pid
+   rm $logfile
+}
+
+create_conf_file()
+{
+   conf_file=$(mktemp)
+   echo "dimm = $1" > $conf_file
+   cat $conf_file
+}
+
+test_filter_dimm()
+{
+   filter_obj=$($NDCTL list -D -b $NFIT_TEST_BUS0 | jq -r .[0].dev)
+   get_filter_dimm "-d $filter_obj"
+   start_monitor "-d $filter_obj"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_bus()
+{
+   get_filter_dimm
+   start_monitor "-b $NFIT_TEST_BUS0"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_region()
+{
+   filter_obj=$($NDCTL list -R -b $NFIT_TEST_BUS0 | jq -r .[0].dev)
+   get_filter_dimm "-r $filter_obj"
+   start_monitor "-r $filter_obj"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_namespace()
+{
+   filter_obj=$($NDCTL list -N -b $NFIT_TEST_BUS0 | jq -r .[0].dev)
+   [ -z $filter_obj ] && filter_obj="namespace1.0"
+   $NDCTL create-namespace -r $($NDCTL list -R -b $NFIT_TEST_BUS0 | jq -r 
.[0].dev) -n $filter_obj
+   get_filter_dimm "-n $filter_obj"
+   start_monitor "-n $filter_obj"
+   call_notify
+   check_result
+   stop_monitor
+   $NDCTL destroy-namespace $filter_obj -f
+}
+
+test_conf_file()
+{
+   filter_obj=$($NDCTL list -D -b $NFIT_TEST_BUS0 | jq -r .[0].dev)
+   filter_dimms=$filter_obj
+   create_conf_file  $filter_obj
+   start_monitor "-c $conf_file"
+   call_notify
+   check_result
+   stop_monitor
+   rm $conf_file
+}
+
+do_tests()
+{
+   test_filter_dimm
+   test_filter_bus
+   test_filter_region
+   test_filter_namespace
+   test_conf_file
+}
+
+init()
+{
+   $NDCTL disable-region -b $NFIT_TEST_BUS0 all
+   $NDCTL zero-labels -b $NFIT_TEST_BUS0 all
+   $NDCTL enable-region -b $NFIT_TEST_BUS0 all
+}
+
+modprobe nfit_test
+rc=1
+init
+do_tests
+_cleanup
+exit 0
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH v2] ndctl, test: add a new unit test for monitor

2018-07-06 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Saturday, July 7, 2018 12:58 PM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [ndctl PATCH v2] ndctl, test: add a new unit test for monitor
> 
> On Thu, Jul 5, 2018 at 10:22 PM, QI Fuli  wrote:
> > Add a new unit test to test the following options of the monitor command.
> >--dimm
> >--bus
> >--region
> >--namespace
> >--logfile
> >--config-file
> >
> > Based-on-patch-by: Yasunori Goto 
> > Acked-by: Masayoshi Mizuma 
> > Signed-off-by: QI Fuli 
> > ---
> > v1 -> v2:
> >  - Add init()
> >  - Add get_filter_dimm() to get the filter dimms by ndctl list command
> >instead of hard-cording
> >  - Add sleep to call_notify()
> >
> >  test/Makefile.am |   3 +-
> >  test/monitor.sh  | 131
> > +++
> >  2 files changed, 133 insertions(+), 1 deletion(-)  create mode 100755
> > test/monitor.sh
> >
> > diff --git a/test/Makefile.am b/test/Makefile.am index
> > cd451e9..8c76462 100644
> > --- a/test/Makefile.am
> > +++ b/test/Makefile.am
> > @@ -21,7 +21,8 @@ TESTS =\
> > btt-pad-compat.sh \
> > firmware-update.sh \
> > ack-shutdown-count-set \
> > -   rescan-partitions.sh
> > +   rescan-partitions.sh \
> > +   monitor.sh
> >
> >  check_PROGRAMS =\
> > libndctl \
> > diff --git a/test/monitor.sh b/test/monitor.sh new file mode 100755
> > index 000..dbd2013
> > --- /dev/null
> > +++ b/test/monitor.sh
> > @@ -0,0 +1,131 @@
> > +#!/bin/bash -Ex
> > +
> > +# SPDX-License-Identifier: GPL-2.0
> > +# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
> > +
> > +rc=77
> > +logfile=""
> > +conf_file=""
> > +filter_dimms=""
> > +monitor_pid=65536
> > +FILTER_DIMM="nmem1"
> > +FILTER_REGION="region1"
> > +FILTER_NAMESPACE="namespace1.0"
> > +CONF_FILE_SET_DIMM="nmem1:nmem3"
> 
> I thought this was going to be changed to not use hard coded values?
> For example on my platform nmem1 is on ACPI.NFIT
> 
> # ndctl list -BD -d nmem1
> {
>   "provider":"ACPI.NFIT",
>   "dev":"ndbus1",
>   "dimms":[
> {
>   "dev":"nmem1",
>   "id":"8680-57341200",
>   "handle":2,
>   "phys_id":0
> }
>   ]
> }
> 
> 
> Are you looking to test one of DIMMs on nfit_test.0?
> 
> Why not just do this?
> 
> FILTER_DIMM=$(ndctl list -D -b nfit_test.0 | jq -r .[0].dev) 
> FILTER_REGION=$(ndctl
> list -R -b nfit_test.0 | jq -r .[0].dev) FILTER_NAMESPACE=$(ndctl list -N -b
> nfit_test.0 | jq -r .[0].dev)
> 

OK, I will fix it.
Thank you very much.
QI
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


Re: [ndctl PATCH v2] ndctl, test: add a new unit test for monitor

2018-07-06 Thread QI Fuli

Hi Masa,

Thank you for your comments.

On 7/7/2018 12:08 AM, Masayoshi Mizuma wrote:

Hi Qi,

On 07/06/2018 01:22 AM, QI Fuli wrote:

Add a new unit test to test the following options of the monitor command.
--dimm
--bus
--region
--namespace
--logfile
--config-file

Based-on-patch-by: Yasunori Goto 
Acked-by: Masayoshi Mizuma 

I think this Acked-by is wrong because I'm not a maintainer :-)

I'm sorry for this mistake.

Anyway, the monitor test looks good to me, however, the monitor has also
'-D' option. Could you add '-D' option test? I believe you can emulate
the situation for test by using ndctl inject-smart command.

I tried to test the '-D' option by using inject-smart command,
but I found the controller temperature cannot be injected.
https://lists.01.org/pipermail/linux-nvdimm/2018-June/016370.html

Also, I found the Alarm Trip bits of each dimm in nfit_test emulation
are set as 1 by default and I couldn't find any interface which can set 
them to 0.

BTW, are you preparing the man page of monitor...?
I believe the man page is needed for users.

Yes, I will add it.

Thank you very much.
QI

Thanks,
Masa


Signed-off-by: QI Fuli 
---
v1 -> v2:
  - Add init()
  - Add get_filter_dimm() to get the filter dimms by ndctl list command
instead of hard-cording
  - Add sleep to call_notify()

  test/Makefile.am |   3 +-
  test/monitor.sh  | 131 +++
  2 files changed, 133 insertions(+), 1 deletion(-)
  create mode 100755 test/monitor.sh

diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c76462 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
  
  check_PROGRAMS =\

libndctl \
diff --git a/test/monitor.sh b/test/monitor.sh
new file mode 100755
index 000..dbd2013
--- /dev/null
+++ b/test/monitor.sh
@@ -0,0 +1,131 @@
+#!/bin/bash -Ex
+
+# SPDX-License-Identifier: GPL-2.0
+# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
+
+rc=77
+logfile=""
+conf_file=""
+filter_dimms=""
+monitor_pid=65536
+FILTER_DIMM="nmem1"
+FILTER_REGION="region1"
+FILTER_NAMESPACE="namespace1.0"
+CONF_FILE_SET_DIMM="nmem1:nmem3"
+
+. ./common
+
+trap 'err $LINENO' ERR
+
+check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
+
+start_monitor()
+{
+   logfile=$(mktemp)
+   $NDCTL monitor -l $logfile $1 &
+   monitor_pid=$!
+   truncate --size 0 $logfile #remove startup log
+}
+
+get_filter_dimm()
+{
+   jlist=$($NDCTL list -D -b $NFIT_TEST_BUS0 $1)
+   filter_dimms=$(jq '.[]."dev"?, ."dev"?' <<<$jlist | sort | uniq | sed -e 
':loop; N; $!b loop; s/\n/:/g' | sed 's/\"//g')
+}
+
+call_notify()
+{
+   ./smart-notify $NFIT_TEST_BUS0
+   sync; sleep 3
+}
+
+check_result()
+{
+   jlog=$(cat $logfile)
+   notify_dimms=$(jq ."dimm"."dev" <<<$jlog | sort | uniq | sed -e ':loop; N; 
$!b loop; s/\n/:/g' | sed 's/\"//g')
+   [[ $filter_dimms == $notify_dimms ]]
+}
+
+stop_monitor()
+{
+   kill $monitor_pid
+   rm $logfile
+}
+
+create_conf_file()
+{
+   conf_file=$(mktemp)
+   echo 'dimm=nmem1 nmem3' > $conf_file
+}
+
+test_filter_dimm()
+{
+   get_filter_dimm "-d $FILTER_DIMM"
+   start_monitor "-d $FILTER_DIMM"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_bus()
+{
+   get_filter_dimm
+   start_monitor "-b $NFIT_TEST_BUS0"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_region()
+{
+   get_filter_dimm "-r $FILTER_REGION"
+   start_monitor "-r $FILTER_REGION"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_namespace()
+{
+   $NDCTL create-namespace -r region1 -n $FILTER_NAMESPACE
+   get_filter_dimm "-n $FILTER_NAMESPACE"
+   start_monitor "-n $FILTER_NAMESPACE"
+   call_notify
+   check_result
+   stop_monitor
+   $NDCTL destroy-namespace $FILTER_NAMESPACE -f
+}
+
+test_conf_file()
+{
+   filter_dimms=$CONF_FILE_SET_DIMM
+   create_conf_file
+   start_monitor "-c $conf_file"
+   call_notify
+   check_result
+   stop_monitor
+   rm $conf_file
+}
+
+do_tests()
+{
+   test_filter_dimm
+   test_filter_bus
+   test_filter_region
+   test_filter_namespace
+   test_conf_file
+}
+
+init()
+{
+   $NDCTL disable-region -b $NFIT_TEST_BUS0 all
+   $NDCTL zero-labels -b $NFIT_TEST_BUS0 all
+   $NDCTL enable-region -b $NFIT_TEST_BUS0 all
+}
+
+modprobe nfit_test
+rc=1
+init
+do_tests
+_cleanup
+exit 0






___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH v2] ndctl, test: add a new unit test for monitor

2018-07-05 Thread QI Fuli
Add a new unit test to test the following options of the monitor command.
   --dimm
   --bus
   --region
   --namespace
   --logfile
   --config-file

Based-on-patch-by: Yasunori Goto 
Acked-by: Masayoshi Mizuma 
Signed-off-by: QI Fuli 
---
v1 -> v2:
 - Add init()
 - Add get_filter_dimm() to get the filter dimms by ndctl list command
   instead of hard-cording
 - Add sleep to call_notify()

 test/Makefile.am |   3 +-
 test/monitor.sh  | 131 +++
 2 files changed, 133 insertions(+), 1 deletion(-)
 create mode 100755 test/monitor.sh

diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c76462 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
diff --git a/test/monitor.sh b/test/monitor.sh
new file mode 100755
index 000..dbd2013
--- /dev/null
+++ b/test/monitor.sh
@@ -0,0 +1,131 @@
+#!/bin/bash -Ex
+
+# SPDX-License-Identifier: GPL-2.0
+# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
+
+rc=77
+logfile=""
+conf_file=""
+filter_dimms=""
+monitor_pid=65536
+FILTER_DIMM="nmem1"
+FILTER_REGION="region1"
+FILTER_NAMESPACE="namespace1.0"
+CONF_FILE_SET_DIMM="nmem1:nmem3"
+
+. ./common
+
+trap 'err $LINENO' ERR
+
+check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
+
+start_monitor()
+{
+   logfile=$(mktemp)
+   $NDCTL monitor -l $logfile $1 &
+   monitor_pid=$!
+   truncate --size 0 $logfile #remove startup log
+}
+
+get_filter_dimm()
+{
+   jlist=$($NDCTL list -D -b $NFIT_TEST_BUS0 $1)
+   filter_dimms=$(jq '.[]."dev"?, ."dev"?' <<<$jlist | sort | uniq | sed 
-e ':loop; N; $!b loop; s/\n/:/g' | sed 's/\"//g')
+}
+
+call_notify()
+{
+   ./smart-notify $NFIT_TEST_BUS0
+   sync; sleep 3
+}
+
+check_result()
+{
+   jlog=$(cat $logfile)
+   notify_dimms=$(jq ."dimm"."dev" <<<$jlog | sort | uniq | sed -e ':loop; 
N; $!b loop; s/\n/:/g' | sed 's/\"//g')
+   [[ $filter_dimms == $notify_dimms ]]
+}
+
+stop_monitor()
+{
+   kill $monitor_pid
+   rm $logfile
+}
+
+create_conf_file()
+{
+   conf_file=$(mktemp)
+   echo 'dimm=nmem1 nmem3' > $conf_file
+}
+
+test_filter_dimm()
+{
+   get_filter_dimm "-d $FILTER_DIMM"
+   start_monitor "-d $FILTER_DIMM"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_bus()
+{
+   get_filter_dimm
+   start_monitor "-b $NFIT_TEST_BUS0"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_region()
+{
+   get_filter_dimm "-r $FILTER_REGION"
+   start_monitor "-r $FILTER_REGION"
+   call_notify
+   check_result
+   stop_monitor
+}
+
+test_filter_namespace()
+{
+   $NDCTL create-namespace -r region1 -n $FILTER_NAMESPACE
+   get_filter_dimm "-n $FILTER_NAMESPACE"
+   start_monitor "-n $FILTER_NAMESPACE"
+   call_notify
+   check_result
+   stop_monitor
+   $NDCTL destroy-namespace $FILTER_NAMESPACE -f
+}
+
+test_conf_file()
+{
+   filter_dimms=$CONF_FILE_SET_DIMM
+   create_conf_file
+   start_monitor "-c $conf_file"
+   call_notify
+   check_result
+   stop_monitor
+   rm $conf_file
+}
+
+do_tests()
+{
+   test_filter_dimm
+   test_filter_bus
+   test_filter_region
+   test_filter_namespace
+   test_conf_file
+}
+
+init()
+{
+   $NDCTL disable-region -b $NFIT_TEST_BUS0 all
+   $NDCTL zero-labels -b $NFIT_TEST_BUS0 all
+   $NDCTL enable-region -b $NFIT_TEST_BUS0 all
+}
+
+modprobe nfit_test
+rc=1
+init
+do_tests
+_cleanup
+exit 0
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [ndctl PATCH] ndctl, test: add a new unit test for monitor

2018-07-05 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Friday, July 6, 2018 6:26 AM
> To: Masayoshi Mizuma 
> Cc: Qi, Fuli/斉 福利 ; linux-nvdimm
> 
> Subject: Re: [ndctl PATCH] ndctl, test: add a new unit test for monitor
> 
> On Thu, Jul 5, 2018 at 2:06 PM, Masayoshi Mizuma  
> wrote:
> > Hi Qi,
> >
> > I tried this test after I applied your latest monitor patches [1].
> > I found some points. Could you check the following?
> >
> > [1] https://lists.01.org/pipermail/linux-nvdimm/2018-June/016562.html
> >
> > On 07/05/2018 07:23 AM, QI Fuli wrote:
> [..]
> > Is this "nmem0:nmem1:nmem2:nmem3"?
> > Or why don't you getting the dimms by ndctl list command instead of 
> > hard-cording?
> 
> Yes, this is needed. The test fails for me because I have an e820 defined bus 
> and
> a typical ACPI NFIT bus along with the nfit_test buses on my test system.

Masa, Dan,

Thank you very much for your comments.
I will fix it in next version.

QI
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH] ndctl, test: add a new unit test for monitor

2018-07-05 Thread QI Fuli
Add a new unit test to test the follow options of the monitor command.
   --dimm
   --bus
   --region
   --namespace
   --logfile
   --config-file

Based-on-patch-by: Yasunori Goto 
Signed-off-by: QI Fuli 
---
 test/Makefile.am |   3 +-
 test/monitor.sh  | 115 +++
 2 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100755 test/monitor.sh

diff --git a/test/Makefile.am b/test/Makefile.am
index cd451e9..8c76462 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@ TESTS =\
btt-pad-compat.sh \
firmware-update.sh \
ack-shutdown-count-set \
-   rescan-partitions.sh
+   rescan-partitions.sh \
+   monitor.sh
 
 check_PROGRAMS =\
libndctl \
diff --git a/test/monitor.sh b/test/monitor.sh
new file mode 100755
index 000..4f113c8
--- /dev/null
+++ b/test/monitor.sh
@@ -0,0 +1,115 @@
+#!/bin/bash -Ex
+
+# SPDX-License-Identifier: GPL-2.0
+# Copyright(c) 2018, FUJITSU LIMITED. All rights reserved.
+
+rc=77
+logfile=""
+conf_file=""
+monitor_pid=65536
+FILTER_DIMM="nmem1"
+FILTER_BUS="ndbus1"
+FILTER_BUS_MAP_DIMM="nmem0:nmem1:nmem2:nmem3"
+FILTER_REGION="region1"
+FILTER_REGION_MAP_DIMM="nmem0:nmem1"
+FILTER_NAMESPACE="namespace1.0"
+FILTER_NAMESPACE_MAP_DIMM="nmem0:nmem1"
+CONF_FILE_SET_DIMM="nmem1:nmem3"
+
+. ./common
+
+trap 'err $LINENO' ERR
+
+check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
+
+start_monitor()
+{
+   logfile=$(mktemp)
+   $NDCTL monitor -l $logfile $1 &
+   monitor_pid=$!
+   truncate --size 0 $logfile #remove startup log
+}
+
+call_notify()
+{
+   ./smart-notify $NFIT_TEST_BUS0
+}
+
+check_result()
+{
+   json=$(cat $logfile)
+   dimms=$(jq ."dimm"."dev" <<<$json | sort | uniq | sed -e ':loop; N; $!b 
loop; s/\n/:/g' | sed 's/\"//g')
+   [[ $1 == $dimms ]]
+}
+
+stop_monitor()
+{
+   kill $monitor_pid
+   rm $logfile
+}
+
+create_conf_file()
+{
+   conf_file=$(mktemp)
+   echo 'dimm=nmem1 nmem3' > $conf_file
+   cat $conf_file
+}
+
+test_filter_dimm()
+{
+   start_monitor "-d $FILTER_DIMM"
+   call_notify
+   check_result "$FILTER_DIMM"
+   stop_monitor
+}
+
+test_filter_bus()
+{
+   start_monitor "-b $FILTER_BUS"
+   call_notify
+   check_result $FILTER_BUS_MAP_DIMM
+   stop_monitor
+}
+
+test_filter_region()
+{
+   start_monitor "-r $FILTER_REGION"
+   call_notify
+   check_result $FILTER_REGION_MAP_DIMM
+   stop_monitor
+}
+
+test_filter_namespace()
+{
+   $NDCTL create-namespace -r region1 -n $FILTER_NAMESPACE
+   start_monitor "-n $FILTER_NAMESPACE"
+   call_notify
+   check_result $FILTER_NAMESPACE_MAP_DIMM
+   stop_monitor
+   $NDCTL destroy-namespace $FILTER_NAMESPACE -f
+}
+
+test_conf_file()
+{
+   create_conf_file
+   start_monitor "-c $conf_file"
+   call_notify
+   check_result $CONF_FILE_SET_DIMM
+   stop_monitor
+   rm $conf_file
+}
+
+do_tests()
+{
+   test_filter_dimm
+   test_filter_bus
+   test_filter_region
+   test_filter_namespace
+   test_conf_file
+}
+
+modprobe nfit_test
+rc=1
+do_tests
+_cleanup
+exit 0
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: Question about ndctl unit tests

2018-07-02 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Sunday, July 1, 2018 3:12 AM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm@lists.01.org; Verma, Vishal L ; 
> Mizuma,
> Masayoshi/水間 理仁 ; Gotou, Yasunori/五島 康文
> 
> Subject: Re: Question about ndctl unit tests
> 
> On Sat, Jun 30, 2018 at 1:07 AM, Qi, Fuli  wrote:
> > Hi,
> >
> >
> >
> > When I ran the unit tests, I found "libndctl" and "dsm-fail" cannot
> > pass the test.
> 
> Yes, sorry, we recently reworked some tests. Grab our work-in-progress kernel 
> branch
> here:
> 
> git://git.kernel.org/pub/scm/linux/kernel/git/djbw/nvdimm.git
> libnvdimm-pending
> 
> This will move to nvdimm/nvdimm.git libnvdimm-for-next in the coming weeks 
> for 4.19.
> 
> You'll also want to grab:
> 
> https://patchwork.kernel.org/patch/10495297/
> 
> ...for the error handling tests. This will appear on github.com/pmem/ndctl 
> master
> soon.

OK, I see.
Thank you very much.
Qi
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


Question about ndctl unit tests

2018-06-30 Thread Qi, Fuli
Hi,

When I ran the unit tests, I found "libndctl" and "dsm-fail" cannot pass the 
test.
Here is the test log.

# less test/libndctl.log
libkmod: kmod_module_remove_module: could not remove 'nfit_test': No such file 
or directory
check_set_config_data: dimm: 0 read2 data miscompare: 0
check_set_config_data: dimm: 0x1 read2 data miscompare: 0
check_set_config_data: dimm: 0x100 read2 data miscompare: 0
check_set_config_data: dimm: 0x101 read2 data miscompare: 0
check_dax_autodetect: dax_ndns: 0x1049f10 ndns: 0x1049f10
namespace1.0: expected write_cache enabled
check_namespaces: namespace1.0 validate_write_cache failed
ndctl-test0 failed: -6
libkmod: kmod_module_remove_module: could not remove 'nfit_test': Resource 
temporarily unavailable
libdaxctl: daxctl_unref: context 0x1038170 released
libndctl: ndctl_unref: context 0x1039290 released
FAIL libndctl (exit status: 1)

# less test/dsm-fail.log
victim: nmem0
validate_namespace_options:571: region1: falling back to a 4K alignment
validate_namespace_options:571: region1: falling back to a 4K alignment
do_test:294
libndctl: ndctl_dimm_enable: nmem0: failed to enable
fail expected nmem0 enable success victim: nmem0 rc: -6
libkmod: kmod_module_remove_module: could not remove 'nfit_test': Resource 
temporarily unavailable
libdaxctl: daxctl_unref: context 0x89f170 released
libndctl: ndctl_unref: context 0x8a0290 released
{
  "dev":"namespace1.0",
  "mode":"raw",
  "size":4194304,
  "uuid":"9a07ca1d-54cd-4252-b87e-475b24af8a1c",
  "sector_size":512,
  "blockdev":"pmem1"
}
{
  "dev":"namespace1.1",
  "mode":"raw",
  "size":4194304,
  "uuid":"2eb40a75-5c5f-4ed9-b36e-4fd0abba7783",
  "sector_size":512,
  "blockdev":"pmem1.1"
}
FAIL dsm-fail (exit status: 250)

And my kernel version:
$ uname -a
Linux 4.18.0-rc2+ #25 SMP

I guess the reason is that the nfit_test module cannot be removed.
# modprobe -r nfit_test
modprobe: FATAL: Module nfit_test is in use.
# lsmod | grep nfit_test
nfit_test  40960  8
nd_pmem20480  1 nfit_test
nfit   65536  1 nfit_test
device_dax 20480  2 dax_pmem,nfit_test
libnvdimm 163840  7 
dax_pmem,nfit_test,nd_btt,nd_pmem,nd_e820,nd_blk,nfit
nfit_test_iomap20480  6 
dax_pmem,nfit_test,device_dax,nd_pmem,libnvdimm,nfit

Does anyone know how to fix it?

Thank you very much.
QI
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v9 0/3] ndctl, monitor: add ndctl monitor daemon

2018-06-28 Thread QI Fuli
This is the v9 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v8:
 - Adding ndctl_cmd_smart_get_event_flags() api
 - Adding monitor_filter_arg to the union in util_filter_ctx
 - Removing is_dir()
 - Replacing malloc + vsprintf with vasprintf() in log_file() and log_syslog()
 - Adding parse_monitor_event()
 - Refactoring util_dimm_event_filter()
 - Adding event_flags to monitor
 - Refactoring dimm_event_to_json()
 - Adding check_dimm_supported_threshold_alarms()
 - Fixing fail token

Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (3):
  ndctl, monitor: add ndctl monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service

 autogen.sh  |   3 +-
 builtin.h   |   1 +
 configure.ac|  22 ++
 ndctl/Makefile.am   |  12 +-
 ndctl/lib/libndctl.sym  |   1 +
 ndctl/lib/smart.c   |  17 +
 ndctl/libndctl.h|   6 +
 ndctl/monitor.c | 646 
 ndctl/monitor.conf  |  41 +++
 ndctl/ndctl-monitor.service |   7 +
 ndctl/ndctl.c   |   1 +
 util/filter.h   |   9 +
 12 files changed, 764 insertions(+), 2 deletions(-)
 create mode 100644 ndctl/monitor.c
 create mode 100644 ndctl/monitor.conf
 create mode 100644 ndctl/ndctl-monitor.service

-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v9 3/3] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-06-28 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index cf44260..a5ba9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -184,6 +205,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index ae3d894..9d008d5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.18.0


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v9 2/3] ndctl, monitor: add main ndctl monitor configuration file

2018-06-28 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/Makefile.am  |   5 ++
 ndctl/monitor.c| 115 +
 ndctl/monitor.conf |  41 
 3 files changed, 161 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 7dbf223..ae3d894 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/multi-pmem.c \
 ../test/core.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 c7b9f6a..2940cf7 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -19,6 +19,7 @@
 
 static struct monitor {
const char *logfile;
+   const char *config_file;
const char *dimm_event;
bool daemon;
unsigned int event_flags;
@@ -452,6 +453,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->logfile)
+   parse_config(&_monitor->logfile, "logfile", 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[] = {
@@ -464,6 +568,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
"namespace-id", "filter by namespace id"),
OPT_FILENAME('l', "logfile", , "file | sys

[PATCH v9 1/3] ndctl, monitor: add ndctl monitor

2018-06-28 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and evnet informations to syslog or a
logfile by setting [--logfile] option. The notifications follow json
format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h  |   1 +
 ndctl/Makefile.am  |   3 +-
 ndctl/lib/libndctl.sym |   1 +
 ndctl/lib/smart.c  |  17 ++
 ndctl/libndctl.h   |   6 +
 ndctl/monitor.c| 531 +
 ndctl/ndctl.c  |   1 +
 util/filter.h  |   9 +
 8 files changed, 568 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index d22a379..7dbf223 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym
index e939993..f64df56 100644
--- a/ndctl/lib/libndctl.sym
+++ b/ndctl/lib/libndctl.sym
@@ -366,4 +366,5 @@ global:
ndctl_namespace_inject_error2;
ndctl_namespace_uninject_error2;
ndctl_cmd_ars_stat_get_flag_overflow;
+   ndctl_cmd_smart_get_event_flags;
 } LIBNDCTL_15;
diff --git a/ndctl/lib/smart.c b/ndctl/lib/smart.c
index 0455252..90a65d0 100644
--- a/ndctl/lib/smart.c
+++ b/ndctl/lib/smart.c
@@ -101,6 +101,23 @@ NDCTL_EXPORT unsigned int 
ndctl_cmd_smart_threshold_get_temperature(
 
 smart_cmd_op(smart_threshold_get_supported_alarms, unsigned int, 0);
 
+NDCTL_EXPORT unsigned int ndctl_cmd_smart_get_event_flags(struct ndctl_cmd 
*cmd)
+{
+   unsigned int alarm_flags, event_flags = 0;
+
+   alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
+   if (alarm_flags & ND_SMART_SPARE_TRIP)
+   event_flags |= ND_EVENT_SPARES_REMAINING;
+   if (alarm_flags & ND_SMART_MTEMP_TRIP)
+   event_flags |= ND_EVENT_MEDIA_TEMPERATURE;
+   if (alarm_flags & ND_SMART_CTEMP_TRIP)
+   event_flags |= ND_EVENT_CTRL_TEMPERATURE;
+   if (ndctl_cmd_smart_get_shutdown_state(cmd))
+   event_flags |= ND_EVENT_UNCLEAN_SHUTDOWN;
+
+   return event_flags;
+}
+
 #define smart_cmd_set_op(op) \
 NDCTL_EXPORT int ndctl_cmd_##op(struct ndctl_cmd *cmd, unsigned int val) \
 { \
diff --git a/ndctl/libndctl.h b/ndctl/libndctl.h
index 9270bae..50f057a 100644
--- a/ndctl/libndctl.h
+++ b/ndctl/libndctl.h
@@ -240,6 +240,11 @@ int ndctl_cmd_ars_stat_get_flag_overflow(struct ndctl_cmd 
*ars_stat);
 #define ND_SMART_NON_CRITICAL_HEALTH   (1 << 0)
 #define ND_SMART_CRITICAL_HEALTH   (1 << 1)
 #define ND_SMART_FATAL_HEALTH  (1 << 2)
+#define ND_EVENT_SPARES_REMAINING  (1 << 0)
+#define ND_EVENT_MEDIA_TEMPERATURE (1 << 1)
+#define ND_EVENT_CTRL_TEMPERATURE  (1 << 2)
+#define ND_EVENT_HEALTH_STATE  (1 << 3)
+#define ND_EVENT_UNCLEAN_SHUTDOWN  (1 << 4)
 
 struct ndctl_cmd *ndctl_dimm_cmd_new_smart(struct ndctl_dimm *dimm);
 unsigned int ndctl_cmd_smart_get_flags(struct ndctl_cmd *cmd);
@@ -253,6 +258,7 @@ unsigned int ndctl_cmd_smart_get_life_used(struct ndctl_cmd 
*cmd);
 unsigned int ndctl_cmd_smart_get_shutdown_state(struct ndctl_cmd *cmd);
 unsigned int ndctl_cmd_smart_get_shutdown_count(struct ndctl_cmd *cmd);
 unsigned int ndctl_cmd_smart_get_vendor_size(struct ndctl_cmd *cmd);
+unsigned int ndctl_cmd_smart_get_event_flags(struct ndctl_cmd *cmd);
 unsigned char *ndctl_cmd_smart_get_vendor_data(struct ndctl_cmd *cmd);
 struct ndctl_cmd *ndctl_dimm_cmd_new_smart_threshold(struct ndctl_dimm *dimm);
 unsigned int ndctl_cmd_smart_threshold_get_alarm_control(struct ndctl_cmd 
*cmd);
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
new file mode 100644
index 000..c7b9f6a
--- /dev/n

RE: [PATCH v8 1/3] ndctl, monitor: add ndctl monitor

2018-06-27 Thread Qi, Fuli
> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Thursday, June 28, 2018 12:53 PM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v8 1/3] ndctl, monitor: add ndctl monitor
> 
> On Wed, Jun 27, 2018 at 8:02 PM, Qi, Fuli  wrote:
> > Thanks for your comments.
> >
> 
> Thanks for continuing to work through the feedback.
> 
> >> > +static struct monitor_dimm *util_dimm_event_filter(struct monitor_dimm
> *mdimm,
> >> > +   const char *__ident) {
> >> > +   char *ident, *save;
> >> > +   const char *name;
> >> > +   struct monitor_dimm *__mdimm;
> >> > +   struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(mdimm->dimm);
> >> > +
> >> > +   __mdimm = calloc(1, sizeof(struct monitor_dimm));
> >> > +   if (!__mdimm) {
> >> > +   fail("\n");
> >> > +   return NULL;
> >> > +   }
> >>
> >> Why do we need a dynamic allocation? Can't this just live on the stack?
> >>
> > Actually, I just use the __dimm to compare current dimm health with the 
> > dimm health
> when the monitor started.
> > When the dimm changed, the __dimm should be initialized. I think a dynamic 
> > allocation
> is smarter than live on the stack.
> 
> Allocating memory should always be avoided when not necessary because it's so 
> error
> prone in C. None of the other util_X_filter() routines need to allocate 
> memory, in
> fact the point is to validate that the passed in object passes the filter, 
> not to
> swap the passed in object with a new one.
> 

OK, I see. 

Thank you very much.
Qi
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [PATCH v8 1/3] ndctl, monitor: add ndctl monitor

2018-06-27 Thread Qi, Fuli
Thanks for your comments.

> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Thursday, June 28, 2018 12:09 AM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v8 1/3] ndctl, monitor: add ndctl monitor
> 
> On Tue, Jun 26, 2018 at 10:24 PM, QI Fuli  wrote:
> > Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
> > When a smart event fires, monitor will output the notifications which
> > include dimm health status and evnet informations to syslog or a
> > logfile by setting [--logfile] option. The notifications follow json
> > format and can be consumed by log collectors like Fluentd.
> >
> > The objects to monitor can be selected by setting [--dimm] [--region]
> > [--namespace] [--bus] options and the event type can be filtered by
> > setting [--dimm-event] option. These options support multiple
> > space-separated arguments.
> >
> > Ndctl monitor can be forked as a daemon by using [--daemon] option,
> > such as:
> ># ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  builtin.h |   1 +
> >  ndctl/Makefile.am |   3 +-
> >  ndctl/monitor.c   | 508 ++
> >  ndctl/ndctl.c |   1 +
> >  4 files changed, 512 insertions(+), 1 deletion(-)
> >  create mode 100644 ndctl/monitor.c
> >
> > diff --git a/builtin.h b/builtin.h
> > index d3cc723..675a6ce 100644
> > --- a/builtin.h
> > +++ b/builtin.h
> > @@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void 
> > *ctx);
> >  int cmd_wait_scrub(int argc, const char **argv, void *ctx);
> >  int cmd_start_scrub(int argc, const char **argv, void *ctx);
> >  int cmd_list(int argc, const char **argv, void *ctx);
> > +int cmd_monitor(int argc, const char **argv, void *ctx);
> >  #ifdef ENABLE_TEST
> >  int cmd_test(int argc, const char **argv, void *ctx);
> >  #endif
> > diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
> > index d22a379..7dbf223 100644
> > --- a/ndctl/Makefile.am
> > +++ b/ndctl/Makefile.am
> > @@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
> > util/json-smart.c \
> > util/json-firmware.c \
> > inject-error.c \
> > -   inject-smart.c
> > +   inject-smart.c \
> > +   monitor.c
> >
> >  if ENABLE_DESTRUCTIVE
> >  ndctl_SOURCES += ../test/blk_namespaces.c \
> > diff --git a/ndctl/monitor.c b/ndctl/monitor.c
> > new file mode 100644
> > index 000..d926a81
> > --- /dev/null
> > +++ b/ndctl/monitor.c
> > @@ -0,0 +1,508 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#define BUF_SIZE 2048
> > +
> > +#define DIMM_SPARES_REMAINING  (1 << 0)
> > +#define DIMM_MEDIA_TEMPERATURE (1 << 1)
> > +#define DIMM_CTRL_TEMPERATURE  (1 << 2)
> > +#define DIMM_HEALTH_STATE  (1 << 3)
> > +#define DIMM_UNCLEAN_SHUTDOWN  (1 << 4)
> > +
> > +static struct monitor {
> > +   const char *logfile;
> > +   const char *dimm_event;
> > +   bool daemon;
> > +} monitor;
> > +
> > +struct monitor_dimm {
> > +   struct ndctl_dimm *dimm;
> > +   int health_eventfd;
> > +   unsigned int health;
> > +   unsigned int event_flags;
> > +   struct list_node list;
> > +};
> > +
> > +struct monitor_filter_arg {
> > +   struct list_head dimms;
> > +   int maxfd_dimm;
> > +   int num_dimm;
> > +   unsigned long flags;
> > +};
> > +
> > +struct util_filter_params param;
> > +
> > +static int did_fail;
> > +
> > +#define fail(fmt, ...) \
> > +do { \
> > +   did_fail = 1; \
> > +   err((struct ndctl_ctx *)ctx, "ndctl-%s:%s:%d: " fmt, \
> > +   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
> > +} while (0)
> 
> It is odd that the monitor command emits errors in this format, but no
> other ndctl uses scheme. Also. any print that includes source code
> line numbers is debug in my opinion. Source code line numbers are
> usef

[PATCH v8 2/3] ndctl, monitor: add main ndctl monitor configuration file

2018-06-26 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/Makefile.am  |   5 ++
 ndctl/monitor.c| 115 +
 ndctl/monitor.conf |  41 
 3 files changed, 161 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 7dbf223..ae3d894 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/multi-pmem.c \
 ../test/core.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 d926a81..56a1d0b 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -25,6 +25,7 @@
 
 static struct monitor {
const char *logfile;
+   const char *config_file;
const char *dimm_event;
bool daemon;
 } monitor;
@@ -432,6 +433,109 @@ out:
return 1;
 }
 
+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->logfile)
+   parse_config(&_monitor->logfile, "logfile", 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)
 {
int i;
@@ -445,6 +549,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
"namespace-id", "filter by namespace id"),
OPT_FILENAME('l', "logfile", , "file | syslog",
&

[PATCH v8 1/3] ndctl, monitor: add ndctl monitor

2018-06-26 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
When a smart event fires, monitor will output the notifications which
include dimm health status and evnet informations to syslog or a
logfile by setting [--logfile] option. The notifications follow json
format and can be consumed by log collectors like Fluentd.

The objects to monitor can be selected by setting [--dimm] [--region]
[--namespace] [--bus] options and the event type can be filtered by
setting [--dimm-event] option. These options support multiple
space-separated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option,
such as:
   # ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h |   1 +
 ndctl/Makefile.am |   3 +-
 ndctl/monitor.c   | 508 ++
 ndctl/ndctl.c |   1 +
 4 files changed, 512 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index d22a379..7dbf223 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
new file mode 100644
index 000..d926a81
--- /dev/null
+++ b/ndctl/monitor.c
@@ -0,0 +1,508 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#define BUF_SIZE 2048
+
+#define DIMM_SPARES_REMAINING  (1 << 0)
+#define DIMM_MEDIA_TEMPERATURE (1 << 1)
+#define DIMM_CTRL_TEMPERATURE  (1 << 2)
+#define DIMM_HEALTH_STATE  (1 << 3)
+#define DIMM_UNCLEAN_SHUTDOWN  (1 << 4)
+
+static struct monitor {
+   const char *logfile;
+   const char *dimm_event;
+   bool daemon;
+} monitor;
+
+struct monitor_dimm {
+   struct ndctl_dimm *dimm;
+   int health_eventfd;
+   unsigned int health;
+   unsigned int event_flags;
+   struct list_node list;
+};
+
+struct monitor_filter_arg {
+   struct list_head dimms;
+   int maxfd_dimm;
+   int num_dimm;
+   unsigned long flags;
+};
+
+struct util_filter_params param;
+
+static int did_fail;
+
+#define fail(fmt, ...) \
+do { \
+   did_fail = 1; \
+   err((struct ndctl_ctx *)ctx, "ndctl-%s:%s:%d: " fmt, \
+   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
+} while (0)
+
+static bool is_dir(char *filepath)
+{
+   DIR *dir = opendir(filepath);
+   if (dir) {
+   closedir(dir);
+   return true;
+   }
+   return false;
+}
+
+static void log_syslog(struct ndctl_ctx *ctx, int priority, const char *file,
+   int line, const char *fn, const char *format, va_list args)
+{
+   char *buf;
+
+   buf = malloc(BUF_SIZE);
+   if (!buf) {
+   fail("malloc for log buffer failed\n");
+   return;
+   }
+   vsnprintf(buf, BUF_SIZE, format, args);
+   syslog(priority, "%s\n", buf);
+
+   free(buf);
+   return;
+}
+
+static void log_file(struct ndctl_ctx *ctx, int priority, const char *file,
+   int line, const char *fn, const char *format, va_list args)
+{
+   FILE *f;
+   char *buf, *log_dir;
+
+   buf = malloc(BUF_SIZE);
+   if (!buf) {
+   fail("malloc for log buffer failed\n");
+   return;
+   }
+   vsnprintf(buf, BUF_SIZE, format, args);
+
+   log_dir = dirname(strdup(monitor.logfile));
+   if (!log_dir) {
+   ndctl_set_log_fn(ctx, log_syslog);
+   fail("strdup %s failed\n%s", monitor.logfile, buf);
+   goto end;
+   }
+   if (!is_dir(log_dir)) {
+   ndctl_set_log_fn(ctx, log_syslog);
+   fail("dir: %s is required\n%s", log_dir, buf);
+   goto end;
+   }
+
+   f = fopen(monitor.logfile, "a+");
+   if (!f) {
+   ndctl_set_log_fn(ctx, log_syslog);
+   fail("open logfile %s failed\n%s", monit

[PATCH v8 3/3] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-06-26 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index cf44260..a5ba9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -184,6 +205,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index ae3d894..9d008d5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.17.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v8 0/3] ndctl, monitor: add ndctl monitor daemon

2018-06-26 Thread QI Fuli
This is the v8 patch for ndctl monitor, a tiny daemon to monitor
the smart events of nvdimm DIMMs. Since NVDIMM does not have a
feature like mirroring, if it breaks down, the data will be
impossible to restore. Ndctl monitor daemon will catch the smart
events notify from firmware and outputs notification to logfile,
therefore users can replace NVDIMM before it is completely broken.

Signed-off-by: QI Fuli 
---
Change log since v7:
 - Replacing logreport() with log_file() and log_syslog()
 - Refactoring read_config_file()
 - Replacing set_confile() with parse_config()
 - Fixing the ndctl/ndct.conf file

Change log since v6:
 - Changing License to GPL-2.0
 - Adding event object to output notification
 - Adding [--dimm-event] option to filter notification by event type
 - Rewriting read_config_file()
 - Replacing monitor_dimm_event() with monitor_event()
 - Renaming some variables

Change log since v5:
 - Fixing systemd unit file cannot be installed bug
 - Adding license to ./util/abspath.c

Change log since v4:
 - Adding OPTION_FILENAME to make sure filename is correct
 - Adding configuration file
 - Adding [--config-file] option to override the default configuration
 - Making some options support multiple space-seperated arguments
 - Making systemctl enable ndctl-monitor.service command work
 - Making systemctl restart ndctl-monitor.service command work
 - Making the directory of systemd unit file to be configurable
 - Changing log_file() and log_syslog() to logreport()
 - Changing date format in notification to nanoseconds since epoch
 - Changing select() to epoll()
 - Adding filter_bus() and filter_region()

Change log since v3:
 - Removing create-monitor, show-monitor, list-monitor, destroy-monitor
 - Adding [--daemon] option to run ndctl monitor as a daemon 
 - Using systemd to manage ndctl monitor daemon
 - Replacing filter_monitor_dimm() with filter_dimm()

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (3):
  ndctl, monitor: add ndctl monitor
  ndctl, monitor: add main ndctl monitor configuration file
  ndctl, monitor: add the unit file of systemd for ndctl-monitor service

 autogen.sh  |   3 +-
 builtin.h   |   1 +
 configure.ac|  22 ++
 ndctl/Makefile.am   |  12 +-
 ndctl/monitor.c | 623 
 ndctl/monitor.conf  |  41 +++
 ndctl/ndctl-monitor.service |   7 +
 ndctl/ndctl.c   |   1 +
 8 files changed, 708 insertions(+), 2 deletions(-)
 create mode 100644 ndctl/monitor.c
 create mode 100644 ndctl/monitor.conf
 create mode 100644 ndctl/ndctl-monitor.service

-- 
2.17.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [PATCH v7 2/3] ndctl, monitor: add main ndctl monitor configuration file

2018-06-26 Thread Qi, Fuli
Hi Dan,
Thanks for your comments.

> -Original Message-
> From: Dan Williams [mailto:dan.j.willi...@intel.com]
> Sent: Tuesday, June 26, 2018 1:43 PM
> To: Qi, Fuli/斉 福利 
> Cc: linux-nvdimm 
> Subject: Re: [PATCH v7 2/3] ndctl, monitor: add main ndctl monitor 
> configuration
> file
> 
> On Mon, Jun 18, 2018 at 1:27 AM, 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 by using [--config-file=] option to
> > override this file. The changed value will work after resetart ndctl
> > monitor service.
> 
> The monitor should also re-read the configuration file on SIGHUP. That can be 
> added
> in a follow-on patch.
Ok, I will add it in the second implementation.

> 
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/Makefile.am  |   5 ++
> >  ndctl/monitor.c| 115 +
> >  ndctl/monitor.conf |  42 +
> >  3 files changed, 162 insertions(+)
> >  create mode 100644 ndctl/monitor.conf
> >
> > diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am index
> > 7dbf223..ae3d894 100644
> > --- a/ndctl/Makefile.am
> > +++ b/ndctl/Makefile.am
> > @@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
> >  ../test/multi-pmem.c \
> >  ../test/core.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 12aa499..8ac20ac
> > 100644
> > --- a/ndctl/monitor.c
> > +++ b/ndctl/monitor.c
> > @@ -31,6 +31,7 @@ static enum log_destination {
> >
> >  static struct {
> > const char *logfile;
> > +   const char *config_file;
> > const char *dimm_event;
> > bool daemon;
> >  } monitor;
> > @@ -71,6 +72,19 @@ static bool is_dir(char *filepath)
> > return false;
> >  }
> >
> > +static void set_config(const char **arg, char *val) {
> > +   struct strbuf value = STRBUF_INIT;
> > +   size_t arg_len = *arg ? strlen(*arg) : 0;
> > +
> > +   if (arg_len) {
> > +   strbuf_add(, *arg, arg_len);
> > +   strbuf_addstr(, " ");
> > +   }
> > +   strbuf_addstr(, val);
> > +   *arg = strbuf_detach(, NULL); }
> > +
> >  static void logreport(struct ndctl_ctx *ctx, int priority, const char 
> > *file,
> > int line, const char *fn, const char *format, va_list
> > args)  { @@ -449,6 +463,102 @@ out:
> > return 1;
> >  }
> >
> > +static int read_config_file(struct ndctl_ctx *ctx, const char
> > +*prefix) {
> 
> I think we should have this routine take a pointer to the 'param' and 
> 'monitor'
> structures so that it is clear that it is initializing the same parameters as 
> the
> command line args. This also allows this function to potentially be moved to 
> a new
> source in the future.

Ok, I will refactor it.

> 
> > +   FILE *f;
> > +   int line = 0;
> > +   size_t len = 0;
> > +   char *buf, *value;
> > +   char *config_file = "/etc/ndctl/monitor.conf";
> > +
> > +   buf = (char *)malloc(BUF_SIZE);
> 
> Unnecessary (char *) cast.

Ok, I see.

> 
> > +   if (!buf) {
> > +   fail("malloc read config-file buf error\n");
> > +   goto out;
> > +   }
> > +   if (monitor.config_file) {
> > +   fix_filename(prefix, (const char **)_file);
> > +   config_file = (char *)monitor.config_file;
> > +   }
> > +
> > +   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 (!val

[PATCH v2] ndctl, list: add controller temperature threshold

2018-06-25 Thread QI Fuli
This patch is used for adding controller_temperature_threshold and
alarm_controller_temperature to list. When a dimm-controller-temperature
event fires, users need to know the current controller temperature
threshold value.

Signed-off-by: QI Fuli 
---
 ndctl/util/json-smart.c | 17 +
 1 file changed, 17 insertions(+)

diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c
index 9482b35..6a1f294 100644
--- a/ndctl/util/json-smart.c
+++ b/ndctl/util/json-smart.c
@@ -47,6 +47,18 @@ static void smart_threshold_to_json(struct ndctl_dimm *dimm,
"temperature_threshold", jobj);
}
 
+   if (alarm_control & ND_SMART_CTEMP_TRIP) {
+   unsigned int temp;
+   double t;
+
+   temp = ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
+   t = ndctl_decode_smart_temperature(temp);
+   jobj = json_object_new_double(t);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "controller_temperature_threshold", jobj);
+   }
+
if (alarm_control & ND_SMART_SPARE_TRIP) {
unsigned int spares;
 
@@ -130,12 +142,17 @@ struct json_object *util_dimm_health_to_json(struct 
ndctl_dimm *dimm)
if (flags & ND_SMART_ALARM_VALID) {
unsigned int alarm_flags = ndctl_cmd_smart_get_alarm_flags(cmd);
bool temp_flag = !!(alarm_flags & ND_SMART_TEMP_TRIP);
+   bool ctrl_temp_flag = !!(alarm_flags & ND_SMART_CTEMP_TRIP);
bool spares_flag = !!(alarm_flags & ND_SMART_SPARE_TRIP);
 
jobj = json_object_new_boolean(temp_flag);
if (jobj)
json_object_object_add(jhealth, "alarm_temperature", 
jobj);
 
+   jobj = json_object_new_boolean(ctrl_temp_flag);
+   if (jobj)
+   json_object_object_add(jhealth, 
"alarm_controller_temperature", jobj);
+
jobj = json_object_new_boolean(spares_flag);
if (jobj)
json_object_object_add(jhealth, "alarm_spares", jobj);
-- 
2.17.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


RE: [PATCH] ndctl, list: add controller temperature threshold

2018-06-25 Thread Qi, Fuli
> -Original Message-
> From: Verma, Vishal L [mailto:vishal.l.ve...@intel.com]
> Sent: Tuesday, June 26, 2018 5:09 AM
> To: linux-nvdimm@lists.01.org; Qi, Fuli/斉 福利 
> Subject: Re: [PATCH] ndctl, list: add controller temperature threshold
> 
> On Mon, 2018-06-25 at 17:42 +0900, QI Fuli wrote:
> > This patch adds controller temperature threshold to list.
> > When the dimm-controller-temperature event fires, users need to know
> > the current controller temperature threshold value.
> >
> > Signed-off-by: QI Fuli 
> > ---
> >  ndctl/util/json-smart.c | 12 
> >  1 file changed, 12 insertions(+)
> >
> > diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c index
> > 9482b35..d590b0c 100644
> > --- a/ndctl/util/json-smart.c
> > +++ b/ndctl/util/json-smart.c
> > @@ -47,6 +47,18 @@ static void smart_threshold_to_json(struct
> > ndctl_dimm *dimm,
> > "temperature_threshold", jobj);
> > }
> >
> > +   if (alarm_control & ND_SMART_TEMP_TRIP) {
> 
> I think you want to use ND_SMART_CTEMP_TRIP here.
> 
Yes, I will fix it.

> > +   unsigned int temp;
> > +   double t;
> > +
> > +   temp =
> > ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
> > +   t = ndctl_decode_smart_temperature(temp);
> > +   jobj = json_object_new_double(t);
> > +   if (jobj)
> > +   json_object_object_add(jhealth,
> > +   "controller_temperature_threshold",
> > jobj);
> > +   }
> > +
> > if (alarm_control & ND_SMART_SPARE_TRIP) {
> > unsigned int spares;
> 
> We also seem to be missing from the listing, an alarm_controller_temperature 
> field
> to show whether or not the alarm is enabled. Can you add that as well as part 
> of
> this?
> 
Sure, I will add it in v2.

Thank you very much.
 Qi

> 
> Thanks,
>   -Vishal
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH] ndctl, list: add controller temperature threshold

2018-06-25 Thread QI Fuli
This patch adds controller temperature threshold to list.
When the dimm-controller-temperature event fires, users need to know
the current controller temperature threshold value.

Signed-off-by: QI Fuli 
---
 ndctl/util/json-smart.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c
index 9482b35..d590b0c 100644
--- a/ndctl/util/json-smart.c
+++ b/ndctl/util/json-smart.c
@@ -47,6 +47,18 @@ static void smart_threshold_to_json(struct ndctl_dimm *dimm,
"temperature_threshold", jobj);
}
 
+   if (alarm_control & ND_SMART_TEMP_TRIP) {
+   unsigned int temp;
+   double t;
+
+   temp = ndctl_cmd_smart_threshold_get_ctrl_temperature(cmd);
+   t = ndctl_decode_smart_temperature(temp);
+   jobj = json_object_new_double(t);
+   if (jobj)
+   json_object_object_add(jhealth,
+   "controller_temperature_threshold", jobj);
+   }
+
if (alarm_control & ND_SMART_SPARE_TRIP) {
unsigned int spares;
 
-- 
2.17.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v7 3/3] ndctl, monitor: add the unit file of systemd for ndctl-monitor service

2018-06-18 Thread QI Fuli
This patch adds the systemd unit file for ndctl-monitor service.
The systemd unit directory can be configured by setting environment
variable "--with-systemd-unit-dir[=DIR]".

A monitor daemon can be started as a system service:
   # systemctl start ndctl-monitor.service

Signed-off-by: QI Fuli 
---
 autogen.sh  |  3 ++-
 configure.ac| 22 ++
 ndctl/Makefile.am   |  4 
 ndctl/ndctl-monitor.service |  7 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/ndctl-monitor.service

diff --git a/autogen.sh b/autogen.sh
index 2a52688..21b0e25 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -17,7 +17,8 @@ libdir() {
 
 args="--prefix=/usr \
 --sysconfdir=/etc \
---libdir=$(libdir /usr/lib)"
+--libdir=$(libdir /usr/lib) \
+--with-systemd-unit-dir"
 
 echo
 echo ""
diff --git a/configure.ac b/configure.ac
index 17b5a65..4013599 100644
--- a/configure.ac
+++ b/configure.ac
@@ -140,6 +140,27 @@ AC_CHECK_FUNCS([ \
secure_getenv\
 ])
 
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemd-unit-dir],
+   AS_HELP_STRING([--with-systemd-unit-dir[=DIR]],
+   [Directory for systemd service files]),
+   [],
+   [with_systemd_unit_dir=yes])
+
+if test "x$with_systemd_unit_dir" = "xyes"; then
+   def_systemd_unit_dir=$($PKG_CONFIG --variable=systemdsystemunitdir 
systemd)
+   if test "x$def_systemd_unit_dir" = "x"; then
+   AC_MSG_ERROR([systemd support requested but pkg-config unable 
to query systemd package])
+   with_systemd_unit_dir=no
+   else
+   with_systemd_unit_dir="$def_systemd_unit_dir"
+   fi
+fi
+
+AS_IF([test "x$with_systemd_unit_dir" != "xno"],
+   [AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
+AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != 
"xno"])
+
 my_CFLAGS="\
 -Wall \
 -Wchar-subscripts \
@@ -181,6 +202,7 @@ AC_MSG_RESULT([
 sysconfdir: ${sysconfdir}
 libdir: ${libdir}
 includedir: ${includedir}
+   systemd-unit-dir:   ${systemd_unitdir}
 
 compiler:   ${CC}
 cflags: ${CFLAGS}
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index ae3d894..9d008d5 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -47,3 +47,7 @@ monitor_config_file = monitor.conf
 monitor_configdir = /etc/ndctl/
 monitor_config_DATA = $(monitor_config_file)
 EXTRA_DIST += $(monitor_config_file)
+
+if ENABLE_SYSTEMD_UNIT_DIR
+systemd_unit_DATA = ndctl-monitor.service
+endif
diff --git a/ndctl/ndctl-monitor.service b/ndctl/ndctl-monitor.service
new file mode 100644
index 000..44f9326
--- /dev/null
+++ b/ndctl/ndctl-monitor.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ndctl Monitor Daemon
+
+[Service]
+Type=forking
+ExecStart=/usr/bin/ndctl monitor --daemon
+ExecStop=/bin/kill ${MAINPID}
-- 
2.17.1


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v7 2/3] ndctl, monitor: add main ndctl monitor configuration file

2018-06-18 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 by using [--config-file=] option to
override this file. The changed value will work after resetart ndctl
monitor service.

Signed-off-by: QI Fuli 
---
 ndctl/Makefile.am  |   5 ++
 ndctl/monitor.c| 115 +
 ndctl/monitor.conf |  42 +
 3 files changed, 162 insertions(+)
 create mode 100644 ndctl/monitor.conf

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 7dbf223..ae3d894 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -42,3 +42,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 ../test/multi-pmem.c \
 ../test/core.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 12aa499..8ac20ac 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -31,6 +31,7 @@ static enum log_destination {
 
 static struct {
const char *logfile;
+   const char *config_file;
const char *dimm_event;
bool daemon;
 } monitor;
@@ -71,6 +72,19 @@ static bool is_dir(char *filepath)
return false;
 }
 
+static void set_config(const char **arg, char *val)
+{
+   struct strbuf value = STRBUF_INIT;
+   size_t arg_len = *arg ? strlen(*arg) : 0;
+
+   if (arg_len) {
+   strbuf_add(, *arg, arg_len);
+   strbuf_addstr(, " ");
+   }
+   strbuf_addstr(, val);
+   *arg = strbuf_detach(, NULL);
+}
+
 static void logreport(struct ndctl_ctx *ctx, int priority, const char *file,
int line, const char *fn, const char *format, va_list args)
 {
@@ -449,6 +463,102 @@ out:
return 1;
 }
 
+static int read_config_file(struct ndctl_ctx *ctx, const char *prefix)
+{
+   FILE *f;
+   int line = 0;
+   size_t len = 0;
+   char *buf, *value;
+   char *config_file = "/etc/ndctl/monitor.conf";
+
+   buf = (char *)malloc(BUF_SIZE);
+   if (!buf) {
+   fail("malloc read config-file buf error\n");
+   goto out;
+   }
+   if (monitor.config_file) {
+   fix_filename(prefix, (const char **)_file);
+   config_file = (char *)monitor.config_file;
+   }
+
+   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;
+
+   if (strcmp(buf, "bus") == 0) {
+   set_config((const char **), value);
+   continue;
+   }
+   if (strcmp(buf, "dimm") == 0) {
+   set_config((const char **), value);
+   continue;
+   }
+   if (strcmp(buf, "region") == 0) {
+   set_config((const char **), value);
+   continue;
+   }
+   if (strcmp(buf, "namespace") == 0) {
+   set_config((const char **), value);
+   continue;
+   }
+   if (strcmp(buf, "dimm-event") == 0) {
+   set_config((const char **)_event, value);
+   continue;
+   }
+   if (strcmp(buf, "logfile") == 0) {
+   if (monitor.logfile)
+   continue;
+   set_config((const char **), value);
+   fix_filename(prefix, (const char **));
+   }
+   }
+

[PATCH v7 1/3] ndctl, monitor: add ndctl monitor

2018-06-18 Thread QI Fuli
Ndctl monitor is used for monitoring the smart events of nvdimm DIMMs.
When a smart event fires, monitor will output the notifications which
including dimm health status and event infomations to syslog, stderr
or a logfile by setting [--logfile] option. The notifications follow
json format and can be consumed by log collectors like Fluentd.
DIMMsto monitor can be selected by [--dimm] [--region] [--namespace]
[--bus] options, and event type can be filtered by [--dimm-event] option,
these options support mutiple space-seperated arguments.

Ndctl monitor can be forked as a daemon by using [--daemon] option, like:
   # ndctl monitor --daemon --logfile /var/log/ndctl/monitor.log

Signed-off-by: QI Fuli 
---
 builtin.h |   1 +
 ndctl/Makefile.am |   3 +-
 ndctl/monitor.c   | 532 ++
 ndctl/ndctl.c |   1 +
 4 files changed, 536 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index d3cc723..675a6ce 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_wait_scrub(int argc, const char **argv, void *ctx);
 int cmd_start_scrub(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
+int cmd_monitor(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
 #endif
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index d22a379..7dbf223 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -16,7 +16,8 @@ ndctl_SOURCES = ndctl.c \
util/json-smart.c \
util/json-firmware.c \
inject-error.c \
-   inject-smart.c
+   inject-smart.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
new file mode 100644
index 000..12aa499
--- /dev/null
+++ b/ndctl/monitor.c
@@ -0,0 +1,532 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2018, FUJITSU LIMITED. All rights reserved. */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#define BUF_SIZE 2048
+
+#define DIMM_SPARES_REMAINING  (1 << 0)
+#define DIMM_MEDIA_TEMPERATURE (1 << 1)
+#define DIMM_CTRL_TEMPERATURE  (1 << 2)
+#define DIMM_HEALTH_STATE  (1 << 3)
+#define DIMM_UNCLEAN_SHUTDOWN  (1 << 4)
+
+static enum log_destination {
+   LOG_DESTINATION_STDERR = 1,
+   LOG_DESTINATION_SYSLOG = 2,
+   LOG_DESTINATION_FILE = 3,
+} log_destination = LOG_DESTINATION_SYSLOG;
+
+static struct {
+   const char *logfile;
+   const char *dimm_event;
+   bool daemon;
+} monitor;
+
+struct monitor_dimm {
+   struct ndctl_dimm *dimm;
+   int health_eventfd;
+   unsigned int health;
+   unsigned int event_flags;
+   struct list_node list;
+};
+
+struct monitor_filter_arg {
+   struct list_head dimms;
+   int maxfd_dimm;
+   int num_dimm;
+   unsigned long flags;
+};
+
+struct util_filter_params param;
+
+static int did_fail;
+
+#define fail(fmt, ...) \
+do { \
+   did_fail = 1; \
+   err((struct ndctl_ctx *)ctx, "ndctl-%s:%s:%d: " fmt, \
+   VERSION, __func__, __LINE__, ##__VA_ARGS__); \
+} while (0)
+
+static bool is_dir(char *filepath)
+{
+   DIR *dir = opendir(filepath);
+   if (dir) {
+   closedir(dir);
+   return true;
+   }
+   return false;
+}
+
+static void logreport(struct ndctl_ctx *ctx, int priority, const char *file,
+   int line, const char *fn, const char *format, va_list args)
+{
+   char *log_dir;
+   FILE *f;
+   char *buf = (char *)malloc(BUF_SIZE);
+   vsnprintf(buf, BUF_SIZE, format, args);
+
+   switch (log_destination) {
+   case LOG_DESTINATION_STDERR:
+   fprintf(stderr, "%s\n", buf);
+   goto end;
+
+   case LOG_DESTINATION_SYSLOG:
+   syslog(priority, "%s\n", buf);
+   goto end;
+
+   case LOG_DESTINATION_FILE:
+   log_dir = dirname(strdup(monitor.logfile));
+   if (!log_dir) {
+   log_destination = LOG_DESTINATION_SYSLOG;
+   fail("strdup %s failed\n%s", monitor.logfile, buf);
+   goto end;
+   }
+   if (!is_dir(log_dir)) {
+   log_destination = LOG_DESTINATION_SYSLOG;
+   fail("dir: %s is required\n%s", log_dir, buf);
+   free(log_dir);
+   goto end;
+   }
+   f = fopen(monitor.logfile, "a+");
+   if (!f) {
+   log_destination = LOG_DESTINATIO

  1   2   >