From patchwork Wed Jul 11 03:00:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: QI Fuli X-Patchwork-Id: 10518725 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 65D4F6032C for ; Wed, 11 Jul 2018 03:01:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B22728BF8 for ; Wed, 11 Jul 2018 03:01:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D45E28C18; Wed, 11 Jul 2018 03:01:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AF44C28C38 for ; Wed, 11 Jul 2018 03:01:10 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A8CA020968910; Tue, 10 Jul 2018 20:01:10 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=202.219.69.171; helo=mgwkm04.jp.fujitsu.com; envelope-from=qi.fuli@jp.fujitsu.com; receiver=linux-nvdimm@lists.01.org Received: from mgwkm04.jp.fujitsu.com (mgwkm04.jp.fujitsu.com [202.219.69.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id D9E0421A143EF for ; Tue, 10 Jul 2018 20:01:08 -0700 (PDT) Received: from kw-mxoi2.gw.nic.fujitsu.com (unknown [192.168.231.133]) by mgwkm04.jp.fujitsu.com with smtp id 5a66_5266_858cfd2c_543d_42fd_8772_455a4033943b; Wed, 11 Jul 2018 12:01:00 +0900 Received: from m3050.s.css.fujitsu.com (msm.b.css.fujitsu.com [10.134.21.208]) by kw-mxoi2.gw.nic.fujitsu.com (Postfix) with ESMTP id 3AAABAC00F0 for ; Wed, 11 Jul 2018 12:00:59 +0900 (JST) Received: from qi-fedora.fujitsu.com (unknown [10.124.196.110]) by m3050.s.css.fujitsu.com (Postfix) with ESMTP id 1A4B4182; Wed, 11 Jul 2018 12:00:59 +0900 (JST) From: QI Fuli To: linux-nvdimm@lists.01.org Subject: [ndctl PATCH v11 2/5] ndctl, monitor: add main ndctl monitor configuration file Date: Wed, 11 Jul 2018 12:00:09 +0900 Message-Id: <20180711030012.9186-3-qi.fuli@jp.fujitsu.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180711030012.9186-1-qi.fuli@jp.fujitsu.com> References: <20180711030012.9186-1-qi.fuli@jp.fujitsu.com> X-TM-AS-MML: disable X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP 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(&value, *arg, arg_len); + strbuf_addstr(&value, " "); + } + strbuf_addstr(&value, val); + *arg = strbuf_detach(&value, 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", &monitor.logfile, "file | syslog", "where to output the monitor's notification"), + OPT_FILENAME('c', "config-file", &monitor.config_file, + "config-file", "override the default config"), OPT_BOOLEAN('f', "daemon", &monitor.daemon, "run ndctl monitor as a daemon"), OPT_STRING('D', "dimm-event", &monitor.dimm_event, @@ -471,6 +577,15 @@ int cmd_monitor(int argc, const char **argv, void *ctx) ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_syslog); ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE); + if (read_config_file((struct ndctl_ctx *)ctx, &monitor, ¶m)) + goto out; + + if (monitor.logfile && (strcmp(monitor.logfile, "./syslog") != 0) + && (strcmp(monitor.logfile, "syslog") != 0)) { + fix_filename(prefix, (const char **)&monitor.logfile); + ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_file); + } + if (monitor.daemon) { if (daemon(0, 0) != 0) { err((struct ndctl_ctx *)ctx, "daemon start failed\n"); diff --git a/ndctl/monitor.conf b/ndctl/monitor.conf new file mode 100644 index 0000000..5c10a8d --- /dev/null +++ b/ndctl/monitor.conf @@ -0,0 +1,41 @@ +# This is the main ndctl monitor configuration file. It contains the +# configuration directives that give ndctl monitor instructions. +# You can change the configuration of ndctl monitor by editing this +# file or using [--config-file=] option to override this one. +# The changed value will work after restart ndctl monitor service. + +# In this file, lines starting with a hash (#) are comments. +# The configurations should follow = style. +# Multiple space-separated values are allowed, but except the following +# characters: : ? / \ % " ' $ & ! * { } [ ] ( ) = < > @ + +# The objects to monitor are filtered via dimm's name by setting key "dimm". +# If this value is different from the value of [--dimm=] option, +# both of the values will work. +# dimm = all + +# The objects to monitor are filtered via its parent bus by setting key "bus". +# If this value is different from the value of [--bus=] option, +# both of the values will work. +# bus = all + +# The objects to monitor are filtered via region by setting key "region". +# If this value is different from the value of [--region=] option, +# both of the values will work. +# region = all + +# The objects to monitor are filtered via namespace by setting key "namespace". +# If this value is different from the value of [--namespace=] option, +# both of the values will work. +# namespace = all + +# The DIMM events to monitor are filtered via event type by setting key +# "dimm-event". If this value is different from the value of +# [--dimm-event=] option, both of the values will work. +# dimm-event = all + +# Users can choose to output the notifications to syslog (logfile=syslog) +# or to write into a special file (logfile=) by setting key "logfile". +# If this value is in conflict with the value of [--logfile=] option, +# this value will be ignored. +# logfile = /var/log/ndctl/monitor.log