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
|
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2005- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "catalog_query.h"
#include "jx_table.h"
#include "jx_print.h"
#include "jx_parse.h"
#include "cctools.h"
#include "debug.h"
#include "getopt_aux.h"
#include "link.h"
#include "stringtools.h"
#include "xxmalloc.h"
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
enum {
MODE_TABLE,
MODE_SHORT,
MODE_LONG,
MODE_TOTAL,
};
static struct jx_table headers[] = {
{"type", "TYPE", JX_TABLE_MODE_PLAIN, JX_TABLE_ALIGN_LEFT, 8},
{"name", "NAME", JX_TABLE_MODE_PLAIN, JX_TABLE_ALIGN_LEFT, -25},
{"port", "PORT", JX_TABLE_MODE_PLAIN, JX_TABLE_ALIGN_LEFT, 5},
{"owner", "OWNER", JX_TABLE_MODE_PLAIN, JX_TABLE_ALIGN_LEFT, 10},
{"version", "VERSION", JX_TABLE_MODE_PLAIN, JX_TABLE_ALIGN_LEFT, 8},
{"total", "TOTAL", JX_TABLE_MODE_METRIC, JX_TABLE_ALIGN_RIGHT, 8},
{"avail", "AVAIL", JX_TABLE_MODE_METRIC, JX_TABLE_ALIGN_RIGHT, 8},
{0, 0, 0, 0, 0}
};
static void show_help(const char *cmd)
{
fprintf(stdout, "chirp_status [options] [ <name> <value> ]\n");
fprintf(stdout, "where options are:\n");
fprintf(stdout, " %-30s Query the catalog on this host.\n", "-c,--catalog=<host>");
fprintf(stdout, " %-30s Enable debugging for this sybsystem\n", "-d,--debug=<flag>");
fprintf(stdout, " %-30s Send debugging to this file. (can also be :stderr, :stdout, :syslog, or :journal)\n", "-o,--debug-file=<file>");
fprintf(stdout, " %-30s Rotate file once it reaches this size. (default 10M, 0 disables)\n", "-O,--debug-rotate-max=<bytes>");
fprintf(stdout, " %-30s Only show servers with this space available. (example: -A 100MB)\n", "-A,--server-space=<size>");
fprintf(stdout, " %-30s Only show servers with this project.\n", " --server-project=<name>");
fprintf(stdout, " %-30s Show all records, not just chirps and catalogs.\n", "-a,--all");
fprintf(stdout, " %-30s Timeout.\n", "-t,--timeout=<time>");
fprintf(stdout, " %-30s Short output.\n", "-s,--brief");
fprintf(stdout, " %-30s Long output.\n", "-l,--verbose");
fprintf(stdout, " %-30s Totals output.\n", "-T,--totals");
fprintf(stdout, " %-30s Show version info.\n", "-v,--version");
fprintf(stdout, " %-30s Filter results by this expression.\n", " --where=<expr>");
fprintf(stdout, " %-30s This message.\n", "-h,--help");
}
int compare_entries(struct jx **a, struct jx **b)
{
int result;
const char *x, *y;
x = jx_lookup_string(*a, "type");
if(!x)
x = "unknown";
y = jx_lookup_string(*b, "type");
if(!y)
y = "unknown";
result = strcasecmp(x, y);
if(result != 0)
return result;
x = jx_lookup_string(*a, "name");
if(!x)
x = "unknown";
y = jx_lookup_string(*b, "name");
if(!y)
y = "unknown";
return strcasecmp(x, y);
}
static struct jx *table[10000];
int main(int argc, char *argv[])
{
enum {
LONGOPT_SERVER_LASTHEARDFROM = INT_MAX-0,
LONGOPT_SERVER_PROJECT = INT_MAX-1,
LONGOPT_WHERE = INT_MAX-2,
};
static const struct option long_options[] = {
{"all", no_argument, 0, 'a'},
{"brief", no_argument, 0, 's'},
{"catalog", required_argument, 0, 'c'},
{"debug", required_argument, 0, 'd'},
{"debug-file", required_argument, 0, 'o'},
{"debug-rotate-max", required_argument, 0, 'O'},
{"help", no_argument, 0, 'h'},
{"server-lastheardfrom", required_argument, 0, LONGOPT_SERVER_LASTHEARDFROM},
{"server-project", required_argument, 0, LONGOPT_SERVER_PROJECT},
{"server-space", required_argument, 0, 'A'},
{"timeout", required_argument, 0, 't'},
{"totals", no_argument, 0, 'T'},
{"verbose", no_argument, 0, 'l'},
{"version", no_argument, 0, 'v'},
{"where", required_argument, 0, LONGOPT_WHERE },
{0, 0, 0, 0}
};
struct catalog_query *q;
struct jx *j;
time_t timeout = 60, stoptime;
const char *catalog_host = 0;
int i;
int c;
int count = 0;
int mode = MODE_TABLE;
INT64_T sum_total = 0, sum_avail = 0;
const char *filter_name = 0;
const char *filter_value = 0;
int show_all_types = 0;
const char *server_project = NULL;
time_t server_lastheardfrom = 0;
uint64_t server_avail = 0;
struct jx *jexpr = jx_boolean(true);
debug_config(argv[0]);
while((c = getopt_long(argc, argv, "aA:c:d:t:o:O:sTlvh", long_options, NULL)) > -1) {
switch (c) {
case 'a':
show_all_types = 1;
break;
case 'c':
catalog_host = optarg;
break;
case 'd':
debug_flags_set(optarg);
break;
case 't':
timeout = string_time_parse(optarg);
break;
case 'A':
server_avail = string_metric_parse(optarg);
break;
case 'o':
debug_config_file(optarg);
break;
case 'O':
debug_config_file_size(string_metric_parse(optarg));
break;
case 'v':
cctools_version_print(stdout, argv[0]);
return 1;
case 's':
mode = MODE_SHORT;
break;
case 'l':
mode = MODE_LONG;
break;
case 'T':
mode = MODE_TOTAL;
break;
case LONGOPT_SERVER_LASTHEARDFROM:
server_lastheardfrom = time(0)-string_time_parse(optarg);
break;
case LONGOPT_SERVER_PROJECT:
server_project = xxstrdup(optarg);
break;
case LONGOPT_WHERE:
jexpr = jx_parse_string(optarg);
if (!jexpr) {
fprintf(stderr,"invalid expression: %s\n", optarg);
return 1;
}
break;
case 'h':
default:
show_help(argv[0]);
return 1;
}
}
cctools_version_debug(D_DEBUG, argv[0]);
if(argc - optind == 0) {
// fine, keep going
} else if((argc - optind) == 1) {
filter_name = "name";
filter_value = argv[optind];
} else if((argc - optind) == 2) {
filter_name = argv[optind];
filter_value = argv[optind + 1];
} else {
show_help(argv[0]);
return 1;
}
int columns = 80;
char *column_str = getenv("COLUMNS");
if(column_str) {
columns = atoi(column_str);
columns = columns < 1 ? 80 : columns;
}
stoptime = time(0) + timeout;
if (!show_all_types) {
jexpr = jx_operator(
JX_OP_AND,
jexpr,
jx_operator(
JX_OP_OR,
jx_operator(
JX_OP_EQ,
jx_symbol("type"),
jx_string("chirp")
),
jx_operator(
JX_OP_EQ,
jx_symbol("type"),
jx_string("catalog")
)
)
);
}
q = catalog_query_create(catalog_host, jexpr, stoptime);
if(!q) {
fprintf(stderr, "couldn't query catalog: %s\n", strerror(errno));
return 1;
}
if(mode == MODE_TABLE) {
jx_table_print_header(headers,stdout,columns);
} else if(mode==MODE_LONG) {
printf("[\n");
}
while((j = catalog_query_read(q, stoptime))) {
table[count++] = j;
}
catalog_query_delete(q);
qsort(table, count, sizeof(*table), (int (*)(const void *, const void *)) compare_entries);
for(i = 0; i < count; i++) {
const char *lastheardfrom = jx_lookup_string(table[i], "lastheardfrom");
if (lastheardfrom && (time_t)strtoul(lastheardfrom, NULL, 10) < server_lastheardfrom)
continue;
const char *avail = jx_lookup_string(table[i], "avail");
if (avail && strtoul(avail, NULL, 10) < server_avail)
continue;
const char *project = jx_lookup_string(table[i], "project");
if (server_project && (project == NULL || !(strcmp(project, server_project) == 0)))
continue;
if(filter_name) {
const char *v = jx_lookup_string(table[i], filter_name);
if(!v || strcmp(filter_value, v))
continue;
}
if(mode == MODE_SHORT) {
const char *t = jx_lookup_string(table[i], "type");
if(t && !strcmp(t, "chirp")) {
printf("%s:%d\n", jx_lookup_string(table[i], "name"), (int) jx_lookup_integer(table[i], "port"));
}
} else if(mode == MODE_LONG) {
if(i!=0) printf(",\n");
jx_print_stream(table[i],stdout);
} else if(mode == MODE_TABLE) {
jx_table_print(headers, table[i], stdout, columns);
} else if(mode == MODE_TOTAL) {
sum_avail += jx_lookup_integer(table[i], "avail");
sum_total += jx_lookup_integer(table[i], "total");
}
}
for(i=0;i<count;i++) {
jx_delete(table[i]);
}
if(mode == MODE_TOTAL) {
printf("NODES: %4d\n", count);
printf("TOTAL: %6sB\n", string_metric(sum_total, -1, 0));
printf("AVAIL: %6sB\n", string_metric(sum_avail, -1, 0));
printf("INUSE: %6sB\n", string_metric(sum_total - sum_avail, -1, 0));
} else if(mode == MODE_TABLE) {
jx_table_print_footer(headers,stdout,columns);
} else if(mode==MODE_LONG) {
printf("\n]\n");
}
return 0;
}
/* vim: set noexpandtab tabstop=4: */
|