From: QI Fuli <[email protected]>

Currently, ndctl monitor only can write the smart event to log.
If users do not read the log on time or the log analysis tools miss
the smart event information, it will cause huge losses.

For example, if the smart health event dimm-spares-remaining takes
place and users do not back up the data on time, it is largely
possible to lose the data. If a bash script used to back up
can be called, this problem will be avoided.

This patch adds a new argument named "run" to ndctl monitor command
to set a bash script. When a smart event is monitored, the bash
script can be run immediately.

The following is the sample code for implementation. If it makes
sense, I will continue to work on the patch. I will appreciate
any comments.

Signed-off-by: QI Fuli <[email protected]>
---
 ndctl/monitor.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index e944c90..ff4513a 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -38,6 +38,7 @@ static struct monitor {
        unsigned int poll_timeout;
        unsigned int event_flags;
        struct log_ctx ctx;
+       const char *run;
 } monitor;
 
 struct monitor_dimm {
@@ -389,6 +390,8 @@ static int monitor_event(struct ndctl_ctx *ctx,
                for (i = 0; i < nfds; i++) {
                        mdimm = events[i].data.ptr;
                        if (util_dimm_event_filter(mdimm, monitor.event_flags)) 
{
+                               rc = system(monitor.run);
+                               ...
                                rc = notify_dimm_event(mdimm);
                                if (rc) {
                                        err(&monitor, "%s: notify dimm event 
failed\n",
@@ -546,6 +549,7 @@ static int parse_monitor_config(const struct config 
*configs,
                set_monitor_conf(&param.region, "region", value, seek);
                set_monitor_conf(&param.namespace, "namespace", value, seek);
                set_monitor_conf(&monitor.dimm_event, "dimm-event", value, 
seek);
+               set_monitor_conf(&monitor.run, "run", value, seek);
 
                if (!monitor.log)
                        set_monitor_conf(&monitor.log, "log", value, seek);
@@ -581,6 +585,8 @@ int cmd_monitor(int argc, const char **argv, struct 
ndctl_ctx *ctx)
                                "emit extra debug messages to log"),
                OPT_UINTEGER('p', "poll", &monitor.poll_timeout,
                             "poll and report events/status every <n> seconds"),
+               OPT_STRING('\0', "run", &monitor.run, "bash script",
+                       "run a script when the smart event is monitored"),
                OPT_END(),
        };
        const char * const u[] = {
@@ -598,6 +604,7 @@ int cmd_monitor(int argc, const char **argv, struct 
ndctl_ctx *ctx)
                CONF_STR("monitor:dimm", &param.dimm, NULL),
                CONF_STR("monitor:namespace", &param.namespace, NULL),
                CONF_STR("monitor:dimm-event", &monitor.dimm_event, NULL),
+               CONF_STR("monitor:run", &monitor.run, NULL),
                CONF_END(),
        };
        const char *prefix = "./";
@@ -646,6 +653,9 @@ int cmd_monitor(int argc, const char **argv, struct 
ndctl_ctx *ctx)
                }
        }
 
+       if (monitor.run && (strncmp(monitor.run, "./", 2) != 0))
+               fix_filename(prefix, (const char **)&monitor.run);
+
        if (monitor.daemon) {
                if (!monitor.log || strncmp(monitor.log, "./", 2) == 0)
                        monitor.ctx.log_fn = log_syslog;
-- 
2.31.1


Reply via email to