]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_status.c
Sort Scheduled Jobs list
[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    /*
210     * List scheduled Jobs
211     */
212    list_scheduled_jobs(ua);
213
214    /* 
215     * List running jobs
216     */
217    list_running_jobs(ua);
218
219    /* 
220     * List terminated jobs
221     */
222    list_terminated_jobs(ua);
223    bsendmsg(ua, "====\n");
224 }
225
226 static void do_storage_status(UAContext *ua, STORE *store)
227 {
228    BSOCK *sd;
229
230    ua->jcr->store = store;
231    /* Try connecting for up to 15 seconds */
232    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
233       store->hdr.name, store->address, store->SDport);
234    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
235       bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
236          store->hdr.name);
237       if (ua->jcr->store_bsock) {
238          bnet_close(ua->jcr->store_bsock);
239          ua->jcr->store_bsock = NULL;
240       }         
241       return;
242    }
243    Dmsg0(20, _("Connected to storage daemon\n"));
244    sd = ua->jcr->store_bsock;
245    bnet_fsend(sd, "status");
246    while (bnet_recv(sd) >= 0) {
247       bsendmsg(ua, "%s", sd->msg);
248    }
249    bnet_sig(sd, BNET_TERMINATE);
250    bnet_close(sd);
251    ua->jcr->store_bsock = NULL;
252    return;  
253 }
254    
255 static void do_client_status(UAContext *ua, CLIENT *client)
256 {
257    BSOCK *fd;
258
259    /* Connect to File daemon */
260
261    ua->jcr->client = client;
262    /* Release any old dummy key */
263    if (ua->jcr->sd_auth_key) {
264       free(ua->jcr->sd_auth_key);
265    }
266    /* Create a new dummy SD auth key */
267    ua->jcr->sd_auth_key = bstrdup("dummy");
268
269    /* Try to connect for 15 seconds */
270    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
271       client->hdr.name, client->address, client->FDport);
272    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
273       bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
274          client->hdr.name);
275       if (ua->jcr->file_bsock) {
276          bnet_close(ua->jcr->file_bsock);
277          ua->jcr->file_bsock = NULL;
278       }         
279       return;
280    }
281    Dmsg0(20, _("Connected to file daemon\n"));
282    fd = ua->jcr->file_bsock;
283    bnet_fsend(fd, "status");
284    while (bnet_recv(fd) >= 0) {
285       bsendmsg(ua, "%s", fd->msg);
286    }
287    bnet_sig(fd, BNET_TERMINATE);
288    bnet_close(fd);
289    ua->jcr->file_bsock = NULL;
290
291    return;  
292 }
293
294 static void prt_runhdr(UAContext *ua)
295 {
296    bsendmsg(ua, _("\nScheduled Jobs:\n"));
297    bsendmsg(ua, _("Level          Type     Pri  Scheduled          Name               Volume\n"));
298    bsendmsg(ua, _("===================================================================================\n"));
299 }
300
301 /* Scheduling packet */
302 struct sched_pkt {
303    dlink link;                        /* keep this as first item!!! */
304    JOB *job;
305    int level;
306    int priority;
307    time_t runtime;
308    POOL *pool;
309 };
310
311 static void prt_runtime(UAContext *ua, sched_pkt *sp)
312 {
313    char dt[MAX_TIME_LENGTH];       
314    const char *level_ptr;
315    bool ok = false;
316    bool close_db = false;
317    JCR *jcr = ua->jcr;
318    MEDIA_DBR mr;
319    memset(&mr, 0, sizeof(mr));
320    if (sp->job->JobType == JT_BACKUP) {
321       jcr->db = NULL;
322       ok = complete_jcr_for_job(jcr, sp->job, sp->pool);
323       if (jcr->db) {
324          close_db = true;             /* new db opened, remember to close it */
325       }
326       if (ok) {
327          ok = find_next_volume_for_append(jcr, &mr, 0);
328       }
329       if (!ok) {
330          bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
331       }
332    }
333    bstrftime_nc(dt, sizeof(dt), sp->runtime);
334    switch (sp->job->JobType) {
335    case JT_ADMIN:
336    case JT_RESTORE:
337       level_ptr = " ";
338       break;
339    default:
340       level_ptr = level_to_str(sp->level);
341       break;
342    }
343    bsendmsg(ua, _("%-14s %-8s %3d  %-18s %-18s %s\n"), 
344       level_ptr, job_type_to_str(sp->job->JobType), sp->priority, dt, 
345       sp->job->hdr.name, mr.VolumeName);
346    if (close_db) {
347       db_close_database(jcr, jcr->db);
348    }
349    jcr->db = ua->db;                  /* restore ua db to jcr */
350
351 }
352
353 /*
354  * Sort items by runtime, priority
355  */
356 static int my_compare(void *item1, void *item2)
357 {
358    sched_pkt *p1 = (sched_pkt *)item1;
359    sched_pkt *p2 = (sched_pkt *)item2;
360    if (p1->runtime < p2->runtime) {
361       return -1;
362    } else if (p1->runtime > p2->runtime) {
363       return 1;
364    }     
365    if (p1->priority < p2->priority) {
366       return -1;
367    } else if (p1->priority > p2->priority) {
368       return 1;
369    }
370    return 0;
371 }
372
373 /*          
374  * Find all jobs to be run in roughly the
375  *  next 24 hours.
376  */
377 static void list_scheduled_jobs(UAContext *ua)
378 {
379    time_t runtime;
380    RUN *run;
381    JOB *job;
382    int level, num_jobs = 0;
383    int priority;
384    bool hdr_printed = false;
385    dlist sched;
386    sched_pkt *sp;
387
388    Dmsg0(200, "enter list_sched_jobs()\n");
389
390    /* Loop through all jobs */
391    LockRes();
392    foreach_res(job, R_JOB) {
393       if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
394          continue;
395       }
396       for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
397          level = job->level;   
398          if (run->level) {
399             level = run->level;
400          }
401          priority = job->Priority;   
402          if (run->Priority) {
403             priority = run->Priority;
404          }
405          if (!hdr_printed) {
406             prt_runhdr(ua);
407             hdr_printed = true;
408          }
409          sp = (sched_pkt *)malloc(sizeof(sched_pkt));
410          sp->job = job;
411          sp->level = level;
412          sp->priority = priority;
413          sp->runtime = runtime;
414          sp->pool = run->pool;
415          sched.binary_insert(sp, my_compare);
416          num_jobs++;
417       }
418    } /* end for loop over resources */
419    UnlockRes();
420    foreach_dlist(sp, &sched) {
421       prt_runtime(ua, sp);
422    }
423    if (num_jobs == 0) {
424       bsendmsg(ua, _("No Scheduled Jobs.\n"));
425    } 
426    bsendmsg(ua, "====\n");
427    Dmsg0(200, "Leave list_sched_jobs_runs()\n");
428 }
429
430 static void list_running_jobs(UAContext *ua)
431 {
432    JCR *jcr;
433    int njobs = 0;
434    const char *msg;
435    char *emsg;                        /* edited message */
436    char dt[MAX_TIME_LENGTH];
437    char level[10];
438    bool pool_mem = false;
439
440    Dmsg0(200, "enter list_run_jobs()\n");
441    bsendmsg(ua, _("\nRunning Jobs:\n"));
442    lock_jcr_chain();
443    foreach_jcr(jcr) {
444       njobs++;
445       if (jcr->JobId == 0) {      /* this is us */
446          /* this is a console or other control job. We only show console
447           * jobs in the status output.
448           */
449          if (jcr->JobType == JT_CONSOLE) {
450             bstrftime_nc(dt, sizeof(dt), jcr->start_time);
451             bsendmsg(ua, _("Console connected at %s\n"), dt);
452          }
453          njobs--;
454       }
455       free_locked_jcr(jcr);
456    }
457    if (njobs == 0) {
458       unlock_jcr_chain();
459       /* Note the following message is used in regress -- don't change */
460       bsendmsg(ua, _("No Jobs running.\n====\n"));
461       Dmsg0(200, "leave list_run_jobs()\n");
462       return;
463    }
464    njobs = 0;
465    bsendmsg(ua, _(" JobId Level   Name                       Status\n"));
466    bsendmsg(ua, _("======================================================================\n"));
467    foreach_jcr(jcr) {
468       if (jcr->JobId == 0 || !acl_access_ok(ua, Job_ACL, jcr->job->hdr.name)) {
469          free_locked_jcr(jcr);
470          continue;
471       }
472       njobs++;
473       switch (jcr->JobStatus) {
474       case JS_Created:
475          msg = _("is waiting execution");
476          break;
477       case JS_Running:
478          msg = _("is running");
479          break;
480       case JS_Blocked:
481          msg = _("is blocked");
482          break;
483       case JS_Terminated:
484          msg = _("has terminated");
485          break;
486       case JS_ErrorTerminated:
487          msg = _("has erred");
488          break;
489       case JS_Error:
490          msg = _("has errors");
491          break;
492       case JS_FatalError:
493          msg = _("has a fatal error");
494          break;
495       case JS_Differences:
496          msg = _("has verify differences");
497          break;
498       case JS_Canceled:
499          msg = _("has been canceled");
500          break;
501       case JS_WaitFD:
502          emsg = (char *) get_pool_memory(PM_FNAME);
503          Mmsg(&emsg, _("is waiting on Client %s"), jcr->client->hdr.name);
504          pool_mem = true;
505          msg = emsg;
506          break;
507       case JS_WaitSD:
508          emsg = (char *) get_pool_memory(PM_FNAME);
509          Mmsg(&emsg, _("is waiting on Storage %s"), jcr->store->hdr.name);
510          pool_mem = true;
511          msg = emsg;
512          break;
513       case JS_WaitStoreRes:
514          msg = _("is waiting on max Storage jobs");
515          break;
516       case JS_WaitClientRes:
517          msg = _("is waiting on max Client jobs");
518          break;
519       case JS_WaitJobRes:
520          msg = _("is waiting on max Job jobs");
521          break;
522       case JS_WaitMaxJobs:
523          msg = _("is waiting on max total jobs");
524          break;
525       case JS_WaitStartTime:
526          msg = _("is waiting for its start time");
527          break;
528       case JS_WaitPriority:
529          msg = _("is waiting for higher priority jobs to finish");
530          break;
531
532       default:
533          emsg = (char *) get_pool_memory(PM_FNAME);
534          Mmsg(&emsg, _("is in unknown state %c"), jcr->JobStatus);
535          pool_mem = true;
536          msg = emsg;
537          break;
538       }
539       /* 
540        * Now report Storage daemon status code 
541        */
542       switch (jcr->SDJobStatus) {
543       case JS_WaitMount:
544          if (pool_mem) {
545             free_pool_memory(emsg);
546             pool_mem = false;
547          }
548          msg = _("is waiting for a mount request");
549          break;
550       case JS_WaitMedia:
551          if (pool_mem) {
552             free_pool_memory(emsg);
553             pool_mem = false;
554          }
555          msg = _("is waiting for an appendable Volume");
556          break;
557       case JS_WaitFD:
558          if (!pool_mem) {
559             emsg = (char *) get_pool_memory(PM_FNAME);
560             pool_mem = true;
561          }
562          Mmsg(&emsg, _("is waiting for Client %s to connect to Storage %s"),
563               jcr->client->hdr.name, jcr->store->hdr.name);
564          msg = emsg;
565          break;
566       }
567       switch (jcr->JobType) {
568       case JT_ADMIN:
569       case JT_RESTORE:
570          bstrncpy(level, "      ", sizeof(level));
571          break;
572       default:
573          bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
574          level[7] = 0;
575          break;
576       }
577
578       bsendmsg(ua, _("%6d %-6s  %-20s %s\n"), 
579          jcr->JobId,
580          level, 
581          jcr->Job,
582          msg);
583
584       if (pool_mem) {
585          free_pool_memory(emsg);
586          pool_mem = false;
587       }
588       free_locked_jcr(jcr);
589    }
590    unlock_jcr_chain();
591    bsendmsg(ua, "====\n");
592    Dmsg0(200, "leave list_run_jobs()\n");
593 }
594
595 static void list_terminated_jobs(UAContext *ua)
596 {
597    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
598    char level[10];
599
600    if (last_jobs->empty()) {
601       bsendmsg(ua, _("No Terminated Jobs.\n"));
602       return;
603    }
604    lock_last_jobs_list();
605    struct s_last_job *je;
606    bsendmsg(ua, _("\nTerminated Jobs:\n"));
607    bsendmsg(ua, _(" JobId  Level     Files      Bytes     Status   Finished        Name \n"));
608    bsendmsg(ua, _("========================================================================\n"));
609    foreach_dlist(je, last_jobs) {
610       char JobName[MAX_NAME_LENGTH];
611       const char *termstat;
612
613       bstrftime_nc(dt, sizeof(dt), je->end_time);
614       switch (je->JobType) {
615       case JT_ADMIN:
616       case JT_RESTORE:
617          bstrncpy(level, "    ", sizeof(level));
618          break;
619       default:
620          bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
621          level[4] = 0;
622          break;
623       }
624       switch (je->JobStatus) {
625       case JS_Created:
626          termstat = "Created";
627          break;
628       case JS_FatalError:
629       case JS_ErrorTerminated:
630          termstat = "Error";
631          break;
632       case JS_Differences:
633          termstat = "Diffs";
634          break;
635       case JS_Canceled:
636          termstat = "Cancel";
637          break;
638       case JS_Terminated:
639          termstat = "OK";
640          break;
641       default:
642          termstat = "Other";
643          break;
644       }
645       bstrncpy(JobName, je->Job, sizeof(JobName));
646       /* There are three periods after the Job name */
647       char *p;
648       for (int i=0; i<3; i++) {
649          if ((p=strrchr(JobName, '.')) != NULL) {
650             *p = 0;
651          }
652       }
653       bsendmsg(ua, _("%6d  %-6s %8s %14s %-7s  %-8s %s\n"), 
654          je->JobId,
655          level, 
656          edit_uint64_with_commas(je->JobFiles, b1),
657          edit_uint64_with_commas(je->JobBytes, b2), 
658          termstat,
659          dt, JobName);
660    }
661    bsendmsg(ua, "\n");
662    unlock_last_jobs_list();
663 }