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 RES **res_head;
41 extern int console_msg_pending;
43 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);
49 static bool list_nextvol(UAContext *ua);
52 * Turn auto display of console messages on/off
54 int autodisplay_cmd(UAContext *ua, const char *cmd)
56 static const char *kw[] = {
61 switch (find_arg_keyword(ua, kw)) {
63 ua->auto_display_messages = 1;
66 ua->auto_display_messages = 0;
69 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
76 * Turn batch processing on/off
78 int gui_cmd(UAContext *ua, const char *cmd)
80 static const char *kw[] = {
85 switch (find_arg_keyword(ua, kw)) {
95 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
103 struct showstruct {const char *res_name; int type;};
104 static struct showstruct reses[] = {
105 {N_("directors"), R_DIRECTOR},
106 {N_("clients"), R_CLIENT},
107 {N_("counters"), R_COUNTER},
108 {N_("devices"), R_DEVICE},
110 {N_("storages"), R_STORAGE},
111 {N_("catalogs"), R_CATALOG},
112 {N_("schedules"), R_SCHEDULE},
113 {N_("filesets"), R_FILESET},
114 {N_("pools"), R_POOL},
115 {N_("messages"), R_MSGS},
126 * show <resource-keyword-name> e.g. show directors
127 * show <resource-keyword-name>=<name> e.g. show director=HeadMan
130 int show_cmd(UAContext *ua, const char *cmd)
137 Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
141 for (i=1; i<ua->argc; i++) {
143 res_name = ua->argk[i];
144 if (!ua->argv[i]) { /* was a name given? */
145 /* No name, dump all resources of specified type */
147 len = strlen(res_name);
148 for (j=0; reses[j].res_name; j++) {
149 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
150 type = reses[j].type;
152 res = res_head[type-r_first];
161 /* Dump a single resource with specified name */
163 len = strlen(res_name);
164 for (j=0; reses[j].res_name; j++) {
165 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
166 type = reses[j].type;
167 res = (RES *)GetResWithName(type, ua->argv[i]);
178 for (j=r_first; j<=r_last; j++) {
179 dump_resource(j, res_head[j-r_first], bsendmsg, ua);
183 bsendmsg(ua, _("Keywords for the show command are:\n"));
184 for (j=0; reses[j].res_name; j++) {
185 bsendmsg(ua, "%s\n", _(reses[j].res_name));
189 bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
192 bsendmsg(ua, _("Resource %s not found\n"), res_name);
195 dump_resource(recurse?type:-type, res, bsendmsg, ua);
208 * List contents of database
210 * list jobs - lists all jobs run
211 * list jobid=nnn - list job data for jobid
212 * list job=name - list job data for job
213 * list jobmedia jobid=<nn>
214 * list jobmedia job=name
215 * list files jobid=<nn> - list files saved for job nn
216 * list files job=name
217 * list pools - list pool records
218 * list jobtotals - list totals for all jobs
219 * list media - list media for given pool (deprecated)
220 * list volumes - list Volumes
221 * list clients - list clients
222 * list nextvol job=xx - list the next vol to be used by job
223 * list nextvolume job=xx - same as above.
227 /* Do long or full listing */
228 int llist_cmd(UAContext *ua, const char *cmd)
230 return do_list_cmd(ua, cmd, VERT_LIST);
233 /* Do short or summary listing */
234 int list_cmd(UAContext *ua, const char *cmd)
236 return do_list_cmd(ua, cmd, HORZ_LIST);
239 static int do_list_cmd(UAContext *ua, const char *cmd, e_list_type llist)
251 memset(&jr, 0, sizeof(jr));
252 memset(&pr, 0, sizeof(pr));
253 memset(&mr, 0, sizeof(mr));
255 Dmsg1(20, "list: %s\n", cmd);
258 bsendmsg(ua, _("Hey! DB is NULL\n"));
261 /* Scan arguments looking for things to do */
262 for (i=1; i<ua->argc; i++) {
264 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
265 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
268 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
269 db_list_job_totals(ua->jcr, ua->db, &jr, prtit, ua);
272 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
274 jobid = str_to_int64(ua->argv[i]);
277 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
282 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
283 bstrncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
285 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
288 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
290 for (j=i+1; j<ua->argc; j++) {
291 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
292 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
294 db_get_job_record(ua->jcr, ua->db, &jr);
296 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
297 jobid = str_to_int64(ua->argv[j]);
302 db_list_files_for_job(ua->jcr, ua->db, jobid, prtit, ua);
307 } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 0) {
309 for (j=i+1; j<ua->argc; j++) {
310 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
311 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
313 db_get_job_record(ua->jcr, ua->db, &jr);
315 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
316 jobid = str_to_int64(ua->argv[j]);
320 db_list_jobmedia_records(ua->jcr, ua->db, jobid, prtit, ua, llist);
324 /* List for all jobs (jobid=0) */
325 db_list_jobmedia_records(ua->jcr, ua->db, 0, prtit, ua, llist);
329 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
330 db_list_pool_records(ua->jcr, ua->db, prtit, ua, llist);
332 } else if (strcasecmp(ua->argk[i], _("clients")) == 0) {
333 db_list_client_records(ua->jcr, ua->db, prtit, ua, llist);
336 /* List MEDIA or VOLUMES */
337 } else if (strcasecmp(ua->argk[i], _("media")) == 0 ||
338 strcasecmp(ua->argk[i], _("volumes")) == 0) {
340 for (j=i+1; j<ua->argc; j++) {
341 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
342 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
344 db_get_job_record(ua->jcr, ua->db, &jr);
346 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
347 jobid = str_to_int64(ua->argv[j]);
351 VolumeName = get_pool_memory(PM_FNAME);
352 n = db_get_job_volume_names(ua->jcr, ua->db, jobid, &VolumeName);
353 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
354 free_pool_memory(VolumeName);
357 /* if no job or jobid keyword found, then we list all media */
361 /* Is a specific pool wanted? */
362 for (i=1; i<ua->argc; i++) {
363 if (strcasecmp(ua->argk[i], _("pool")) == 0) {
364 if (!get_pool_dbr(ua, &pr)) {
365 bsendmsg(ua, _("No Pool specified.\n"));
368 mr.PoolId = pr.PoolId;
369 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
373 /* List Volumes in all pools */
374 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
375 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"),
376 db_strerror(ua->db));
379 if (num_pools <= 0) {
382 for (i=0; i < num_pools; i++) {
384 if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
385 bsendmsg(ua, _("Pool: %s\n"), pr.Name);
388 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
393 /* List a specific volume */
394 } else if (strcasecmp(ua->argk[i], _("volume")) == 0) {
396 bsendmsg(ua, _("No Volume Name specified.\n"));
399 bstrncpy(mr.VolumeName, ua->argv[i], sizeof(mr.VolumeName));
400 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
402 /* List next volume */
403 } else if (strcasecmp(ua->argk[i], _("nextvol")) == 0 ||
404 strcasecmp(ua->argk[i], _("nextvolume")) == 0) {
407 bsendmsg(ua, _("Unknown list keyword: %s\n"), NPRT(ua->argk[i]));
413 static bool list_nextvol(UAContext *ua)
423 memset(&mr, 0, sizeof(mr));
424 int i = find_arg_with_value(ua, "job");
426 if ((job = select_job_resource(ua)) == NULL) {
430 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
432 Jmsg(jcr, M_ERROR, 0, _("%s is not a job name.\n"), ua->argv[i]);
433 if ((job = select_job_resource(ua)) == NULL) {
438 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
439 pool = run->pool ? run->pool : NULL;
440 if (!complete_jcr_for_job(jcr, job, pool)) {
443 mr.PoolId = jcr->PoolId;
445 jcr->store = run->storage;
447 if (!find_next_volume_for_append(jcr, &mr, 0)) {
448 bsendmsg(ua, _("Could not find next Volume.\n"));
450 bsendmsg(ua, _("The next Volume to be used by Job \"%s\" will be %s\n"),
451 job->hdr.name, mr.VolumeName);
454 if (jcr->db && jcr->db != ua->db) {
455 db_close_database(jcr, jcr->db);
460 bsendmsg(ua, _("Could not find next Volume.\n"));
468 * For a given job, we examine all his run records
469 * to see if it is scheduled today or tomorrow.
471 RUN *find_next_run(RUN *run, JOB *job, time_t &runtime)
473 time_t now, tomorrow;
476 int mday, wday, month, wom, tmday, twday, tmonth, twom, i;
480 sched = job->schedule;
481 if (sched == NULL) { /* scheduled? */
482 return NULL; /* no nothing to report */
484 /* Break down current time into components */
486 localtime_r(&now, &tm);
487 mday = tm.tm_mday - 1;
493 /* Break down tomorrow into components */
494 tomorrow = now + 60 * 60 * 24;
495 localtime_r(&tomorrow, &tm);
496 tmday = tm.tm_mday - 1;
500 twoy = tm_woy(tomorrow);
507 for ( ; run; run=run->next) {
509 * Find runs in next 24 hours
511 tod = bit_is_set(mday, run->mday) && bit_is_set(wday, run->wday) &&
512 bit_is_set(month, run->month) && bit_is_set(wom, run->wom) &&
513 bit_is_set(woy, run->woy);
515 tom = bit_is_set(tmday, run->mday) && bit_is_set(twday, run->wday) &&
516 bit_is_set(tmonth, run->month) && bit_is_set(twom, run->wom) &&
517 bit_is_set(twoy, run->woy);
520 Dmsg2(000, "tod=%d tom=%d\n", tod, tom);
521 Dmsg1(000, "bit_set_mday=%d\n", bit_is_set(mday, run->mday));
522 Dmsg1(000, "bit_set_wday=%d\n", bit_is_set(wday, run->wday));
523 Dmsg1(000, "bit_set_month=%d\n", bit_is_set(month, run->month));
524 Dmsg1(000, "bit_set_wom=%d\n", bit_is_set(wom, run->wom));
525 Dmsg1(000, "bit_set_woy=%d\n", bit_is_set(woy, run->woy));
527 if (tod) { /* Jobs scheduled today (next 24 hours) */
529 char buf[300], num[10];
530 bsnprintf(buf, sizeof(buf), "tm.hour=%d hour=", tm.tm_hour);
531 for (i=0; i<24; i++) {
532 if (bit_is_set(i, run->hour)) {
533 bsnprintf(num, sizeof(num), "%d ", i);
534 bstrncat(buf, num, sizeof(buf));
537 bstrncat(buf, "\n", sizeof(buf));
538 Dmsg1(000, "%s", buf);
540 /* find time (time_t) job is to be run */
541 localtime_r(&now, &tm);
542 for (i=tm.tm_hour; i < 24; i++) {
543 if (bit_is_set(i, run->hour)) {
545 tm.tm_min = run->minute;
547 runtime = mktime(&tm);
548 Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
550 Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
551 return run; /* found it, return run resource */
557 // Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
558 if (tom) { /* look at jobs scheduled tomorrow */
559 localtime_r(&tomorrow, &tm);
560 for (i=0; i < 24; i++) {
561 if (bit_is_set(i, run->hour)) {
563 tm.tm_min = run->minute;
565 runtime = mktime(&tm);
566 Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
567 if (runtime < tomorrow) {
568 Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
569 return run; /* found it, return run resource */
574 } /* end for loop over runs */
579 * Fill in the remaining fields of the jcr as if it
580 * is going to run the job.
582 int complete_jcr_for_job(JCR *jcr, JOB *job, POOL *pool)
586 memset(&pr, 0, sizeof(POOL_DBR));
587 set_jcr_defaults(jcr, job);
589 jcr->pool = pool; /* override */
591 jcr->db = jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
592 jcr->catalog->db_password, jcr->catalog->db_address,
593 jcr->catalog->db_port, jcr->catalog->db_socket,
594 jcr->catalog->mult_db_connections);
595 if (!jcr->db || !db_open_database(jcr, jcr->db)) {
596 Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
597 jcr->catalog->db_name);
599 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
603 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
604 while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
605 /* Try to create the pool */
606 if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
607 Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
608 db_strerror(jcr->db));
610 db_close_database(jcr, jcr->db);
615 Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
618 jcr->PoolId = pr.PoolId;
619 jcr->jr.PoolId = pr.PoolId;
624 static void con_lock_release(void *arg)
629 void do_messages(UAContext *ua, const char *cmd)
633 int do_truncate = FALSE;
636 pthread_cleanup_push(con_lock_release, (void *)NULL);
638 while (fgets(msg, sizeof(msg), con_fd)) {
640 ua->UA_sock->msg = check_pool_memory_size(ua->UA_sock->msg, mlen+1);
641 strcpy(ua->UA_sock->msg, msg);
642 ua->UA_sock->msglen = mlen;
643 bnet_send(ua->UA_sock);
647 ftruncate(fileno(con_fd), 0L);
649 console_msg_pending = FALSE;
650 ua->user_notified_msg_pending = FALSE;
651 pthread_cleanup_pop(0);
656 int qmessagescmd(UAContext *ua, const char *cmd)
658 if (console_msg_pending && ua->auto_display_messages) {
659 do_messages(ua, cmd);
664 int messagescmd(UAContext *ua, const char *cmd)
666 if (console_msg_pending) {
667 do_messages(ua, cmd);
669 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
675 * Callback routine for "printing" database file listing
677 void prtit(void *ctx, const char *msg)
679 UAContext *ua = (UAContext *)ctx;
681 bnet_fsend(ua->UA_sock, "%s", msg);
685 * Format message and send to other end.
687 * If the UA_sock is NULL, it means that there is no user
688 * agent, so we are being called from Bacula core. In
689 * that case direct the messages to the Job.
691 void bsendmsg(void *ctx, const char *fmt, ...)
694 UAContext *ua = (UAContext *)ctx;
695 BSOCK *bs = ua->UA_sock;
702 msg = get_pool_memory(PM_EMSG);
706 maxlen = sizeof_pool_memory(msg) - 1;
707 va_start(arg_ptr, fmt);
708 len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
710 if (len < 0 || len >= maxlen) {
711 msg = realloc_pool_memory(msg, maxlen + maxlen/2);
719 } else { /* No UA, send to Job */
720 Jmsg(ua->jcr, M_INFO, 0, "%s", msg);
721 free_pool_memory(msg);