home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / lowlav / lowlav.c < prev   
Encoding:
C/C++ Source or Header  |  1989-04-08  |  2.0 KB  |  101 lines

  1. /*
  2. Given a list of machine names, return the name of the machine in that list
  3. with the lowest load average in /usr/spool/rwho.
  4.  
  5. Written by John DiMarco, March 1989 at the University of Toronto
  6. Based on a program 'asun' by Jonathon Haruni.
  7.  
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define STREQ(a,b)(strcmp((a),(b)) == 0)
  14. #define TRUE  1
  15. #define FALSE 0
  16.  
  17. #define TOOBIGLOAD 30000
  18.  
  19. extern char *getenv();
  20. extern char *optarg;
  21. extern int opterr;
  22.  
  23. struct partofrwhodfile {
  24.     char fill[4];
  25.     int garbage[2];
  26.     char hostname[32];
  27.     int loadav;
  28. };
  29.  
  30.  
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     FILE *fp, *input;
  36.     int c, verbose, bestload = TOOBIGLOAD;
  37.     char besthost[32];
  38.     char thishost[80];
  39.     char filename[120];
  40.     struct partofrwhodfile entry;
  41.  
  42.     *besthost = '\0';
  43.  
  44.     verbose = FALSE;
  45.     input = stdin;
  46.  
  47.     while((c = getopt(argc, argv, "vf:")) != EOF){
  48.         switch(c){
  49.         case 'v':
  50.             /* Verbose mode: print out error messages for      */
  51.             /* hostnames which don't appear in /usr/spool/rwho */
  52.             verbose = TRUE;
  53.             break;
  54.         case 'f':
  55.             /* Take input from a file rather than stdin */
  56.             input = fopen(optarg, "r");
  57.             if(NULL==input){
  58.                 (void)fprintf(stderr,
  59.                           "%s: Can't open file %s \n",
  60.                           argv[0], optarg);
  61.                 (void)exit(1);
  62.             }
  63.             break;
  64.         case '?':
  65.             (void)fprintf(stderr, "Usage: %s [-v] [-f filename]\n",
  66.                       argv[0]);
  67.             (void)exit(1);
  68.             break;
  69.         }
  70.  
  71.     }
  72.     
  73.     while(!feof(input)){
  74.         (void)fscanf(input, "%80s ", thishost);
  75.         (void)sprintf(filename,"/usr/spool/rwho/whod.%s",
  76.                   thishost);
  77.         if((fp = fopen(filename,"r")) != NULL) {
  78.             (void)fread(&entry,sizeof(struct partofrwhodfile),
  79.                   1,fp);
  80.             (void)fclose(fp);
  81.             if(entry.loadav < bestload) {
  82.                 (void)strcpy(besthost,thishost);
  83.                 bestload = entry.loadav;
  84.             }
  85.         } else {
  86.             if(verbose){
  87.                 fprintf(stderr, "%s: %s not found\n", 
  88.                     argv[0], filename);
  89.             }
  90.         }
  91.     }
  92.     if(TOOBIGLOAD == bestload){
  93.         (void)fprintf(stderr, "%s: No data for specified hosts\n", 
  94.                   argv[0]);
  95.         (void)exit(1);
  96.     }
  97.     (void)printf("%s\n",besthost);
  98.     if(input!=stdin)
  99.         (void)fclose(input);
  100. }
  101.