]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/status.c
Use SMARTALLOC+memset instead of overload new/delete that doesn't work in bat
[bacula/bacula] / bacula / src / stored / status.c
index 5a82b753aedd48cf83473cd16a99a4dea47e8f09..1d51e1f2dd6bb64ddd2c735680aa33c9af300acd 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2003-2010 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
@@ -30,7 +30,6 @@
  *
  *     Kern Sibbald, May MMIII
  *
- *   Version $Id$
  *
  */
 
@@ -42,8 +41,6 @@
 
 /* Imported variables */
 extern BSOCK *filed_chan;
-extern int r_first, r_last;
-extern struct s_res resources[];
 extern void *start_heap;
 
 /* Static variables */
@@ -59,6 +56,7 @@ static void sendit(POOL_MEM &msg, int len, STATUS_PKT *sp);
 static void sendit(const char *msg, int len, void *arg);
 
 static void send_blocked_status(DEVICE *dev, STATUS_PKT *sp);
+static void send_device_status(DEVICE *dev, STATUS_PKT *sp);
 static void list_terminated_jobs(STATUS_PKT *sp);
 static void list_running_jobs(STATUS_PKT *sp);
 static void list_jobs_waiting_on_reservation(STATUS_PKT *sp);
@@ -98,11 +96,11 @@ void output_status(STATUS_PKT *sp)
    list_devices(sp);
 
 
-   len = Mmsg(msg, _("In Use Volume status:\n"));
-   sendit(msg, len, sp);
+   len = Mmsg(msg, _("Used Volume status:\n"));
+   if (!sp->api) sendit(msg, len, sp);
 
    list_volumes(sendit, (void *)sp);
-   sendit("====\n\n", 6, sp);
+   if (!sp->api) sendit("====\n\n", 6, sp);
 
 #ifdef xxx
    if (debug_level > 10) {
@@ -113,6 +111,7 @@ void output_status(STATUS_PKT *sp)
 #endif
 
    list_spool_stats(sendit, (void *)sp);
+   if (!sp->api) sendit("====\n\n", 6, sp);
 }
 
 
@@ -127,7 +126,7 @@ static void list_devices(STATUS_PKT *sp)
    int bpb;
 
    len = Mmsg(msg, _("\nDevice status:\n"));
-   sendit(msg, len, sp);
+   if (!sp->api) sendit(msg, len, sp);
 
    foreach_res(changer, R_AUTOCHANGER) {
       len = Mmsg(msg, _("Autochanger \"%s\" with devices:\n"),
@@ -206,7 +205,7 @@ static void list_devices(STATUS_PKT *sp)
          }
       }
    }
-   sendit("====\n\n", 6, sp);
+   if (!sp->api) sendit("====\n\n", 6, sp);
 }
 
 static void list_status_header(STATUS_PKT *sp)
@@ -223,8 +222,8 @@ static void list_status_header(STATUS_PKT *sp)
    bstrftime_nc(dt, sizeof(dt), daemon_start_time);
 
 
