コード例 #1
0
ファイル: mafdcmd.c プロジェクト: hfs/afd
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ mafdcmd() $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
int
main(int argc, char *argv[])
{
   int  errors = 0,
        i,
        position,
        user_offset;
   char fake_user[MAX_FULL_USER_ID_LENGTH],
        *perm_buffer,
        *ptr,
        user[MAX_FULL_USER_ID_LENGTH],
        work_dir[MAX_PATH_LENGTH];

   CHECK_FOR_VERSION(argc, argv);

   if ((argc > 1) &&
       (argv[1][0] == '-') && (argv[1][1] == 'v') && (argv[1][2] == '\0'))
   {
      (void)fprintf(stdout, "%s\n", PACKAGE_VERSION);
      exit(SUCCESS);
   }

   if (get_mon_path(&argc, argv, work_dir) < 0)
   {
      exit(INCORRECT);
   }
   p_work_dir = work_dir;
   if (get_arg(&argc, argv, "-p", user, MAX_PROFILE_NAME_LENGTH) == INCORRECT)
   {
      user_offset = 0;
   }
   else
   {
      user_offset = strlen(user);
   }
#ifdef WITH_SETUID_PROGS
   set_afd_euid(work_dir);
#endif

   if (argc < 2)
   {
      usage(argv[0]);
      exit(INCORRECT);
   }
   check_fake_user(&argc, argv, MON_CONFIG_FILE, fake_user);
   eval_input(argc, argv);
   get_user(user, fake_user, user_offset);

   /*
    * Ensure that the user may use this program.
    */
   switch (get_permissions(&perm_buffer, fake_user))
   {
      case NO_ACCESS : /* Cannot access afd.users file. */
         {
            char afd_user_file[MAX_PATH_LENGTH];

            (void)strcpy(afd_user_file, p_work_dir);
            (void)strcat(afd_user_file, ETC_DIR);
            (void)strcat(afd_user_file, AFD_USER_FILE);

            (void)fprintf(stderr,
                          "Failed to access `%s', unable to determine users permissions.\n",
                          afd_user_file);
         }
         exit(INCORRECT);

      case NONE :
         (void)fprintf(stderr, "%s\n", PERMISSION_DENIED_STR);
         exit(INCORRECT);

      case SUCCESS : /* Lets evaluate the permissions and see what */
                     /* the user may do.                           */
         {
            int permission = NO;

            if ((perm_buffer[0] == 'a') && (perm_buffer[1] == 'l') &&
                (perm_buffer[2] == 'l') &&
                ((perm_buffer[3] == '\0') || (perm_buffer[3] == ' ') ||
                 (perm_buffer[3] == '\t')))
            {
               permission = YES;
            }
            else
            {
               if (lposi(perm_buffer, MAFD_CMD_PERM,
                         MAFD_CMD_PERM_LENGTH) != NULL)
               {
                  permission = YES;

                  /*
                   * Check if the user may do everything
                   * he has requested. If not remove it
                   * from the option list.
                   */
                  if ((options & ENABLE_AFD_OPTION) ||
                      (options & DISABLE_AFD_OPTION))
                  {
                     if (lposi(perm_buffer, DISABLE_AFD_PERM,
                               DISABLE_AFD_PERM_LENGTH) == NULL)
                     {
                        if (options & ENABLE_AFD_OPTION)
                        {
                           options ^= ENABLE_AFD_OPTION;
                        }
                        if (options & DISABLE_AFD_OPTION)
                        {
                           options ^= DISABLE_AFD_OPTION;
                        }
                        if (options & TOGGLE_AFD_OPTION)
                        {
                           options ^= TOGGLE_AFD_OPTION;
                        }
                        (void)fprintf(stderr,
                                      "User %s not permitted to enable/disable a AFD.\n",
                                      user);
                     }
                  }
                  if (options & RETRY_OPTION)
                  {
                     if (lposi(perm_buffer, RETRY_PERM,
                               RETRY_PERM_LENGTH) == NULL)
                     {
                        options ^= RETRY_OPTION;
                        (void)fprintf(stderr,
                                      "User %s not permitted to retry.\n",
                                      user);
                     }
                  }
                  if (options & SWITCH_AFD_OPTION)
                  {
                     if (lposi(perm_buffer, SWITCH_HOST_PERM,
                               SWITCH_HOST_PERM_LENGTH) == NULL)
                     {
                        options ^= SWITCH_AFD_OPTION;
                        (void)fprintf(stderr,
                                      "User %s not permitted to retry.\n",
                                      user);
                     }
                  }
               }
            }
            free(perm_buffer);
            if (permission != YES)
            {
               (void)fprintf(stderr, "%s\n", PERMISSION_DENIED_STR);
               exit(INCORRECT);
            }
         }
         break;

      case INCORRECT: /* Hmm. Something did go wrong. Since we want to */
                      /* be able to disable permission checking let    */
                      /* the user have all permissions.                */
         break;

      default :
         (void)fprintf(stderr, "Impossible!! Remove the programmer!\n");
         exit(INCORRECT);
   }

   if (msa_attach() < 0)
   {
      (void)fprintf(stderr, "ERROR   : Failed to attach to MSA. (%s %d)\n",
                    __FILE__, __LINE__);
      exit(INCORRECT);
   }

   for (i = 0; i < no_of_afd_names; i++)
   {
      position = -1;
      ptr = afds[i];
      while ((*ptr != '\0') && (isdigit((int)(*ptr))))
      {
         ptr++;
      }
      if ((*ptr == '\0') && (ptr != afds[i]))
      {
         position = atoi(afds[i]);
         if ((position < 0) || (position > (no_of_afds - 1)))
         {
            (void)fprintf(stderr,
                          "WARNING : Position %d out of range. Ignoring. (%s %d)\n",
                          position, __FILE__, __LINE__);
            errors++;
            continue;
         }
      }
      if (position < 0)
      {
         if ((position = get_afd_position(msa, afds[i], no_of_afds)) < 0)
         {
            (void)fprintf(stderr,
                          "WARNING : Could not find AFD %s in MSA. (%s %d)\n",
                          afds[i], __FILE__, __LINE__);
            errors++;
            continue;
         }
      }

      /*
       * ENABLE AFD
       */
      if (options & ENABLE_AFD_OPTION)
      {
         if (msa[position].connect_status == DISABLED)
         {
            int  fd;
#ifdef WITHOUT_FIFO_RW_SUPPORT
            int  readfd;
#endif
            char mon_cmd_fifo[MAX_PATH_LENGTH];

            (void)sprintf(mon_cmd_fifo, "%s%s%s",
                          p_work_dir, FIFO_DIR, MON_CMD_FIFO);
#ifdef WITHOUT_FIFO_RW_SUPPORT
            if (open_fifo_rw(mon_cmd_fifo, &readfd, &fd) == -1)
#else
            if ((fd = open(mon_cmd_fifo, O_RDWR)) == -1)
#endif
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          "Failed to open() %s : %s",
                          mon_cmd_fifo, strerror(errno));
               errors++;
            }
            else
            {
               char cmd[1 + SIZEOF_INT];

               cmd[0] = ENABLE_MON;
               (void)memcpy(&cmd[1], &position, SIZEOF_INT);
               if (write(fd, cmd, (1 + SIZEOF_INT)) != (1 + SIZEOF_INT))
               {
                  system_log(ERROR_SIGN, __FILE__, __LINE__,
                             "Failed to write() to %s : %s",
                             mon_cmd_fifo, strerror(errno));
                  errors++;
               }
               else
               {
                  system_log(DEBUG_SIGN, NULL, 0,
                             "%-*s: ENABLED (%s) [mafdcmd].",
                             MAX_AFD_NAME_LENGTH, msa[position].afd_alias, user);
               }
#ifdef WITHOUT_FIFO_RW_SUPPORT
               if (close(readfd) == -1)
               {
                  system_log(DEBUG_SIGN, __FILE__, __LINE__,
                             "Failed to close() FIFO %s : %s",
                             mon_cmd_fifo, strerror(errno));
               }
#endif
               if (close(fd) == -1)
               {
                  system_log(DEBUG_SIGN, __FILE__, __LINE__,
                             "Failed to close() FIFO %s : %s",
                             mon_cmd_fifo, strerror(errno));
               }
            }
         }
         else
         {
            (void)fprintf(stderr,
                          "INFO    : AFD %s is already enabled.\n",
                          msa[position].afd_alias);
         }
      }

      /*
       * DISABLE AFD
       */
      if (options & DISABLE_AFD_OPTION)
      {
         if (msa[position].connect_status == DISABLED)
         {
            (void)fprintf(stderr,
                          "INFO    : AFD %s is already disabled.\n",
                          msa[position].afd_alias);
         }
         else
         {
            int  fd;
#ifdef WITHOUT_FIFO_RW_SUPPORT
            int  readfd;
#endif
            char mon_cmd_fifo[MAX_PATH_LENGTH];

            (void)sprintf(mon_cmd_fifo, "%s%s%s",
                          p_work_dir, FIFO_DIR, MON_CMD_FIFO);
#ifdef WITHOUT_FIFO_RW_SUPPORT
            if (open_fifo_rw(mon_cmd_fifo, &readfd, &fd) == -1)
#else
            if ((fd = open(mon_cmd_fifo, O_RDWR)) == -1)
#endif
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          "Failed to open() %s : %s",
                          mon_cmd_fifo, strerror(errno));
               errors++;
            }
            else
            {
               char cmd[1 + SIZEOF_INT];

               cmd[0] = DISABLE_MON;
               (void)memcpy(&cmd[1], &position, SIZEOF_INT);
               if (write(fd, cmd, (1 + SIZEOF_INT)) != (1 + SIZEOF_INT))
               {
                  system_log(ERROR_SIGN, __FILE__, __LINE__,
                             "Failed to write() to %s : %s",
                             mon_cmd_fifo, strerror(errno));
                  errors++;
               }
               else
               {
                  system_log(DEBUG_SIGN, NULL, 0,
                             "%-*s: DISABLED (%s) [mafdcmd].",
                             MAX_AFD_NAME_LENGTH, msa[position].afd_alias, user);
               }
#ifdef WITHOUT_FIFO_RW_SUPPORT
               if (close(readfd) == -1)
               {
                  system_log(DEBUG_SIGN, __FILE__, __LINE__,
                             "Failed to close() FIFO %s : %s",
                             mon_cmd_fifo, strerror(errno));
               }
#endif
               if (close(fd) == -1)
               {
                  system_log(DEBUG_SIGN, __FILE__, __LINE__,
                             "Failed to close() FIFO %s : %s",
                             mon_cmd_fifo, strerror(errno));
               }
            }
         }
      }

      /*
       * ENABLE or DISABLE a AFD.
       */
      if (options & TOGGLE_AFD_OPTION)
      {
         int  fd;
#ifdef WITHOUT_FIFO_RW_SUPPORT
         int  readfd;
#endif
         char mon_cmd_fifo[MAX_PATH_LENGTH];

         (void)sprintf(mon_cmd_fifo, "%s%s%s",
                       p_work_dir, FIFO_DIR, MON_CMD_FIFO);
#ifdef WITHOUT_FIFO_RW_SUPPORT
         if (open_fifo_rw(mon_cmd_fifo, &readfd, &fd) == -1)
#else
         if ((fd = open(mon_cmd_fifo, O_RDWR)) == -1)
#endif
         {
            system_log(ERROR_SIGN, __FILE__, __LINE__,
                       "Failed to open() %s : %s",
                       mon_cmd_fifo, strerror(errno));
            errors++;
         }
         else
         {
            char cmd[1 + SIZEOF_INT];

            if (msa[position].connect_status == DISABLED)
            {
               cmd[0] = ENABLE_MON;
            }
            else /* DISABLE AFD */
            {
               cmd[0] = DISABLE_MON;
            }
            (void)memcpy(&cmd[1], &position, SIZEOF_INT);
            if (write(fd, cmd, (1 + SIZEOF_INT)) != (1 + SIZEOF_INT))
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          "Failed to write() to %s : %s",
                          mon_cmd_fifo, strerror(errno));
               errors++;
            }
            else
            {
               system_log(DEBUG_SIGN, NULL, 0,
                          "%-*s: %s (%s) [mafdcmd].",
                          MAX_AFD_NAME_LENGTH, msa[position].afd_alias,
                          (cmd[0] == DISABLE_MON) ? "DISABLE" : "ENABLE", user);
            }
