]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_status.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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 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    if (sp->job->JobType == JT_BACKUP) {
376       jcr->db = NULL;
377       ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
378       if (jcr->db) {
379          close_db = true;             /* new db opened, remember to close it */
380       }
381       if (ok) {
382          ok = find_next_volume_for_append(jcr, &mr, 0);
383       }
384       if (!ok) {
385          bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
386       }
387    }
388    bstrftime_nc(dt, sizeof(dt), sp->runtime);
389    switch (sp->job->JobType) {
390    case JT_ADMIN:
391    case JT_RESTORE:
392       level_ptr = " ";
393       break;
394    default:
395       level_ptr = level_to_str(sp->level);
396       break;
397    }
398    bsendmsg(ua, _("%-14s %-8s %3d  %-18s %-18s %s\n"),
399       level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt,
400       sp->job->hdr.name, mr.VolumeName);
401    if (close_db) {
402       db_close_database(jcr, jcr->db);
403    }
404    jcr->db = ua->db;                  /* restore ua db to jcr */
405
406 }
407
408 /*
409  * Sort items by runtime, priority
410  */
411 static int my_compare(void *item1, void *item2)
412 {
413    sched_pkt *p1 = (sched_pkt *)item1;
414    sched_pkt *p2 = (sched_pkt *)item2;
415    if (p1->runtime < p2->runtime) {
416       return -1;
417    } else if (p1->runtime > p2->runtime) {
418       return 1;
419    }
420    if (p1->priority < p2->priority) {
421       return -1;
422    } else if (p1->priority > p2->priority) {
423       return 1;
424    }
425    return 0;
426 }
427
428 /*
429  * Find all jobs to be run in roughly the
430  *  next 24 hours.
431  */
432 static void list_scheduled_jobs(UAContext *ua)
433 {
434    time_t runtime;
435    RUN *run;
436    JOB *job;
437    int level, num_jobs = 0;
438    int priority;
439    bool hdr_printed = false;
440    dlist sched;
441    sched_pkt *sp;
442
443    Dmsg0(200, "enter list_sched_jobs()\n");
444
445    /* Loop through all jobs */
446    LockRes();
447    foreach_res(job, R_JOB) {
448       if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
449          continue;
450       }
451       for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
452          level = job->JobLevel;
453          if (run->level) {
454             level = run->level;
455          }
456          priority = job->Priority;
457          if (run->Priority) {
458             priority = run->Priority;
459          }
460          if (!hdr_printed) {
461             prt_runhdr(ua);
462             hdr_printed = true;
463          }
464          sp = (sched_pkt *)malloc(sizeof(sched_pkt));
465          sp->job = job;
466          sp->level = level;
467          sp->priority = priority;
468          sp->runtime = runtime;
469          sp->pool = run->pool;
470          sched.binary_insert(sp, my_compare);
471          num_jobs++;
472       }
473    } /* end for loop over resources */
474    UnlockRes();
475    foreach_dlist(sp, &sched) {
476       prt_runtime(ua, sp);
477    }
478    if (num_jobs == 0) {
479       bsendmsg(ua, _("No Scheduled Jobs.\n"));
480    }
481    bsendmsg(ua, "====\n");
482    Dmsg0(200, "Leave list_sched_jobs_runs()\n");
483 }
484
485 static void list_running_jobs(UAContext *ua)
486 {
487    JCR *jcr;
488    int njobs = 0;
489    const char *msg;
490    char *emsg;                        /* edited message */
491    char dt[MAX_TIME_LENGTH];
492    char level[10];
493    bool pool_mem = false;
494
495    Dmsg0(200, "enter list_run_jobs()\n");
496    bsendmsg(ua, _("\nRunning Jobs:\n"));
497    lock_jcr_chain();
498    foreach_jcr(jcr) {
499       njobs++;
500       if (jcr->JobId == 0) {      /* this is us */
501          /* this is a console or other control job. We only show console
502           * jobs in the status output.
503           */
504          if (jcr->JobType == JT_CONSOLE) {
505             bstrftime_nc(dt, sizeof(dt), jcr->start_time);
506             bsendmsg(ua, _("Console connected at %s\n"), dt);
507          }
508          njobs--;
509       }
510       free_locked_jcr(jcr);
511    }
512    if (njobs == 0) {
513       unlock_jcr_chain();
514       /* Note the following message is used in regress -- don't change */
515       bsendmsg(ua, _("No Jobs running.\n====\n"));
516       Dmsg0(200, "leave list_run_jobs()\n");
517       return;
518    }
519    njobs = 0;
520    bsendmsg(ua, _(" JobId Level   Name                       Status\n"));
521    bsendmsg(ua, _("======================================================================\n"));
522    foreach_jcr(jcr) {
523       if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
524          free_locked_jcr(jcr);
525          continue;
526       }
527       njobs++;
528       switch (jcr->JobStatus) {
529       case JS_Created:
530          msg = _("is waiting execution");
531          break;
532       case JS_Running:
533          msg = _("is running");
534          break;
535       case JS_Blocked:
536          msg = _("is blocked");
537          break;
538       case JS_Terminated:
539          msg = _("has terminated");
540          break;
541       case JS_ErrorTerminated:
542          msg = _("has erred");
543          break;
544       case JS_Error:
545          msg = _("has errors");
546          break;
547       case JS_FatalError:
548          msg = _("has a fatal error");
549          break;
550       case JS_Differences:
551          msg = _("has verify differences");
552          break;
553       case JS_Canceled:
554          msg = _("has been canceled");
555          break;
556       case JS_WaitFD:
557          emsg = (char *) get_pool_memory(PM_FNAME);
558          Mmsg(emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
559          pool_mem = true;
560          msg = emsg;
561          break;
562       case JS_WaitSD:
563          emsg = (char *) get_pool_memory(PM_FNAME);
564          Mmsg(emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
565          pool_mem = true;
566          msg = emsg;
567          break;
568       case JS_WaitStoreRes:
569          msg = _("is waiting on max Storage jobs");
570          break;
571       case JS_WaitClientRes:
572          msg = _("is waiting on max Client jobs");
573          break;
574       case JS_WaitJobRes:
575          msg = _("is waiting on max Job jobs");
576          break;
577       case JS_WaitMaxJobs:
578          msg = _("is waiting on max total jobs");
579          break;
580       case JS_WaitStartTime:
581          msg = _("is waiting for its start time");
582          break;
583       case JS_WaitPriority:
584          msg = _("is waiting for higher priority jobs to finish");
585          break;
586
587       default:
588          emsg = (char *) get_pool_memory(PM_FNAME);
589          Mmsg(emsg, _("is in unknown state %c"), jcr->JobStatus);
590          pool_mem = true;
591          msg = emsg;
592          break;
593       }
594       /*
595        * Now report Storage daemon status code
596        */
597       switch (jcr->SDJobStatus) {
598       case JS_WaitMount:
599          if (pool_mem) {
600             free_pool_memory(emsg);
601             pool_mem = false;
602          }
603          msg = _("is waiting for a mount request");
604          break;
605       case JS_WaitMedia:
606          if (pool_mem) {
607             free_pool_memory(emsg);
608             pool_mem = false;
609          }
610          msg = _("is waiting for an appendable Volume");
611          break;
612       case JS_WaitFD:
613          if (!pool_mem) {
614             emsg = (char *) get_pool_memory(PM_FNAME);
615             pool_mem = true;
616          }
617          Mmsg(emsg, _("is waiting for Client %s to connect to Storage %s"),
618               jcr->client->hdr.name, jcr->store->hdr.name);
619          msg = emsg;
620          break;
621       }
622       switch (jcr->JobType) {
623       case JT_ADMIN:
624       case JT_RESTORE:
625          bstrncpy(level, "      ", sizeof(level));
626          break;
627       default:
628          bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
629          level[7] = 0;
630          break;
631       }
632
633       bsendmsg(ua, _("%6d %-6s  %-20s %s\n"),
634          jcr->JobId,
635          level,
636          jcr->Job,
637          msg);
638
639       if (pool_mem) {
640          free_pool_memory(emsg);
641          pool_mem = false;
642       }
643       free_locked_jcr(jcr);
644    }
645    unlock_jcr_chain();
646    bsendmsg(ua, "====\n");
647    Dmsg0(200, "leave list_run_jobs()\n");
648 }
649
650 static void list_terminated_jobs(UAContext *ua)
651 {
652    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
653    char level[10];
654
655    if (last_jobs->empty()) {
656       bsendmsg(ua, _("No Terminated Jobs.\n"));
657       return;
658    }
659    lock_last_jobs_list();
660    struct s_last_job *je;
661    bsendmsg(ua, _("\nTerminated Jobs:\n"));
662    bsendmsg(ua, _(" JobId  Level     Files      Bytes     Status   Finished        Name \n"));
663    bsendmsg(ua, _("========================================================================\n"));
664    foreach_dlist(je, last_jobs) {
665       char JobName[MAX_NAME_LENGTH];
666       const char *termstat;
667
668       bstrftime_nc(dt, sizeof(dt), je->end_time);
669       switch (je->JobType) {
670       case JT_ADMIN:
671       case JT_RESTORE:
672          bstrncpy(level, "    ", sizeof(level));
673          break;
674       default:
675          bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
676          level[4] = 0;
677          break;
678       }
679       switch (je->JobStatus) {
680       case JS_Created:
681          termstat = "Created";
682          break;
683       case JS_FatalError:
684       case JS_ErrorTerminated:
685          termstat = "Error";
686          break;
687       case JS_Differences:
688          termstat = "Diffs";
689          break;
690       case JS_Canceled:
691          termstat = "Cancel";
692          break;
693       case JS_Terminated:
694          termstat = "OK";
695          break;
696       default:
697          termstat = "Other";
698          break;
699       }
700       bstrncpy(JobName, je->Job, sizeof(JobName));
701       /* There are three periods after the Job name */
702       char *p;
703       for (int i=0; i<3; i++) {
704          if ((p=strrchr(JobName, '.')) != NULL) {
705             *p = 0;
706          }
707       }
708       bsendmsg(ua, _("%6d  %-6s %8s %14s %-7s  %-8s %s\n"),
709          je->JobId,
710          level,
711          edit_uint64_with_commas(je->JobFiles, b1),
712          edit_uint64_with_commas(je->JobBytes, b2),
713          termstat,
714          dt, JobName);
715    }
716    bsendmsg(ua, "\n");
717    unlock_last_jobs_list();
718 }