3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
11 Copyright (C) 2000-2005 Kern Sibbald
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);
45 static char OKqstatus[] = "1000 OK .status\n";
46 static char DotStatusJob[] = "JobId=%d JobStatus=%c JobErrors=%d\n";
51 int qstatus_cmd(UAContext *ua, const char *cmd)
59 Dmsg1(20, "status:%s:\n", cmd);
61 if ((ua->argc != 3) || (strcasecmp(ua->argk[1], "dir"))) {
62 bsendmsg(ua, "1900 Bad .status command, missing arguments.\n");
66 if (strcasecmp(ua->argk[2], "current") == 0) {
67 bsendmsg(ua, OKqstatus, ua->argk[2]);
70 if (njcr->JobId != 0) {
71 bsendmsg(ua, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
73 free_locked_jcr(njcr);
77 else if (strcasecmp(ua->argk[2], "last") == 0) {
78 bsendmsg(ua, OKqstatus, ua->argk[2]);
79 if ((last_jobs) && (last_jobs->size() > 0)) {
80 job = (s_last_job*)last_jobs->last();
81 bsendmsg(ua, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
85 bsendmsg(ua, "1900 Bad .status command, wrong argument.\n");
95 int status_cmd(UAContext *ua, const char *cmd)
104 Dmsg1(20, "status:%s:\n", cmd);
106 for (i=1; i<ua->argc; i++) {
107 if (strcasecmp(ua->argk[i], _("all")) == 0) {
110 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
111 strcasecmp(ua->argk[i], _("director")) == 0) {
112 do_director_status(ua);
114 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
115 client = get_client_resource(ua);
117 do_client_status(ua, client);
121 store = get_storage_resource(ua, 0);
123 do_storage_status(ua, store);
128 /* If no args, ask for status type */
130 char prmt[MAX_NAME_LENGTH];
132 start_prompt(ua, _("Status available for:\n"));
133 add_prompt(ua, _("Director"));
134 add_prompt(ua, _("Storage"));
135 add_prompt(ua, _("Client"));
136 add_prompt(ua, _("All"));
137 Dmsg0(20, "do_prompt: select daemon\n");
138 if ((item=do_prompt(ua, "", _("Select daemon type for status"), prmt, sizeof(prmt))) < 0) {
141 Dmsg1(20, "item=%d\n", item);
143 case 0: /* Director */
144 do_director_status(ua);
147 store = select_storage_resource(ua);
149 do_storage_status(ua, store);
153 client = select_client_resource(ua);
155 do_client_status(ua, client);
168 static void do_all_status(UAContext *ua)
170 STORE *store, **unique_store;
171 CLIENT *client, **unique_client;
175 do_director_status(ua);
177 /* Count Storage items */
180 foreach_res(store, R_STORAGE) {
183 unique_store = (STORE **) malloc(i * sizeof(STORE));
184 /* Find Unique Storage address/port */
186 foreach_res(store, R_STORAGE) {
188 if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
191 for (j=0; j<i; j++) {
192 if (strcmp(unique_store[j]->address, store->address) == 0 &&
193 unique_store[j]->SDport == store->SDport) {
199 unique_store[i++] = store;
200 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
205 /* Call each unique Storage daemon */
206 for (j=0; j<i; j++) {
207 do_storage_status(ua, unique_store[j]);
211 /* Count Client items */
214 foreach_res(client, R_CLIENT) {
217 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
218 /* Find Unique Client address/port */
220 foreach_res(client, R_CLIENT) {
222 if (!acl_access_ok(ua, Client_ACL, client->hdr.name)) {
225 for (j=0; j<i; j++) {
226 if (strcmp(unique_client[j]->address, client->address) == 0 &&
227 unique_client[j]->FDport == client->FDport) {
233 unique_client[i++] = client;
234 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
239 /* Call each unique File daemon */
240 for (j=0; j<i; j++) {
241 do_client_status(ua, unique_client[j]);
247 static void do_director_status(UAContext *ua)
249 char dt[MAX_TIME_LENGTH];
251 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
252 HOST_OS, DISTNAME, DISTVER);
253 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
254 bsendmsg(ua, _("Daemon started %s, %d Job%s run since started.\n"),
255 dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
256 if (debug_level > 0) {
257 char b1[35], b2[35], b3[35], b4[35];
258 bsendmsg(ua, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
259 edit_uint64_with_commas(sm_bytes, b1),
260 edit_uint64_with_commas(sm_max_bytes, b2),
261 edit_uint64_with_commas(sm_buffers, b3),
262 edit_uint64_with_commas(sm_max_buffers, b4));
265 * List scheduled Jobs
267 list_scheduled_jobs(ua);
272 list_running_jobs(ua);
275 * List terminated jobs
277 list_terminated_jobs(ua);
278 bsendmsg(ua, "====\n");
281 static void do_storage_status(UAContext *ua, STORE *store)
285 set_storage(ua->jcr, store);
286 /* Try connecting for up to 15 seconds */
287 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
288 store->hdr.name, store->address, store->SDport);
289 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
290 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
292 if (ua->jcr->store_bsock) {
293 bnet_close(ua->jcr->store_bsock);
294 ua->jcr->store_bsock = NULL;
298 Dmsg0(20, _("Connected to storage daemon\n"));
299 sd = ua->jcr->store_bsock;
300 bnet_fsend(sd, "status");
301 while (bnet_recv(sd) >= 0) {
302 bsendmsg(ua, "%s", sd->msg);
304 bnet_sig(sd, BNET_TERMINATE);
306 ua->jcr->store_bsock = NULL;
310 static void do_client_status(UAContext *ua, CLIENT *client)
314 /* Connect to File daemon */
316 ua->jcr->client = client;
317 /* Release any old dummy key */
318 if (ua->jcr->sd_auth_key) {
319 free(ua->jcr->sd_auth_key);
321 /* Create a new dummy SD auth key */
322 ua->jcr->sd_auth_key = bstrdup("dummy");
324 /* Try to connect for 15 seconds */
325 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
326 client->hdr.name, client->address, client->FDport);
327 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
328 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
330 if (ua->jcr->file_bsock) {
331 bnet_close(ua->jcr->file_bsock);
332 ua->jcr->file_bsock = NULL;
336 Dmsg0(20, _("Connected to file daemon\n"));
337 fd = ua->jcr->file_bsock;
338 bnet_fsend(fd, "status");
339 while (bnet_recv(fd) >= 0) {
340 bsendmsg(ua, "%s", fd->msg);
342 bnet_sig(fd, BNET_TERMINATE);
344 ua->jcr->file_bsock = NULL;
349 static void prt_runhdr(UAContext *ua)
351 bsendmsg(ua, _("\nScheduled Jobs:\n"));
352 bsendmsg(ua, _("Level Type Pri Scheduled Name Volume\n"));
353 bsendmsg(ua, _("===================================================================================\n"));
356 /* Scheduling packet */
358 dlink link; /* keep this as first item!!! */
366 static void prt_runtime(UAContext *ua, sched_pkt *sp)
368 char dt[MAX_TIME_LENGTH];
369 const char *level_ptr;
371 bool close_db = false;
375 memset(&mr, 0, sizeof(mr));
376 if (sp->job->JobType == JT_BACKUP) {
378 ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
380 close_db = true; /* new db opened, remember to close it */
383 mr.PoolId = jcr->PoolId;
384 ok = find_next_volume_for_append(jcr, &mr, 0);
387 bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
390 bstrftime_nc(dt, sizeof(dt), sp->runtime);
391 switch (sp->job->JobType) {
397 level_ptr = level_to_str(sp->level);
400 bsendmsg(ua, _("%-14s %-8s %3d %-18s %-18s %s\n"),
401 level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt,
402 sp->job->hdr.name, mr.VolumeName);
404 db_close_database(jcr, jcr->db);
406 jcr->db = ua->db; /* restore ua db to jcr */
411 * Sort items by runtime, priority
413 static int my_compare(void *item1, void *item2)
415 sched_pkt *p1 = (sched_pkt *)item1;
416 sched_pkt *p2 = (sched_pkt *)item2;
417 if (p1->runtime < p2->runtime) {
419 } else if (p1->runtime > p2->runtime) {
422 if (p1->priority < p2->priority) {
424 } else if (p1->priority > p2->priority) {
431 * Find all jobs to be run in roughly the
434 static void list_scheduled_jobs(UAContext *ua)
439 int level, num_jobs = 0;
441 bool hdr_printed = false;
445 Dmsg0(200, "enter list_sched_jobs()\n");
447 /* Loop through all jobs */
449 foreach_res(job, R_JOB) {
450 if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
453 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
454 level = job->JobLevel;
458 priority = job->Priority;
460 priority = run->Priority;
466 sp = (sched_pkt *)malloc(sizeof(sched_pkt));
469 sp->priority = priority;
470 sp->runtime = runtime;
471 sp->pool = run->pool;
472 sched.binary_insert(sp, my_compare);
475 } /* end for loop over resources */
477 foreach_dlist(sp, &sched) {
481 bsendmsg(ua, _("No Scheduled Jobs.\n"));
483 bsendmsg(ua, "====\n");
484 Dmsg0(200, "Leave list_sched_jobs_runs()\n");
487 static void list_running_jobs(UAContext *ua)
492 char *emsg; /* edited message */
493 char dt[MAX_TIME_LENGTH];
495 bool pool_mem = false;
497 Dmsg0(200, "enter list_run_jobs()\n");
498 bsendmsg(ua, _("\nRunning Jobs:\n"));
502 if (jcr->JobId == 0) { /* this is us */
503 /* this is a console or other control job. We only show console
504 * jobs in the status output.
506 if (jcr->JobType == JT_CONSOLE) {
507 bstrftime_nc(dt, sizeof(dt), jcr->start_time);
508 bsendmsg(ua, _("Console connected at %s\n"), dt);
512 free_locked_jcr(jcr);
516 /* Note the following message is used in regress -- don't change */
517 bsendmsg(ua, _("No Jobs running.\n====\n"));
518 Dmsg0(200, "leave list_run_jobs()\n");
522 bsendmsg(ua, _(" JobId Level Name Status\n"));
523 bsendmsg(ua, _("======================================================================\n"));
525 if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
526 free_locked_jcr(jcr);
530 switch (jcr->JobStatus) {
532 msg = _("is waiting execution");
535 msg = _("is running");
538 msg = _("is blocked");
541 msg = _("has terminated");
543 case JS_ErrorTerminated:
544 msg = _("has erred");
547 msg = _("has errors");
550 msg = _("has a fatal error");
553 msg = _("has verify differences");
556 msg = _("has been canceled");
559 emsg = (char *) get_pool_memory(PM_FNAME);
560 Mmsg(emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
565 emsg = (char *) get_pool_memory(PM_FNAME);
566 Mmsg(emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
570 case JS_WaitStoreRes:
571 msg = _("is waiting on max Storage jobs");
573 case JS_WaitClientRes:
574 msg = _("is waiting on max Client jobs");
577 msg = _("is waiting on max Job jobs");
580 msg = _("is waiting on max total jobs");
582 case JS_WaitStartTime:
583 msg = _("is waiting for its start time");
585 case JS_WaitPriority:
586 msg = _("is waiting for higher priority jobs to finish");
590 emsg = (char *) get_pool_memory(PM_FNAME);
591 Mmsg(emsg, _("is in unknown state %c"), jcr->JobStatus);
597 * Now report Storage daemon status code
599 switch (jcr->SDJobStatus) {
602 free_pool_memory(emsg);
605 msg = _("is waiting for a mount request");
609 free_pool_memory(emsg);
612 msg = _("is waiting for an appendable Volume");
616 emsg = (char *) get_pool_memory(PM_FNAME);
619 Mmsg(emsg, _("is waiting for Client %s to connect to Storage %s"),
620 jcr->client->hdr.name, jcr->store->hdr.name);
624 switch (jcr->JobType) {
627 bstrncpy(level, " ", sizeof(level));
630 bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
635 bsendmsg(ua, _("%6d %-6s %-20s %s\n"),
642 free_pool_memory(emsg);
645 free_locked_jcr(jcr);
648 bsendmsg(ua, "====\n");
649 Dmsg0(200, "leave list_run_jobs()\n");
652 static void list_terminated_jobs(UAContext *ua)
654 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
657 if (last_jobs->empty()) {
658 bsendmsg(ua, _("No Terminated Jobs.\n"));
661 lock_last_jobs_list();
662 struct s_last_job *je;
663 bsendmsg(ua, _("\nTerminated Jobs:\n"));
664 bsendmsg(ua, _(" JobId Level Files Bytes Status Finished Name \n"));
665 bsendmsg(ua, _("========================================================================\n"));
666 foreach_dlist(je, last_jobs) {
667 char JobName[MAX_NAME_LENGTH];
668 const char *termstat;
670 bstrncpy(JobName, je->Job, sizeof(JobName));
671 /* There are three periods after the Job name */
673 for (int i=0; i<3; i++) {
674 if ((p=strrchr(JobName, '.')) != NULL) {
679 if (!acl_access_ok(ua, Job_ACL, JobName)) {
683 bstrftime_nc(dt, sizeof(dt), je->end_time);
684 switch (je->JobType) {
687 bstrncpy(level, " ", sizeof(level));
690 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
694 switch (je->JobStatus) {
696 termstat = "Created";
699 case JS_ErrorTerminated:
715 bsendmsg(ua, _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
718 edit_uint64_with_commas(je->JobFiles, b1),
719 edit_uint64_with_commas(je->JobBytes, b2),
724 unlock_last_jobs_list();