3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 extern char my_name[];
34 extern time_t daemon_start_time;
35 extern struct s_last_job last_job;
37 static void list_scheduled_jobs(UAContext *ua);
38 static void list_running_jobs(UAContext *ua);
39 static void list_terminated_jobs(UAContext *ua);
40 static void do_storage_status(UAContext *ua, STORE *store);
41 static void do_client_status(UAContext *ua, CLIENT *client);
42 static void do_director_status(UAContext *ua, char *cmd);
43 static void do_all_status(UAContext *ua, char *cmd);
48 int status_cmd(UAContext *ua, char *cmd)
57 Dmsg1(20, "status:%s:\n", cmd);
59 for (i=1; i<ua->argc; i++) {
60 if (strcasecmp(ua->argk[i], _("all")) == 0) {
61 do_all_status(ua, cmd);
63 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
64 strcasecmp(ua->argk[i], _("director")) == 0) {
65 do_director_status(ua, cmd);
67 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
68 client = get_client_resource(ua);
70 do_client_status(ua, client);
74 store = get_storage_resource(ua, 0);
76 do_storage_status(ua, store);
81 /* If no args, ask for status type */
83 start_prompt(ua, _("Status available for:\n"));
84 add_prompt(ua, _("Director"));
85 add_prompt(ua, _("Storage"));
86 add_prompt(ua, _("Client"));
87 add_prompt(ua, _("All"));
88 Dmsg0(20, "do_prompt: select daemon\n");
89 if ((item=do_prompt(ua, "", _("Select daemon type for status"), cmd, MAX_NAME_LENGTH)) < 0) {
92 Dmsg1(20, "item=%d\n", item);
94 case 0: /* Director */
95 do_director_status(ua, cmd);
98 store = select_storage_resource(ua);
100 do_storage_status(ua, store);
104 client = select_client_resource(ua);
106 do_client_status(ua, client);
110 do_all_status(ua, cmd);
119 static void do_all_status(UAContext *ua, char *cmd)
121 STORE *store, **unique_store;
122 CLIENT *client, **unique_client;
125 do_director_status(ua, cmd);
127 /* Count Storage items */
130 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
132 unique_store = (STORE **) malloc(i * sizeof(STORE));
133 /* Find Unique Storage address/port */
134 store = (STORE *)GetNextRes(R_STORAGE, NULL);
136 unique_store[i++] = store;
137 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
139 for (j=0; j<i; j++) {
140 if (strcmp(unique_store[j]->address, store->address) == 0 &&
141 unique_store[j]->SDport == store->SDport) {
147 unique_store[i++] = store;
148 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
153 /* Call each unique Storage daemon */
154 for (j=0; j<i; j++) {
155 do_storage_status(ua, unique_store[j]);
159 /* Count Client items */
162 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
164 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
165 /* Find Unique Client address/port */
166 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
168 unique_client[i++] = client;
169 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
171 for (j=0; j<i; j++) {
172 if (strcmp(unique_client[j]->address, client->address) == 0 &&
173 unique_client[j]->FDport == client->FDport) {
179 unique_client[i++] = client;
180 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
185 /* Call each unique File daemon */
186 for (j=0; j<i; j++) {
187 do_client_status(ua, unique_client[j]);
193 static void do_director_status(UAContext *ua, char *cmd)
195 char dt[MAX_TIME_LENGTH];
197 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
198 HOST_OS, DISTNAME, DISTVER);
199 bstrftime(dt, sizeof(dt), daemon_start_time);
200 bsendmsg(ua, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
201 last_job.NumJobs == 1 ? "" : "s");
203 * List scheduled Jobs
205 list_scheduled_jobs(ua);
210 list_running_jobs(ua);
213 * List terminated jobs
215 list_terminated_jobs(ua);
216 bsendmsg(ua, "====\n");
219 static void do_storage_status(UAContext *ua, STORE *store)
223 ua->jcr->store = store;
224 /* Try connecting for up to 15 seconds */
225 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
226 store->hdr.name, store->address, store->SDport);
227 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
228 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
230 if (ua->jcr->store_bsock) {
231 bnet_close(ua->jcr->store_bsock);
232 ua->jcr->store_bsock = NULL;
236 Dmsg0(20, _("Connected to storage daemon\n"));
237 sd = ua->jcr->store_bsock;
238 bnet_fsend(sd, "status");
239 while (bnet_recv(sd) >= 0) {
240 bsendmsg(ua, "%s", sd->msg);
242 bnet_sig(sd, BNET_TERMINATE);
244 ua->jcr->store_bsock = NULL;
248 static void do_client_status(UAContext *ua, CLIENT *client)
252 /* Connect to File daemon */
254 ua->jcr->client = client;
255 /* Release any old dummy key */
256 if (ua->jcr->sd_auth_key) {
257 free(ua->jcr->sd_auth_key);
259 /* Create a new dummy SD auth key */
260 ua->jcr->sd_auth_key = bstrdup("dummy");
262 /* Try to connect for 15 seconds */
263 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
264 client->hdr.name, client->address, client->FDport);
265 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
266 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
268 if (ua->jcr->file_bsock) {
269 bnet_close(ua->jcr->file_bsock);
270 ua->jcr->file_bsock = NULL;
274 Dmsg0(20, _("Connected to file daemon\n"));
275 fd = ua->jcr->file_bsock;
276 bnet_fsend(fd, "status");
277 while (bnet_recv(fd) >= 0) {
278 bsendmsg(ua, "%s", fd->msg);
280 bnet_sig(fd, BNET_TERMINATE);
282 ua->jcr->file_bsock = NULL;
287 static void prt_runhdr(UAContext *ua)
289 bsendmsg(ua, _("\nScheduled Jobs:\n"));
290 bsendmsg(ua, _("Level Type Scheduled Name Volume\n"));
291 bsendmsg(ua, _("===============================================================================\n"));
294 static void prt_runtime(UAContext *ua, JOB *job, int level, time_t runtime, POOL *pool)
296 char dt[MAX_TIME_LENGTH];
299 bool close_db = false;
302 memset(&mr, 0, sizeof(mr));
303 if (job->JobType == JT_BACKUP) {
305 ok = complete_jcr_for_job(jcr, job, pool);
307 close_db = true; /* new db opened, remember to close it */
310 ok = find_next_volume_for_append(jcr, &mr, 0);
313 bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
316 bstrftime(dt, sizeof(dt), runtime);
317 switch (job->JobType) {
323 level_ptr = level_to_str(level);
326 bsendmsg(ua, _("%-14s %-8s %-18s %-18s %s\n"),
327 level_ptr, job_type_to_str(job->JobType), dt, job->hdr.name, mr.VolumeName);
329 db_close_database(jcr, jcr->db);
331 jcr->db = ua->db; /* restore ua db to jcr */
336 * Find all jobs to be run in roughly the
339 static void list_scheduled_jobs(UAContext *ua)
344 int level, num_jobs = 0;
345 bool hdr_printed = false;
347 Dmsg0(200, "enter find_runs()\n");
349 /* Loop through all jobs */
351 for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
352 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
361 prt_runtime(ua, job, level, runtime, run->pool);
365 } /* end for loop over resources */
368 bsendmsg(ua, _("No Scheduled Jobs.\n"));
372 Dmsg0(200, "Leave find_runs()\n");
375 static void list_running_jobs(UAContext *ua)
380 char dt[MAX_TIME_LENGTH];
382 bool pool_mem = false;
385 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
386 if (jcr->JobId == 0) { /* this is us */
387 bstrftime(dt, sizeof(dt), jcr->start_time);
388 bsendmsg(ua, _("Console connected at %s\n"), dt);
391 free_locked_jcr(jcr);
395 bsendmsg(ua, _("No Running Jobs.\n"));
399 bsendmsg(ua, _("\nRunning Jobs:\n"));
400 bsendmsg(ua, _("Level JobId Job Status\n"));
401 bsendmsg(ua, _("====================================================================\n"));
402 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
403 if (jcr->JobId == 0) { /* this is us */
405 free_locked_jcr(jcr);
408 switch (jcr->JobStatus) {
410 msg = _("is waiting execution");
413 msg = _("is running");
416 msg = _("is blocked");
419 msg = _("has terminated");
421 case JS_ErrorTerminated:
422 msg = _("has erred");
425 msg = _("has errors");
428 msg = _("has a fatal error");
431 msg = _("has verify differences");
434 msg = _("has been canceled");
437 msg = (char *) get_pool_memory(PM_FNAME);
438 Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
442 msg = (char *) get_pool_memory(PM_FNAME);
443 Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
446 case JS_WaitStoreRes:
447 msg = _("is waiting on max Storage jobs");
449 case JS_WaitClientRes:
450 msg = _("is waiting on max Client jobs");
453 msg = _("is waiting on max Job jobs");
456 msg = _("is waiting on max total jobs");
458 case JS_WaitStartTime:
459 msg = _("is waiting for its start time");
461 case JS_WaitPriority:
462 msg = _("is waiting for higher priority jobs to finish");
466 msg = (char *) get_pool_memory(PM_FNAME);
467 Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
472 * Now report Storage daemon status code
474 switch (jcr->SDJobStatus) {
477 free_pool_memory(msg);
480 msg = _("is waiting for a mount request");
484 free_pool_memory(msg);
487 msg = _("is waiting for an appendable Volume");
491 msg = (char *) get_pool_memory(PM_FNAME);
494 Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
495 jcr->client->hdr.name, jcr->store->hdr.name);
498 switch (jcr->JobType) {
501 bstrncpy(level, " ", sizeof(level));
504 bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
509 bsendmsg(ua, _("%-4s %6d %-20s %s\n"),
516 free_pool_memory(msg);
519 free_locked_jcr(jcr);
526 static void list_terminated_jobs(UAContext *ua)
528 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
531 if (last_job.NumJobs == 0) {
532 bsendmsg(ua, _("No Terminated Jobs.\n"));
535 lock_last_jobs_list();
536 struct s_last_job *je;
537 bsendmsg(ua, _("\nTerminated Jobs:\n"));
538 bsendmsg(ua, _("Level Files Bytes Status Finished Name \n"));
539 bsendmsg(ua, _("====================================================================\n"));
540 for (je=NULL; (je=(s_last_job *)last_jobs->next(je)); ) {
541 char JobName[MAX_NAME_LENGTH];
544 bstrftime(dt, sizeof(dt), je->end_time);
545 strcpy(dt+7, dt+9); /* cut century */
546 switch (je->JobType) {
549 bstrncpy(level, " ", sizeof(level));
552 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
556 switch (je->JobStatus) {
558 termstat = "Created";
561 case JS_ErrorTerminated:
577 bstrncpy(JobName, je->Job, sizeof(JobName));
578 /* There are three periods after the Job name */
580 for (int i=0; i<3; i++) {
581 if ((p=strrchr(JobName, '.')) != NULL) {
585 bsendmsg(ua, _("%-4s %8s %12s %-7s %-8s %s\n"),
587 edit_uint64_with_commas(je->JobFiles, b1),
588 edit_uint64_with_commas(je->JobBytes, b2),
593 unlock_last_jobs_list();