]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_status.c
71546d31c71150049eb572cf74b18127a5a6ea61
[bacula/bacula] / bacula / src / dird / ua_status.c
1 /*
2  *
3  *   Bacula Director -- User Agent Status Command
4  *
5  *     Kern Sibbald, August MMI
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 extern char my_name[];
34 extern time_t daemon_start_time;
35 extern int num_jobs_run;
36
37 static void list_scheduled_jobs(UAContext *ua);
38 static void list_running_jobs(UAContext *ua);
39 static void list_terminated_jobs(UAContext *ua);
40 static void do_storage_status(UAContext *ua, STORE *store);
41 static void do_client_status(UAContext *ua, CLIENT *client);
42 static void do_director_status(UAContext *ua);
43 static void do_all_status(UAContext *ua);
44
45 /*
46  * status command
47  */
48 int status_cmd(UAContext *ua, const char *cmd)
49 {
50    STORE *store;
51    CLIENT *client;
52    int item, i;
53
54    if (!open_db(ua)) {
55       return 1;
56    }
57    Dmsg1(20, "status:%s:\n", cmd);
58
59    for (i=1; i<ua->argc; i++) {
60       if (strcasecmp(ua->argk[i], _("all")) == 0) {
61          do_all_status(ua);
62          return 1;
63       } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
64                  strcasecmp(ua->argk[i], _("director")) == 0) {
65          do_director_status(ua);
66          return 1;
67       } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
68          client = get_client_resource(ua);
69          if (client) {
70             do_client_status(ua, client);
71          }
72          return 1;
73       } else {
74          store = get_storage_resource(ua, 0);
75          if (store) {
76             do_storage_status(ua, store);
77          }
78          return 1;
79       }
80    }
81    /* If no args, ask for status type */
82    if (ua->argc == 1) {                                    
83        char prmt[MAX_NAME_LENGTH];              
84        
85       start_prompt(ua, _("Status available for:\n"));
86       add_prompt(ua, _("Director"));
87       add_prompt(ua, _("Storage"));
88       add_prompt(ua, _("Client"));
89       add_prompt(ua, _("All"));
90       Dmsg0(20, "do_prompt: select daemon\n");
91       if ((item=do_prompt(ua, "",  _("Select daemon type for status"), prmt, sizeof(prmt))) < 0) {
92          return 1;
93       }
94       Dmsg1(20, "item=%d\n", item);
95       switch (item) { 
96       case 0:                         /* Director */
97          do_director_status(ua);
98          break;
99       case 1:
100          store = select_storage_resource(ua);
101          if (store) {
102             do_storage_status(ua, store);
103          }
104          break;
105       case 2:
106          client = select_client_resource(ua);
107          if (client) {
108             do_client_status(ua, client);
109          }
110          break;
111       case 3:
112          do_all_status(ua);
113          break;
114       default:
115          break;
116       }
117    }
118    return 1;
119 }
120
121 static void do_all_status(UAContext *ua)
122 {
123    STORE *store, **unique_store;
124    CLIENT *client, **unique_client;
125    int i, j;
126    bool found;
127
128    do_director_status(ua);
129
130    /* Count Storage items */
131    LockRes();
132    i = 0;
133    foreach_res(store, R_STORAGE) {
134       i++;
135    }
136    unique_store = (STORE **) malloc(i * sizeof(STORE));
137    /* Find Unique Storage address/port */         
138    i = 0;
139    foreach_res(store, R_STORAGE) {
140       found = false;
141       if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
142          continue;
143       }
144       for (j=0; j<i; j++) {
145          if (strcmp(unique_store[j]->address, store->address) == 0 &&
146              unique_store[j]->SDport == store->SDport) {
147             found = true;
148             break;
149          }
150       }
151       if (!found) {
152          unique_store[i++] = store;
153          Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
154       }
155    }
156    UnlockRes();
157
158    /* Call each unique Storage daemon */
159    for (j=0; j<i; j++) {
160       do_storage_status(ua, unique_store[j]);
161    }
162    free(unique_store);
163
164    /* Count Client items */
165    LockRes();
166    i = 0;
167    foreach_res(client, R_CLIENT) {
168       i++;
169    }
170    unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
171    /* Find Unique Client address/port */         
172    i = 0;
173    foreach_res(client, R_CLIENT) {
174       found = false;
175       if (!acl_access_ok(ua, Client_ACL, client->hdr.name)) {
176          continue;
177       }
178       for (j=0; j<i; j++) {
179          if (strcmp(unique_client[j]->address, client->address) == 0 &&
180              unique_client[j]->FDport == client->FDport) {
181             found = true;
182             break;
183          }
184       }
185       if (!found) {
186          unique_client[i++] = client;
187          Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
188       }
189    }
190    UnlockRes();
191
192    /* Call each unique File daemon */
193    for (j=0; j<i; j++) {
194       do_client_status(ua, unique_client[j]);
195    }
196    free(unique_client);
197    
198 }
199
200 static void do_director_status(UAContext *ua)
201 {
202    char dt[MAX_TIME_LENGTH];
203
204    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
205             HOST_OS, DISTNAME, DISTVER);
206    bstrftime_nc(dt, sizeof(dt), daemon_start_time);
207    bsendmsg(ua, _("Daemon started %s, %d Job%s run since started.\n"), 
208         dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
209    if (debug_level > 0) {
210       char b1[35], b2[35], b3[35], b4[35];
211       bsendmsg(ua, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
212             edit_uint64_with_commas(sm_bytes, b1),
213             edit_uint64_with_commas(sm_max_bytes, b2),
214             edit_uint64_with_commas(sm_buffers, b3),
215             edit_uint64_with_commas(sm_max_buffers, b4));
216    }
217    /*
218     * List scheduled Jobs
219     */
220    list_scheduled_jobs(ua);
221
222    /* 
223     * List running jobs
224     */
225    list_running_jobs(ua);
226
227    /* 
228     * List terminated jobs
229     */
230    list_terminated_jobs(ua);
231    bsendmsg(ua, "====\n");
232 }
233
234 static void do_storage_status(UAContext *ua, STORE *store)
235 {
236    BSOCK *sd;
237
238    ua->jcr->store = store;
239    /* Try connecting for up to 15 seconds */
240    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
241       store->hdr.name, store->address, store->SDport);
242    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
243       bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
244          store->hdr.name);
245       if (ua->jcr->store_bsock) {
246          bnet_close(ua->jcr->store_bsock);
247          ua->jcr->store_bsock = NULL;
248       }         
249       return;
250    }
251    Dmsg0(20, _("Connected to storage daemon\n"));
252    sd = ua->jcr->store_bsock;
253    bnet_fsend(sd, "status");
254    while (bnet_recv(sd) >= 0) {
255       bsendmsg(ua, "%s", sd->msg);
256    }
257    bnet_sig(sd, BNET_TERMINATE);
258    bnet_close(sd);
259    ua->jcr->store_bsock = NULL;
260    return;  
261 }
262    
263 static void do_client_status(UAContext *ua, CLIENT *client)
264 {
265    BSOCK *fd;
266
267    /* Connect to File daemon */
268
269    ua->jcr->client = client;
270    /* Release any old dummy key */
271    if (ua->jcr->sd_auth_key) {
272       free(ua->jcr->sd_auth_key);
273    }
274    /* Create a new dummy SD auth key */
275    ua->jcr->sd_auth_key = bstrdup("dummy");
276
277    /* Try to connect for 15 seconds */
278    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
279       client->hdr.name, client->address, client->FDport);
280    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
281       bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
282          client->hdr.name);
283       if (ua->jcr->file_bsock) {
284          bnet_close(ua->jcr->file_bsock);
285          ua->jcr->file_bsock = NULL;
286       }         
287       return;
288    }
289    Dmsg0(20, _("Connected to file daemon\n"));
290    fd = ua->jcr->file_bsock;
291    bnet_fsend(fd, "status");
292    while (bnet_recv(fd) >= 0) {
293       bsendmsg(ua, "%s", fd->msg);
294    }
295    bnet_sig(fd, BNET_TERMINATE);
296    bnet_close(fd);
297    ua->jcr->file_bsock = NULL;
298
299    return;  
300 }
301
302 static void prt_runhdr(UAContext *ua)
303 {
304    bsendmsg(ua, _("\nScheduled Jobs:\n"));
305    bsendmsg(ua, _("Level          Type     Pri  Scheduled          Name               Volume\n"));
306    bsendmsg(ua, _("===================================================================================\n"));
307 }
308
309 /* Scheduling packet */
310 struct sched_pkt {
311    dlink link;                        /* keep this as first item!!! */
312    JOB *job;
313    int level;
314    int priority;
315    time_t runtime;
316    POOL *pool;
317 };
318
319 static void prt_runtime(UAContext *ua, sched_pkt *sp)
320 {
321    char dt[MAX_TIME_LENGTH];       
322    const char *level_ptr;
323    bool ok = false;
324    bool close_db = false;
325    JCR *jcr = ua->jcr;
326    MEDIA_DBR mr;
327    memset(&mr, 0, sizeof(mr));
328    if (sp->job->JobType == JT_BACKUP) {
329       jcr->db = NULL;
330       ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
331       if (jcr->db) {
332          close_db = true;             /* new db opened, remember to close it */
333       }
334       if (ok) {
335          ok = find_next_volume_for_append(jcr, &mr, 0);
336       }
337       if (!ok) {
338          bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
339       }
340    }
341    bstrftime_nc(dt, sizeof(dt), sp->runtime);
342    switch (sp->job->JobType) {
343    case JT_ADMIN:
344    case JT_RESTORE:
345       level_ptr = " ";
346       break;
347    default:
348       level_ptr = level_to_str(sp->level);
349       break;
350    }
351    bsendmsg(ua, _("%-14s %-8s %3d  %-18s %-18s %s\n"), 
352       level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt, 
353       sp->job->hdr.name, mr.VolumeName);
354    if (close_db) {
355       db_close_database(jcr, jcr->db);
356    }
357    jcr->db = ua->db;                  /* restore ua db to jcr */
358
359 }
360
361 /*
362  * Sort items by runtime, priority
363  */
364 static int my_compare(void *item1, void *item2)
365 {
366    sched_pkt *p1 = (sched_pkt *)item1;
367    sched_pkt *p2 = (sched_pkt *)item2;
368    if (p1->runtime < p2->runtime) {
369       return -1;
370    } else if (p1->runtime > p2->runtime) {
371       return 1;
372    }     
373    if (p1->priority < p2->priority) {
374       return -1;
375    } else if (p1->priority > p2->priority) {
376       return 1;
377    }
378    return 0;
379 }
380
381 /*          
382  * Find all jobs to be run in roughly the
383  *  next 24 hours.
384  */
385 static void list_scheduled_jobs(UAContext *ua)
386 {
387    time_t runtime;
388    RUN *run;
389    JOB *job;
390    int level, num_jobs = 0;
391    int priority;
392    bool hdr_printed = false;
393    dlist sched;
394    sched_pkt *sp, *ip;
395
396    Dmsg0(200, "enter list_sched_jobs()\n");
397
398    /* Loop through all jobs */
399    LockRes();
400    foreach_res(job, R_JOB) {
401       if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
402          continue;
403       }
404       for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
405          level = job->level;   
406          if (run->level) {
407             level = run->level;
408          }
409          priority = job->Priority;   
410          if (run->Priority) {
411             priority = run->Priority;
412          }
413          if (!hdr_printed) {
414             prt_runhdr(ua);
415             hdr_printed = true;
416          }
417          sp = (sched_pkt *)malloc(sizeof(sched_pkt));
418          sp->job = job;
419          sp->level = level;
420          sp->priority = priority;
421          sp->runtime = runtime;
422          sp->pool = run->pool;
423          ip = (sched_pkt *)sched.binary_insert(sp, my_compare);
424          if (ip != sp) {
425             /* Identical entry already, we must explictly insert it */
426             sched.insert_after(sp, ip);
427          }
428          num_jobs++;
429       }
430    } /* end for loop over resources */
431    UnlockRes();
432    foreach_dlist(sp, &sched) {
433       prt_runtime(ua, sp);
434    }
435    if (num_jobs == 0) {
436       bsendmsg(ua, _("No Scheduled Jobs.\n"));
437    } 
438    bsendmsg(ua, "====\n");
439    Dmsg0(200, "Leave list_sched_jobs_runs()\n");
440 }
441
442 static void list_running_jobs(UAContext *ua)
443 {
444    JCR *jcr;
445    int njobs = 0;
446    const char *msg;
447    char *emsg;                        /* edited message */
448    char dt[MAX_TIME_LENGTH];
449    char level[10];
450    bool pool_mem = false;
451
452    Dmsg0(200, "enter list_run_jobs()\n");
453    bsendmsg(ua, _("\nRunning Jobs:\n"));
454    lock_jcr_chain();
455    foreach_jcr(jcr) {
456       njobs++;
457       if (jcr->JobId == 0) {      /* this is us */
458          /* this is a console or other control job. We only show console
459           * jobs in the status output.
460           */
461          if (jcr->JobType == JT_CONSOLE) {
462             bstrftime_nc(dt, sizeof(dt), jcr->start_time);
463             bsendmsg(ua, _("Console connected at %s\n"), dt);
464          }
465          njobs--;
466       }
467       free_locked_jcr(jcr);
468    }
469    if (njobs == 0) {
470       unlock_jcr_chain();
471       /* Note the following message is used in regress -- don't change */
472       bsendmsg(ua, _("No Jobs running.\n====\n"));
473       Dmsg0(200, "leave list_run_jobs()\n");
474       return;
475    }
476    njobs = 0;
477    bsendmsg(ua, _(" JobId Level   Name                       Status\n"));
478    bsendmsg(ua, _("======================================================================\n"));
479    foreach_jcr(jcr) {
480       if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
481          free_locked_jcr(jcr);
482          continue;
483       }
484       njobs++;
485       switch (jcr->JobStatus) {
486       case JS_Created:
487          msg = _("is waiting execution");
488          break;
489       case JS_Running:
490          msg = _("is running");
491          break;
492       case JS_Blocked:
493          msg = _("is blocked");
494          break;
495       case JS_Terminated:
496          msg = _("has terminated");
497          break;
498       case JS_ErrorTerminated:
499          msg = _("has erred");
500          break;
501       case JS_Error:
502          msg = _("has errors");
503          break;
504       case JS_FatalError:
505          msg = _("has a fatal error");
506          break;
507       case JS_Differences:
508          msg = _("has verify differences");
509          break;
510       case JS_Canceled:
511          msg = _("has been canceled");
512          break;
513       case JS_WaitFD:
514          emsg = (char *) get_pool_memory(PM_FNAME);
515          Mmsg(&emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
516          pool_mem = true;
517          msg = emsg;
518          break;
519       case JS_WaitSD:
520          emsg = (char *) get_pool_memory(PM_FNAME);
521          Mmsg(&emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
522          pool_mem = true;
523          msg = emsg;
524          break;
525       case JS_WaitStoreRes:
526          msg = _("is waiting on max Storage jobs");
527          break;
528       case JS_WaitClientRes:
529          msg = _("is waiting on max Client jobs");
530          break;
531       case JS_WaitJobRes:
532          msg = _("is waiting on max Job jobs");
533          break;
534       case JS_WaitMaxJobs:
535          msg = _("is waiting on max total jobs");
536          break;
537       case JS_WaitStartTime:
538          msg = _("is waiting for its start time");
539          break;
540       case JS_WaitPriority:
541          msg = _("is waiting for higher priority jobs to finish");
542          break;
543
544       default:
545          emsg = (char *) get_pool_memory(PM_FNAME);
546          Mmsg(&emsg, _("is in unknown state %c"), jcr->JobStatus);
547          pool_mem = true;
548          msg = emsg;
549          break;
550       }
551       /* 
552        * Now report Storage daemon status code 
553        */
554       switch (jcr->SDJobStatus) {
555       case JS_WaitMount:
556          if (pool_mem) {
557             free_pool_memory(emsg);
558             pool_mem = false;
559          }
560          msg = _("is waiting for a mount request");
561          break;
562       case JS_WaitMedia:
563          if (pool_mem) {
564             free_pool_memory(emsg);
565             pool_mem = false;
566          }
567          msg = _("is waiting for an appendable Volume");
568          break;
569       case JS_WaitFD:
570          if (!pool_mem) {
571             emsg = (char *) get_pool_memory(PM_FNAME);
572             pool_mem = true;
573          }
574          Mmsg(&emsg, _("is waiting for Client %s to connect to Storage %s"),
575               jcr->client->hdr.name, jcr->store->hdr.name);
576          msg = emsg;
577          break;
578       }
579       switch (jcr->JobType) {
580       case JT_ADMIN:
581       case JT_RESTORE:
582          bstrncpy(level, "      ", sizeof(level));
583          break;
584       default:
585          bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
586          level[7] = 0;
587          break;
588       }
589
590       bsendmsg(ua, _("%6d %-6s  %-20s %s\n"), 
591          jcr->JobId,
592          level, 
593          jcr->Job,
594          msg);
595
596       if (pool_mem) {
597          free_pool_memory(emsg);
598          pool_mem = false;
599       }
600       free_locked_jcr(jcr);
601    }
602    unlock_jcr_chain();
603    bsendmsg(ua, "====\n");
604    Dmsg0(200, "leave list_run_jobs()\n");
605 }
606
607 static void list_terminated_jobs(UAContext *ua)
608 {
609    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
610    char level[10];
611
612    if (last_jobs->empty()) {
613       bsendmsg(ua, _("No Terminated Jobs.\n"));
614       return;
615    }
616    lock_last_jobs_list();
617    struct s_last_job *je;
618    bsendmsg(ua, _("\nTerminated Jobs:\n"));
619    bsendmsg(ua, _(" JobId  Level     Files      Bytes     Status   Finished        Name \n"));
620    bsendmsg(ua, _("========================================================================\n"));
621    foreach_dlist(je, last_jobs) {
622       char JobName[MAX_NAME_LENGTH];
623       const char *termstat;
624
625       bstrftime_nc(dt, sizeof(dt), je->end_time);
626       switch (je->JobType) {
627       case JT_ADMIN:
628       case JT_RESTORE:
629          bstrncpy(level, "    ", sizeof(level));
630          break;
631       default:
632          bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
633          level[4] = 0;
634          break;
635       }
636       switch (je->JobStatus) {
637       case JS_Created:
638          termstat = "Created";
639          break;
640       case JS_FatalError:
641       case JS_ErrorTerminated:
642          termstat = "Error";
643          break;
644       case JS_Differences:
645          termstat = "Diffs";
646          break;
647       case JS_Canceled:
648          termstat = "Cancel";
649          break;
650       case JS_Terminated:
651          termstat = "OK";
652          break;
653       default:
654          termstat = "Other";
655          break;
656       }
657       bstrncpy(JobName, je->Job, sizeof(JobName));
658       /* There are three periods after the Job name */
659       char *p;
660       for (int i=0; i<3; i++) {
661          if ((p=strrchr(JobName, '.')) != NULL) {
662             *p = 0;
663          }
664       }
665       bsendmsg(ua, _("%6d  %-6s %8s %14s %-7s  %-8s %s\n"), 
666          je->JobId,
667          level, 
668          edit_uint64_with_commas(je->JobFiles, b1),
669          edit_uint64_with_commas(je->JobBytes, b2), 
670          termstat,
671          dt, JobName);
672    }
673    bsendmsg(ua, "\n");
674    unlock_last_jobs_list();
675 }