3 * Bacula Director -- User Agent Output Commands
4 * I.e. messages, listing database, showing resources, ...
6 * Kern Sibbald, September MM
12 Copyright (C) 2000-2003 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 struct s_res 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, char *cmd, e_list_type llist);
51 * Turn auto display of console messages on/off
53 int autodisplaycmd(UAContext *ua, char *cmd)
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 struct showstruct {char *res_name; int type;};
76 static struct showstruct reses[] = {
77 {N_("directors"), R_DIRECTOR},
78 {N_("clients"), R_CLIENT},
79 {N_("counters"), R_COUNTER},
81 {N_("storages"), R_STORAGE},
82 {N_("catalogs"), R_CATALOG},
83 {N_("schedules"), R_SCHEDULE},
84 {N_("filesets"), R_FILESET},
85 {N_("groups"), R_GROUP},
86 {N_("pools"), R_POOL},
87 {N_("messages"), R_MSGS},
98 * show <resource-keyword-name> e.g. show directors
99 * show <resource-keyword-name>=<name> e.g. show director=HeadMan
102 int show_cmd(UAContext *ua, char *cmd)
109 Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
112 for (i=1; i<ua->argc; i++) {
114 res_name = ua->argk[i];
115 if (!ua->argv[i]) { /* was a name given? */
116 /* No name, dump all resources of specified type */
118 len = strlen(res_name);
119 for (j=0; reses[j].res_name; j++) {
120 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
121 type = reses[j].type;
123 res = resources[type-r_first].res_head;
131 /* Dump a single resource with specified name */
133 len = strlen(res_name);
134 for (j=0; reses[j].res_name; j++) {
135 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
136 type = reses[j].type;
137 res = (RES *)GetResWithName(type, ua->argv[i]);
148 for (j=r_first; j<=r_last; j++) {
149 dump_resource(j, resources[j-r_first].res_head, bsendmsg, ua);
153 bsendmsg(ua, _("Keywords for the show command are:\n"));
154 for (j=0; reses[j].res_name; j++) {
155 bsendmsg(ua, "%s\n", _(reses[j].res_name));
159 bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
162 bsendmsg(ua, _("Resource %s not found\n"), res_name);
165 dump_resource(recurse?type:-type, res, bsendmsg, ua);
176 * List contents of database
178 * list jobs - lists all jobs run
179 * list jobid=nnn - list job data for jobid
180 * list job=name - list job data for job
181 * list jobmedia jobid=<nn>
182 * list jobmedia job=name
183 * list files jobid=<nn> - list files saved for job nn
184 * list files job=name
185 * list pools - list pool records
186 * list jobtotals - list totals for all jobs
187 * list media - list media for given pool (deprecated)
188 * list volumes - list Volumes
189 * list clients - list clients
190 * list nextvol job=xx - list the next vol to be used by job
191 * list nextvolume job=xx - same as above.
195 /* Do long or full listing */
196 int llist_cmd(UAContext *ua, char *cmd)
198 return do_list_cmd(ua, cmd, VERT_LIST);
201 /* Do short or summary listing */
202 int list_cmd(UAContext *ua, char *cmd)
204 return do_list_cmd(ua, cmd, HORZ_LIST);
207 static int do_list_cmd(UAContext *ua, char *cmd, e_list_type llist)
219 memset(&jr, 0, sizeof(jr));
220 memset(&pr, 0, sizeof(pr));
221 memset(&mr, 0, sizeof(mr));
223 Dmsg1(20, "list: %s\n", cmd);
226 bsendmsg(ua, _("Hey! DB is NULL\n"));
229 /* Scan arguments looking for things to do */
230 for (i=1; i<ua->argc; i++) {
232 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
233 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
236 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
237 db_list_job_totals(ua->jcr, ua->db, &jr, prtit, ua);
240 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
242 jobid = atoi(ua->argv[i]);
245 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
250 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
251 bstrncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
253 db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
256 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
258 for (j=i+1; j<ua->argc; j++) {
259 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
260 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
262 db_get_job_record(ua->jcr, ua->db, &jr);
264 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
265 jobid = atoi(ua->argv[j]);
270 db_list_files_for_job(ua->jcr, ua->db, jobid, prtit, ua);
275 } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 0) {
277 for (j=i+1; j<ua->argc; j++) {
278 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
279 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
281 db_get_job_record(ua->jcr, ua->db, &jr);
283 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
284 jobid = atoi(ua->argv[j]);
288 db_list_jobmedia_records(ua->jcr, ua->db, jobid, prtit, ua, llist);
292 /* List for all jobs (jobid=0) */
293 db_list_jobmedia_records(ua->jcr, ua->db, 0, prtit, ua, llist);
297 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
298 db_list_pool_records(ua->jcr, ua->db, prtit, ua, llist);
300 } else if (strcasecmp(ua->argk[i], _("clients")) == 0) {
301 db_list_client_records(ua->jcr, ua->db, prtit, ua, llist);
304 /* List MEDIA or VOLUMES */
305 } else if (strcasecmp(ua->argk[i], _("media")) == 0 ||
306 strcasecmp(ua->argk[i], _("volumes")) == 0) {
308 for (j=i+1; j<ua->argc; j++) {
309 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
310 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
312 db_get_job_record(ua->jcr, ua->db, &jr);
314 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
315 jobid = atoi(ua->argv[j]);
319 VolumeName = get_pool_memory(PM_FNAME);
320 n = db_get_job_volume_names(ua->jcr, ua->db, jobid, &VolumeName);
321 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
322 free_pool_memory(VolumeName);
325 /* if no job or jobid keyword found, then we list all media */
329 /* Is a specific pool wanted? */
330 for (i=1; i<ua->argc; i++) {
331 if (strcasecmp(ua->argk[i], _("pool")) == 0) {
332 if (!get_pool_dbr(ua, &pr)) {
333 bsendmsg(ua, _("No Pool specified.\n"));
336 mr.PoolId = pr.PoolId;
337 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
341 /* List Volumes in all pools */
342 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
343 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"),
344 db_strerror(ua->db));
347 if (num_pools <= 0) {
350 for (i=0; i < num_pools; i++) {
352 if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
353 bsendmsg(ua, _("Pool: %s\n"), pr.Name);
356 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
361 /* List a specific volume */
362 } else if (strcasecmp(ua->argk[i], _("volume")) == 0) {
364 bsendmsg(ua, _("No Volume Name specified.\n"));
367 bstrncpy(mr.VolumeName, ua->argv[i], sizeof(mr.VolumeName));
368 db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
370 /* List next volume */
371 } else if (strcasecmp(ua->argk[i], _("nextvol")) == 0 ||
372 strcasecmp(ua->argk[i], _("nextvolume")) == 0) {
380 i = find_arg_with_value(ua, "job");
382 if ((job = select_job_resource(ua)) == NULL) {
386 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
388 Jmsg(jcr, M_ERROR, 0, _("%s is not a job name.\n"), ua->argv[i]);
389 if ((job = select_job_resource(ua)) == NULL) {
394 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
395 pool = run ? run->pool : NULL;
396 if (!complete_jcr_for_job(jcr, job, pool)) {
400 if (!find_next_volume_for_append(jcr, &mr, 0)) {
401 bsendmsg(ua, _("Could not find next Volume.\n"));
403 db_close_database(jcr, jcr->db);
408 bsendmsg(ua, _("The next Volume to be used by Job \"%s\" will be %s\n"),
409 job->hdr.name, mr.VolumeName);
413 db_close_database(jcr, jcr->db);
418 db_close_database(jcr, jcr->db);
422 bsendmsg(ua, _("Could not find next Volume.\n"));
426 bsendmsg(ua, _("Unknown list keyword: %s\n"), NPRT(ua->argk[i]));
433 * For a given job, we examine all his run records
434 * to see if it is scheduled today or tomorrow.
436 RUN *find_next_run(RUN *run, JOB *job, time_t &runtime)
438 time_t now, tomorrow;
441 int mday, wday, month, wom, tmday, twday, tmonth, twom, i, hour;
445 Dmsg0(200, "enter find_runs()\n");
447 sched = job->schedule;
448 if (sched == NULL) { /* scheduled? */
449 return NULL; /* no nothing to report */
451 /* Break down current time into components */
453 localtime_r(&now, &tm);
454 mday = tm.tm_mday - 1;
460 /* Break down tomorrow into components */
461 tomorrow = now + 60 * 60 * 24;
462 localtime_r(&tomorrow, &tm);
463 tmday = tm.tm_mday - 1;
467 twoy = tm_woy(tomorrow);
474 for ( ; run; run=run->next) {
476 * Find runs in next 24 hours
478 tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) &&
479 bit_is_set(month, run->month) && bit_is_set(wom, run->wom) &&
480 bit_is_set(woy, run->woy);
482 tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
483 bit_is_set(tmonth, run->month) && bit_is_set(twom, run->wom) &&
484 bit_is_set(twoy, run->woy);
486 // Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
487 // Dmsg2(200, "wom=%d twom=%d\n", wom, twom);
488 // Dmsg1(200, "bit_set_wom=%d\n", bit_is_set(wom, run->wom));
489 if (tod) { /* Jobs scheduled today (next 24 hours) */
490 /* find time (time_t) job is to be run */
491 localtime_r(&now, &tm);
493 for (i=tm.tm_hour; i < 24; i++) {
494 if (bit_is_set(i, run->hour)) {
496 tm.tm_min = run->minute;
498 runtime = mktime(&tm);
500 Dmsg2(000, "Found it level=%d %c\n", run->level, run->level);
501 return run; /* found it, return run resource */
507 // Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
508 if (tom) { /* look at jobs scheduled tomorrow */
509 localtime_r(&tomorrow, &tm);
511 for (i=0; i < 24; i++) {
512 if (bit_is_set(i, run->hour)) {
518 tm.tm_min = run->minute;
520 runtime = mktime(&tm);
521 Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
522 if (runtime < tomorrow) {
523 return run; /* found it, return run resource */
526 } /* end for loop over runs */
531 * Fill in the remaining fields of the jcr as if it
532 * is going to run the job.
534 int complete_jcr_for_job(JCR *jcr, JOB *job, POOL *pool)
538 memset(&pr, 0, sizeof(POOL_DBR));
539 set_jcr_defaults(jcr, job);
541 jcr->pool = pool; /* override */
543 jcr->db = jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
544 jcr->catalog->db_password, jcr->catalog->db_address,
545 jcr->catalog->db_port, jcr->catalog->db_socket);
546 if (!jcr->db || !db_open_database(jcr, jcr->db)) {
547 Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
548 jcr->catalog->db_name);
550 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
554 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
555 while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
556 /* Try to create the pool */
557 if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
558 Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
559 db_strerror(jcr->db));
561 db_close_database(jcr, jcr->db);
566 Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
569 jcr->PoolId = pr.PoolId;
570 jcr->jr.PoolId = pr.PoolId;
575 static void con_lock_release(void *arg)
580 void do_messages(UAContext *ua, char *cmd)
584 int do_truncate = FALSE;
587 pthread_cleanup_push(con_lock_release, (void *)NULL);
589 while (fgets(msg, sizeof(msg), con_fd)) {
591 ua->UA_sock->msg = check_pool_memory_size(ua->UA_sock->msg, mlen+1);
592 strcpy(ua->UA_sock->msg, msg);
593 ua->UA_sock->msglen = mlen;
594 bnet_send(ua->UA_sock);
598 ftruncate(fileno(con_fd), 0L);
600 console_msg_pending = FALSE;
601 ua->user_notified_msg_pending = FALSE;
602 pthread_cleanup_pop(0);
607 int qmessagescmd(UAContext *ua, char *cmd)
609 if (console_msg_pending && ua->auto_display_messages) {
610 do_messages(ua, cmd);
615 int messagescmd(UAContext *ua, char *cmd)
617 if (console_msg_pending) {
618 do_messages(ua, cmd);
620 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
626 * Callback routine for "printing" database file listing
628 void prtit(void *ctx, char *msg)
630 UAContext *ua = (UAContext *)ctx;
632 bnet_fsend(ua->UA_sock, "%s", msg);
636 * Format message and send to other end.
638 * If the UA_sock is NULL, it means that there is no user
639 * agent, so we are being called from Bacula core. In
640 * that case direct the messages to the Job.
642 void bsendmsg(void *ctx, char *fmt, ...)
645 UAContext *ua = (UAContext *)ctx;
646 BSOCK *bs = ua->UA_sock;
653 msg = get_pool_memory(PM_EMSG);
657 maxlen = sizeof_pool_memory(msg) - 1;
658 va_start(arg_ptr, fmt);
659 len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
661 if (len < 0 || len >= maxlen) {
662 msg = realloc_pool_memory(msg, maxlen + maxlen/2);
670 } else { /* No UA, send to Job */
671 Jmsg(ua->jcr, M_INFO, 0, "%s", msg);
672 free_pool_memory(msg);