Example #1
0
bool MonitorItem::docmd(const char* command)
{
   if (!doconnect()) {
      return false;
   }

   if (command[0] != 0) {
      writecmd(command);
   }

   emit clearText(get_name());
   bool jobRunning = false;

   while (1) {
      int stat;
      if ((stat = bnet_recv(d->DSock)) >= 0) {
         strip_trailing_newline(d->DSock->msg);
         QString msg = QString::fromUtf8(d->DSock->msg);
         emit appendText(QString::fromUtf8(get_name()), msg);
         if (d->type == R_CLIENT) {
             if (msg.contains("Job started:"))
                jobRunning = true;
         }
      } else if (stat == BNET_SIGNAL) {
         if (d->DSock->msglen == BNET_EOD) {
            // qDebug() << "<< EOD >>";
             if (d->type == R_CLIENT)
                emit jobIsRunning (jobRunning);
            return true;
         }
         else if (d->DSock->msglen == BNET_SUB_PROMPT) {
            // qDebug() << "<< PROMPT >>";
            return false;
         }
         else if (d->DSock->msglen == BNET_HEARTBEAT) {
            bnet_sig(d->DSock, BNET_HB_RESPONSE);
         }
         else {
            qDebug() << bnet_sig_to_ascii(d->DSock);
         }
      } else { /* BNET_HARDEOF || BNET_ERROR */
         d->DSock = NULL;
         d->state = MonitorItem::Error;
         emit statusChanged(get_name(), d->state);
         emit showStatusbarMessage("Error : BNET_HARDEOF or BNET_ERROR");
         //fprintf(stderr, "<< ERROR >>\n"));
         return false;
      } /* if ((stat = bnet_recv(d->DSock)) >= 0) */

      if (is_bnet_stop(d->DSock)) {
         d->DSock = NULL;
         d->state = MonitorItem::Error;
         emit statusChanged(get_name(), d->state);
         emit showStatusbarMessage("Error : Connection closed.");
         //fprintf(stderr, "<< STOP >>\n");
         return false;            /* error or term */
      } /* if (is_bnet_stop(d->DSock) */

   } /* while (1) */
}
Example #2
0
int docmd(monitoritem* item, const char* command, char *answer) {

   int stat;
   char num;
   const char *dname;

   dname = "";

   if (!item->D_sock) {

      DIRRES* dird;
      CLIENT* filed;
      STORE* stored;

      switch (item->type) {
      case R_DIRECTOR:
                 dird = (DIRRES*)item->resource;
                 item->D_sock = new_bsock();
                 item->D_sock->connect(NULL, 0, 0, 0, "Director daemon", dird->address, NULL, dird->DIRport, 0);
                 dname = "Director";
                 break;
      case R_CLIENT:
                 filed = (CLIENT*)item->resource;
                 item->D_sock = new_bsock();
                 item->D_sock->connect(NULL, 0, 0, 0, "File daemon", filed->address, NULL, filed->FDport, 0);
                 dname = "FileDaemon";
                 break;
      case R_STORAGE:
                 stored = (STORE*)item->resource;
                 item->D_sock = new_bsock();
                 item->D_sock->connect(NULL, 0, 0, 0, "Storage daemon", stored->address, NULL, stored->SDport, 0);
                 dname = "StorageDaemon";
                 break;
      default:
                 printf("Error, currentitem is not a Client, a Storage or a Director..\n");
                 return STATE_UNKNOWN;
      }

      if (item->D_sock == NULL) {
                 sprintf (answer, "BACULA CRITICAL - Cannot connect to %s!", dname);
                 return STATE_CRITICAL;
      }

      if (!authenticate_daemon(item)) {
                sprintf (answer, "BACULA CRITICAL - Cannot authenticate to %s: %s", dname, item->D_sock->msg);
                item->D_sock = NULL;
                return STATE_CRITICAL;
      }

   }

   if (command[0] != 0)
      writecmd(item, command);

   while(1) {
      if ((stat = item->D_sock->recv()) >= 0) {

        /* welcome message of director */
        if ((item->type == R_DIRECTOR) && (strncmp(item->D_sock->msg, "Using ", 6) == 0))
                continue;

        if (sscanf(item->D_sock->msg, OKqstatus, &num) != 1) {
                /* Error, couldn't find OK */
                sprintf (answer, "BACULA CRITICAL - %s Status: %s", dname, item->D_sock->msg);
                return STATE_CRITICAL;
        } else {
                sprintf (answer, "BACULA OK - %s Status OK", dname);
                return STATE_OK;
        }
      }
      else if (stat == BNET_SIGNAL) {
                if (item->D_sock->msglen == BNET_EOD) {
                strcpy(answer, "BACULA WARNING - << EOD >>");
                return STATE_WARNING;
         }
         else if (item->D_sock->msglen == BNET_SUB_PROMPT) {
            strcpy(answer, "BACULA WARNING - BNET_SUB_PROMPT signal received.");
            return STATE_WARNING;
         }
         else if (item->D_sock->msglen == BNET_HEARTBEAT) {
            item->D_sock->signal(BNET_HB_RESPONSE);
         }
         else {
                sprintf(answer, "BACULA WARNING - Unexpected signal received : %s ", bnet_sig_to_ascii(item->D_sock));
         }
      }
      else { /* BNET_HARDEOF || BNET_ERROR */
                 strcpy(answer, "BACULA CRITICAL - ERROR: BNET_HARDEOF or BNET_ERROR");
                 item->D_sock = NULL;
                 return STATE_CRITICAL;
      }

      if (item->D_sock->is_stop()) {
                 item->D_sock = NULL;
                 return STATE_WARNING;
      }
   }
}
Example #3
0
static void read_and_process_input(FILE *input, BSOCK *UA_sock)
{
   const char *prompt = "*";
   bool at_prompt = false;
   int tty_input = isatty(fileno(input));
   int status;
   btimer_t *tid = NULL;

   while (1) {
      if (at_prompt) {                /* don't prompt multiple times */
         prompt = "";
      } else {
         prompt = "*";
         at_prompt = true;
      }
      if (tty_input) {
         status = get_cmd(input, prompt, UA_sock, 30);
         if (usrbrk() == 1) {
            clrbrk();
         }
         if (usrbrk()) {
            break;
         }
      } else {
         /*
          * Reading input from a file
          */
         int len = sizeof_pool_memory(UA_sock->msg) - 1;
         if (usrbrk()) {
            break;
         }
         if (fgets(UA_sock->msg, len, input) == NULL) {
            status = -1;
         } else {
            sendit(UA_sock->msg);     /* echo to terminal */
            strip_trailing_junk(UA_sock->msg);
            UA_sock->msglen = strlen(UA_sock->msg);
            status = 1;
         }
      }
      if (status < 0) {
         break;                       /* error or interrupt */
      } else if (status == 0) {       /* timeout */
         if (bstrcmp(prompt, "*")) {
            tid = start_bsock_timer(UA_sock, timeout);
            UA_sock->fsend(".messages");
            stop_bsock_timer(tid);
         } else {
            continue;
         }
      } else {
         at_prompt = false;
         /*
          * @ => internal command for us
          */
         if (UA_sock->msg[0] == '@') {
            parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
            if (!do_a_command(input, UA_sock)) {
               break;
            }
            continue;
         }
         tid = start_bsock_timer(UA_sock, timeout);
         if (!UA_sock->send()) {      /* send command */
            stop_bsock_timer(tid);
            break;                    /* error */
         }
         stop_bsock_timer(tid);
      }

      if (bstrcmp(UA_sock->msg, ".quit") || bstrcmp(UA_sock->msg, ".exit")) {
         break;
      }

      tid = start_bsock_timer(UA_sock, timeout);
      while ((status = UA_sock->recv()) >= 0 ||
             ((status == BNET_SIGNAL) && (
              (UA_sock->msglen != BNET_EOD) &&
              (UA_sock->msglen != BNET_MAIN_PROMPT) &&
              (UA_sock->msglen != BNET_SUB_PROMPT)))) {
         if (status == BNET_SIGNAL) {
            if (UA_sock->msglen == BNET_START_RTREE) {
               file_selection = true;
            } else if (UA_sock->msglen == BNET_END_RTREE) {
               file_selection = false;
            }
            continue;
         }

         if (at_prompt) {
            if (!stop) {
               sendit("\n");
            }
            at_prompt = false;
         }

         /*
          * Suppress output if running in background or user hit ctl-c
          */
         if (!stop && !usrbrk()) {
            if (UA_sock->msg) {
               sendit(UA_sock->msg);
            }
         }
      }
      stop_bsock_timer(tid);

      if (usrbrk() > 1) {
         break;
      } else {
         clrbrk();
      }
      if (!stop) {
         fflush(stdout);
      }

      if (is_bnet_stop(UA_sock)) {
         break;                       /* error or term */
      } else if (status == BNET_SIGNAL) {
         if (UA_sock->msglen == BNET_SUB_PROMPT) {
            at_prompt = true;
         }
         Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
      }
   }
}