1 /****************************************************************************\
2  *  opts.c - functions for processing sdiag parameters
3  *****************************************************************************
4  *  Produced at Barcelona Supercomputing Center, December 2011
5  *  Written by Alejandro Lucero <alucero@bsc.es>
6  *
7  *  This file is part of Slurm, a resource management program.
8  *  For details, see <https://slurm.schedmd.com/>.
9  *  Please also read the included file: DISCLAIMER.
10  *
11  *  Slurm is free software; you can redistribute it and/or modify it under
12  *  the terms of the GNU General Public License as published by the Free
13  *  Software Foundation; either version 2 of the License, or (at your option)
14  *  any later version.
15  *
16  *  In addition, as a special exception, the copyright holders give permission
17  *  to link the code of portions of this program with the OpenSSL library under
18  *  certain conditions as described in each individual source file, and
19  *  distribute linked combinations including the two. You must obey the GNU
20  *  General Public License in all respects for all of the code used other than
21  *  OpenSSL. If you modify file(s) with this exception, you may extend this
22  *  exception to your version of the file(s), but you are not obligated to do
23  *  so. If you do not wish to do so, delete this exception statement from your
24  *  version.  If you delete this exception statement from all source files in
25  *  the program, then also delete it here.
26  *
27  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
28  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
29  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
30  *  details.
31  *
32  *  You should have received a copy of the GNU General Public License along
33  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
34  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
35 \*****************************************************************************/
36 
37 #define _GNU_SOURCE
38 
39 #include <getopt.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #include "src/common/xstring.h"
44 #include "src/common/proc_args.h"
45 
46 #include "sdiag.h"
47 
48 #define OPT_LONG_USAGE 0x101
49 
50 static void  _help( void );
51 static void  _usage( void );
52 
_opt_env(void)53 static void _opt_env(void)
54 {
55 	char *env_val;
56 
57 	if ((env_val = getenv("SLURM_CLUSTERS"))) {
58 		if (!(params.clusters = slurmdb_get_info_cluster(env_val))) {
59 			print_db_notok(env_val, 1);
60 			exit(1);
61 		}
62 	}
63 }
64 
65 /*
66  * parse_command_line, fill in params data structure with data
67  */
parse_command_line(int argc,char ** argv)68 extern void parse_command_line(int argc, char **argv)
69 {
70 	int opt_char;
71 	int option_index;
72 	static struct option long_options[] = {
73 		{"all",		no_argument,	0,	'a'},
74 		{"help",	no_argument,	0,	'h'},
75 		{"reset",	no_argument,	0,	'r'},
76 		{"sort-by-id",	no_argument,	0,	'i'},
77 		{"cluster",     required_argument, 0,   'M'},
78 		{"clusters",    required_argument, 0,   'M'},
79 		{"sort-by-time",no_argument,	0,	't'},
80 		{"sort-by-time2",no_argument,	0,	'T'},
81 		{"usage",	no_argument,	0,	OPT_LONG_USAGE},
82 		{"version",     no_argument,	0,	'V'},
83 		{NULL,		0,		0,	0}
84 	};
85 
86 	/* default options */
87 	params.mode = STAT_COMMAND_GET;
88 	params.sort = SORT_COUNT;
89 
90 	/* get defaults from environment */
91 	_opt_env();
92 
93 	while ((opt_char = getopt_long(argc, argv, "ahiM:rtTV", long_options,
94 				       &option_index)) != -1) {
95 		switch (opt_char) {
96 			case (int)'?':
97 				fprintf(stderr,
98 					"Try \"sdiag --help\" for more information\n");
99 				exit(1);
100 				break;
101 			case (int)'a':
102 				params.mode = STAT_COMMAND_GET;
103 				break;
104 			case (int)'h':
105 				_help();
106 				exit(0);
107 				break;
108 			case (int)'i':
109 				params.sort = SORT_ID;
110 				break;
111 			case (int)'M':
112 				if (params.clusters)
113 					FREE_NULL_LIST(params.clusters);
114 				if (!(params.clusters = slurmdb_get_info_cluster(optarg))) {
115 					print_db_notok(optarg, 0);
116 					exit(1);
117 				}
118 				break;
119 			case (int)'r':
120 				params.mode = STAT_COMMAND_RESET;
121 				break;
122 			case (int)'t':
123 				params.sort = SORT_TIME;
124 				break;
125 			case (int)'T':
126 				params.sort = SORT_TIME2;
127 				break;
128 			case (int) 'V':
129 				print_slurm_version();
130 				exit(0);
131 				break;
132 			case (int)OPT_LONG_USAGE:
133 				_usage();
134 				exit(0);
135 				break;
136 		}
137 	}
138 
139 	if (params.clusters) {
140 		if (list_count(params.clusters) > 1) {
141 			fatal("Only one cluster can be used at a time with sdiag");
142 		}
143 		working_cluster_rec = list_peek(params.clusters);
144 	}
145 }
146 
147 
_usage(void)148 static void _usage( void )
149 {
150 	printf("Usage: sdiag [-M cluster] [-aritT] \n");
151 }
152 
_help(void)153 static void _help( void )
154 {
155 	printf ("\
156 Usage: sdiag [OPTIONS]\n\
157   -a, --all           all statistics\n\
158   -r, --reset         reset statistics\n\
159   -M, --cluster       direct the request to a specific cluster\n\
160   -i, --sort-by-id    sort RPCs by id\n\
161   -t, --sort-by-time  sort RPCs by total run time\n\
162   -T, --sort-by-time2 sort RPCs by average run time\n\
163   -V, --version       display current version number\n\
164 \nHelp options:\n\
165   --help          show this help message\n\
166   --usage         display brief usage message\n");
167 }
168