]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_output.c
b79001982e9d941adf14c83b8e4156f3263521f0
[bacula/bacula] / bacula / src / dird / ua_output.c
1 /*
2  *
3  *   Bacula Director -- User Agent Output Commands
4  *     I.e. messages, listing database, showing resources, ...
5  *
6  *     Kern Sibbald, September MM
7  *
8  *   Version $Id$
9  */
10
11 /*
12    Copyright (C) 2000-2003 Kern Sibbald and John Walker
13
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.
18
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.
23
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,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33
34 /* Imported subroutines */
35
36 /* Imported variables */
37 extern int r_first;
38 extern int r_last;
39 extern struct s_res resources[];
40 extern int console_msg_pending;
41 extern FILE *con_fd;
42 extern brwlock_t con_lock;
43
44
45 /* Imported functions */
46
47 /* Forward referenced functions */
48 static int do_list_cmd(UAContext *ua, char *cmd, e_list_type llist);
49
50 /*
51  * Turn auto display of console messages on/off
52  */
53 int autodisplaycmd(UAContext *ua, char *cmd)
54 {
55    static char *kw[] = {
56       N_("on"), 
57       N_("off"),
58       NULL};
59
60    switch (find_arg_keyword(ua, kw)) {
61    case 0:
62       ua->auto_display_messages = 1;
63       break;
64    case 1:
65       ua->auto_display_messages = 0;
66       break;
67    default:
68       bsendmsg(ua, _("ON or OFF keyword missing.\n"));
69       break;
70    }
71    return 1; 
72 }
73
74
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},
80    {N_("jobs"),       R_JOB},
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},
88    {N_("all"),        -1},
89    {N_("help"),       -2},
90    {NULL,           0}
91 };
92
93
94 /*
95  *  Displays Resources
96  *
97  *  show all
98  *  show <resource-keyword-name>  e.g. show directors
99  *  show <resource-keyword-name>=<name> e.g. show director=HeadMan
100  *
101  */
102 int show_cmd(UAContext *ua, char *cmd)
103 {
104    int i, j, type, len; 
105    int recurse;
106    char *res_name;
107    RES *res = NULL;
108
109    Dmsg1(20, "show: %s\n", ua->UA_sock->msg);
110
111
112    for (i=1; i<ua->argc; i++) {
113       type = 0;
114       res_name = ua->argk[i]; 
115       if (!ua->argv[i]) {             /* was a name given? */
116          /* No name, dump all resources of specified type */
117          recurse = 1;
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;
122                if (type > 0) {
123                   res = resources[type-r_first].res_head;
124                } else {
125                   res = NULL;
126                }
127                break;
128             }
129          }
130       } else {
131          /* Dump a single resource with specified name */
132          recurse = 0;
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]);
138                if (!res) {
139                   type = -3;
140                }
141                break;
142             }
143          }
144       }
145
146       switch (type) {
147       case -1:                           /* all */
148          for (j=r_first; j<=r_last; j++) {
149             dump_resource(j, resources[j-r_first].res_head, bsendmsg, ua);     
150          }
151          break;
152       case -2:
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));
156          }
157          return 1;
158       case -3:
159          bsendmsg(ua, _("%s resource %s not found.\n"), res_name, ua->argv[i]);
160          return 1;
161       case 0:
162          bsendmsg(ua, _("Resource %s not found\n"), res_name);
163          return 1;
164       default:
165          dump_resource(recurse?type:-type, res, bsendmsg, ua);
166          break;
167       }
168    }
169    return 1;
170 }
171
172
173
174
175 /*
176  *  List contents of database
177  *
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.
192  *
193  */
194
195 /* Do long or full listing */
196 int llist_cmd(UAContext *ua, char *cmd)
197 {
198    return do_list_cmd(ua, cmd, VERT_LIST);
199 }
200
201 /* Do short or summary listing */
202 int list_cmd(UAContext *ua, char *cmd)
203 {
204    return do_list_cmd(ua, cmd, HORZ_LIST);
205 }
206
207 static int do_list_cmd(UAContext *ua, char *cmd, e_list_type llist)
208 {
209    POOLMEM *VolumeName;
210    int jobid, n;
211    int i, j;
212    JOB_DBR jr;
213    POOL_DBR pr;
214    MEDIA_DBR mr;
215
216    if (!open_db(ua))
217       return 1;
218
219    memset(&jr, 0, sizeof(jr));
220    memset(&pr, 0, sizeof(pr));
221    memset(&mr, 0, sizeof(mr));
222
223    Dmsg1(20, "list: %s\n", cmd);
224
225    if (!ua->db) {
226       bsendmsg(ua, _("Hey! DB is NULL\n"));
227    }
228
229    /* Scan arguments looking for things to do */
230    for (i=1; i<ua->argc; i++) {
231       /* List JOBS */
232       if (strcasecmp(ua->argk[i], _("jobs")) == 0) {
233          db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
234
235          /* List JOBTOTALS */
236       } else if (strcasecmp(ua->argk[i], _("jobtotals")) == 0) {
237          db_list_job_totals(ua->jcr, ua->db, &jr, prtit, ua);
238
239       /* List JOBID */
240       } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
241          if (ua->argv[i]) {
242             jobid = atoi(ua->argv[i]);
243             if (jobid > 0) {
244                jr.JobId = jobid;
245                db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
246             }
247          }
248
249       /* List JOB */
250       } else if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
251          bstrncpy(jr.Job, ua->argv[i], MAX_NAME_LENGTH);
252          jr.JobId = 0;
253          db_list_job_records(ua->jcr, ua->db, &jr, prtit, ua, llist);
254
255       /* List FILES */
256       } else if (strcasecmp(ua->argk[i], _("files")) == 0) {
257
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);
261                jr.JobId = 0;
262                db_get_job_record(ua->jcr, ua->db, &jr);
263                jobid = jr.JobId;
264             } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
265                jobid = atoi(ua->argv[j]);
266             } else {
267                continue;
268             }
269             if (jobid > 0) {
270                db_list_files_for_job(ua->jcr, ua->db, jobid, prtit, ua);
271             }
272          }
273       
274       /* List JOBMEDIA */
275       } else if (strcasecmp(ua->argk[i], _("jobmedia")) == 0) {
276          int done = FALSE;
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);
280                jr.JobId = 0;
281                db_get_job_record(ua->jcr, ua->db, &jr);
282                jobid = jr.JobId;
283             } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
284                jobid = atoi(ua->argv[j]);
285             } else {
286                continue;
287             }
288             db_list_jobmedia_records(ua->jcr, ua->db, jobid, prtit, ua, llist);
289             done = TRUE;
290          }
291          if (!done) {
292             /* List for all jobs (jobid=0) */
293             db_list_jobmedia_records(ua->jcr, ua->db, 0, prtit, ua, llist);
294          }
295
296       /* List POOLS */
297       } else if (strcasecmp(ua->argk[i], _("pools")) == 0) {
298          db_list_pool_records(ua->jcr, ua->db, prtit, ua, llist);
299
300       } else if (strcasecmp(ua->argk[i], _("clients")) == 0) {
301          db_list_client_records(ua->jcr, ua->db, prtit, ua, llist);
302
303
304       /* List MEDIA or VOLUMES */
305       } else if (strcasecmp(ua->argk[i], _("media")) == 0 ||
306                  strcasecmp(ua->argk[i], _("volumes")) == 0) {
307          int done = FALSE;
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);
311                jr.JobId = 0;
312                db_get_job_record(ua->jcr, ua->db, &jr);
313                jobid = jr.JobId;
314             } else if (strcasecmp(ua->argk[j], _("jobid")) == 0 && ua->argv[j]) {
315                jobid = atoi(ua->argv[j]);
316             } else {
317                continue;
318             }
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);
323             done = TRUE;
324          }
325          /* if no job or jobid keyword found, then we list all media */
326          if (!done) {
327             int num_pools;
328             uint32_t *ids;
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"));
334                      return 1;
335                   }
336                   mr.PoolId = pr.PoolId;
337                   db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
338                   return 1;
339                }
340             }
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));
345                return 1;
346             }
347             if (num_pools <= 0) {
348                return 1;
349             }
350             for (i=0; i < num_pools; i++) {
351                pr.PoolId = ids[i];
352                if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
353                   bsendmsg(ua, _("Pool: %s\n"), pr.Name);
354                }
355                mr.PoolId = ids[i];
356                db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
357             }
358             free(ids);
359             return 1;
360          }
361       /* List a specific volume */
362       } else if (strcasecmp(ua->argk[i], _("volume")) == 0) {
363          if (!ua->argv[i]) {
364             bsendmsg(ua, _("No Volume Name specified.\n"));
365             return 1;
366          }
367          bstrncpy(mr.VolumeName, ua->argv[i], sizeof(mr.VolumeName));
368          db_list_media_records(ua->jcr, ua->db, &mr, prtit, ua, llist);
369          return 1;
370       /* List next volume */
371       } else if (strcasecmp(ua->argk[i], _("nextvol")) == 0 || 
372                  strcasecmp(ua->argk[i], _("nextvolume")) == 0) {
373          JOB *job;
374          JCR *jcr = ua->jcr;
375          i = find_arg_with_value(ua, "job");
376          if (i <= 0) {
377             if ((job = select_job_resource(ua)) == NULL) {
378                return 1;
379             }
380          } else {
381             job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
382             if (!job) {
383                Jmsg(jcr, M_ERROR, 0, _("%s is not a job name.\n"), ua->argv[i]);
384                if ((job = select_job_resource(ua)) == NULL) {
385                   return 1;
386                }
387             }
388          }
389          if (!complete_jcr_for_job(jcr, job, NULL)) {
390             return 1;
391          }
392            
393          if (!find_next_volume_for_append(jcr, &mr, 0)) {
394             bsendmsg(ua, "Could not find next Volume\n");
395             db_close_database(jcr, jcr->db);
396             jcr->db = NULL;
397             return 1;
398          } else {
399             bsendmsg(ua, "The next Volume to be used by Job \"%s\" will be %s\n", 
400                job->hdr.name, mr.VolumeName);
401          }
402          db_close_database(jcr, jcr->db);
403          jcr->db = NULL;
404       } else {
405          bsendmsg(ua, _("Unknown list keyword: %s\n"), NPRT(ua->argk[i]));
406       }
407    }
408    return 1;
409 }
410
411 /* 
412  * Fill in the remaining fields of the jcr as if it
413  *  is going to run the job.
414  */
415 int complete_jcr_for_job(JCR *jcr, JOB *job, POOL *pool)
416 {
417    POOL_DBR pr;
418
419    memset(&pr, 0, sizeof(POOL_DBR));   
420    set_jcr_defaults(jcr, job);
421    if (pool) {
422       jcr->pool = pool;               /* override */
423    }
424    jcr->db = jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
425                       jcr->catalog->db_password, jcr->catalog->db_address,
426                       jcr->catalog->db_port, jcr->catalog->db_socket);
427    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
428       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
429                  jcr->catalog->db_name);
430       if (jcr->db) {
431          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
432       }
433       return 0;
434    }
435    bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
436    while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
437       /* Try to create the pool */
438       if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
439          Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name, 
440             db_strerror(jcr->db));
441          db_close_database(jcr, jcr->db);
442          jcr->db = NULL;
443          return 0;
444       } else {
445          Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
446       }
447    }
448    jcr->PoolId = pr.PoolId; 
449    jcr->jr.PoolId = pr.PoolId;
450    return 1;
451 }
452
453
454 static void con_lock_release(void *arg)
455 {
456    Vw(con_lock);
457 }
458
459 void do_messages(UAContext *ua, char *cmd)
460 {
461    char msg[2000];
462    int mlen; 
463    int do_truncate = FALSE;
464
465    Pw(con_lock);
466    pthread_cleanup_push(con_lock_release, (void *)NULL);
467    rewind(con_fd);
468    while (fgets(msg, sizeof(msg), con_fd)) {
469       mlen = strlen(msg);
470       ua->UA_sock->msg = check_pool_memory_size(ua->UA_sock->msg, mlen+1);
471       strcpy(ua->UA_sock->msg, msg);
472       ua->UA_sock->msglen = mlen;
473       bnet_send(ua->UA_sock);
474       do_truncate = TRUE;
475    }
476    if (do_truncate) {
477       ftruncate(fileno(con_fd), 0L);
478    }
479    console_msg_pending = FALSE;
480    ua->user_notified_msg_pending = FALSE;
481    pthread_cleanup_pop(0);
482    Vw(con_lock);
483 }
484
485
486 int qmessagescmd(UAContext *ua, char *cmd)
487 {
488    if (console_msg_pending && ua->auto_display_messages) {
489       do_messages(ua, cmd);
490    }
491    return 1;
492 }
493
494 int messagescmd(UAContext *ua, char *cmd)
495 {
496    if (console_msg_pending) {
497       do_messages(ua, cmd);
498    } else {
499       bnet_fsend(ua->UA_sock, _("You have no messages.\n"));
500    }
501    return 1;
502 }
503
504 /*
505  * Callback routine for "printing" database file listing
506  */
507 void prtit(void *ctx, char *msg)
508 {
509    UAContext *ua = (UAContext *)ctx;
510  
511    bnet_fsend(ua->UA_sock, "%s", msg);
512 }
513
514 /* 
515  * Format message and send to other end.  
516
517  * If the UA_sock is NULL, it means that there is no user
518  * agent, so we are being called from Bacula core. In
519  * that case direct the messages to the Job.
520  */
521 void bsendmsg(void *ctx, char *fmt, ...)
522 {
523    va_list arg_ptr;
524    UAContext *ua = (UAContext *)ctx;
525    BSOCK *bs = ua->UA_sock;
526    int maxlen, len;
527    POOLMEM *msg;
528
529    if (bs) {
530       msg = bs->msg;
531    } else {
532       msg = get_pool_memory(PM_EMSG);
533    }
534
535 again:
536    maxlen = sizeof_pool_memory(msg) - 1;
537    va_start(arg_ptr, fmt);
538    len = bvsnprintf(msg, maxlen, fmt, arg_ptr);
539    va_end(arg_ptr);
540    if (len < 0 || len >= maxlen) {
541       msg = realloc_pool_memory(msg, maxlen + 200);
542       goto again;
543    }
544
545    if (bs) {
546       bs->msglen = len;
547       bnet_send(bs);
548    } else {                           /* No UA, send to Job */
549       Jmsg(ua->jcr, M_INFO, 0, "%s", msg);
550       free_pool_memory(msg);
551    }
552
553 }