-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsge_share_mon.cc
380 lines (327 loc) · 11.4 KB
/
sge_share_mon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Portions of this software are Copyright (c) 2023-2024 HPC-Gridware GmbH
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include "uti/sge_dstring.h"
#include "uti/sge_language.h"
#include "uti/sge_profiling.h"
#include "uti/sge_rmon_macros.h"
#include "uti/sge_stdio.h"
#include "uti/sge_component.h"
#include "gdi/ocs_gdi_ClientBase.h"
#include "gdi/ocs_gdi_Request.h"
#include "sgeobj/sge_answer.h"
#include "sgeobj/cull/sge_all_listsL.h"
#include "sgeobj/sge_schedd_conf.h"
#include "sched/sge_sharetree_printing.h"
#include "msg_smon.h"
static int
free_lists(lList **sharetree, lList **users, lList **projects, lList **usersets, lList **config)
{
lFreeList(sharetree);
lFreeList(users);
lFreeList(projects);
lFreeList(usersets);
lFreeList(config);
return 0;
}
static int
setup_lists(lList **sharetree, lList **users, lList **projects, lList **usersets, lList **config)
{
lList *alp = nullptr; /* answer list for individual gdi_multi */
lEnumeration *what;
ocs::gdi::Request gdi_multi{};
int sharetree_id = 0;
int sched_conf_id = 0;
int user_id = 0;
int project_id = 0;
int userset_id = 0;
bool error;
/* get share tree */
what = lWhat("%T(ALL)", STN_Type);
sharetree_id = gdi_multi.request(&alp, ocs::Mode::RECORD, ocs::gdi::Target::TargetValue::SGE_STN_LIST, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, nullptr, nullptr, what, true);
lFreeWhat(&what);
error = answer_list_output(&alp);
/* get config list */
if (!error) {
what = lWhat("%T(ALL)", SC_Type);
sched_conf_id = gdi_multi.request(&alp, ocs::Mode::RECORD, ocs::gdi::Target::TargetValue::SGE_SC_LIST, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, nullptr, nullptr, what, true);
lFreeWhat(&what);
error = answer_list_output(&alp);
}
/* get user list */
if (!error) {
what = lWhat("%T(ALL)", UU_Type);
user_id = gdi_multi.request(&alp, ocs::Mode::RECORD, ocs::gdi::Target::SGE_UU_LIST, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, nullptr, nullptr, what, true);
lFreeWhat(&what);
error = answer_list_output(&alp);
}
/* get project list */
if (!error) {
what = lWhat("%T(ALL)", PR_Type);
project_id = gdi_multi.request(&alp, ocs::Mode::RECORD, ocs::gdi::Target::SGE_PR_LIST, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, nullptr, nullptr, what, true);
lFreeWhat(&what);
error = answer_list_output(&alp);
}
/*
* get userset list
* send gdi multi request to qmaster
*/
if (!error) {
what = lWhat("%T(ALL)", US_Type);
userset_id = gdi_multi.request(&alp, ocs::Mode::SEND, ocs::gdi::Target::SGE_US_LIST, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, nullptr, nullptr, what, true);
gdi_multi.wait();
lFreeWhat(&what);
error = answer_list_output(&alp);
}
/* if any of the above operations failed, exit */
if (error) {
exit(3);
}
/* extract the sharetree lists */
gdi_multi.get_response(&alp, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, ocs::gdi::Target::SGE_STN_LIST, sharetree_id, sharetree);
error = answer_list_output(&alp);
/* if we have no sharetree, output message and exit */
if (!error) {
if (!*sharetree || lFirst(*sharetree) == nullptr) {
fprintf(stderr, "%s\n", MSG_SGESHAREMON_NOSHARETREE);
lFreeList(sharetree);
exit(2);
}
}
/* extract sched_conf, users, projects, usersets */
if (!error) {
gdi_multi.get_response(&alp, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, ocs::gdi::Target::SGE_SC_LIST, sched_conf_id, config);
error = answer_list_output(&alp);
}
if (!error) {
gdi_multi.get_response(&alp, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, ocs::gdi::Target::SGE_UU_LIST, user_id, users);
error = answer_list_output(&alp);
}
if (!error) {
gdi_multi.get_response(&alp, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, ocs::gdi::Target::SGE_PR_LIST, project_id, projects);
error = answer_list_output(&alp);
}
if (!error) {
gdi_multi.get_response(&alp, ocs::gdi::Command::SGE_GDI_GET, ocs::gdi::SubCommand::SGE_GDI_SUB_NONE, ocs::gdi::Target::SGE_US_LIST, userset_id, usersets);
error = answer_list_output(&alp);
}
/* if any of the above operations failed, exit */
if (error) {
free_lists(sharetree, users, projects, usersets, config);
exit(3);
}
return 0;
}
static void
usage()
{
fprintf(stderr, "%s sge_share_mon [-cdfhilmnorsux] [node_names ...]\n", MSG_USAGE);
fprintf(stderr, "\n" );
fprintf(stderr, " -c count %s\n", MSG_SGESHAREMON_c_OPT_USAGE );
fprintf(stderr, " -d delimiter %s\n", MSG_SGESHAREMON_d_OPT_USAGE );
fprintf(stderr, " -f field[,field] %s\n", MSG_SGESHAREMON_f_OPT_USAGE );
fprintf(stderr, " -h %s\n", MSG_SGESHAREMON_h_OPT_USAGE );
fprintf(stderr, " -i interval %s\n", MSG_SGESHAREMON_i_OPT_USAGE );
fprintf(stderr, " -l delimiter %s\n", MSG_SGESHAREMON_l_OPT_USAGE );
fprintf(stderr, " -m output_mode %s\n", MSG_SGESHAREMON_m_OPT_USAGE );
fprintf(stderr, " -n %s\n", MSG_SGESHAREMON_n_OPT_USAGE );
fprintf(stderr, " -o output_file %s\n", MSG_SGESHAREMON_o_OPT_USAGE );
fprintf(stderr, " -r delimiter %s\n", MSG_SGESHAREMON_r_OPT_USAGE );
fprintf(stderr, " -s string_format %s\n", MSG_SGESHAREMON_s_OPT_USAGE );
fprintf(stderr, " -t %s\n", MSG_SGESHAREMON_t_OPT_USAGE );
fprintf(stderr, " -u %s\n", MSG_SGESHAREMON_u_OPT_USAGE );
fprintf(stderr, " -x %s\n", MSG_SGESHAREMON_x_OPT_USAGE );
fprintf(stderr,"\n");
}
static FILE *
open_output(const char *file_name, const char *mode)
{
FILE *file = stdout;
if (file_name != nullptr) {
file = fopen(file_name, mode);
if (file == nullptr) {
fprintf(stderr, MSG_FILE_COULDNOTOPENXFORY_SS , file_name, mode);
fprintf(stderr, "\n");
exit(1);
}
}
return file;
}
static FILE *
close_output(FILE *file)
{
if (file != stdout) {
FCLOSE(file);
file = nullptr;
}
FCLOSE_ERROR:
return file;
}
int
main(int argc, char **argv)
{
lList *sharetree = nullptr;
lList *users = nullptr;
lList *projects = nullptr;
lList *usersets = nullptr;
lList *config = nullptr;
int interval=15;
int err=0;
int count=-1;
int header=0;
format_t format;
char *ofile=nullptr;
const char **names=nullptr;
bool group_nodes=true;
bool decay_usage=false;
int c;
extern char *optarg;
extern int optind;
FILE *outfile = nullptr;
const char *output_mode = "a";
dstring output_dstring = DSTRING_INIT;
lList *alp = nullptr;
DENTER_MAIN(TOP_LAYER, "share_mon");
format.name_format = false;
format.delim = "\t";
format.line_delim = "\n";
format.rec_delim = "\n";
format.str_format = "%s";
format.field_names = nullptr;
format.format_times = false;
format.line_prefix = nullptr;
#ifdef __SGE_COMPILE_WITH_GETTEXT__
/* init language output for gettext() , it will use the right language */
sge_init_language_func((gettext_func_type) gettext,
(setlocale_func_type) setlocale,
(bindtextdomain_func_type) bindtextdomain,
(textdomain_func_type) textdomain);
sge_init_language(nullptr,nullptr);
#endif /* __SGE_COMPILE_WITH_GETTEXT__ */
while ((c = getopt(argc, argv, "i:c:f:d:hl:m:no:r:s:tux")) != -1) {
switch(c) {
case 'i':
if (sscanf(optarg, "%d", &interval) != 1) {
fprintf(stderr, MSG_ERROR_XISNOTAVALIDINTERVAL_S , optarg);
err++;
}
break;
case 'c':
if (sscanf(optarg, "%d", &count) != 1) {
fprintf(stderr, MSG_ERROR_XISNOTAVALIDCOUNT_S , optarg);
err++;
}
break;
case 'f':
format.field_names = optarg;
break;
case 'o':
ofile = optarg;
break;
case 'd':
format.delim = optarg;
break;
case 'h':
header = 1;
break;
case 'm':
output_mode = optarg;
break;
case 'n':
format.name_format = true;
break;
case 'l':
format.line_delim = optarg;
break;
case 'r':
format.rec_delim = optarg;
break;
case 's':
format.str_format = optarg;
break;
case 't':
format.format_times = true;
break;
case 'u':
decay_usage = true;
break;
case 'x':
group_nodes = false;
break;
case '?':
err++;
}
}
if (err) {
usage();
sge_prof_cleanup();
sge_exit(1);
}
if ((argc - optind) > 0) {
names = (const char **)&argv[optind];
}
if (ocs::gdi::ClientBase::setup_and_enroll(SGE_SHARE_MON, MAIN_THREAD, &alp) != ocs::gdi::ErrorValue::AE_OK) {
answer_list_output(&alp);
sge_exit(1);
}
if (header) {
print_hdr(&output_dstring, &format);
}
while (count == -1 || count-- > 0) {
setup_lists(&sharetree, &users, &projects, &usersets, &config);
sconf_set_config(&config, nullptr);
sge_sharetree_print(&output_dstring, nullptr, sharetree, users, projects, usersets, group_nodes, decay_usage,
names,
&format);
if (count && strlen(format.rec_delim)) {
sge_dstring_sprintf_append(&output_dstring, "%s", format.rec_delim);
}
outfile = open_output(ofile, output_mode);
fwrite(sge_dstring_get_string(&output_dstring),
sge_dstring_strlen(&output_dstring), 1, outfile);
outfile = close_output(outfile);
free_lists(&sharetree, &users, &projects, &usersets, &config);
sge_dstring_clear(&output_dstring);
if (count) {
sleep(interval);
}
}
ocs::gdi::ClientBase::shutdown();
sge_prof_cleanup();
sge_dstring_free(&output_dstring);
DRETURN(0);
}