-   len = Mmsg(msg, _("Daemon started %s, %d Job%s run since started.\n"),
-        dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
+   len = Mmsg(msg, _("Daemon started %s. Jobs: run=%d, running=%d.\n"),
+        dt, num_jobs_run, job_count());
    sendit(msg, len, sp);
 
    len = Mmsg(msg, _(" Heap: heap=%s smbytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
@@ -307,11 +306,11 @@ static void send_blocked_status(DEVICE *dev, STATUS_PKT *sp)
    }
    /* Send autochanger slot status */
    if (dev->is_autochanger()) {
-      if (dev->Slot > 0) {
+      if (dev->get_slot() > 0) {
          len = Mmsg(msg, _("    Slot %d is loaded in drive %d.\n"), 
-            dev->Slot, dev->drive_index);
+            dev->get_slot(), dev->drive_index);
          sendit(msg, len, sp);
-      } else if (dev->Slot == 0) {
+      } else if (dev->get_slot() == 0) {
          len = Mmsg(msg, _("    Drive %d is not loaded.\n"), dev->drive_index);
          sendit(msg, len, sp);
       } else {
@@ -320,58 +319,67 @@ static void send_blocked_status(DEVICE *dev, STATUS_PKT *sp)
       }
    }
    if (debug_level > 1) {
-      len = Mmsg(msg, _("Configured device capabilities:\n"));
-      sendit(msg, len, sp);
+      send_device_status(dev, sp);
+   }
+}
 
-      len = Mmsg(msg, "%sEOF %sBSR %sBSF %sFSR %sFSF %sEOM %sREM %sRACCESS %sAUTOMOUNT %sLABEL %sANONVOLS %sALWAYSOPEN\n",
-         dev->capabilities & CAP_EOF ? "" : "!", 
-         dev->capabilities & CAP_BSR ? "" : "!", 
-         dev->capabilities & CAP_BSF ? "" : "!", 
-         dev->capabilities & CAP_FSR ? "" : "!", 
-         dev->capabilities & CAP_FSF ? "" : "!", 
-         dev->capabilities & CAP_EOM ? "" : "!", 
-         dev->capabilities & CAP_REM ? "" : "!", 
-         dev->capabilities & CAP_RACCESS ? "" : "!",
-         dev->capabilities & CAP_AUTOMOUNT ? "" : "!", 
-         dev->capabilities & CAP_LABEL ? "" : "!", 
-         dev->capabilities & CAP_ANONVOLS ? "" : "!", 
-         dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
-      sendit(msg, len, sp);
+static void send_device_status(DEVICE *dev, STATUS_PKT *sp)
+{
+   POOL_MEM msg(PM_MESSAGE);
+   int len;
 
-      len = Mmsg(msg, _("Device state:\n"));
-      sendit(msg, len, sp);
+   len = Mmsg(msg, _("Configured device capabilities:\n"));
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, "%sOPENED %sTAPE %sLABEL %sMALLOC %sAPPEND %sREAD %sEOT %sWEOT %sEOF %sNEXTVOL %sSHORT %sMOUNTED\n", 
-         dev->is_open() ? "" : "!", 
-         dev->is_tape() ? "" : "!", 
-         dev->is_labeled() ? "" : "!", 
-         dev->state & ST_MALLOC ? "" : "!", 
-         dev->can_append() ? "" : "!", 
-         dev->can_read() ? "" : "!", 
-         dev->at_eot() ? "" : "!", 
-         dev->state & ST_WEOT ? "" : "!", 
-         dev->at_eof() ? "" : "!", 
-         dev->state & ST_NEXTVOL ? "" : "!", 
-         dev->state & ST_SHORT ? "" : "!", 
-         dev->state & ST_MOUNTED ? "" : "!");
-      sendit(msg, len, sp);
+   len = Mmsg(msg, "%sEOF %sBSR %sBSF %sFSR %sFSF %sEOM %sREM %sRACCESS %sAUTOMOUNT %sLABEL %sANONVOLS %sALWAYSOPEN\n",
+      dev->capabilities & CAP_EOF ? "" : "!", 
+      dev->capabilities & CAP_BSR ? "" : "!", 
+      dev->capabilities & CAP_BSF ? "" : "!", 
+      dev->capabilities & CAP_FSR ? "" : "!", 
+      dev->capabilities & CAP_FSF ? "" : "!", 
+      dev->capabilities & CAP_EOM ? "" : "!", 
+      dev->capabilities & CAP_REM ? "" : "!", 
+      dev->capabilities & CAP_RACCESS ? "" : "!",
+      dev->capabilities & CAP_AUTOMOUNT ? "" : "!", 
+      dev->capabilities & CAP_LABEL ? "" : "!", 
+      dev->capabilities & CAP_ANONVOLS ? "" : "!", 
+      dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, _("num_writers=%d block=%d\n\n"), dev->num_writers, dev->blocked());
-      sendit(msg, len, sp);
+   len = Mmsg(msg, _("Device state:\n"));
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, _("Device parameters:\n"));
-      sendit(msg, len, sp);
+   len = Mmsg(msg, "%sOPENED %sTAPE %sLABEL %sMALLOC %sAPPEND %sREAD %sEOT %sWEOT %sEOF %sNEXTVOL %sSHORT %sMOUNTED\n", 
+      dev->is_open() ? "" : "!", 
+      dev->is_tape() ? "" : "!", 
+      dev->is_labeled() ? "" : "!", 
+      dev->state & ST_MALLOC ? "" : "!", 
+      dev->can_append() ? "" : "!", 
+      dev->can_read() ? "" : "!", 
+      dev->at_eot() ? "" : "!", 
+      dev->state & ST_WEOT ? "" : "!", 
+      dev->at_eof() ? "" : "!", 
+      dev->state & ST_NEXTVOL ? "" : "!", 
+      dev->state & ST_SHORT ? "" : "!", 
+      dev->state & ST_MOUNTED ? "" : "!");
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, _("Archive name: %s Device name: %s\n"), dev->archive_name(),
-         dev->name());
-      sendit(msg, len, sp);
+   len = Mmsg(msg, _("num_writers=%d reserved=%d block=%d\n\n"), dev->num_writers, 
+              dev->num_reserved(), dev->blocked());
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, _("File=%u block=%u\n"), dev->file, dev->block_num);
-      sendit(msg, len, sp);
+   len = Mmsg(msg, _("Device parameters:\n"));
+   sendit(msg, len, sp);
 
