3 * Bacula Director -- User Agent Output Commands
4 * I.e. messages, listing database, showing resources, ...
6 * Kern Sibbald, September MM
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported subroutines */
34 extern void run_job(JCR *jcr);
36 /* Imported variables */
39 extern struct s_res resources[];
40 extern int console_msg_pending;
44 /* Imported functions */
46 /* Forward referenced functions */
50 * Turn auto display of console messages on/off
52 int autodisplaycmd(UAContext *ua, char *cmd)
59 switch (find_arg_keyword(ua, kw)) {
61 ua->auto_display_messages = 1;
64 ua->auto_display_messages = 0;
67 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
74 struct showstruct {char *res_name; int type;};
75 static struct showstruct reses[] = {
76 {N_("directors"), R_DIRECTOR},
77 {N_("clients"), R_CLIENT},
79 {N_("storages"), R_STORAGE},
80 {N_("catalogs"), R_CATALOG},
81 {N_("schedules"), R_SCHEDULE},
82 {N_("filesets"), R_FILESET},
83 {N_("groups"), R_GROUP},
84 {N_("pools"), R_POOL},
85 {N_("messages"), R_MSGS},
96 * show <resource-keyword-name> e.g. show directors
97 * show <resource-keyword-name>=<name> e.g. show director=HeadMan
100 int showcmd(UAContext *ua, char *cmd)
107 Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
110 for (i=1; i<ua->argc; i++) {
112 res_name = ua->argk[i];
113 if (!ua->argv[i]) { /* was a name given? */
114 /* No name, dump all resources of specified type */
116 len = strlen(res_name);
117 for (j=0; reses[j].res_name; j++) {
118 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
119 type = reses[j].type;
121 res = resources[type-r_first].res_head;
129 /* Dump a single resource with specified name */
131 len = strlen(res_name);
132 for (j=0; reses[j].res_name; j++) {
133 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
134 type = reses[j].type;
135 res = (RES *)GetResWithName(type, ua->argv[i]);
146 for (j=r_first; j<=r_last; j++) {
147 dump_resource(j, resources[j-r_first].res_head, bsendmsg, ua);
151 bsendmsg(ua, _("Keywords for the show command are:\n"));
152 for (j=0; reses[j].res_name; j++) {
153 bsendmsg(ua, "%s\n", _(reses[j].res_name));
157 bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
160 bsendmsg(ua, _("Resource %s not found\n"), res_name);
163 dump_resource(recurse?type:-type, res, bsendmsg, ua);
172 * Callback routine for "printing" database file listing
174 void prtit(void *ctx, char *msg)
176 UAContext *ua = (UAContext *)ctx;
178 bnet_fsend(ua->UA_sock, "%s", msg);
181 /* Format message and send to other end */
182 void bsendmsg(void *ctx, char *fmt, ...)
185 UAContext *ua = (UAContext *)ctx;
186 BSOCK *bs = ua->UA_sock;
190 maxlen = sizeof_pool_memory(bs->msg) - 1;
191 va_start(arg_ptr, fmt);
192 bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
194 if (bs->msglen < 0 || bs->msglen >= maxlen) {
195 bs->msg = (char *) realloc_pool_memory(bs->msg, maxlen + 200);
203 * List contents of database
205 * list jobs - lists all jobs run
206 * list jobid=nnn - list job data for jobid
207 * list job=name - list job data for job
208 * list jobmedia jobid=<nn>
209 * list jobmedia job=name
210 * list files jobid=<nn> - list files saved for job nn
211 * list files job=name
212 * list pools - list pool records
213 * list jobtotals - list totals for all jobs
214 * list media - list media for given pool
217 int listcmd(UAContext *ua, char *cmd)
229 memset(&jr, 0, sizeof(jr));
230 memset(&pr, 0, sizeof(pr));
231 memset(&mr, 0, sizeof(mr));
233 Dmsg1(20, "list: %s\n", cmd);
236 bsendmsg(ua, _("Hey! DB is NULL\n"));
239 /* Scan arguments looking for things to do */
240 for (i=1; i<ua->argc; i++) {
242 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
243 db_list_job_records(ua->db, &jr, prtit, ua);
246 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
247 db_list_job_totals(ua->db, &jr, prtit, ua);
250 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
252 jobid = atoi(ua->argv[i]);
255 db_list_job_records(ua->db, &jr, prtit, ua);
260 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
261 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
262 jr.Job[MAX_NAME_LENGTH-1] = 0;
264 db_list_job_records(ua->db, &jr, prtit, ua);
267 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
269 for (j=i+1; j<ua->argc; j++) {
270 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
271 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
272 jr.Job[MAX_NAME_LENGTH-1] = 0;
274 db_get_job_record(ua->db, &jr);
276 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
277 jobid = atoi(ua->argv[j]);
282 db_list_files_for_job(ua->db, jobid, prtit, ua);
287 } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 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 db_list_jobmedia_records(ua->db, jobid, prtit, ua);
305 /* List for all jobs (jobid=0) */
306 db_list_jobmedia_records(ua->db, 0, prtit, ua);
310 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
311 db_list_pool_records(ua->db, prtit, ua);
314 } else if (strcasecmp(ua->argk[i], _("media")) == 0) {
316 for (j=i+1; j<ua->argc; j++) {
317 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
318 strncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
319 jr.Job[MAX_NAME_LENGTH-1] = 0;
321 db_get_job_record(ua->db, &jr);
323 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
324 jobid = atoi(ua->argv[j]);
328 VolumeName = (char *) get_pool_memory(PM_FNAME);
329 n = db_get_job_volume_names(ua->db, jobid, VolumeName);
330 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
331 free_memory(VolumeName);
334 /* if no job or jobid keyword found, then we list all media */
336 if (!get_pool_dbr(ua, &pr)) {
339 mr.PoolId = pr.PoolId;
340 db_list_media_records(ua->db, &mr, prtit, ua);
343 bsendmsg(ua, _("Unknown list keyword: %s\n"), ua->argk[i]);
349 void do_messages(UAContext *ua, char *cmd)
354 fcntl(fileno(con_fd), F_SETLKW);
356 while (fgets(msg, sizeof(msg), con_fd)) {
358 ua->UA_sock->msg = (char *) check_pool_memory_size(
359 ua->UA_sock->msg, mlen+1);
360 strcpy(ua->UA_sock->msg, msg);
361 ua->UA_sock->msglen = mlen;
362 bnet_send(ua->UA_sock);
364 ftruncate(fileno(con_fd), 0L);
365 console_msg_pending = FALSE;
366 fcntl(fileno(con_fd), F_UNLCK);
367 ua->user_notified_msg_pending = FALSE;
371 int qmessagescmd(UAContext *ua, char *cmd)
373 if (console_msg_pending && ua->auto_display_messages) {
374 do_messages(ua, cmd);
379 int messagescmd(UAContext *ua, char *cmd)
381 if (console_msg_pending) {
382 do_messages(ua, cmd);
384 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));