#ifdef WITHOUT_FIFO_RW_SUPPORT
            if (close(readfd) == -1)
            {
               system_log(DEBUG_SIGN, __FILE__, __LINE__,
                          "Failed to close() FIFO %s : %s",
                          mon_cmd_fifo, strerror(errno));
            }
#endif
            if (close(fd) == -1)
            {
               system_log(DEBUG_SIGN, __FILE__, __LINE__,
                          "Failed to close() FIFO %s : %s",
                          mon_cmd_fifo, strerror(errno));
            }
         }
      }

      if (options & RETRY_OPTION)
      {
         int  fd;
#ifdef WITHOUT_FIFO_RW_SUPPORT
         int  readfd;
#endif
         char retry_fifo[MAX_PATH_LENGTH];

         (void)sprintf(retry_fifo, "%s%s%s%d",
                       p_work_dir, FIFO_DIR, RETRY_MON_FIFO, position);
#ifdef WITHOUT_FIFO_RW_SUPPORT
         if (open_fifo_rw(retry_fifo, &readfd, &fd) == -1)
#else
         if ((fd = open(retry_fifo, O_RDWR)) == -1)
#endif
         {
            (void)fprintf(stderr,
                          "WARNING : Failed to open() %s : %s (%s %d)\n",
                          retry_fifo, strerror(errno),
                          __FILE__, __LINE__);
            errors++;
         }
         else
         {
            if (write(fd, &position, sizeof(int)) != sizeof(int))
            {
               (void)fprintf(stderr,
                             "WARNING : Failed to write() to %s : %s (%s %d)\n",
                             retry_fifo, strerror(errno),
                             __FILE__, __LINE__);
               errors++;
            }
#ifdef WITHOUT_FIFO_RW_SUPPORT
            if (close(readfd) == -1)
            {
               system_log(DEBUG_SIGN, __FILE__, __LINE__,
                          "Failed to close() FIFO %s : %s"
                          RETRY_FD_FIFO, strerror(errno));
            }
#endif
            if (close(fd) == -1)
            {
               system_log(DEBUG_SIGN, __FILE__, __LINE__,
                          "Failed to close() FIFO %s : %s"
                          RETRY_FD_FIFO, strerror(errno));
            }
         }
      }

      /*
       * Switch AFD
       */
      if (options & SWITCH_AFD_OPTION)
      {
         if (msa[position].afd_switching == NO_SWITCHING)
         {
            (void)fprintf(stderr,
                          "INFO    : AFD %s cannot be switched.\n",
                          msa[position].afd_alias);
            errors++;
         }
         else
         {
            if (msa[position].afd_toggle == (HOST_ONE - 1))
            {
               msa[position].afd_toggle = (HOST_TWO - 1);
            }
            else
            {
               msa[position].afd_toggle = (HOST_ONE - 1);
            }
            system_log(DEBUG_SIGN, NULL, 0,
                       "%-*s: SWITCHED (%s) [mafdcmd].",
                       MAX_AFD_NAME_LENGTH, msa[position].afd_alias, user);
         }
      }
   } /* for (i = 0; i < no_of_afd_names; i++) */

   (void)msa_detach();

   exit(errors);
}
コード例 #2
0
ファイル: mon_info.c プロジェクト: hfs/afd
/*++++++++++++++++++++++++++++ init_mon_info() ++++++++++++++++++++++++++*/
static void
init_mon_info(int *argc, char *argv[])
{
   int i,
       user_offset;
   char fake_user[MAX_FULL_USER_ID_LENGTH],
        *perm_buffer,
        profile[MAX_PROFILE_NAME_LENGTH + 1];

   if ((get_arg(argc, argv, "-?", NULL, 0) == SUCCESS) ||
       (get_arg(argc, argv, "-help", NULL, 0) == SUCCESS) ||
       (get_arg(argc, argv, "--help", NULL, 0) == SUCCESS))
   {
      usage(argv[0]);
      exit(SUCCESS);
   }
   if (get_arg(argc, argv, "-f", font_name, 40) == INCORRECT)
   {
      (void)strcpy(font_name, "fixed");
   }
   if (get_arg(argc, argv, "-a", afd_name, MAX_AFDNAME_LENGTH + 1) == INCORRECT)
   {
      usage(argv[0]);
      exit(INCORRECT);
   }
   if (get_arg(argc, argv, "-p", profile, MAX_PROFILE_NAME_LENGTH) == INCORRECT)
   {
      user_offset = 0;
      profile[0] = '\0';
   }
   else
   {
      (void)strcpy(user, profile);
      user_offset = strlen(profile);
   }
   if (get_mon_path(argc, argv, p_work_dir) < 0)
   {
      (void)fprintf(stderr,
                    "Failed to get working directory of AFD_MON. (%s %d)\n",
                    __FILE__, __LINE__);
      exit(INCORRECT);
   }

   /* Now lets see if user may use this program. */
   check_fake_user(argc, argv, MON_CONFIG_FILE, fake_user);
   switch (get_permissions(&perm_buffer, fake_user))
   {
      case NO_ACCESS : /* Cannot access afd.users file. */
         {
            char afd_user_file[MAX_PATH_LENGTH];

            (void)strcpy(afd_user_file, p_work_dir);
            (void)strcat(afd_user_file, ETC_DIR);
            (void)strcat(afd_user_file, AFD_USER_FILE);

            (void)fprintf(stderr,
                          "Failed to access `%s', unable to determine users permissions.\n",
                          afd_user_file);
         }
         exit(INCORRECT);

      case NONE :
         (void)fprintf(stderr, "%s\n", PERMISSION_DENIED_STR);
         exit(INCORRECT);

      case SUCCESS : /* Lets evaluate the permissions and see what */
                     /* the user may do.                           */
         eval_permissions(perm_buffer);
         free(perm_buffer);
         break;

      case INCORRECT : /* Hmm. Something did go wrong. Since we want to */
                       /* be able to disable permission checking let    */
                       /* the user have all permissions.                */
         editable = NO;
         break;

      default :
         (void)fprintf(stderr, "Impossible!! Remove the programmer!\n");
         exit(INCORRECT);
   }

   get_user(user, fake_user, user_offset);

   /* Attach to the MSA. */
   if ((i = msa_attach_passive()) < 0)
   {
      if (i == INCORRECT_VERSION)
      {
         (void)fprintf(stderr, "This program is not able to attach to the MSA due to incorrect version. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      else
      {
         (void)fprintf(stderr, "Failed to attach to MSA. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      exit(INCORRECT);
   }
   for (i = 0; i < no_of_afds; i++)
   {
      if (strcmp(msa[i].afd_alias, afd_name) == 0)
      {
         afd_position = i;
         break;
      }
   }
   if (afd_position < 0)
   {
      (void)fprintf(stderr,
                    "WARNING : Could not find AFD %s in MSA. (%s %d)\n",
                    afd_name, __FILE__, __LINE__);
      exit(INCORRECT);
   }

   /* Initialize values in MSA structure. */
   (void)strcpy(prev.real_hostname[0], msa[afd_position].hostname[0]);
   (void)strcpy(prev.real_hostname[1], msa[afd_position].hostname[1]);
   (void)strcpy(prev.r_work_dir, msa[afd_position].r_work_dir);
   (void)strcpy(prev.afd_version, msa[afd_position].afd_version);
   prev.port[0] = msa[afd_position].port[0];
   prev.port[1] = msa[afd_position].port[1];
   prev.afd_toggle = msa[afd_position].afd_toggle;
   prev.poll_interval = msa[afd_position].poll_interval;
   prev.max_connections = msa[afd_position].max_connections;
   prev.no_of_hosts = msa[afd_position].no_of_hosts;
   prev.last_data_time = msa[afd_position].last_data_time;
   prev.top_not = msa[afd_position].top_no_of_transfers[0];
   prev.top_tr = msa[afd_position].top_tr[0];
   prev.top_fr = msa[afd_position].top_fr[0];

   if (atexit(mon_info_exit) != 0)
   {
      (void)xrec(WARN_DIALOG, "Failed to set exit handler for %s : %s",
                 MON_INFO, strerror(errno));
   }
   check_window_ids(MON_INFO);

   return;
}
コード例 #3
0
ファイル: afd_mon_status.c プロジェクト: hfs/afd
/*$$$$$$$$$$$$$$$$$$$$$$$$$$ afd_mon_status() $$$$$$$$$$$$$$$$$$$$$$$$$$$*/
int
main(int argc, char *argv[])
{
   int  i;
   char work_dir[MAX_PATH_LENGTH];

   CHECK_FOR_VERSION(argc, argv);

   if (get_mon_path(&argc, argv, work_dir) < 0)
   {
      exit(INCORRECT);
   }
   p_work_dir = work_dir;

   /* Attach to the AFD Status Area */
   if (attach_afd_mon_status() < 0)
   {
      (void)fprintf(stderr,
                    "ERROR   : Failed to map to AFD_MON status area. (%s %d)\n",
                    __FILE__, __LINE__);
      exit(INCORRECT);
   }

   (void)fprintf(stdout, "AFD_MON              : %d\n",
                 (int)p_afd_mon_status->afd_mon);
   (void)fprintf(stdout, "Mon Sys Log          : %d\n",
                 (int)p_afd_mon_status->mon_sys_log);
   (void)fprintf(stdout, "Monitor Log          : %d\n",
                 (int)p_afd_mon_status->mon_log);
   (void)fprintf(stdout, "Monsyslog indicator  : %u <",
                 p_afd_mon_status->mon_sys_log_ec);
   for (i = 0; i < LOG_FIFO_SIZE; i++)
   {
      switch (p_afd_mon_status->mon_sys_log_fifo[i])
      {
         case INFO_ID :
            (void)fprintf(stdout, " I");
            break;
         case ERROR_ID :
            (void)fprintf(stdout, " E");
            break;
         case WARNING_ID :
            (void)fprintf(stdout, " W");
            break;
         case CONFIG_ID :
            (void)fprintf(stdout, " C");
            break;
         case FAULTY_ID :
            (void)fprintf(stdout, " F");
            break;
         default :
            (void)fprintf(stdout, " ?");
            break;
      }
   }
   (void)fprintf(stdout, " >\n");
   (void)fprintf(stdout, "Monlog indicator     : %u <",
                 p_afd_mon_status->mon_log_ec);
   for (i = 0; i < LOG_FIFO_SIZE; i++)
   {
      switch (p_afd_mon_status->mon_log_fifo[i])
      {
         case INFO_ID :
            (void)fprintf(stdout, " I");
            break;
         case CONFIG_ID :
            (void)fprintf(stdout, " C");
            break;
         case ERROR_ID :
            (void)fprintf(stdout, " E");
            break;
         case WARNING_ID :
            (void)fprintf(stdout, " W");
            break;
         case FAULTY_ID :
            (void)fprintf(stdout, " F");
            break;
         default :
            (void)fprintf(stdout, " ?");
            break;
      }
   }
   (void)fprintf(stdout, " >\n");
   (void)fprintf(stdout, "AFD_MON start time   : %s",
                 ctime(&p_afd_mon_status->start_time));

   exit(SUCCESS);
}
コード例 #4
0
ファイル: mon.c プロジェクト: hfs/afd
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ mon() $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
int
main(int argc, char *argv[])
{
   int            bytes_done = 0,
                  bytes_buffered = 0,
                  i,
                  retry_fd,
#ifdef WITHOUT_FIFO_RW_SUPPORT
                  retry_writefd,
#endif
                  status;
   time_t         new_day_time,
                  now,
                  retry_interval = RETRY_INTERVAL,
                  start_time;
   char           mon_log_fifo[MAX_PATH_LENGTH],
                  retry_fifo[MAX_PATH_LENGTH],
                  work_dir[MAX_PATH_LENGTH];
   fd_set         rset;
   struct stat    stat_buf;
   struct timeval timeout;

   CHECK_FOR_VERSION(argc, argv);

   if (get_mon_path(&argc, argv, work_dir) < 0)
   {
      exit(INCORRECT);
   }
   p_work_dir = work_dir;

   if (argc != 2)
   {
      (void)fprintf(stderr,
                    "Usage: %s [-w working directory] AFD-number\n", argv[0]);
      exit(SYNTAX_ERROR);
   }
   else
   {
      char *ptr = argv[1];

      while (*ptr != '\0')
      {
         if (isdigit((int)(*ptr)) == 0)
         {
            (void)fprintf(stderr,
                          "Usage: %s [-w working directory] AFD-number\n",
                          argv[0]);
            exit(SYNTAX_ERROR);
         }
         ptr++;
      }
      afd_no = atoi(argv[1]);
   }

   /* Initialize variables. */
   (void)strcpy(mon_log_fifo, p_work_dir);
   (void)strcat(mon_log_fifo, FIFO_DIR);
   (void)strcpy(retry_fifo, mon_log_fifo);
   (void)strcat(retry_fifo, RETRY_MON_FIFO);
   (void)strcat(retry_fifo, argv[1]);
   (void)strcat(mon_log_fifo, MON_LOG_FIFO);

   /* Open (create) monitor log fifo. */
   if ((stat(mon_log_fifo, &stat_buf) < 0) || (!S_ISFIFO(stat_buf.st_mode)))
   {
      if (make_fifo(mon_log_fifo) < 0)
      {
         (void)fprintf(stderr,
                       "ERROR   : Could not create fifo %s. (%s %d)\n",
                       mon_log_fifo, __FILE__, __LINE__);
         exit(INCORRECT);
      }
   }
#ifdef WITHOUT_FIFO_RW_SUPPORT
   if (open_fifo_rw(mon_log_fifo, &mon_log_readfd, &mon_log_fd) == -1)
#else
   if ((mon_log_fd = open(mon_log_fifo, O_RDWR)) == -1)
#endif
   {
      (void)fprintf(stderr, "ERROR   : Could not open() fifo %s : %s (%s %d)\n",
                    mon_log_fifo, strerror(errno), __FILE__, __LINE__);
      exit(INCORRECT);
   }

   /* Open (create) retry fifo. */
   if ((stat(retry_fifo, &stat_buf) < 0) || (!S_ISFIFO(stat_buf.st_mode)))
   {
      if (make_fifo(retry_fifo) < 0)
      {
         (void)fprintf(stderr,
                       "ERROR   : Could not create fifo %s. (%s %d)\n",
                       retry_fifo, __FILE__, __LINE__);
         exit(INCORRECT);
      }
   }
#ifdef WITHOUT_FIFO_RW_SUPPORT
   if (open_fifo_rw(retry_fifo, &retry_fd, &retry_writefd) == -1)
#else
   if ((retry_fd = open(retry_fifo, O_RDWR)) == -1)
#endif
   {
      (void)fprintf(stderr,
                    "ERROR   : Could not open() fifo %s : %s (%s %d)\n",
                    retry_fifo, strerror(errno), __FILE__, __LINE__);
      exit(INCORRECT);
   }

   /* Do some cleanups when we exit. */
   if (atexit(mon_exit) != 0)
   {
      system_log(FATAL_SIGN, __FILE__, __LINE__,
                 "Could not register exit handler : %s", strerror(errno));
      exit(INCORRECT);
   }
   if ((signal(SIGINT, sig_exit) == SIG_ERR) ||
       (signal(SIGQUIT, sig_exit) == SIG_ERR) ||
       (signal(SIGTERM, sig_exit) == SIG_ERR) ||
       (signal(SIGSEGV, sig_segv) == SIG_ERR) ||
       (signal(SIGBUS, sig_bus) == SIG_ERR) ||
       (signal(SIGPIPE, SIG_IGN) == SIG_ERR) ||
       (signal(SIGHUP, SIG_IGN) == SIG_ERR))
   {
      system_log(FATAL_SIGN, __FILE__, __LINE__,
                 "Could not set signal handlers : %s", strerror(errno));
      exit(INCORRECT);
   }

   if (msa_attach() != SUCCESS)
   {
      system_log(FATAL_SIGN, __FILE__, __LINE__, "Failed to attach to MSA.");
      exit(INCORRECT);
   }
   msa[afd_no].tr = 0;
   p_mon_alias = msa[afd_no].afd_alias;
   now = time(NULL);
   new_day_time = ((now / 86400) * 86400) + 86400;
   new_hour_time = ((now / 3600) * 3600) + 3600;
   for (i = 0; i < NO_OF_LOG_HISTORY; i++)
   {
      shift_log_his[i] = DONE;
   }

   for (;;)
   {
      msa[afd_no].connect_status = CONNECTING;
      timeout_flag = OFF;
      if ((status = tcp_connect(msa[afd_no].hostname[(int)msa[afd_no].afd_toggle],
                                msa[afd_no].port[(int)msa[afd_no].afd_toggle],
                                NO)) != SUCCESS)
      {
         if (timeout_flag == OFF)
         {
            if (status != INCORRECT)
            {
               mon_log(WARN_SIGN, NULL, 0, 0L, msg_str, "Failed to connect.");
            }

            /*
             * The tcp_connect() function wrote to the log file if status
             * is INCORRECT. Thus it is not necessary to do it here again.
             */
         }
         else
         {
            mon_log(ERROR_SIGN, __FILE__, __LINE__, 0L, NULL,
                    "Failed to connect due to timeout.");
         }
         msa[afd_no].connect_status = CONNECTION_DEFUNCT;
         retry_interval = RETRY_INTERVAL;

         if (msa[afd_no].afd_switching == AUTO_SWITCHING)
         {
            if (msa[afd_no].afd_toggle == (HOST_ONE - 1))
            {
               msa[afd_no].afd_toggle = (HOST_TWO - 1);
            }
            else
            {
               msa[afd_no].afd_toggle = (HOST_ONE - 1);
            }
            mon_log(WARN_SIGN, NULL, 0, 0L, NULL,
                    "Automatic switching to %s%d (%s at port %d).",
                    msa[afd_no].afd_alias, (int)msa[afd_no].afd_toggle + 1,
                    msa[afd_no].hostname[(int)msa[afd_no].afd_toggle],
                    msa[afd_no].port[(int)msa[afd_no].afd_toggle]);
         }
      }
      else
      {
         char current_afd_toggle;

         got_log_capabilities = NO;
         send_log_request = NO;
         if (msa[afd_no].afd_switching != NO_SWITCHING)
         {
            current_afd_toggle = msa[afd_no].afd_toggle;
         }
         msa[afd_no].connect_status = CONNECTION_ESTABLISHED;
         mon_log(INFO_SIGN, NULL, 0, 0L, NULL,
                 "========> AFDD Connected <========");
         if ((bytes_buffered = tcp_cmd("%s", START_STAT_CMD)) < 0)
         {
            if (bytes_buffered == -AFDD_SHUTTING_DOWN)
            {
               timeout_flag = ON;
               (void)tcp_quit();
               timeout_flag = OFF;
               msa[afd_no].connect_status = DISCONNECTED;
            }
            else
            {
               mon_log(ERROR_SIGN, __FILE__, __LINE__, 0L, msg_str,
                       "Failed to send %s command.", START_STAT_CMD);
               (void)tcp_quit();
               msa[afd_no].connect_status = CONNECTION_DEFUNCT;
            }
            retry_interval = RETRY_INTERVAL;
         }
         else
         {
            if ((msa[afd_no].connect_time != 0) &&
                (msa[afd_no].disconnect_time != 0))
            {
               start_time = time(NULL);
            }
            FD_ZERO(&rset);
            for (;;)
            {
               /*
                * Check if it is midnight so we can reset the top
                * rate counters.
                */
               if (time(&now) > new_day_time)
               {
                  for (i = (STORAGE_TIME - 1); i > 0; i--)
                  {
                     msa[afd_no].top_no_of_transfers[i] = msa[afd_no].top_no_of_transfers[i - 1];
                     msa[afd_no].top_tr[i] = msa[afd_no].top_tr[i - 1];
                     msa[afd_no].top_fr[i] = msa[afd_no].top_fr[i - 1];
                  }
                  msa[afd_no].top_no_of_transfers[0] = 0;
                  msa[afd_no].top_tr[0] = msa[afd_no].top_fr[0] = 0;
                  msa[afd_no].top_not_time = msa[afd_no].top_tr_time = msa[afd_no].top_fr_time = 0L;
                  new_day_time = ((now / 86400) * 86400) + 86400;
               }
               if (now > (new_hour_time + 120))
               {
                  for (i = 0; i < NO_OF_LOG_HISTORY; i++)
                  {
                     shift_log_his[i] = NO;
                  }
                  new_hour_time = ((now / 3600) * 3600) + 3600;
               }

               while (bytes_buffered > 0)
               {
                  if ((bytes_buffered = read_msg()) == INCORRECT)
                  {
                     bytes_buffered -= bytes_done;
                     msa[afd_no].connect_status = CONNECTION_DEFUNCT;
                     retry_interval = RETRY_INTERVAL;
                     goto done;
                  }
                  else
                  {
                     if (evaluate_message(&bytes_done) == AFDD_SHUTTING_DOWN)
                     {
                        bytes_buffered -= bytes_done;
                        retry_interval = RETRY_INTERVAL;
                        goto done;
                     }
                     bytes_buffered -= bytes_done;
                  }
               }

               if ((msa[afd_no].log_capabilities > 0) &&
                   (got_log_capabilities == YES) && (send_log_request == NO))
               {
                  if (send_got_log_capabilities(afd_no) == SUCCESS)
                  {
                     send_log_request = YES;
                  }
               }

               /* Initialise descriptor set and timeout. */
               FD_SET(sock_fd, &rset);
               timeout.tv_usec = 0;
               timeout.tv_sec = msa[afd_no].poll_interval;

               /* Wait for message x seconds and then continue. */
               status = select(sock_fd + 1, &rset, NULL, NULL, &timeout);

               if (FD_ISSET(sock_fd, &rset))
               {
                  msa[afd_no].last_data_time = time(NULL);
                  do
                  {
                     if ((bytes_buffered = read_msg()) == INCORRECT)
                     {
                        msa[afd_no].connect_status = CONNECTION_DEFUNCT;
                        retry_interval = RETRY_INTERVAL;
                        goto done;
                     }
                     else
                     {
                        if (evaluate_message(&bytes_done) == AFDD_SHUTTING_DOWN)
                        {
                           bytes_buffered -= bytes_done;
                           retry_interval = RETRY_INTERVAL;
                           goto done;
                        }
                        bytes_buffered -= bytes_done;
                     }
                  } while (bytes_buffered > 0);
               }
               else if (status == 0)
                    {
                       if ((bytes_buffered = tcp_cmd("%s", STAT_CMD)) < 0)
                       {
                          if (bytes_buffered != -AFDD_SHUTTING_DOWN)
                          {
                             mon_log(ERROR_SIGN, __FILE__, __LINE__, 0L, msg_str,
                                     "Failed to send %s command.", STAT_CMD);
                             (void)tcp_quit();
                             msa[afd_no].connect_status = CONNECTION_DEFUNCT;
                          }
                          retry_interval = RETRY_INTERVAL;
                          break;
                       }
                       else
                       {
                          msa[afd_no].last_data_time = now + msa[afd_no].poll_interval;
                          if (evaluate_message(&bytes_done) == AFDD_SHUTTING_DOWN)
                          {
                             bytes_buffered -= bytes_done;
                             retry_interval = RETRY_INTERVAL;
                             break;
                          }
                          bytes_buffered -= bytes_done;
                       }
                    }
                    else
                    {
                       system_log(FATAL_SIGN, __FILE__, __LINE__,
                                  "select() error : %s", strerror(errno));
                       exit(SELECT_ERROR);
                    }

               if ((msa[afd_no].connect_time != 0) &&
                   (msa[afd_no].disconnect_time != 0))
               {
                  if ((time(NULL) - start_time) >= msa[afd_no].connect_time)
                  {
                     if (tcp_quit() != SUCCESS)
                     {
                        mon_log(ERROR_SIGN, __FILE__, __LINE__, 0L, msg_str,
                                "Failed to send %s command.", QUIT_CMD);
                     }
                     retry_interval = msa[afd_no].disconnect_time;
                     msa[afd_no].connect_status = DISCONNECTED;
                     mon_log(INFO_SIGN, NULL, 0, 0L, NULL,
                             "========> Disconnect (due to connect interval %ds) <========",
                             msa[afd_no].connect_time);
                     break;
                  }
               }
               if ((msa[afd_no].afd_switching != NO_SWITCHING) &&
                   (current_afd_toggle != msa[afd_no].afd_toggle))
               {
                  if (tcp_quit() != SUCCESS)
                  {
                     mon_log(ERROR_SIGN, __FILE__, __LINE__, 0L, msg_str,
                             "Failed to send %s command.", QUIT_CMD);
                  }
                  msa[afd_no].connect_status = DISCONNECTED;
                  retry_interval = 0L;
                  mon_log(WARN_SIGN, NULL, 0, 0L, NULL,
                          "Switching to %s%d (%s at port %d).",
                          msa[afd_no].afd_alias, (int)msa[afd_no].afd_toggle,
                          msa[afd_no].hostname[(int)msa[afd_no].afd_toggle],
                          msa[afd_no].port[(int)msa[afd_no].afd_toggle]);
                  break;
               }
            } /* for (;;) */
         }
      }

done:
      if (adl != NULL)
      {
#ifdef HAVE_MMAP
         if (munmap((void *)adl, adl_size) == -1)
#else
         if (munmap_emu((void *)adl) == -1)
#endif
         {
            system_log(ERROR_SIGN, __FILE__, __LINE__,
                       "munmap() error : %s", strerror(errno));
         }
         adl = NULL;
      }
      if (ajl != NULL)
      {
#ifdef HAVE_MMAP
         if (munmap((void *)ajl, ajl_size) == -1)
#else
         if (munmap_emu((void *)ajl) == -1)
#endif
         {
            system_log(ERROR_SIGN, __FILE__, __LINE__,
                       "munmap() error : %s", strerror(errno));
         }
         ajl = NULL;
      }
      msa[afd_no].tr = 0;

      /* Initialise descriptor set and timeout. */
      FD_ZERO(&rset);
      FD_SET(retry_fd, &rset);
      timeout.tv_usec = 0L;
      timeout.tv_sec = retry_interval;

      /* Wait for message x seconds and then continue. */
      status = select(retry_fd + 1, &rset, NULL, NULL, &timeout);

      if (FD_ISSET(retry_fd, &rset))
      {
         /*
          * This fifo is just here to wake up, so disregard whats
          * in it.
          */
         if (read(retry_fd, msg_str, MAX_RET_MSG_LENGTH) < 0)
         {
            system_log(ERROR_SIGN, __FILE__, __LINE__,
                       "read() error on %s : %s", retry_fifo, strerror(errno));
         }
      }
      else if (status == -1)
           {
              system_log(FATAL_SIGN, __FILE__, __LINE__,
                         "select() error : %s", strerror(errno));
              exit(INCORRECT);
           }

      if (retry_interval == 0L)
      {
         if (msa[afd_no].disconnect_time != 0L)
         {
            retry_interval = msa[afd_no].disconnect_time;
         }
         else
         {
            retry_interval = RETRY_INTERVAL;
         }
      }
   } /* for (;;) */
}
コード例 #5
0
ファイル: hmon_ctrl.c プロジェクト: holger24/AFD
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$ msa_view() $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
int
main(int argc, char *argv[])
{
   int          flush_output,
                i, j,
                last = 0,
                position = -1;
   unsigned int interval;
   FILE         *output;
   char         afdname[MAX_AFDNAME_LENGTH + 1],
                *ptr,
                str_fc[5],
                str_fs[5],
                str_tr[5],
                str_fr[4],
                str_jq[4],
                str_at[4],
                str_ec[3],
                str_hec[3],
                val[MAX_PATH_LENGTH + 1],
                work_dir[MAX_PATH_LENGTH];
   const char   *color_pool[COLOR_POOL_SIZE] =
                {
                   HTML_COLOR_0,
                   HTML_COLOR_1,
                   HTML_COLOR_2,
                   HTML_COLOR_3,
                   HTML_COLOR_4,
                   HTML_COLOR_5,
                   HTML_COLOR_6,
                   HTML_COLOR_7,
                   HTML_COLOR_8,
                   HTML_COLOR_9,
                   HTML_COLOR_10,
                   HTML_COLOR_11,
                   HTML_COLOR_12,
                   HTML_COLOR_13,
                   HTML_COLOR_14,
                   HTML_COLOR_15,
                   HTML_COLOR_16,
                   HTML_COLOR_17,
                   HTML_COLOR_18,
#ifdef _WITH_WMO_SUPPORT
                   HTML_COLOR_19,
                   HTML_COLOR_20
#else
                   HTML_COLOR_19
#endif
                };

   CHECK_FOR_VERSION(argc, argv);

   if (get_mon_path(&argc, argv, work_dir) < 0)
   {
      exit(INCORRECT);
   }
   p_work_dir = work_dir;
#ifdef WITH_SETUID_PROGS
   set_afd_euid(work_dir);
#endif

   if (get_arg(&argc, argv, "-d", val, MAX_INT_LENGTH) == SUCCESS)
   {
      interval = atoi(val);
   }
   else
   {
      interval = 0;
   }

   if (get_arg(&argc, argv, "-o", val, MAX_PATH_LENGTH) == SUCCESS)
   {
      if ((output = fopen(val, "w")) == NULL)
      {
         (void)fprintf(stderr, "Failed to fopen() %s : %s\n",
                       val, strerror(errno));
         exit(INCORRECT);
      }
      flush_output = YES;
   }
   else
   {
      output = stdout;
      flush_output = NO;
   }

   if (argc == 2)
   {
      if (isdigit((int)(argv[1][0])) != 0)
      {
         position = atoi(argv[1]);
         last = position + 1;
      }
      else
      {
         (void)my_strncpy(afdname, argv[1], MAX_AFDNAME_LENGTH + 1);
      }
   }
   else if (argc == 1)
        {
           position = -2;
        }
        else
        {
           usage();
           exit(INCORRECT);
        }

   if ((i = msa_attach_passive()) < 0)
   {
      if (i == INCORRECT_VERSION)
      {
         (void)fprintf(stderr,
                       "ERROR   : This program is not able to attach to the MSA due to incorrect version. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      else
      {
         (void)fprintf(stderr, "ERROR   : Failed to attach to MSA. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      exit(INCORRECT);
   }

   if (position == -1)
   {
      for (i = 0; i < no_of_afds; i++)
      {
         if (my_strcmp(msa[i].afd_alias, afdname) == 0)
         {
            position = i;
            break;
         }
      }
      if (position < 0)
      {
         (void)fprintf(stderr,
                       "WARNING : Could not find AFD `%s' in MSA. (%s %d)\n",
                       afdname, __FILE__, __LINE__);
         exit(INCORRECT);
      }
      last = position + 1;
   }
   else if (position == -2)
        {
           last = no_of_afds;
           position = 0;
        }
   else if (position >= no_of_afds)
        {
           (void)fprintf(stderr,
                         "WARNING : There are only %d AFD's in the MSA. (%s %d)\n",
                         no_of_afds, __FILE__, __LINE__);
           exit(INCORRECT);
        }

   ptr = (char *)msa;
   ptr -= AFD_WORD_OFFSET;

   for (;;)
   {
      (void)fprintf(output,
                    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
      (void)fprintf(output,
                    "<html>\n<head>\n   <meta charset=\"utf-8\"/>\n");
      (void)fprintf(output,
                    "   <meta http-equiv=\"refresh\" content=\"5\"/>\n");
      (void)fprintf(output,
                    "   <title>AFD Monitor</title>\n</head>\n");
      (void)fprintf(output, "<body bgcolor=\"#F0ECD6\">\n");
      (void)fprintf(output, "<table align=center bgcolor=\"%s\">\n",
                    color_pool[(int)DEFAULT_BG]);
      (void)fprintf(output, "<tr>\n");
      (void)fprintf(output, "<th style=\"width:%dem;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">%s</th>\n",
                    MAX_AFDNAME_LENGTH, color_pool[(int)LABEL_BG], "AFD");
      (void)fprintf(output, "<th style=\"width:4em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">fc</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:4em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">fs</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:4em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">tr</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:3em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">fr</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:3em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">jq</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:3em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">at</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:2em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">ec</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "<th style=\"width:2em;\" align=\"center\" valign=\"middle\" bgcolor=\"%s\">eh</th>\n",
                    color_pool[(int)LABEL_BG]);
      (void)fprintf(output, "</tr>\n");
      for (j = position; j < last; j++)
      {
         (void)fprintf(output, "<tr>\n");
         if (msa[j].connect_status == NOT_WORKING2)
         {
            (void)fprintf(output,
                          "<td align=\"left\" valign=\"middle\" style=\"background-color:%s; color:%s\">%-*s</td>\n",
                          color_pool[(int)NOT_WORKING2], color_pool[(int)WHITE],
                          MAX_AFDNAME_LENGTH, msa[j].afd_alias);
         }
         else
         {
            (void)fprintf(output,
                          "<td align=\"left\" valign=\"middle\" bgcolor=\"%s\">%-*s</td>\n",
                          color_pool[(int)msa[j].connect_status],
                          MAX_AFDNAME_LENGTH, msa[j].afd_alias);
         }
         CREATE_FC_STRING(str_fc, msa[j].fc);
         (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n", color_pool[(int)CHAR_BACKGROUND], str_fc);
         CREATE_FS_STRING(str_fs,  msa[j].fs);
         (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n", color_pool[(int)CHAR_BACKGROUND], str_fs);
         CREATE_FS_STRING(str_tr,  msa[j].tr);
         (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n", color_pool[(int)CHAR_BACKGROUND], str_tr);
         CREATE_JQ_STRING(str_fr, msa[j].fr);
         (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n", color_pool[(int)CHAR_BACKGROUND], str_fr);
         CREATE_JQ_STRING(str_jq, msa[j].jobs_in_queue);
         if ((msa[j].danger_no_of_jobs != 0) &&
             (msa[j].jobs_in_queue > msa[j].danger_no_of_jobs) &&
             (msa[j].jobs_in_queue <= ((msa[j].danger_no_of_jobs * 2) - STOP_AMG_THRESHOLD - DIRS_IN_FILE_DIR)))
         {
            (void)fprintf(output,
                          "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n",
                          color_pool[(int)WARNING_ID], str_jq);
         }
         else if ((msa[j].danger_no_of_jobs != 0) &&
                  (msa[j].jobs_in_queue > ((msa[j].danger_no_of_jobs * 2) - STOP_AMG_THRESHOLD - DIRS_IN_FILE_DIR)))
              {
                 (void)fprintf(output,
                               "<td align=\"right\" valign=\"middle\" style=\"background-color:%s; color:%s\">%s</td>\n",
                               color_pool[(int)NOT_WORKING2], color_pool[(int)WHITE],
                               str_jq);
              }
              else
              {
                 (void)fprintf(output,
                               "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n",
                               color_pool[(int)CHAR_BACKGROUND], str_jq);
              }
         CREATE_JQ_STRING(str_at, msa[j].no_of_transfers);
         (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n", color_pool[(int)CHAR_BACKGROUND], str_at);
         CREATE_EC_STRING(str_ec, msa[j].ec);
         if (msa[j].ec > 0)
         {
            (void)fprintf(output, "<td align=\"right\" valign=\"middle\" style=\"background-color:%s; color:%s\">%s</td>\n",
                          color_pool[(int)CHAR_BACKGROUND],
                          color_pool[(int)NOT_WORKING2], str_ec);
         }
         else
         {
            (void)fprintf(output,
                          "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n",
                          color_pool[(int)CHAR_BACKGROUND], str_ec);
         }
         CREATE_EC_STRING(str_hec, msa[j].host_error_counter);
         if (msa[j].host_error_counter > 0)
         {
            (void)fprintf(output,
                          "<td align=\"right\" valign=\"middle\" style=\"background-color:%s; color:%s\">%s</td>\n",
                          color_pool[(int)NOT_WORKING2], color_pool[(int)WHITE],
                          str_hec);
         }
         else
         {
            (void)fprintf(output, "<td align=\"right\" valign=\"middle\" bgcolor=\"%s\">%s</td>\n",
                          color_pool[(int)CHAR_BACKGROUND], str_hec);
         }
         (void)fprintf(output, "</tr>\n");
      }
      (void)fprintf(output, "</table>\n</body>\n</html>\n");

      if (interval > 0)
      {
         if (flush_output == YES)
         {
            (void)fflush(output);
         }
         (void)sleep(interval);
         if (flush_output == YES)
         {
            (void)ftruncate(fileno(output), 0);
            (void)fseek(output, 0L, SEEK_SET);
         }
      }
      else
      {
         break;
      }
   }

   exit(SUCCESS);
}
コード例 #6
0
ファイル: msa_view.c プロジェクト: hfs/afd
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$ msa_view() $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
int
main(int argc, char *argv[])
{
   int  i, j,
        last = 0,
        position = -1;
   char afdname[MAX_AFDNAME_LENGTH + 1],
        *ptr,
        work_dir[MAX_PATH_LENGTH];

   CHECK_FOR_VERSION(argc, argv);

   if (get_mon_path(&argc, argv, work_dir) < 0)
   {
      exit(INCORRECT);
   }
   p_work_dir = work_dir;

   if (argc == 2)
   {
      if (isdigit((int)(argv[1][0])) != 0)
      {
         position = atoi(argv[1]);
         last = position + 1;
      }
      else
      {
         (void)my_strncpy(afdname, argv[1], MAX_AFDNAME_LENGTH + 1);
      }
   }
   else if (argc == 1)
        {
           position = -2;
        }
        else
        {
           usage();
           exit(INCORRECT);
        }

   if ((i = msa_attach_passive()) < 0)
   {
      if (i == INCORRECT_VERSION)
      {
         (void)fprintf(stderr,
                       "ERROR   : This program is not able to attach to the MSA due to incorrect version. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      else
      {
         (void)fprintf(stderr, "ERROR   : Failed to attach to MSA. (%s %d)\n",
                       __FILE__, __LINE__);
      }
      exit(INCORRECT);
   }

   if (position == -1)
   {
      for (i = 0; i < no_of_afds; i++)
      {
         if (strcmp(msa[i].afd_alias, afdname) == 0)
         {
            position = i;
            break;
         }
      }
      if (position < 0)
      {
         (void)fprintf(stderr,
                       "WARNING : Could not find AFD `%s' in MSA. (%s %d)\n",
                       afdname, __FILE__, __LINE__);
         exit(INCORRECT);
      }
      last = position + 1;
   }
   else if (position == -2)
        {
           last = no_of_afds;
           position = 0;
        }
   else if (position >= no_of_afds)
        {
           (void)fprintf(stderr,
                         "WARNING : There are only %d AFD's in the MSA. (%s %d)\n",
                         no_of_afds, __FILE__, __LINE__);
           exit(INCORRECT);
        }

   ptr = (char *)msa;
   ptr -= AFD_WORD_OFFSET;
   (void)fprintf(stdout, " Number of hosts: %d  MSA ID: %d  Struct Version: %d\n\n",
                 no_of_afds, msa_id, (int)(*(ptr + SIZEOF_INT + 1 + 1 + 1)));

   for (j = position; j < last; j++)
   {
      (void)fprintf(stdout, "=============================> %s (%d) <=============================\n",
                    msa[j].afd_alias, j);
#ifdef NEW_MSA
      (void)fprintf(stdout, "AFD alias CRC      : %x\n", msa[j].afd_id);
#endif
      (void)fprintf(stdout, "Remote work dir    : %s\n", msa[j].r_work_dir);
      (void)fprintf(stdout, "Remote AFD version : %s\n", msa[j].afd_version);
      (void)fprintf(stdout, "Remote command     : %s\n", msa[j].rcmd);
      (void)fprintf(stdout, "Remote options     : %d =>", msa[j].options);
      if (msa[j].options == 0)
      {
         (void)fprintf(stdout, " None");
      }
      else
      {
         if (msa[j].options & COMPRESS_FLAG)
         {
            (void)fprintf(stdout, " COMPRESS");
         }
         if (msa[j].options & MINUS_Y_FLAG)
         {
            (void)fprintf(stdout, " MINUS_Y");
         }
         if (msa[j].options & DONT_USE_FULL_PATH_FLAG)
         {
            (void)fprintf(stdout, " DONT_USE_FULL_PATH");
         }
         if (msa[j].options & ENABLE_SSL_ENCRYPTION)
         {
            (void)fprintf(stdout, " ENABLE_SSL_ENCRYPTION");
         }
         if (msa[j].options & AFDD_SYSTEM_LOG)
         {
            (void)fprintf(stdout, " System");
         }
         if (msa[j].options & AFDD_RECEIVE_LOG)
         {
            (void)fprintf(stdout, " Receive");
         }
         if (msa[j].options & AFDD_TRANSFER_LOG)
         {
            (void)fprintf(stdout, " Transfer");
         }
         if (msa[j].options & AFDD_TRANSFER_DEBUG_LOG)
         {
            (void)fprintf(stdout, " Trans_db");
         }
#ifdef _INPUT_LOG
         if (msa[j].options & AFDD_INPUT_LOG)
         {
            (void)fprintf(stdout, " Input");
         }
#endif
#ifdef _DISTRIBUTION_LOG
         if (msa[j].options & AFDD_DISTRIBUTION_LOG)
         {
            (void)fprintf(stdout, " Distribution");
         }
#endif
#ifdef _PRODUCTION_LOG
         if (msa[j].options & AFDD_PRODUCTION_LOG)
         {
            (void)fprintf(stdout, " Production");
         }
#endif
#ifdef _OUTPUT_LOG
         if (msa[j].options & AFDD_OUTPUT_LOG)
         {
            (void)fprintf(stdout, " Output");
         }
#endif
#ifdef _DELETE_LOG
         if (msa[j].options & AFDD_DELETE_LOG)
         {
            (void)fprintf(stdout, " Delete");
         }
#endif
         if (msa[j].options & AFDD_JOB_DATA)
         {
            (void)fprintf(stdout, " Job_data");
         }
         if (msa[j].options & AFDD_COMPRESSION_1)
         {
            (void)fprintf(stdout, " Compression1");
         }
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Log capabilities   : %d =>", msa[j].log_capabilities);
      if (msa[j].log_capabilities == 0)
      {
         (void)fprintf(stdout, " None");
      }
      else
      {
         if (msa[j].log_capabilities & AFDD_SYSTEM_LOG)
         {
            (void)fprintf(stdout, " System");
         }
         if (msa[j].log_capabilities & AFDD_EVENT_LOG)
         {
            (void)fprintf(stdout, " Event");
         }
         if (msa[j].log_capabilities & AFDD_RECEIVE_LOG)
         {
            (void)fprintf(stdout, " Receive");
         }
         if (msa[j].log_capabilities & AFDD_TRANSFER_LOG)
         {
            (void)fprintf(stdout, " Transfer");
         }
         if (msa[j].log_capabilities & AFDD_TRANSFER_DEBUG_LOG)
         {
            (void)fprintf(stdout, " Trans_db");
         }
#ifdef _INPUT_LOG
         if (msa[j].log_capabilities & AFDD_INPUT_LOG)
         {
            (void)fprintf(stdout, " Input");
         }
#endif
#ifdef _DISTRIBUTION_LOG
         if (msa[j].log_capabilities & AFDD_DISTRIBUTION_LOG)
         {
            (void)fprintf(stdout, " Distribution");
         }
#endif
#ifdef _PRODUCTION_LOG
         if (msa[j].log_capabilities & AFDD_PRODUCTION_LOG)
         {
            (void)fprintf(stdout, " Production");
         }
#endif
#ifdef _OUTPUT_LOG
         if (msa[j].log_capabilities & AFDD_OUTPUT_LOG)
         {
            (void)fprintf(stdout, " Output");
         }
#endif
#ifdef _DELETE_LOG
         if (msa[j].log_capabilities & AFDD_DELETE_LOG)
         {
            (void)fprintf(stdout, " Delete");
         }
#endif
         if (msa[j].log_capabilities & AFDD_JOB_DATA)
         {
            (void)fprintf(stdout, " Job_data");
         }
         if (msa[j].log_capabilities & AFDD_COMPRESSION_1)
         {
            (void)fprintf(stdout, " Compression1");
         }
      }
      (void)fprintf(stdout, "\n");
      if (msa[j].afd_switching != NO_SWITCHING)
      {
         (void)fprintf(stdout, "Real hostname 0    : %s\n", msa[j].hostname[0]);
         (void)fprintf(stdout, "TCP port 0         : %d\n", msa[j].port[0]);
         (void)fprintf(stdout, "Real hostname 1    : %s\n", msa[j].hostname[1]);
         (void)fprintf(stdout, "TCP port 1         : %d\n", msa[j].port[1]);
         (void)fprintf(stdout, "Current host       : AFD %d\n", msa[j].afd_toggle);
         (void)fprintf(stdout, "Switch type        : %s\n", (msa[j].afd_switching == AUTO_SWITCHING) ? "Auto" : "User");
      }
      else
      {
         (void)fprintf(stdout, "Real hostname      : %s\n", msa[j].hostname[0]);
         (void)fprintf(stdout, "TCP port           : %d\n", msa[j].port[0]);
         (void)fprintf(stdout, "Switch type        : No switching.\n");
      }
      (void)fprintf(stdout, "Poll interval      : %d\n", msa[j].poll_interval);
      (void)fprintf(stdout, "Connect time       : %d\n", msa[j].connect_time);
      (void)fprintf(stdout, "Disconnect time    : %d\n", msa[j].disconnect_time);
      (void)fprintf(stdout, "Status of AMG      : %d\n", (int)msa[j].amg);
      (void)fprintf(stdout, "Status of FD       : %d\n", (int)msa[j].fd);
      (void)fprintf(stdout, "Status of AW       : %d\n", (int)msa[j].archive_watch);
      (void)fprintf(stdout, "Jobs in queue      : %d\n", msa[j].jobs_in_queue);
      (void)fprintf(stdout, "Active transfers   : %d\n", msa[j].no_of_transfers);
      (void)fprintf(stdout, "TOP no. process    : %d", msa[j].top_no_of_transfers[0]);
      for (i = 1; i < STORAGE_TIME; i++)
      {
         (void)fprintf(stdout, " %d", msa[j].top_no_of_transfers[i]);
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Last TOP no process: %s", ctime(&msa[j].top_not_time));
      (void)fprintf(stdout, "Maximum connections: %d\n", msa[j].max_connections);
      (void)fprintf(stdout, "Sys log EC         : %u  |", msa[j].sys_log_ec);
      for (i = 0; i < LOG_FIFO_SIZE; i++)
      {
         switch (msa[j].sys_log_fifo[i])
         {
            case INFO_ID :
               (void)fprintf(stdout, " I");
               break;

            case ERROR_ID :
               (void)fprintf(stdout, " E");
               break;

            case WARNING_ID :
               (void)fprintf(stdout, " W");
               break;

            case CONFIG_ID :
               (void)fprintf(stdout, " C");
               break;

            case FAULTY_ID :
               (void)fprintf(stdout, " F");
               break;

            default :
               (void)fprintf(stdout, " ?");
               break;
         }
      }
      (void)fprintf(stdout, " |\n");
      (void)fprintf(stdout, "Receive History    :");
      for (i = 0; i < MAX_LOG_HISTORY; i++)
      {
         switch (msa[j].log_history[RECEIVE_HISTORY][i])
         {
            case INFO_ID :
               (void)fprintf(stdout, " I");
               break;

            case ERROR_ID :
               (void)fprintf(stdout, " E");
               break;

            case WARNING_ID :
               (void)fprintf(stdout, " W");
               break;

            case FAULTY_ID :
               (void)fprintf(stdout, " F");
               break;

            default :
               (void)fprintf(stdout, " ?");
               break;
         }
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "System History     :");
      for (i = 0; i < MAX_LOG_HISTORY; i++)
      {
         switch (msa[j].log_history[SYSTEM_HISTORY][i])
         {
            case INFO_ID :
               (void)fprintf(stdout, " I");
               break;

            case ERROR_ID :
               (void)fprintf(stdout, " E");
               break;

            case WARNING_ID :
               (void)fprintf(stdout, " W");
               break;

            case CONFIG_ID :
               (void)fprintf(stdout, " C");
               break;

            case FAULTY_ID :
               (void)fprintf(stdout, " F");
               break;

            default :
               (void)fprintf(stdout, " ?");
               break;
         }
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Transfer History   :");
      for (i = 0; i < MAX_LOG_HISTORY; i++)
      {
         switch (msa[j].log_history[TRANSFER_HISTORY][i])
         {
            case INFO_ID :
               (void)fprintf(stdout, " I");
               break;

            case ERROR_ID :
               (void)fprintf(stdout, " E");
               break;

            case WARNING_ID :
               (void)fprintf(stdout, " W");
               break;

            case ERROR_OFFLINE_ID :
               (void)fprintf(stdout, " O");
               break;

            case FAULTY_ID :
               (void)fprintf(stdout, " F");
               break;

            default :
               (void)fprintf(stdout, " ?");
               break;
         }
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Host error counter : %d\n", msa[j].host_error_counter);
      (void)fprintf(stdout, "Number of hosts    : %d\n", msa[j].no_of_hosts);
      (void)fprintf(stdout, "Number of dirs     : %d\n", msa[j].no_of_dirs);
      (void)fprintf(stdout, "Number of jobs     : %d\n", msa[j].no_of_jobs);
      (void)fprintf(stdout, "fc                 : %u\n", msa[j].fc);
#if SIZEOF_OFF_T == 4
      (void)fprintf(stdout, "fs                 : %lu\n", msa[j].fs);
      (void)fprintf(stdout, "tr                 : %lu\n", msa[j].tr);
      (void)fprintf(stdout, "TOP tr             : %lu", msa[j].top_tr[0]);
#else
      (void)fprintf(stdout, "fs                 : %llu\n", msa[j].fs);
      (void)fprintf(stdout, "tr                 : %llu\n", msa[j].tr);
      (void)fprintf(stdout, "TOP tr             : %llu", msa[j].top_tr[0]);
#endif
      for (i = 1; i < STORAGE_TIME; i++)
      {
#if SIZEOF_OFF_T == 4
         (void)fprintf(stdout, " %lu", msa[j].top_tr[i]);
#else
         (void)fprintf(stdout, " %llu", msa[j].top_tr[i]);
#endif
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Last TOP tr time   : %s", ctime(&msa[j].top_tr_time));
      (void)fprintf(stdout, "fr                 : %u\n", msa[j].fr);
      (void)fprintf(stdout, "TOP fr             : %u", msa[j].top_fr[0]);
      for (i = 1; i < STORAGE_TIME; i++)
      {
         (void)fprintf(stdout, " %u", msa[j].top_fr[i]);
      }
      (void)fprintf(stdout, "\n");
      (void)fprintf(stdout, "Last TOP fr time   : %s", ctime(&msa[j].top_fr_time));
      (void)fprintf(stdout, "ec                 : %u\n", msa[j].ec);
      (void)fprintf(stdout, "Last data time     : %s", ctime(&msa[j].last_data_time));
      for (i = 0; i < SUM_STORAGE; i++)
      {
         (void)fprintf(stdout, "                   : --- %s sum values ---\n", sum_stat_type[i]);
         (void)fprintf(stdout, "files_received     : %u\n", msa[j].files_received[i]);
#if SIZEOF_OFF_T == 4
         (void)fprintf(stdout, "bytes_received     : %lu\n", msa[j].bytes_received[i]);
#else
         (void)fprintf(stdout, "bytes_received     : %llu\n", msa[j].bytes_received[i]);
#endif
         (void)fprintf(stdout, "files_send         : %u\n", msa[j].files_send[i]);
#if SIZEOF_OFF_T == 4
         (void)fprintf(stdout, "bytes_send         : %lu\n", msa[j].bytes_send[i]);
#else
         (void)fprintf(stdout, "bytes_send         : %llu\n", msa[j].bytes_send[i]);
#endif
         (void)fprintf(stdout, "connections        : %u\n", msa[j].connections[i]);
         (void)fprintf(stdout, "total_errors       : %u\n", msa[j].total_errors[i]);
#if SIZEOF_OFF_T == 4
         (void)fprintf(stdout, "log_bytes_received : %lu\n", msa[j].log_bytes_received[i]);
#else
         (void)fprintf(stdout, "log_bytes_received : %llu\n", msa[j].log_bytes_received[i]);
#endif
      }
      (void)fprintf(stdout, "                   : ---------------------\n");
      switch (msa[j].connect_status)
      {
         case CONNECTION_ESTABLISHED :
            (void)fprintf(stdout, "Connect status     : CONNECTION_ESTABLISHED\n");
            break;
         case CONNECTION_DEFUNCT :
            (void)fprintf(stdout, "Connect status     : CONNECTION_DEFUNCT\n");
            break;
         case DISCONNECTED :
            (void)fprintf(stdout, "Connect status     : DISCONNECTED\n");
            break;
         case DISABLED : /* This AFD is disabled, ie should not be monitored. */
            (void)fprintf(stdout, "Connect status     : DISABLED\n");
            break;
         default : /* Should not get here. */
            (void)fprintf(stdout, "Connect status     : Unknown\n");
      }
      (void)fprintf(stdout, "Special flag (%3d) :", msa[j].special_flag);
      if (msa[j].special_flag & SUM_VAL_INITIALIZED)
      {
         (void)fprintf(stdout, " SUM_VAL_INITIALIZED");
      }
      (void)fprintf(stdout, "\n");
      if (msa[j].convert_username[0][0][0] != '\0')
      {
         (void)fprintf(stdout, "Convert user name  : %s -> %s\n",
                       msa[j].convert_username[0][0],
                       msa[j].convert_username[0][1]);
         for (i = 1; i < MAX_CONVERT_USERNAME; i++)
         {
            (void)fprintf(stdout, "                   : %s -> %s\n",
                          msa[j].convert_username[i][0],
                          msa[j].convert_username[i][1]);
         }
      }
   }

   exit(SUCCESS);
}