Whamcloud - gitweb
LU-17205 utils: add lctl get_param -H option 30/52730/6
authorAurelien Degremont <adegremont@nvidia.com>
Tue, 17 Oct 2023 13:07:45 +0000 (15:07 +0200)
committerOleg Drokin <green@whamcloud.com>
Wed, 8 Nov 2023 22:05:36 +0000 (22:05 +0000)
- Add a new '-H' option to 'lctl get_param' that will prefix
each output line with the parameter name instead of only
the first line by default.

That makes grepping lctl get_param with wildcards much easier
as you can now easily know which parameter returns which value.

  $ lctl get_param -H osc.*.state | grep current
  osc.lustre-OST0000-osc-ff1148c0.state=current_state: FULL
  osc.lustre-OST0001-osc-ff1248c0.state=current_state: DISCONN
  osc.lustre-OST0002-osc-ff1348c0.state=current_state: FULL
  osc.lustre-OST0003-osc-ff1448c0.state=current_state: FULL
  osc.lustre-OST0004-osc-ff1548c0.state=current_state: FULL

It also prints an output line even for empty values. That also
makes like easier for admins.

- The patch also removes the force line feed if the parameter
value was larger than 80 chars. This was considered a misfeature
and is now drop for all usages, with or without -H.

Test-Parameters: trivial
Signed-off-by: Aurelien Degremont <adegremont@nvidia.com>
Change-Id: Ib1fa0dc400db4c19fed10ad4cced9be5668418e3
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52730
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Alexander Boyko <alexander.boyko@hpe.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/doc/lctl-get_param.8
lustre/utils/lctl.c
lustre/utils/lctl_thread.h
lustre/utils/lustre_cfg.c

index 97d6879..9ea1529 100644 (file)
@@ -3,7 +3,7 @@
 lctl-get_param \- retrieve configuration parameters
 .SH SYNOPSIS
 .br
-.IR "\fBlctl get_param " [ -F "] [" -n | -N "] [" -R "] <" parameter ...>
+.IR "\fBlctl get_param " [ -F "] [" -H "] [" -n | -N "] [" -R "] <" parameter ...>
 .br
 .SH DESCRIPTION
 Get the value of the named Lustre or LNet
@@ -36,6 +36,11 @@ parameters, respectively.
 is equivalent to
 .BR "lctl list_param -F" .
 .TP
+.B -H
+Prefix each parameter value line with the parameter name, as a header. It
+also print a line for empty values. It could be useful when wildcards are
+used and filtering the output.
+.TP
 .B -n
 Print only the parameter value and not parameter name.  This may be confusing
 if multiple parameter names are specified, as the parameters are not
index 6502d28..825a2be 100644 (file)
@@ -190,6 +190,7 @@ command_t cmdlist[] = {
         "The path can contain shell-style filename patterns.\n"
         "  -F  When -N specified, add '/', '@' or '=' for directories,\n"
         "      symlinks and writeable files, respectively.\n"
+        "  -H  Prefix each output line with the parameter name.\n"
         "  -n  Print only the value and not parameter name.\n"
         "  -N  Print only matched parameter names and not the values.\n"
         "      (Especially useful when using patterns.)\n"
index 12f70ce..c9f32af 100644 (file)
@@ -46,6 +46,7 @@ struct param_opts {
        unsigned int po_file:1;
        unsigned int po_yaml:1;
        unsigned int po_detail:1;
+       unsigned int po_header:1;
        unsigned int po_parallel_threads;
 };
 #define popt_is_parallel(popt) ((popt).po_parallel_threads > 0)
index df3fd3d..3a47aa0 100644 (file)
@@ -816,17 +816,35 @@ read_param(const char *path, const char *param_name, struct param_opts *popt)
                goto free_buf;
        }
        /* don't print anything for empty files */
-       if (buf[0] == '\0')
+       if (buf[0] == '\0') {
+               if (popt->po_header)
+                       printf("%s=\n", param_name);
                goto free_buf;
+       }
+
+       if (popt->po_header) {
+               char *oldbuf = buf;
+               char *next;
+
+               do {
+                       /* Split at first \n, if any */
+                       next = strchrnul(oldbuf, '\n');
+
+                       printf("%s=%.*s\n", param_name, (int)(next - oldbuf),
+                              oldbuf);
+
+                       buflen -= next - oldbuf + 1;
+                       oldbuf = next + 1;
 
-       if (popt->po_show_path) {
-               bool longbuf;
+               } while (buflen > 0);
 
-               longbuf = memchr(buf, '\n', buflen - 1) ||
-                         buflen + strlen(param_name) >= 80;
-               printf("%s=%s", param_name, longbuf ? "\n" : "");
+       } else if (popt->po_show_path) {
+               bool multilines = memchr(buf, '\n', buflen - 1);
+
+               printf("%s=%s%s", param_name, multilines ? "\n" : "", buf);
+       } else {
+               printf("%s", buf);
        }
-       printf("%s", buf);
 
 free_buf:
        free(buf);
@@ -1367,11 +1385,14 @@ static int getparam_cmdline(int argc, char **argv, struct param_opts *popt)
 
        popt->po_show_path = 1;
 
-       while ((ch = getopt(argc, argv, "FnNRy")) != -1) {
+       while ((ch = getopt(argc, argv, "FHnNRy")) != -1) {
                switch (ch) {
                case 'F':
                        popt->po_show_type = 1;
                        break;
+               case 'H':
+                       popt->po_header = 1;
+                       break;
                case 'n':
                        popt->po_show_path = 0;
                        break;