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