[12283] | 1 | /* |
---|
| 2 | * Campus Cluster Monitering Program |
---|
| 3 | * John Welch and Chris Vanharen |
---|
| 4 | * MIT's Project Athena |
---|
| 5 | * Summer 1990 |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
| 9 | * check.c contacts the cview daemon to retreive cluster info and |
---|
| 10 | * then displays it in the main window. |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #include <sys/types.h> |
---|
| 14 | #include <sys/socket.h> /* socket definitions */ |
---|
| 15 | #include <netinet/in.h> /* random network-type defines */ |
---|
| 16 | #include <netdb.h> /* random network-type defines */ |
---|
| 17 | #include <hesiod.h> /* hesiod lookup functions */ |
---|
| 18 | |
---|
| 19 | #include "xcluster.h" /* global include file. */ |
---|
| 20 | #include "net.h" |
---|
| 21 | |
---|
| 22 | #include <Jets/Jets.h> |
---|
| 23 | #include <Jets/Drawing.h> |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | /* |
---|
| 27 | * DEFINES |
---|
| 28 | */ |
---|
| 29 | #define ERRMSG "Unable to find any info on any machines." |
---|
| 30 | #ifndef MAX |
---|
| 31 | #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) ) |
---|
| 32 | #endif |
---|
| 33 | #ifndef MIN |
---|
| 34 | #define MIN(a,b) ( ((a) < (b)) ? (a) : (b) ) |
---|
| 35 | #endif |
---|
| 36 | |
---|
| 37 | static char *intsonly[] = {"intsonlyplease3"}; |
---|
| 38 | extern char machtypes[][25]; |
---|
| 39 | extern int num_machtypes; |
---|
| 40 | static char *headers[] = {"Cluster", "free / total"}; |
---|
| 41 | static char *totals = "Totals"; |
---|
| 42 | static char *printers = "Printers"; |
---|
| 43 | static char *status = "Status"; |
---|
| 44 | static char *jobs = "Jobs"; |
---|
| 45 | static char *cluster_hdr = "Cluster"; |
---|
| 46 | static char *phone_hdr = "Phone"; |
---|
| 47 | |
---|
| 48 | int xspace = 0; |
---|
| 49 | int height = 0; |
---|
| 50 | |
---|
| 51 | struct cluster *find_text(a, b) |
---|
| 52 | int a, b; |
---|
| 53 | { |
---|
| 54 | int y_init, y; |
---|
| 55 | int x = 20; |
---|
| 56 | int i; |
---|
| 57 | struct cluster *c; |
---|
| 58 | |
---|
| 59 | y_init = 20 + height+4; |
---|
| 60 | y = 20 + 2*height+4; |
---|
| 61 | |
---|
| 62 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 63 | { |
---|
| 64 | for(i = 0; strcmp(c->cluster_names[i], "XXXXX"); i++) |
---|
| 65 | { |
---|
| 66 | if (b < y && b > y_init && |
---|
| 67 | a > x && a < x + (num_machtypes+2)*xspace) |
---|
| 68 | return c; |
---|
| 69 | y += height; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | return c; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /* |
---|
| 77 | * check_cluster prints out the information for the specified cluster. |
---|
| 78 | */ |
---|
| 79 | int check_cluster(me, foo, data) |
---|
| 80 | DrawingJet me; |
---|
| 81 | int foo; |
---|
| 82 | caddr_t data; |
---|
| 83 | { |
---|
| 84 | char buf[BUF_SIZE]; /* temporary storage buffer. */ |
---|
| 85 | char *ptr; /* pointer to string. */ |
---|
| 86 | int x = 20, y = 20; /* location to begin printing. */ |
---|
| 87 | int ht; /* height of area used for phone numbers */ |
---|
| 88 | int len, len2; /* Lengths of buffers (pixels). */ |
---|
| 89 | int i, j; /* Counters */ |
---|
| 90 | static long time_cached = 0; /* time info was cached. */ |
---|
| 91 | int s; /* Socket to connect to cview daemon. */ |
---|
| 92 | FILE *f; /* stream for reading data from cviewd. */ |
---|
| 93 | struct cluster *c; |
---|
| 94 | static struct cluster *old_c = NULL; |
---|
| 95 | char name[15], stat[15], number[5]; |
---|
| 96 | static int init = 0, plen = 0, slen = 0, jlen =0; |
---|
| 97 | static struct cluster total; |
---|
| 98 | static int err = 0; |
---|
| 99 | |
---|
| 100 | if (!init) |
---|
| 101 | { |
---|
| 102 | init = 1; |
---|
| 103 | |
---|
| 104 | strcpy(total.cluster_names[0], totals); |
---|
| 105 | |
---|
| 106 | height = me->drawing.font->max_bounds.ascent |
---|
| 107 | + me->drawing.font->max_bounds.descent; |
---|
| 108 | |
---|
| 109 | plen = XTextWidth(me->drawing.font, printers, strlen(printers)); |
---|
| 110 | slen = XTextWidth(me->drawing.font, status, strlen(status)) + 10; |
---|
| 111 | jlen = XTextWidth(me->drawing.font, jobs, strlen(jobs)); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | for (j = 0; j <= num_machtypes + 1; j++) |
---|
| 115 | { |
---|
| 116 | len = MAX(XTextWidth(me->drawing.font, headers[j ? 1 : 0], |
---|
| 117 | strlen(headers[j ? 1 : 0])), |
---|
| 118 | XTextWidth(me->drawing.font, machtypes[j], |
---|
| 119 | strlen(machtypes[j]))); |
---|
| 120 | if (len > xspace) |
---|
| 121 | xspace = len; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 126 | { |
---|
| 127 | for(i = 0; ptr=c->cluster_names[i], strcmp(ptr, "XXXXX"); i++) |
---|
| 128 | { |
---|
| 129 | len = XTextWidth(me->drawing.font, ptr, strlen(ptr)); |
---|
| 130 | if (len > xspace) |
---|
| 131 | xspace = len; |
---|
| 132 | } |
---|
| 133 | for(i=0; ptr=c->prntr_name[i], strcmp(ptr, "XXXXX"); i++) |
---|
| 134 | { |
---|
| 135 | len = XTextWidth(me->drawing.font, ptr, strlen(ptr)); |
---|
| 136 | if (len > plen) |
---|
| 137 | plen = len; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | xspace += 5; |
---|
| 142 | plen += 5; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | if (time_cached + 60 < time(0)) /* cache info for 60 seconds */ |
---|
| 147 | { |
---|
| 148 | time_cached = time(0); |
---|
| 149 | s = net(progname, 1, intsonly); |
---|
| 150 | |
---|
| 151 | if (s < 1) |
---|
| 152 | { |
---|
| 153 | XClearArea(XjDisplay(me), XjWindow(me), x, y, 0, 0, 0); |
---|
| 154 | ptr = "Error while contacting server."; |
---|
| 155 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 156 | x, y, ptr, strlen(ptr)); |
---|
| 157 | XFlush(XjDisplay(me)); |
---|
| 158 | err = 1; |
---|
| 159 | return 0; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | f = fdopen(s, "r"); |
---|
| 163 | |
---|
| 164 | for (j=0; j < (num_machtypes+1)*2; j++) |
---|
| 165 | total.cluster_info[0][j] = 0; |
---|
| 166 | |
---|
| 167 | while(fscanf(f, "%s", buf) != EOF) |
---|
| 168 | { |
---|
| 169 | if (!strcmp(buf, "cluster")) |
---|
| 170 | { |
---|
| 171 | (void) fscanf(f, "%s", buf); |
---|
| 172 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 173 | { |
---|
| 174 | for(i = 0; strcmp(c->cluster_names[i], "XXXXX"); i++) |
---|
| 175 | if (!strcmp(buf, c->cluster_names[i])) |
---|
| 176 | { |
---|
| 177 | int free, inuse; |
---|
| 178 | int tot_free=0, tot_inuse=0; |
---|
| 179 | |
---|
| 180 | for (j=0; j < num_machtypes; j++) |
---|
| 181 | { |
---|
| 182 | fscanf(f, "%d %d", &free, &inuse); |
---|
| 183 | c->cluster_info[i][j*2] = free; |
---|
| 184 | c->cluster_info[i][j*2+1] = inuse; |
---|
| 185 | total.cluster_info[0][j*2] += free; |
---|
| 186 | total.cluster_info[0][j*2+1] += inuse; |
---|
| 187 | tot_free += free; |
---|
| 188 | tot_inuse += inuse; |
---|
| 189 | } |
---|
| 190 | c->cluster_info[i][j*2] = tot_free; |
---|
| 191 | c->cluster_info[i][j*2+1] = tot_inuse; |
---|
| 192 | total.cluster_info[0][j*2] += tot_free; |
---|
| 193 | total.cluster_info[0][j*2+1] += tot_inuse; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | if (!strcmp(buf, "printer")) |
---|
| 199 | { |
---|
| 200 | (void) fscanf(f, "%s %s %s", name, stat, number); |
---|
| 201 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 202 | { |
---|
| 203 | for(i=0; |
---|
| 204 | ptr=c->prntr_name[i], strcmp(ptr, "XXXXX"); |
---|
| 205 | i++) |
---|
| 206 | { |
---|
| 207 | if (!strcmp(name, ptr)) |
---|
| 208 | { |
---|
| 209 | strcpy(c->prntr_current_status[i], stat); |
---|
| 210 | strcpy(c->prntr_num_jobs[i], number); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | (void) fclose(f); |
---|
| 218 | XClearArea(XjDisplay(me), XjWindow(me), x+xspace, y+height+3, 0, 0, 0); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | /* |
---|
| 223 | * display the data |
---|
| 224 | */ |
---|
| 225 | |
---|
| 226 | if (err) |
---|
| 227 | { |
---|
| 228 | XClearArea(XjDisplay(me), XjWindow(me), x, y, 0, 0, 0); |
---|
| 229 | err = 0; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | for (j = 0; j <= num_machtypes + 1; j++) |
---|
| 233 | { |
---|
| 234 | len = XTextWidth(me->drawing.font, headers[j ? 1 : 0], |
---|
| 235 | strlen(headers[j ? 1 : 0])) |
---|
| 236 | - XTextWidth(me->drawing.font, machtypes[j], strlen(machtypes[j])); |
---|
| 237 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 238 | x + xspace*j + len/2, y, machtypes[j], |
---|
| 239 | strlen(machtypes[j])); |
---|
| 240 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 241 | x + xspace*j, y + height, headers[j ? 1 : 0], |
---|
| 242 | strlen(headers[j ? 1 : 0])); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | XDrawLine(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 246 | x, y+height+2, x + (num_machtypes+2)*xspace, y+height+2); |
---|
| 247 | y += 2*height + 2; |
---|
| 248 | |
---|
| 249 | /* |
---|
| 250 | * Check for multiple clusters in the same area |
---|
| 251 | */ |
---|
| 252 | |
---|
| 253 | len = XTextWidth(me->drawing.font, "free ", 5); |
---|
| 254 | |
---|
| 255 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 256 | { |
---|
| 257 | for(i = 0; strcmp(c->cluster_names[i], "XXXXX"); i++) |
---|
| 258 | { |
---|
| 259 | GC gc; |
---|
| 260 | |
---|
| 261 | if (c == Current) |
---|
| 262 | { |
---|
| 263 | XFillRectangle(XjDisplay(me), XjWindow(me), |
---|
| 264 | me->drawing.foreground_gc, |
---|
| 265 | x, y+2-height, (num_machtypes+2)*xspace, height); |
---|
| 266 | gc = me->drawing.background_gc; |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | gc = me->drawing.foreground_gc; |
---|
| 270 | |
---|
| 271 | if (old_c != Current && c == old_c) |
---|
| 272 | { |
---|
| 273 | XFillRectangle(XjDisplay(me), XjWindow(me), |
---|
| 274 | me->drawing.background_gc, |
---|
| 275 | x, y+2-height, (num_machtypes+2)*xspace, height); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | strcpy(buf, c->cluster_names[i]); |
---|
| 279 | XDrawString(XjDisplay(me), XjWindow(me), gc, |
---|
| 280 | x, y, buf, strlen(buf)); |
---|
| 281 | |
---|
| 282 | for (j = 0; j < num_machtypes+1; j++) |
---|
| 283 | { |
---|
| 284 | if (c->cluster_info[i][2*j] == 0 |
---|
| 285 | && c->cluster_info[i][2*j+1] == 0) |
---|
| 286 | { |
---|
| 287 | XDrawString(XjDisplay(me), XjWindow(me), gc, |
---|
| 288 | x+xspace*(j+1) + len, y, "-", 1); |
---|
| 289 | } |
---|
| 290 | else |
---|
| 291 | { |
---|
| 292 | sprintf(buf, "%d ", |
---|
| 293 | c->cluster_info[i][2*j]); |
---|
| 294 | len2 = XTextWidth(me->drawing.font, buf, strlen(buf)); |
---|
| 295 | sprintf(buf, "%d / %d", |
---|
| 296 | c->cluster_info[i][2*j], |
---|
| 297 | c->cluster_info[i][2*j] + c->cluster_info[i][2*j+1]); |
---|
| 298 | XDrawString(XjDisplay(me), XjWindow(me), gc, |
---|
| 299 | x+xspace*(j+1) + len - len2, |
---|
| 300 | y, buf, strlen(buf)); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | y += height; |
---|
| 305 | XFlush(XjDisplay(me)); |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /* |
---|
| 310 | * print out totals... |
---|
| 311 | */ |
---|
| 312 | |
---|
| 313 | strcpy(buf, total.cluster_names[0]); |
---|
| 314 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 315 | x, y, buf, strlen(buf)); |
---|
| 316 | |
---|
| 317 | for (j = 0; j < num_machtypes+1; j++) |
---|
| 318 | { |
---|
| 319 | if (total.cluster_info[0][2*j] == 0 |
---|
| 320 | && total.cluster_info[0][2*j+1] == 0) |
---|
| 321 | { |
---|
| 322 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 323 | x+xspace*(j+1) + len, y, "-", 1); |
---|
| 324 | } |
---|
| 325 | else |
---|
| 326 | { |
---|
| 327 | sprintf(buf, "%d ", |
---|
| 328 | total.cluster_info[0][2*j]); |
---|
| 329 | len2 = XTextWidth(me->drawing.font, buf, strlen(buf)); |
---|
| 330 | sprintf(buf, "%d / %d", |
---|
| 331 | total.cluster_info[0][2*j], |
---|
| 332 | total.cluster_info[0][2*j] + total.cluster_info[0][2*j+1]); |
---|
| 333 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 334 | x+xspace*(j+1) + len - len2, |
---|
| 335 | y, buf, strlen(buf)); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | /* |
---|
| 340 | * print out phone number(s) for the specified cluster. |
---|
| 341 | */ |
---|
| 342 | |
---|
| 343 | x = me->core.width - plen - slen - jlen - 20; |
---|
| 344 | y -= (2 + 3*height); |
---|
| 345 | ht = y - height; |
---|
| 346 | |
---|
| 347 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 348 | x, y, cluster_hdr, strlen(cluster_hdr)); |
---|
| 349 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 350 | x+MAX(xspace,plen), y, phone_hdr, strlen(phone_hdr)); |
---|
| 351 | y += 2; |
---|
| 352 | XDrawLine(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 353 | x, y, x+plen+slen+jlen, y); |
---|
| 354 | |
---|
| 355 | if (old_c != Current) |
---|
| 356 | XClearArea(XjDisplay(me), XjWindow(me), x, y+1, 0, 0, 0); |
---|
| 357 | |
---|
| 358 | for (c = cluster_list; c != NULL; c = c->next) |
---|
| 359 | { |
---|
| 360 | for(i = 0; strcmp(c->cluster_names[i], "XXXXX"); i++) |
---|
| 361 | { |
---|
| 362 | if (c == Current) |
---|
| 363 | { |
---|
| 364 | y += height; |
---|
| 365 | strcpy(buf, c->cluster_names[i]); |
---|
| 366 | XDrawString(XjDisplay(me), XjWindow(me), |
---|
| 367 | me->drawing.foreground_gc, |
---|
| 368 | x, y, buf, strlen(buf)); |
---|
| 369 | strcpy(buf, c->phone_num[i]); |
---|
| 370 | XDrawString(XjDisplay(me), XjWindow(me), |
---|
| 371 | me->drawing.foreground_gc, |
---|
| 372 | x+MAX(xspace,plen), y, buf, strlen(buf)); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /* |
---|
| 378 | * print out printer information for the specified cluster. |
---|
| 379 | */ |
---|
| 380 | |
---|
| 381 | x = me->core.width - plen - slen - jlen - 20; |
---|
| 382 | y = 20 + height; |
---|
| 383 | |
---|
| 384 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 385 | x, y, printers, strlen(printers)); |
---|
| 386 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 387 | x+plen, y, status, strlen(status)); |
---|
| 388 | XDrawString(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 389 | x+plen+slen, y, jobs, strlen(jobs)); |
---|
| 390 | y += 2; |
---|
| 391 | XDrawLine(XjDisplay(me), XjWindow(me), me->drawing.foreground_gc, |
---|
| 392 | x, y, x+plen+slen+jlen, y); |
---|
| 393 | |
---|
| 394 | if (old_c != Current) |
---|
| 395 | XClearArea(XjDisplay(me), XjWindow(me), x, y+1, 0, ht - (y+1), 0); |
---|
| 396 | |
---|
| 397 | if (Current != NULL && strcmp(Current->prntr_name[0], "XXXXX")) |
---|
| 398 | { |
---|
| 399 | for(i=0; |
---|
| 400 | ptr=Current->prntr_name[i], strcmp(ptr, "XXXXX"); |
---|
| 401 | i++) |
---|
| 402 | { |
---|
| 403 | /* display printer info */ |
---|
| 404 | |
---|
| 405 | y += height; |
---|
| 406 | XDrawString(XjDisplay(me), XjWindow(me), |
---|
| 407 | me->drawing.foreground_gc, |
---|
| 408 | x, y, ptr, strlen(ptr)); |
---|
| 409 | ptr = Current->prntr_current_status[i]; |
---|
| 410 | XDrawString(XjDisplay(me), XjWindow(me), |
---|
| 411 | me->drawing.foreground_gc, |
---|
| 412 | x+plen+5, y, ptr, strlen(ptr)); |
---|
| 413 | ptr = Current->prntr_num_jobs[i]; |
---|
| 414 | XDrawString(XjDisplay(me), XjWindow(me), |
---|
| 415 | me->drawing.foreground_gc, |
---|
| 416 | x+plen+slen+5, y, ptr, strlen(ptr)); |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | |
---|
| 422 | old_c = Current; |
---|
| 423 | |
---|
| 424 | return 0; |
---|
| 425 | } |
---|