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 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);
43 static void do_all_status(UAContext *ua);
48 int status_cmd(UAContext *ua, const 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) {
63 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
64 strcasecmp(ua->argk[i], _("director")) == 0) {
65 do_director_status(ua);
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 char prmt[MAX_NAME_LENGTH];
85 start_prompt(ua, _("Status available for:\n"));
86 add_prompt(ua, _("Director"));
87 add_prompt(ua, _("Storage"));
88 add_prompt(ua, _("Client"));
89 add_prompt(ua, _("All"));
90 Dmsg0(20, "do_prompt: select daemon\n");
91 if ((item=do_prompt(ua, "", _("Select daemon type for status"), prmt, sizeof(prmt))) < 0) {
94 Dmsg1(20, "item=%d\n", item);
96 case 0: /* Director */
97 do_director_status(ua);
100 store = select_storage_resource(ua);
102 do_storage_status(ua, store);
106 client = select_client_resource(ua);
108 do_client_status(ua, client);
121 static void do_all_status(UAContext *ua)
123 STORE *store, **unique_store;
124 CLIENT *client, **unique_client;
128 do_director_status(ua);
130 /* Count Storage items */
133 foreach_res(store, R_STORAGE) {
136 unique_store = (STORE **) malloc(i * sizeof(STORE));
137 /* Find Unique Storage address/port */
139 foreach_res(store, R_STORAGE) {
141 if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
144 for (j=0; j<i; j++) {
145 if (strcmp(unique_store[j]->address, store->address) == 0 &&
146 unique_store[j]->SDport == store->SDport) {
152 unique_store[i++] = store;
153 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
158 /* Call each unique Storage daemon */
159 for (j=0; j<i; j++) {
160 do_storage_status(ua, unique_store[j]);
164 /* Count Client items */
167 foreach_res(client, R_CLIENT) {
170 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
171 /* Find Unique Client address/port */
173 foreach_res(client, R_CLIENT) {
175 if (!acl_access_ok(ua, Client_ACL, client->hdr.name)) {
178 for (j=0; j<i; j++) {
179 if (strcmp(unique_client[j]->address, client->address) == 0 &&
180 unique_client[j]->FDport == client->FDport) {
186 unique_client[i++] = client;
187 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
192 /* Call each unique File daemon */
193 for (j=0; j<i; j++) {
194 do_client_status(ua, unique_client[j]);
200 static void do_director_status(UAContext *ua)
202 char dt[MAX_TIME_LENGTH];
204 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
205 HOST_OS, DISTNAME, DISTVER);
206 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
207 bsendmsg(ua, _("Daemon started %s, %d Job%s run since started.\n"),
208 dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
209 if (debug_level > 0) {
210 char b1[35], b2[35], b3[35], b4[35];
211 bsendmsg(ua, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
212 edit_uint64_with_commas(sm_bytes, b1),
213 edit_uint64_with_commas(sm_max_bytes, b2),
214 edit_uint64_with_commas(sm_buffers, b3),
215 edit_uint64_with_commas(sm_max_buffers, b4));
218 * List scheduled Jobs
220 list_scheduled_jobs(ua);
225 list_running_jobs(ua);
228 * List terminated jobs
230 list_terminated_jobs(ua);
231 bsendmsg(ua, "====\n");
234 static void do_storage_status(UAContext *ua, STORE *store)
238 ua->jcr->store = store;
239 /* Try connecting for up to 15 seconds */
240 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
241 store->hdr.name, store->address, store->SDport);
242 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
243 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
245 if (ua->jcr->store_bsock) {
246 bnet_close(ua->jcr->store_bsock);
247 ua->jcr->store_bsock = NULL;
251 Dmsg0(20, _("Connected to storage daemon\n"));
252 sd = ua->jcr->store_bsock;
253 bnet_fsend(sd, "status");
254 while (bnet_recv(sd) >= 0) {
255 bsendmsg(ua, "%s", sd->msg);
257 bnet_sig(sd, BNET_TERMINATE);
259 ua->jcr->store_bsock = NULL;
263 static void do_client_status(UAContext *ua, CLIENT *client)
267 /* Connect to File daemon */
269 ua->jcr->client = client;
270 /* Release any old dummy key */
271 if (ua->jcr->sd_auth_key) {
272 free(ua->jcr->sd_auth_key);
274 /* Create a new dummy SD auth key */
275 ua->jcr->sd_auth_key = bstrdup("dummy");
277 /* Try to connect for 15 seconds */
278 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
279 client->hdr.name, client->address, client->FDport);
280 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
281 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
283 if (ua->jcr->file_bsock) {
284 bnet_close(ua->jcr->file_bsock);
285 ua->jcr->file_bsock = NULL;
289 Dmsg0(20, _("Connected to file daemon\n"));
290 fd = ua->jcr->file_bsock;
291 bnet_fsend(fd, "status");
292 while (bnet_recv(fd) >= 0) {
293 bsendmsg(ua, "%s", fd->msg);
295 bnet_sig(fd, BNET_TERMINATE);
297 ua->jcr->file_bsock = NULL;
302 static void prt_runhdr(UAContext *ua)
304 bsendmsg(ua, _("\nScheduled Jobs:\n"));
305 bsendmsg(ua, _("Level Type Pri Scheduled Name Volume\n"));
306 bsendmsg(ua, _("===================================================================================\n"));
309 /* Scheduling packet */
311 dlink link; /* keep this as first item!!! */
319 static void prt_runtime(UAContext *ua, sched_pkt *sp)
321 char dt[MAX_TIME_LENGTH];
322 const char *level_ptr;
324 bool close_db = false;
327 memset(&mr, 0, sizeof(mr));
328 if (sp->job->JobType == JT_BACKUP) {
330 ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
332 close_db = true; /* new db opened, remember to close it */
335 ok = find_next_volume_for_append(jcr, &mr, 0);
338 bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
341 bstrftime_nc(dt, sizeof(dt), sp->runtime);
342 switch (sp->job->JobType) {
348 level_ptr = level_to_str(sp->level);
351 bsendmsg(ua, _("%-14s %-8s %3d %-18s %-18s %s\n"),
352 level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt,
353 sp->job->hdr.name, mr.VolumeName);
355 db_close_database(jcr, jcr->db);
357 jcr->db = ua->db; /* restore ua db to jcr */
362 * Sort items by runtime, priority
364 static int my_compare(void *item1, void *item2)
366 sched_pkt *p1 = (sched_pkt *)item1;
367 sched_pkt *p2 = (sched_pkt *)item2;
368 if (p1->runtime < p2->runtime) {
370 } else if (p1->runtime > p2->runtime) {
373 if (p1->priority < p2->priority) {
375 } else if (p1->priority > p2->priority) {
382 * Find all jobs to be run in roughly the
385 static void list_scheduled_jobs(UAContext *ua)
390 int level, num_jobs = 0;
392 bool hdr_printed = false;
396 Dmsg0(200, "enter list_sched_jobs()\n");
398 /* Loop through all jobs */
400 foreach_res(job, R_JOB) {
401 if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
404 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
409 priority = job->Priority;
411 priority = run->Priority;
417 sp = (sched_pkt *)malloc(sizeof(sched_pkt));
420 sp->priority = priority;
421 sp->runtime = runtime;
422 sp->pool = run->pool;
423 sched.binary_insert(sp, my_compare);
426 } /* end for loop over resources */
428 foreach_dlist(sp, &sched) {
432 bsendmsg(ua, _("No Scheduled Jobs.\n"));
434 bsendmsg(ua, "====\n");
435 Dmsg0(200, "Leave list_sched_jobs_runs()\n");
438 static void list_running_jobs(UAContext *ua)
443 char *emsg; /* edited message */
444 char dt[MAX_TIME_LENGTH];
446 bool pool_mem = false;
448 Dmsg0(200, "enter list_run_jobs()\n");
449 bsendmsg(ua, _("\nRunning Jobs:\n"));
453 if (jcr->JobId == 0) { /* this is us */
454 /* this is a console or other control job. We only show console
455 * jobs in the status output.
457 if (jcr->JobType == JT_CONSOLE) {
458 bstrftime_nc(dt, sizeof(dt), jcr->start_time);
459 bsendmsg(ua, _("Console connected at %s\n"), dt);
463 free_locked_jcr(jcr);
467 /* Note the following message is used in regress -- don't change */
468 bsendmsg(ua, _("No Jobs running.\n====\n"));
469 Dmsg0(200, "leave list_run_jobs()\n");
473 bsendmsg(ua, _(" JobId Level Name Status\n"));
474 bsendmsg(ua, _("======================================================================\n"));
476 if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
477 free_locked_jcr(jcr);
481 switch (jcr->JobStatus) {
483 msg = _("is waiting execution");
486 msg = _("is running");
489 msg = _("is blocked");
492 msg = _("has terminated");
494 case JS_ErrorTerminated:
495 msg = _("has erred");
498 msg = _("has errors");
501 msg = _("has a fatal error");
504 msg = _("has verify differences");
507 msg = _("has been canceled");
510 emsg = (char *) get_pool_memory(PM_FNAME);
511 Mmsg(&emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
516 emsg = (char *) get_pool_memory(PM_FNAME);
517 Mmsg(&emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
521 case JS_WaitStoreRes:
522 msg = _("is waiting on max Storage jobs");
524 case JS_WaitClientRes:
525 msg = _("is waiting on max Client jobs");
528 msg = _("is waiting on max Job jobs");
531 msg = _("is waiting on max total jobs");
533 case JS_WaitStartTime:
534 msg = _("is waiting for its start time");
536 case JS_WaitPriority:
537 msg = _("is waiting for higher priority jobs to finish");
541 emsg = (char *) get_pool_memory(PM_FNAME);
542 Mmsg(&emsg, _("is in unknown state %c"), jcr->JobStatus);
548 * Now report Storage daemon status code
550 switch (jcr->SDJobStatus) {
553 free_pool_memory(emsg);
556 msg = _("is waiting for a mount request");
560 free_pool_memory(emsg);
563 msg = _("is waiting for an appendable Volume");
567 emsg = (char *) get_pool_memory(PM_FNAME);
570 Mmsg(&emsg, _("is waiting for Client %s to connect to Storage %s"),
571 jcr->client->hdr.name, jcr->store->hdr.name);
575 switch (jcr->JobType) {
578 bstrncpy(level, " ", sizeof(level));
581 bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
586 bsendmsg(ua, _("%6d %-6s %-20s %s\n"),
593 free_pool_memory(emsg);
596 free_locked_jcr(jcr);
599 bsendmsg(ua, "====\n");
600 Dmsg0(200, "leave list_run_jobs()\n");
603 static void list_terminated_jobs(UAContext *ua)
605 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
608 if (last_jobs->empty()) {
609 bsendmsg(ua, _("No Terminated Jobs.\n"));
612 lock_last_jobs_list();
613 struct s_last_job *je;
614 bsendmsg(ua, _("\nTerminated Jobs:\n"));
615 bsendmsg(ua, _(" JobId Level Files Bytes Status Finished Name \n"));
616 bsendmsg(ua, _("========================================================================\n"));
617 foreach_dlist(je, last_jobs) {
618 char JobName[MAX_NAME_LENGTH];
619 const char *termstat;
621 bstrftime_nc(dt, sizeof(dt), je->end_time);
622 switch (je->JobType) {
625 bstrncpy(level, " ", sizeof(level));
628 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
632 switch (je->JobStatus) {
634 termstat = "Created";
637 case JS_ErrorTerminated:
653 bstrncpy(JobName, je->Job, sizeof(JobName));
654 /* There are three periods after the Job name */
656 for (int i=0; i<3; i++) {
657 if ((p=strrchr(JobName, '.')) != NULL) {
661 bsendmsg(ua, _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
664 edit_uint64_with_commas(je->JobFiles, b1),
665 edit_uint64_with_commas(je->JobBytes, b2),
670 unlock_last_jobs_list();