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,
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;
44 extern brwlock_t con_lock;
47 /* Imported functions */
49 /* Forward referenced functions */
53 * Turn auto display of console messages on/off
55 int autodisplaycmd(UAContext *ua, char *cmd)
62 switch (find_arg_keyword(ua, kw)) {
64 ua->auto_display_messages = 1;
67 ua->auto_display_messages = 0;
70 bsendmsg(ua, _("ON or OFF keyword missing.\n"));
77 struct showstruct {char *res_name; int type;};
78 static struct showstruct reses[] = {
79 {N_("directors"), R_DIRECTOR},
80 {N_("clients"), R_CLIENT},
82 {N_("storages"), R_STORAGE},
83 {N_("catalogs"), R_CATALOG},
84 {N_("schedules"), R_SCHEDULE},
85 {N_("filesets"), R_FILESET},
86 {N_("groups"), R_GROUP},
87 {N_("pools"), R_POOL},
88 {N_("messages"), R_MSGS},
99 * show <resource-keyword-name> e.g. show directors
100 * show <resource-keyword-name>=<name> e.g. show director=HeadMan
103 int showcmd(UAContext *ua, char *cmd)
110 Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
113 for (i=1; i<ua->argc; i++) {
115 res_name = ua->argk[i];
116 if (!ua->argv[i]) { /* was a name given? */
117 /* No name, dump all resources of specified type */
119 len = strlen(res_name);
120 for (j=0; reses[j].res_name; j++) {
121 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
122 type = reses[j].type;
124 res = resources[type-r_first].res_head;
132 /* Dump a single resource with specified name */
134 len = strlen(res_name);
135 for (j=0; reses[j].res_name; j++) {
136 if (strncasecmp(res_name, _(reses[j].res_name), len) == 0) {
137 type = reses[j].type;
138 res = (RES *)GetResWithName(type, ua->argv[i]);
149 for (j=r_first; j<=r_last; j++) {
150 dump_resource(j, resources[j-r_first].res_head, bsendmsg, ua);
154 bsendmsg(ua, _("Keywords for the show command are:\n"));
155 for (j=0; reses[j].res_name; j++) {
156 bsendmsg(ua, "%s\n", _(reses[j].res_name));
160 bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
163 bsendmsg(ua, _("Resource %s not found\n"), res_name);
166 dump_resource(recurse?type:-type, res, bsendmsg, ua);
177 * List contents of database
179 * list jobs - lists all jobs run
180 * list jobid=nnn - list job data for jobid
181 * list job=name - list job data for job
182 * list jobmedia jobid=<nn>
183 * list jobmedia job=name
184 * list files jobid=<nn> - list files saved for job nn
185 * list files job=name
186 * list pools - list pool records
187 * list jobtotals - list totals for all jobs
188 * list media - list media for given pool
189 * list clients - list clients
192 int listcmd(UAContext *ua, char *cmd)
204 memset(&jr, 0, sizeof(jr));
205 memset(&pr, 0, sizeof(pr));
206 memset(&mr, 0, sizeof(mr));
208 Dmsg1(20, "list: %s\n", cmd);
211 bsendmsg(ua, _("Hey! DB is NULL\n"));
214 /* Scan arguments looking for things to do */
215 for (i=1; i<ua->argc; i++) {
217 if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
218 db_list_job_records(ua->db, &jr, prtit, ua);
221 } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
222 db_list_job_totals(ua->db, &jr, prtit, ua);
225 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
227 jobid = atoi(ua->argv[i]);
230 db_list_job_records(ua->db, &jr, prtit, ua);
235 } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
236 bstrncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
238 db_list_job_records(ua->db, &jr, prtit, ua);
241 } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
243 for (j=i+1; j<ua->argc; j++) {
244 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
245 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
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 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
266 db_get_job_record(ua->db, &jr);
268 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
269 jobid = atoi(ua->argv[j]);
273 db_list_jobmedia_records(ua->db, jobid, prtit, ua);
277 /* List for all jobs (jobid=0) */
278 db_list_jobmedia_records(ua->db, 0, prtit, ua);
282 } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
283 db_list_pool_records(ua->db, prtit, ua);
285 } else if (strcasecmp(ua->argk[i], _("clients")) == 0) {
286 db_list_client_records(ua->db, prtit, ua);
289 /* List MEDIA or VOLUMES */
290 } else if (strcasecmp(ua->argk[i], _("media")) == 0 ||
291 strcasecmp(ua->argk[i], _("volumes")) == 0) {
293 for (j=i+1; j<ua->argc; j++) {
294 if (strcasecmp(ua->argk[j], _("job")) == 0 && ua->argv[j]) {
295 bstrncpy(jr.Job, ua->argv[j], MAX_NAME_LENGTH);
297 db_get_job_record(ua->db, &jr);
299 } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
300 jobid = atoi(ua->argv[j]);
304 VolumeName = get_pool_memory(PM_FNAME);
305 n = db_get_job_volume_names(ua->db, jobid, &VolumeName);
306 bsendmsg(ua, _("Jobid %d used %d Volume(s): %s\n"), jobid, n, VolumeName);
307 free_pool_memory(VolumeName);
310 /* if no job or jobid keyword found, then we list all media */
312 if (!get_pool_dbr(ua, &pr)) {
315 mr.PoolId = pr.PoolId;
316 db_list_media_records(ua->db, &mr, prtit, ua);
319 bsendmsg(ua, _("Unknown list keyword: %s\n"), NPRT(ua->argk[i]));
325 static void con_lock_release(void *arg)
330 void do_messages(UAContext *ua, char *cmd)
334 int do_truncate = FALSE;
337 pthread_cleanup_push(con_lock_release, (void *)NULL);
339 while (fgets(msg, sizeof(msg), con_fd)) {
341 ua->UA_sock->msg = check_pool_memory_size(ua->UA_sock->msg, mlen+1);
342 strcpy(ua->UA_sock->msg, msg);
343 ua->UA_sock->msglen = mlen;
344 bnet_send(ua->UA_sock);
348 ftruncate(fileno(con_fd), 0L);
350 console_msg_pending = FALSE;
351 ua->user_notified_msg_pending = FALSE;
352 pthread_cleanup_pop(0);
357 int qmessagescmd(UAContext *ua, char *cmd)
359 if (console_msg_pending && ua->auto_display_messages) {
360 do_messages(ua, cmd);
365 int messagescmd(UAContext *ua, char *cmd)
367 if (console_msg_pending) {
368 do_messages(ua, cmd);
370 bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
376 * Callback routine for "printing" database file listing
378 void prtit(void *ctx, char *msg)
380 UAContext *ua = (UAContext *)ctx;
382 bnet_fsend(ua->UA_sock, "%s", msg);
386 * Format message and send to other end.
388 * If the UA_sock is NULL, it means that there is no user
389 * agent, so we are being called from Bacula core. In
390 * that case direct the messages to the Job.
392 void bsendmsg(void *ctx, char *fmt, ...)
395 UAContext *ua = (UAContext *)ctx;
396 BSOCK *bs = ua->UA_sock;
403 msg = get_pool_memory(PM_EMSG);
407 maxlen = sizeof_pool_memory(msg) - 1;
408 va_start(arg_ptr, fmt);
409 len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
411 if (len < 0 || len >= maxlen) {
412 msg = realloc_pool_memory(msg, maxlen + 200);
419 } else { /* No UA, send to Job */
420 Jmsg(ua->jcr, M_INFO, 0, msg);
421 free_pool_memory(msg);