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