3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
11 Copyright (C) 2000-2004 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 int num_jobs_run;
37 extern uint64_t sm_max_bytes;
38 extern uint64_t sm_bytes;
39 extern uint32_t sm_max_buffers;
40 extern uint32_t sm_buffers;
43 static void list_scheduled_jobs(UAContext *ua);
44 static void list_running_jobs(UAContext *ua);
45 static void list_terminated_jobs(UAContext *ua);
46 static void do_storage_status(UAContext *ua, STORE *store);
47 static void do_client_status(UAContext *ua, CLIENT *client);
48 static void do_director_status(UAContext *ua);
49 static void do_all_status(UAContext *ua);
54 int status_cmd(UAContext *ua, const char *cmd)
63 Dmsg1(20, "status:%s:\n", cmd);
65 for (i=1; i<ua->argc; i++) {
66 if (strcasecmp(ua->argk[i], _("all")) == 0) {
69 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
70 strcasecmp(ua->argk[i], _("director")) == 0) {
71 do_director_status(ua);
73 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
74 client = get_client_resource(ua);
76 do_client_status(ua, client);
80 store = get_storage_resource(ua, 0);
82 do_storage_status(ua, store);
87 /* If no args, ask for status type */
89 char prmt[MAX_NAME_LENGTH];
91 start_prompt(ua, _("Status available for:\n"));
92 add_prompt(ua, _("Director"));
93 add_prompt(ua, _("Storage"));
94 add_prompt(ua, _("Client"));
95 add_prompt(ua, _("All"));
96 Dmsg0(20, "do_prompt: select daemon\n");
97 if ((item=do_prompt(ua, "", _("Select daemon type for status"), prmt, sizeof(prmt))) < 0) {
100 Dmsg1(20, "item=%d\n", item);
102 case 0: /* Director */
103 do_director_status(ua);
106 store = select_storage_resource(ua);
108 do_storage_status(ua, store);
112 client = select_client_resource(ua);
114 do_client_status(ua, client);
127 static void do_all_status(UAContext *ua)
129 STORE *store, **unique_store;
130 CLIENT *client, **unique_client;
134 do_director_status(ua);
136 /* Count Storage items */
139 foreach_res(store, R_STORAGE) {
142 unique_store = (STORE **) malloc(i * sizeof(STORE));
143 /* Find Unique Storage address/port */
145 foreach_res(store, R_STORAGE) {
147 if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
150 for (j=0; j<i; j++) {
151 if (strcmp(unique_store[j]->address, store->address) == 0 &&
152 unique_store[j]->SDport == store->SDport) {
158 unique_store[i++] = store;
159 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
164 /* Call each unique Storage daemon */
165 for (j=0; j<i; j++) {
166 do_storage_status(ua, unique_store[j]);
170 /* Count Client items */
173 foreach_res(client, R_CLIENT) {
176 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
177 /* Find Unique Client address/port */
179 foreach_res(client, R_CLIENT) {
181 if (!acl_access_ok(ua, Client_ACL, client->hdr.name)) {
184 for (j=0; j<i; j++) {
185 if (strcmp(unique_client[j]->address, client->address) == 0 &&
186 unique_client[j]->FDport == client->FDport) {
192 unique_client[i++] = client;
193 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
198 /* Call each unique File daemon */
199 for (j=0; j<i; j++) {
200 do_client_status(ua, unique_client[j]);
206 static void do_director_status(UAContext *ua)
208 char dt[MAX_TIME_LENGTH];
210 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
211 HOST_OS, DISTNAME, DISTVER);
212 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
213 bsendmsg(ua, _("Daemon started %s, %d Job%s run since started.\n"),
214 dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
216 if (debug_level > 0) {
217 char b1[35], b2[35], b3[35], b4[35];
218 bsendmsg(ua, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
219 edit_uint64_with_commas(sm_bytes, b1),
220 edit_uint64_with_commas(sm_max_bytes, b2),
221 edit_uint64_with_commas(sm_buffers, b3),
222 edit_uint64_with_commas(sm_max_buffers, b4));
226 * List scheduled Jobs
228 list_scheduled_jobs(ua);
233 list_running_jobs(ua);
236 * List terminated jobs
238 list_terminated_jobs(ua);
239 bsendmsg(ua, "====\n");
242 static void do_storage_status(UAContext *ua, STORE *store)
246 ua->jcr->store = store;
247 /* Try connecting for up to 15 seconds */
248 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
249 store->hdr.name, store->address, store->SDport);
250 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
251 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
253 if (ua->jcr->store_bsock) {
254 bnet_close(ua->jcr->store_bsock);
255 ua->jcr->store_bsock = NULL;
259 Dmsg0(20, _("Connected to storage daemon\n"));
260 sd = ua->jcr->store_bsock;
261 bnet_fsend(sd, "status");
262 while (bnet_recv(sd) >= 0) {
263 bsendmsg(ua, "%s", sd->msg);
265 bnet_sig(sd, BNET_TERMINATE);
267 ua->jcr->store_bsock = NULL;
271 static void do_client_status(UAContext *ua, CLIENT *client)
275 /* Connect to File daemon */
277 ua->jcr->client = client;
278 /* Release any old dummy key */
279 if (ua->jcr->sd_auth_key) {
280 free(ua->jcr->sd_auth_key);
282 /* Create a new dummy SD auth key */
283 ua->jcr->sd_auth_key = bstrdup("dummy");
285 /* Try to connect for 15 seconds */
286 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
287 client->hdr.name, client->address, client->FDport);
288 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
289 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
291 if (ua->jcr->file_bsock) {
292 bnet_close(ua->jcr->file_bsock);
293 ua->jcr->file_bsock = NULL;
297 Dmsg0(20, _("Connected to file daemon\n"));
298 fd = ua->jcr->file_bsock;
299 bnet_fsend(fd, "status");
300 while (bnet_recv(fd) >= 0) {
301 bsendmsg(ua, "%s", fd->msg);
303 bnet_sig(fd, BNET_TERMINATE);
305 ua->jcr->file_bsock = NULL;
310 static void prt_runhdr(UAContext *ua)
312 bsendmsg(ua, _("\nScheduled Jobs:\n"));
313 bsendmsg(ua, _("Level Type Pri Scheduled Name Volume\n"));
314 bsendmsg(ua, _("===================================================================================\n"));
317 /* Scheduling packet */
319 dlink link; /* keep this as first item!!! */
327 static void prt_runtime(UAContext *ua, sched_pkt *sp)
329 char dt[MAX_TIME_LENGTH];
330 const char *level_ptr;
332 bool close_db = false;
335 memset(&mr, 0, sizeof(mr));
336 if (sp->job->JobType == JT_BACKUP) {
338 ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
340 close_db = true; /* new db opened, remember to close it */
343 ok = find_next_volume_for_append(jcr, &mr, 0);
346 bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
349 bstrftime_nc(dt, sizeof(dt), sp->runtime);
350 switch (sp->job->JobType) {
356 level_ptr = level_to_str(sp->level);
359 bsendmsg(ua, _("%-14s %-8s %3d %-18s %-18s %s\n"),
360 level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt,
361 sp->job->hdr.name, mr.VolumeName);
363 db_close_database(jcr, jcr->db);
365 jcr->db = ua->db; /* restore ua db to jcr */
370 * Sort items by runtime, priority
372 static int my_compare(void *item1, void *item2)
374 sched_pkt *p1 = (sched_pkt *)item1;
375 sched_pkt *p2 = (sched_pkt *)item2;
376 if (p1->runtime < p2->runtime) {
378 } else if (p1->runtime > p2->runtime) {
381 if (p1->priority < p2->priority) {
383 } else if (p1->priority > p2->priority) {
390 * Find all jobs to be run in roughly the
393 static void list_scheduled_jobs(UAContext *ua)
398 int level, num_jobs = 0;
400 bool hdr_printed = false;
404 Dmsg0(200, "enter list_sched_jobs()\n");
406 /* Loop through all jobs */
408 foreach_res(job, R_JOB) {
409 if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
412 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
417 priority = job->Priority;
419 priority = run->Priority;
425 sp = (sched_pkt *)malloc(sizeof(sched_pkt));
428 sp->priority = priority;
429 sp->runtime = runtime;
430 sp->pool = run->pool;
431 sched.binary_insert(sp, my_compare);
434 } /* end for loop over resources */
436 foreach_dlist(sp, &sched) {
440 bsendmsg(ua, _("No Scheduled Jobs.\n"));
442 bsendmsg(ua, "====\n");
443 Dmsg0(200, "Leave list_sched_jobs_runs()\n");
446 static void list_running_jobs(UAContext *ua)
451 char *emsg; /* edited message */
452 char dt[MAX_TIME_LENGTH];
454 bool pool_mem = false;
456 Dmsg0(200, "enter list_run_jobs()\n");
457 bsendmsg(ua, _("\nRunning Jobs:\n"));
461 if (jcr->JobId == 0) { /* this is us */
462 /* this is a console or other control job. We only show console
463 * jobs in the status output.
465 if (jcr->JobType == JT_CONSOLE) {
466 bstrftime_nc(dt, sizeof(dt), jcr->start_time);
467 bsendmsg(ua, _("Console connected at %s\n"), dt);
471 free_locked_jcr(jcr);
475 /* Note the following message is used in regress -- don't change */
476 bsendmsg(ua, _("No Jobs running.\n====\n"));
477 Dmsg0(200, "leave list_run_jobs()\n");
481 bsendmsg(ua, _(" JobId Level Name Status\n"));
482 bsendmsg(ua, _("======================================================================\n"));
484 if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
485 free_locked_jcr(jcr);
489 switch (jcr->JobStatus) {
491 msg = _("is waiting execution");
494 msg = _("is running");
497 msg = _("is blocked");
500 msg = _("has terminated");
502 case JS_ErrorTerminated:
503 msg = _("has erred");
506 msg = _("has errors");
509 msg = _("has a fatal error");
512 msg = _("has verify differences");
515 msg = _("has been canceled");
518 emsg = (char *) get_pool_memory(PM_FNAME);
519 Mmsg(&emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
524 emsg = (char *) get_pool_memory(PM_FNAME);
525 Mmsg(&emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
529 case JS_WaitStoreRes:
530 msg = _("is waiting on max Storage jobs");
532 case JS_WaitClientRes:
533 msg = _("is waiting on max Client jobs");
536 msg = _("is waiting on max Job jobs");
539 msg = _("is waiting on max total jobs");
541 case JS_WaitStartTime:
542 msg = _("is waiting for its start time");
544 case JS_WaitPriority:
545 msg = _("is waiting for higher priority jobs to finish");
549 emsg = (char *) get_pool_memory(PM_FNAME);
550 Mmsg(&emsg, _("is in unknown state %c"), jcr->JobStatus);
556 * Now report Storage daemon status code
558 switch (jcr->SDJobStatus) {
561 free_pool_memory(emsg);
564 msg = _("is waiting for a mount request");
568 free_pool_memory(emsg);
571 msg = _("is waiting for an appendable Volume");
575 emsg = (char *) get_pool_memory(PM_FNAME);
578 Mmsg(&emsg, _("is waiting for Client %s to connect to Storage %s"),
579 jcr->client->hdr.name, jcr->store->hdr.name);
583 switch (jcr->JobType) {
586 bstrncpy(level, " ", sizeof(level));
589 bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
594 bsendmsg(ua, _("%6d %-6s %-20s %s\n"),
601 free_pool_memory(emsg);
604 free_locked_jcr(jcr);
607 bsendmsg(ua, "====\n");
608 Dmsg0(200, "leave list_run_jobs()\n");
611 static void list_terminated_jobs(UAContext *ua)
613 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
616 if (last_jobs->empty()) {
617 bsendmsg(ua, _("No Terminated Jobs.\n"));
620 lock_last_jobs_list();
621 struct s_last_job *je;
622 bsendmsg(ua, _("\nTerminated Jobs:\n"));
623 bsendmsg(ua, _(" JobId Level Files Bytes Status Finished Name \n"));
624 bsendmsg(ua, _("========================================================================\n"));
625 foreach_dlist(je, last_jobs) {
626 char JobName[MAX_NAME_LENGTH];
627 const char *termstat;
629 bstrftime_nc(dt, sizeof(dt), je->end_time);
630 switch (je->JobType) {
633 bstrncpy(level, " ", sizeof(level));
636 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
640 switch (je->JobStatus) {
642 termstat = "Created";
645 case JS_ErrorTerminated:
661 bstrncpy(JobName, je->Job, sizeof(JobName));
662 /* There are three periods after the Job name */
664 for (int i=0; i<3; i++) {
665 if ((p=strrchr(JobName, '.')) != NULL) {
669 bsendmsg(ua, _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
672 edit_uint64_with_commas(je->JobFiles, b1),
673 edit_uint64_with_commas(je->JobBytes, b2),
678 unlock_last_jobs_list();