-      len = Mmsg(msg, _("Min block=%u Max block=%u\n"), dev->min_block_size, dev->max_block_size);
-      sendit(msg, len, sp);
-   }
+   len = Mmsg(msg, _("Archive name: %s Device name: %s\n"), dev->archive_name(),
+      dev->name());
+   sendit(msg, len, sp);
+
+   len = Mmsg(msg, _("File=%u block=%u\n"), dev->file, dev->block_num);
+   sendit(msg, len, sp);
+
+   len = Mmsg(msg, _("Min block=%u Max block=%u\n"), dev->min_block_size, dev->max_block_size);
+   sendit(msg, len, sp);
 }
 
 static void list_running_jobs(STATUS_PKT *sp)
@@ -386,17 +394,17 @@ static void list_running_jobs(STATUS_PKT *sp)
    POOL_MEM msg(PM_MESSAGE);
 
    len = Mmsg(msg, _("\nRunning Jobs:\n"));
-   sendit(msg, len, sp);
+   if (!sp->api) sendit(msg, len, sp);
 
    foreach_jcr(jcr) {
       if (jcr->JobStatus == JS_WaitFD) {
          len = Mmsg(msg, _("%s Job %s waiting for Client connection.\n"),
-            job_type_to_str(jcr->JobType), jcr->Job);
+            job_type_to_str(jcr->getJobType()), jcr->Job);
          sendit(msg, len, sp);
       }
       dcr = jcr->dcr;
       rdcr = jcr->read_dcr;
