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