3 * Bacula Director -- User Agent Output Commands
4 * I.e. messages, listing database, showing resources, ...
6 * Kern Sibbald, September MM
12 Copyright (C) 2000-2004 Kern Sibbald and John Walker
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* Imported subroutines */
36 /* Imported variables */
39 extern RES_TABLE resources[];
40 extern int console_msg_pending;
42 extern brwlock_t con_lock;
45 /* Imported functions */
47 /* Forward referenced functions */
48 static int do_list_cmd(UAContext *ua, const char *cmd, e_list_type llist);
51 * Turn auto display of console messages on/off
53 int autodisplay_cmd(UAContext *ua, const char *cmd)
55 static const char *kw[] = {
60 switch (find_arg_keyword(ua, kw)) {
62 ua->auto_display_messages = 1;
65 ua->auto_display_messages = 0;
68 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
75 * Turn gui processing on/off
77 int gui_cmd(UAContext *ua, const char *cmd)
79 static const char *kw[] = {
84 switch (find_arg_keyword(ua, kw)) {
92 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
100 struct showstruct {const char *res_name; int type;};
101 static struct showstruct reses[] = {
102 {N_("directors"), R_DIRECTOR},
103 {N_("clients"), R_CLIENT},
104 {N_("counters"), R_COUNTER},
106 {N_("storages"), R_STORAGE},
107 {N_("catalogs"), R_CATALOG},
108 {N_("schedules"), R_SCHEDULE},
109 {N_("filesets"), R_FILESET},
110 {N_("pools"), R_POOL},
111 {N_("messages"), R_MSGS},
122 * show <resource-keyword-name> e.g. show directors
123 * show <resource-keyword-name>=<name> e.g. show director=HeadMan
126 int show_cmd(UAContext *ua, const char *cmd)
133 Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
137 for (i=1; i<ua->argc; i++) {
139 res_name = ua->argk[i];
140 if (!ua->argv[i]) { /* was a name given? */
141 /* No name, dump all resources of specified type */
143 len = strlen(res_name);
144 for (j=0; reses[j].res_name; j++) {
145 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
146 type = reses[j].type;
148 res = resources[type-r_first].res_head;
157 /* Dump a single resource with specified name */
159 len = strlen(res_name);
160 for (j=0; reses[j].res_name; j++) {
161 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
162 type = reses[j].type;
163 res = (RES *)GetResWithName(type, ua->argv[i]);
174 for (j=r_first; j<=r_last; j++) {
175 dump_resource(j, resources[j-r_first].res_head, bsendmsg, ua);
179 bsendmsg(ua, _("Keywords for the show command are:\n"));
180 for (j=0; reses[j].res_name; j++) {
181 bsendmsg(ua, "%s\n", _(reses[j].res_name));
185 bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
188 bsendmsg(ua, _("Resource %s not found\n"), res_name);
191 dump_resource(recurse?type:-type, res, bsendmsg, ua);
204 * List contents of database
206 * list jobs - lists all jobs run
207 * list jobid=nnn - list job data for jobid
208 * list job=name - list job data for job
209 * list jobmedia jobid=<nn>
210 * list jobmedia job=name
211 * list files jobid=<nn> - list files saved for job nn
212 * list files job=name
213 * list pools - list pool records
214 * list jobtotals - list totals for all jobs
215 * list media - list media for given pool (deprecated)
216 * list volumes - list Volumes
217 * list clients - list clients
218 * list nextvol job=xx - list the next vol to be used by job
219 * list nextvolume job=xx - same as above.
223 /* Do long or full listing */
224 int llist_cmd(UAContext *ua, const char *cmd)
226 return do_list_cmd(ua, cmd, VERT_LIST);
229 /* Do short or summary listing */
230 int list_cmd(UAContext *ua, const char *cmd)
232 return do_list_cmd(ua, cmd, HORZ_LIST);
235 static int do_list_cmd(UAContext *ua, const char *cmd, e_list_type llist)
247 memset(&jr, 0, sizeof(jr));
248 memset(&pr, 0, sizeof(pr));
249 memset(&mr, 0, sizeof(mr));
251 Dmsg1(20, "list: %s\n", cmd);
254 bsendmsg(ua, _("Hey! DB is NULL\n"));
257 /* Scan arguments looking for things to do */
258 for (i=1; i<ua->argc; i++) {
260 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
261 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
264 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
265 db_list_job_totals(ua->jcr, ua->db, &jr, prtit, ua);
268 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
270 jobid = atoi(ua->argv[i]);
273 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
278 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
279 bstrncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
281 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
284 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
286 for (j=i+1; j<ua->argc; j++) {
287 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
288 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
290 db_get_job_record(ua->jcr, ua->db, &jr);
292 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
293 jobid = atoi(ua->argv[j]);
298 db_list_files_for_job(ua->jcr, ua->db, jobid, prtit, ua);
303 } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 0) {
305 for (j=i+1; j<ua->argc; j++) {
306 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
307 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
309 db_get_job_record(ua->jcr, ua->db, &jr);
311 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
312 jobid = atoi(ua->argv[j]);
316 db_list_jobmedia_records(ua->jcr, ua->db, jobid, prtit, ua, llist);
320 /* List for all jobs (jobid=0) */
321 db_list_jobmedia_records(ua->jcr, ua->db, 0, prtit, ua, llist);
325 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
326 db_list_pool_records(ua->jcr, ua->db, prtit, ua, llist);
328 } else if (strcasecmp(ua->argk[i], _("clients")) == 0) {
329 db_list_client_records(ua->jcr, ua->db, prtit, ua, llist);
332 /* List MEDIA or VOLUMES */
333 } else if (strcasecmp(ua->argk[i], _("media")) == 0 ||
334 strcasecmp(ua->argk[i], _("volumes")) == 0) {
336 for (j=i+1; j<ua->argc; j++) {
337 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
338 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
340 db_get_job_record(ua->jcr, ua->db, &jr);
342 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
343 jobid = atoi(ua->argv[j]);
347 VolumeName = get_pool_memory(PM_FNAME);
348 n = db_get_job_volume_names(ua->jcr, ua->db, jobid, &VolumeName);
349 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
350 free_pool_memory(VolumeName);
353 /* if no job or jobid keyword found, then we list all media */
357 /* Is a specific pool wanted? */
358 for (i=1; i<ua->argc; i++) {
359 if (strcasecmp(ua->argk[i], _("pool")) == 0) {
360 if (!get_pool_dbr(ua, &pr)) {
361 bsendmsg(ua, _("No Pool specified.\n"));
364 mr.PoolId = pr.PoolId;
365 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
369 /* List Volumes in all pools */
370 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
371 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"),
372 db_strerror(ua->db));
375 if (num_pools <= 0) {
378 for (i=0; i < num_pools; i++) {
380 if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
381 bsendmsg(ua, _("Pool: %s\n"), pr.Name);
384 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
389 /* List a specific volume */
390 } else if (strcasecmp(ua->argk[i], _("volume")) == 0) {
392 bsendmsg(ua, _("No Volume Name specified.\n"));
395 bstrncpy(mr.VolumeName, ua->argv[i], sizeof(mr.VolumeName));
396 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
398 /* List next volume */
399 } else if (strcasecmp(ua->argk[i], _("nextvol")) == 0 ||
400 strcasecmp(ua->argk[i], _("nextvolume")) == 0) {
408 i = find_arg_with_value(ua, "job");
410 if ((job = select_job_resource(ua)) == NULL) {
414 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
416 Jmsg(jcr, M_ERROR, 0, _("%s is not a job name.\n"), ua->argv[i]);
417 if ((job = select_job_resource(ua)) == NULL) {
422 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
423 pool = run ? run->pool : NULL;
424 if (!complete_jcr_for_job(jcr, job, pool)) {
428 if (!find_next_volume_for_append(jcr, &mr, 0)) {
429 bsendmsg(ua, _("Could not find next Volume.\n"));
431 db_close_database(jcr, jcr->db);
436 bsendmsg(ua, _("The next Volume to be used by Job \"%s\" will be %s\n"),
437 job->hdr.name, mr.VolumeName);
441 db_close_database(jcr, jcr->db);
446 db_close_database(jcr, jcr->db);
450 bsendmsg(ua, _("Could not find next Volume.\n"));
454 bsendmsg(ua, _("Unknown list keyword: %s\n"), NPRT(ua->argk[i]));
461 * For a given job, we examine all his run records
462 * to see if it is scheduled today or tomorrow.
464 RUN *find_next_run(RUN *run, JOB *job, time_t &runtime)
466 time_t now, tomorrow;
469 int mday, wday, month, wom, tmday, twday, tmonth, twom, i;
473 sched = job->schedule;
474 if (sched == NULL) { /* scheduled? */
475 return NULL; /* no nothing to report */
477 /* Break down current time into components */
479 localtime_r(&now, &tm);
480 mday = tm.tm_mday - 1;
486 /* Break down tomorrow into components */
487 tomorrow = now + 60 * 60 * 24;
488 localtime_r(&tomorrow, &tm);
489 tmday = tm.tm_mday - 1;
493 twoy = tm_woy(tomorrow);
500 for ( ; run; run=run->next) {
502 * Find runs in next 24 hours
504 tod = bit_is_set(mday, run->mday) && bit_is_set(wday, run->wday) &&
505 bit_is_set(month, run->month) && bit_is_set(wom, run->wom) &&
506 bit_is_set(woy, run->woy);
508 tom = bit_is_set(tmday, run->mday) && bit_is_set(twday, run->wday) &&
509 bit_is_set(tmonth, run->month) && bit_is_set(twom, run->wom) &&
510 bit_is_set(twoy, run->woy);
513 Dmsg2(000, "tod=%d tom=%d\n", tod, tom);
514 Dmsg1(000, "bit_set_mday=%d\n", bit_is_set(mday, run->mday));
515 Dmsg1(000, "bit_set_wday=%d\n", bit_is_set(wday, run->wday));
516 Dmsg1(000, "bit_set_month=%d\n", bit_is_set(month, run->month));
517 Dmsg1(000, "bit_set_wom=%d\n", bit_is_set(wom, run->wom));
518 Dmsg1(000, "bit_set_woy=%d\n", bit_is_set(woy, run->woy));
520 if (tod) { /* Jobs scheduled today (next 24 hours) */
522 char buf[300], num[10];
523 bsnprintf(buf, sizeof(buf), "tm.hour=%d hour=", tm.tm_hour);
524 for (i=0; i<24; i++) {
525 if (bit_is_set(i, run->hour)) {
526 bsnprintf(num, sizeof(num), "%d ", i);
527 bstrncat(buf, num, sizeof(buf));
530 bstrncat(buf, "\n", sizeof(buf));
531 Dmsg1(000, "%s", buf);
533 /* find time (time_t) job is to be run */
534 localtime_r(&now, &tm);
535 for (i=tm.tm_hour; i < 24; i++) {
536 if (bit_is_set(i, run->hour)) {
538 tm.tm_min = run->minute;
540 runtime = mktime(&tm);
541 Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
543 Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
544 return run; /* found it, return run resource */
550 // Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
551 if (tom) { /* look at jobs scheduled tomorrow */
552 localtime_r(&tomorrow, &tm);
553 for (i=0; i < 24; i++) {
554 if (bit_is_set(i, run->hour)) {
556 tm.tm_min = run->minute;
558 runtime = mktime(&tm);
559 Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
560 if (runtime < tomorrow) {
561 Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
562 return run; /* found it, return run resource */
567 } /* end for loop over runs */
572 * Fill in the remaining fields of the jcr as if it
573 * is going to run the job.
575 int complete_jcr_for_job(JCR *jcr, JOB *job, POOL *pool)
579 memset(&pr, 0, sizeof(POOL_DBR));
580 set_jcr_defaults(jcr, job);
582 jcr->pool = pool; /* override */
584 jcr->db = jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
585 jcr->catalog->db_password, jcr->catalog->db_address,
586 jcr->catalog->db_port, jcr->catalog->db_socket);
587 if (!jcr->db || !db_open_database(jcr, jcr->db)) {
588 Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
589 jcr->catalog->db_name);
591 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
595 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
596 while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
597 /* Try to create the pool */
598 if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
599 Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
600 db_strerror(jcr->db));
602 db_close_database(jcr, jcr->db);
607 Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
610 jcr->PoolId = pr.PoolId;
611 jcr->jr.PoolId = pr.PoolId;
616 static void con_lock_release(void *arg)
621 void do_messages(UAContext *ua, const char *cmd)
625 int do_truncate = FALSE;
628 pthread_cleanup_push(con_lock_release, (void *)NULL);
630 while (fgets(msg, sizeof(msg), con_fd)) {
632 ua->UA_sock->msg = check_pool_memory_size(ua->UA_sock->msg, mlen+1);
633 strcpy(ua->UA_sock->msg, msg);
634 ua->UA_sock->msglen = mlen;
635 bnet_send(ua->UA_sock);
639 ftruncate(fileno(con_fd), 0L);
641 console_msg_pending = FALSE;
642 ua->user_notified_msg_pending = FALSE;
643 pthread_cleanup_pop(0);
648 int qmessagescmd(UAContext *ua, const char *cmd)
650 if (console_msg_pending && ua->auto_display_messages) {
651 do_messages(ua, cmd);
656 int messagescmd(UAContext *ua, const char *cmd)
658 if (console_msg_pending) {
659 do_messages(ua, cmd);
661 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
667 * Callback routine for "printing" database file listing
669 void prtit(void *ctx, const char *msg)
671 UAContext *ua = (UAContext *)ctx;
673 bnet_fsend(ua->UA_sock, "%s", msg);
677 * Format message and send to other end.
679 * If the UA_sock is NULL, it means that there is no user
680 * agent, so we are being called from Bacula core. In
681 * that case direct the messages to the Job.
683 void bsendmsg(void *ctx, const char *fmt, ...)
686 UAContext *ua = (UAContext *)ctx;
687 BSOCK *bs = ua->UA_sock;
694 msg = get_pool_memory(PM_EMSG);
698 maxlen = sizeof_pool_memory(msg) - 1;
699 va_start(arg_ptr, fmt);
700 len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
702 if (len < 0 || len >= maxlen) {
703 msg = realloc_pool_memory(msg, maxlen + maxlen/2);
711 } else { /* No UA, send to Job */
712 Jmsg(ua->jcr, M_INFO, 0, "%s", msg);
713 free_pool_memory(msg);