3 * Bacula Director -- User Agent Output Commands
4 * I.e. messages, listing database, showing resources, ...
6 * Kern Sibbald, September MM
12 Copyright (C) 2000, 2001, 2002 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,
35 /* Imported subroutines */
36 extern void run_job(JCR *jcr);
38 /* Imported variables */
41 extern struct s_res resources[];
42 extern int console_msg_pending;
46 /* Imported functions */
48 /* Forward referenced functions */
52 * Turn auto display of console messages on/off
54 int autodisplaycmd(UAContext *ua, char *cmd)
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 struct showstruct {char *res_name; int type;};
77 static struct showstruct reses[] = {
78 {N_("directors"), R_DIRECTOR},
79 {N_("clients"), R_CLIENT},
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 showcmd(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
190 int listcmd(UAContext *ua, char *cmd)
202 memset(&jr, 0, sizeof(jr));
203 memset(&pr, 0, sizeof(pr));
204 memset(&mr, 0, sizeof(mr));
206 Dmsg1(20, "list: %s\n", cmd);
209 bsendmsg(ua, _("Hey! DB is NULL\n"));
212 /* Scan arguments looking for things to do */
213 for (i=1; i<ua->argc; i++) {
215 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
216 db_list_job_records(ua->db, &jr, prtit, ua);
219 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
220 db_list_job_totals(ua->db, &jr, prtit, ua);
223 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
225 jobid = atoi(ua->argv[i]);
228 db_list_job_records(ua->db, &jr, prtit, ua);
233 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
234 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
235 jr.Job[MAX_NAME_LENGTH-1] = 0;
237 db_list_job_records(ua->db, &jr, prtit, ua);
240 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
242 for (j=i+1; j<ua->argc; j++) {
243 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
244 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
245 jr.Job[MAX_NAME_LENGTH-1] = 0;
247 db_get_job_record(ua->db, &jr);
249 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
250 jobid = atoi(ua->argv[j]);
255 db_list_files_for_job(ua->db, jobid, prtit, ua);
260 } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 0) {
262 for (j=i+1; j<ua->argc; j++) {
263 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
264 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
265 jr.Job[MAX_NAME_LENGTH-1] = 0;
267 db_get_job_record(ua->db, &jr);
269 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
270 jobid = atoi(ua->argv[j]);
274 db_list_jobmedia_records(ua->db, jobid, prtit, ua);
278 /* List for all jobs (jobid=0) */
279 db_list_jobmedia_records(ua->db, 0, prtit, ua);
283 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
284 db_list_pool_records(ua->db, prtit, ua);
287 } else if (strcasecmp(ua->argk[i], _("media")) == 0) {
289 for (j=i+1; j<ua->argc; j++) {
290 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
291 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
292 jr.Job[MAX_NAME_LENGTH-1] = 0;
294 db_get_job_record(ua->db, &jr);
296 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
297 jobid = atoi(ua->argv[j]);
301 VolumeName = (char *) get_pool_memory(PM_FNAME);
302 n = db_get_job_volume_names(ua->db, jobid, VolumeName);
303 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
304 free_memory(VolumeName);
307 /* if no job or jobid keyword found, then we list all media */
309 if (!get_pool_dbr(ua, &pr)) {
312 mr.PoolId = pr.PoolId;
313 db_list_media_records(ua->db, &mr, prtit, ua);
316 bsendmsg(ua, _("Unknown list keyword: %s\n"), ua->argk[i]);
322 void do_messages(UAContext *ua, char *cmd)
327 fcntl(fileno(con_fd), F_SETLKW);
329 while (fgets(msg, sizeof(msg), con_fd)) {
331 ua->UA_sock->msg = (char *) check_pool_memory_size(
332 ua->UA_sock->msg, mlen+1);
333 strcpy(ua->UA_sock->msg, msg);
334 ua->UA_sock->msglen = mlen;
335 bnet_send(ua->UA_sock);
337 ftruncate(fileno(con_fd), 0L);
338 console_msg_pending = FALSE;
339 fcntl(fileno(con_fd), F_UNLCK);
340 ua->user_notified_msg_pending = FALSE;
344 int qmessagescmd(UAContext *ua, char *cmd)
346 if (console_msg_pending && ua->auto_display_messages) {
347 do_messages(ua, cmd);
352 int messagescmd(UAContext *ua, char *cmd)
354 if (console_msg_pending) {
355 do_messages(ua, cmd);
357 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
363 * Callback routine for "printing" database file listing
365 void prtit(void *ctx, char *msg)
367 UAContext *ua = (UAContext *)ctx;
369 bnet_fsend(ua->UA_sock, "%s", msg);
373 * Format message and send to other end.
375 * If the UA_sock is NULL, it means that there is no user
376 * agent, so we are being called from Bacula core. In
377 * that case direct the messages to the Job.
379 void bsendmsg(void *ctx, char *fmt, ...)
382 UAContext *ua = (UAContext *)ctx;
383 BSOCK *bs = ua->UA_sock;
390 msg = (char *)get_pool_memory(PM_EMSG);
394 maxlen = sizeof_pool_memory(msg) - 1;
395 va_start(arg_ptr, fmt);
396 len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
398 if (len < 0 || len >= maxlen) {
399 msg = (char *) realloc_pool_memory(msg, maxlen + 200);
406 } else { /* No UA, send to Job */
407 Jmsg(ua->jcr, M_INFO, 0, msg);