-      if ((dcr && dcr->device) || rdcr && rdcr->device) {
+      if ((dcr && dcr->device) || (rdcr && rdcr->device)) {
          bstrncpy(JobName, jcr->Job, sizeof(JobName));
          /* There are three periods after the Job name */
          char *p;
@@ -408,8 +416,8 @@ static void list_running_jobs(STATUS_PKT *sp)
          if (rdcr && rdcr->device) {
             len = Mmsg(msg, _("Reading: %s %s job %s JobId=%d Volume=\"%s\"\n"
                             "    pool=\"%s\" device=%s\n"),
-                   job_level_to_str(jcr->JobLevel),
-                   job_type_to_str(jcr->JobType),
+                   job_level_to_str(jcr->getJobLevel()),
+                   job_type_to_str(jcr->getJobType()),
                    JobName,
                    jcr->JobId,
                    rdcr->VolumeName,
@@ -421,8 +429,8 @@ static void list_running_jobs(STATUS_PKT *sp)
          if (dcr && dcr->device) {
             len = Mmsg(msg, _("Writing: %s %s job %s JobId=%d Volume=\"%s\"\n"
                             "    pool=\"%s\" device=%s\n"),
-                   job_level_to_str(jcr->JobLevel),
-                   job_type_to_str(jcr->JobType),
+                   job_level_to_str(jcr->getJobLevel()),
+                   job_type_to_str(jcr->getJobType()),
                    JobName,
                    jcr->JobId,
                    dcr->VolumeName,
@@ -463,9 +471,9 @@ static void list_running_jobs(STATUS_PKT *sp)
 
    if (!found) {
       len = Mmsg(msg, _("No Jobs running.\n"));
-      sendit(msg, len, sp);
+      if (!sp->api) sendit(msg, len, sp);
    }
-   sendit("====\n", 5, sp);
+   if (!sp->api) sendit("====\n", 5, sp);
 }
 
 static void list_jobs_waiting_on_reservation(STATUS_PKT *sp)
@@ -475,7 +483,7 @@ static void list_jobs_waiting_on_reservation(STATUS_PKT *sp)
    int len;
 
    len = Mmsg(msg, _("\nJobs waiting to reserve a drive:\n"));
-   sendit(msg, len, sp);
+   if (!sp->api) sendit(msg, len, sp);
 
    foreach_jcr(jcr) {
       if (!jcr->reserve_msgs) {
@@ -485,7 +493,7 @@ static void list_jobs_waiting_on_reservation(STATUS_PKT *sp)
    }
    endeach_jcr(jcr);
 
-   sendit("====\n", 5, sp);
+   if (!sp->api) sendit("====\n", 5, sp);
 }
 
 
@@ -497,16 +505,16 @@ static void list_terminated_jobs(STATUS_PKT *sp)
    const char *msg;
 
    msg =  _("\nTerminated Jobs:\n");
-   sendit(msg, strlen(msg), sp);
+   if (!sp->api) sendit(msg, strlen(msg), sp);
    if (last_jobs->size() == 0) {
-      sendit("====\n", 5, sp);
+      if (!sp->api) sendit("====\n", 5, sp);
       return;
    }
    lock_last_jobs_list();
    msg =  _(" JobId  Level    Files      Bytes   Status   Finished        Name \n");
-   sendit(msg, strlen(msg), sp);
+   if (!sp->api) sendit(msg, strlen(msg), sp);
    msg =  _("===================================================================\n");
-   sendit(msg, strlen(msg), sp);
+   if (!sp->api) sendit(msg, strlen(msg), sp);
    foreach_dlist(je, last_jobs) {
       char JobName[MAX_NAME_LENGTH];
       const char *termstat;
@@ -540,6 +548,9 @@ static void list_terminated_jobs(STATUS_PKT *sp)
       case JS_Terminated:
          termstat = _("OK");
          break;
+      case JS_Warnings:
+         termstat = _("OK -- with warnings");
+         break;
       default:
          termstat = _("Other");
          break;
@@ -552,17 +563,27 @@ static void list_terminated_jobs(STATUS_PKT *sp)
             *p = 0;
          }
       }
-      bsnprintf(buf, sizeof(buf), _("%6d  %-6s %8s %10s  %-7s  %-8s %s\n"),
-         je->JobId,
-         level,
-         edit_uint64_with_commas(je->JobFiles, b1),
-         edit_uint64_with_suffix(je->JobBytes, b2),
-         termstat,
-         dt, JobName);
+      if (sp->api) {
+         bsnprintf(buf, sizeof(buf), _("%6d\t%-6s\t%8s\t%10s\t%-7s\t%-8s\t%s\n"),
+            je->JobId,
+            level,
+            edit_uint64_with_commas(je->JobFiles, b1),
+            edit_uint64_with_suffix(je->JobBytes, b2),
+            termstat,
+            dt, JobName);
+      } else {
+         bsnprintf(buf, sizeof(buf), _("%6d  %-6s %8s %10s  %-7s  %-8s %s\n"),
+            je->JobId,
+            level,
+            edit_uint64_with_commas(je->JobFiles, b1),
+            edit_uint64_with_suffix(je->JobBytes, b2),
+            termstat,
+            dt, JobName);
+      }  
       sendit(buf, strlen(buf), sp);
    }
    unlock_last_jobs_list();
-   sendit("====\n", 5, sp);
+   if (!sp->api) sendit("====\n", 5, sp);
 }
 
 /*
@@ -650,14 +671,14 @@ static void sendit(POOL_MEM &msg, int len, STATUS_PKT *sp)
  */
 bool status_cmd(JCR *jcr)
 {
-   BSOCK *bs = jcr->dir_bsock;
+   BSOCK *dir = jcr->dir_bsock;
    STATUS_PKT sp;
 
-   bs->fsend("\n");
-   sp.bs = bs;
+   dir->fsend("\n");
+   sp.bs = dir;
    output_status(&sp);
-   bs->signal(BNET_EOD);
-   return 1;
+   dir->signal(BNET_EOD);
+   return true;
 }
 
 /*
@@ -666,33 +687,58 @@ bool status_cmd(JCR *jcr)
 bool qstatus_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
-   POOL_MEM time;
+   POOL_MEM cmd;
    JCR *njcr;
    s_last_job* job;
+   STATUS_PKT sp;
 
-   if (sscanf(dir->msg, qstatus, time.c_str()) != 1) {
+   sp.bs = dir;
+   if (sscanf(dir->msg, qstatus, cmd.c_str()) != 1) {
       pm_strcpy(jcr->errmsg, dir->msg);
       Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
       dir->fsend(_("3900 Bad .status command, missing argument.\n"));
       dir->signal(BNET_EOD);
       return false;
    }
-   unbash_spaces(time);
+   unbash_spaces(cmd);
+
+   Dmsg1(200, "cmd=%s\n", cmd.c_str());
 
-   if (strcmp(time.c_str(), "current") == 0) {
-      dir->fsend(OKqstatus, time.c_str());
+   if (strcmp(cmd.c_str(), "current") == 0) {
+      dir->fsend(OKqstatus, cmd.c_str());
       foreach_jcr(njcr) {
          if (njcr->JobId != 0) {
             dir->fsend(DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
          }
       }
       endeach_jcr(njcr);
-   } else if (strcmp(time.c_str(), "last") == 0) {
-      dir->fsend(OKqstatus, time.c_str());
+   } else if (strcmp(cmd.c_str(), "last") == 0) {
+      dir->fsend(OKqstatus, cmd.c_str());
       if ((last_jobs) && (last_jobs->size() > 0)) {
          job = (s_last_job*)last_jobs->last();
          dir->fsend(DotStatusJob, job->JobId, job->JobStatus, job->Errors);
       }
+   } else if (strcasecmp(cmd.c_str(), "header") == 0) {
+       sp.api = true;
+       list_status_header(&sp);
+   } else if (strcasecmp(cmd.c_str(), "running") == 0) {
+       sp.api = true;
+       list_running_jobs(&sp);
+   } else if (strcasecmp(cmd.c_str(), "waitreservation") == 0) {
+       sp.api = true;
+       list_jobs_waiting_on_reservation(&sp);
+   } else if (strcasecmp(cmd.c_str(), "devices") == 0) {
+       sp.api = true;
+       list_devices(&sp);
+   } else if (strcasecmp(cmd.c_str(), "volumes") == 0) {
+       sp.api = true;
+       list_volumes(sendit, &sp);
+   } else if (strcasecmp(cmd.c_str(), "spooling") == 0) {
+       sp.api = true;
+       list_spool_stats(sendit, &sp);
+   } else if (strcasecmp(cmd.c_str(), "terminated") == 0) {
+       sp.api = true;
+       list_terminated_jobs(&sp);
    } else {
       pm_strcpy(jcr->errmsg, dir->msg);
       Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);