]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_status.c
44ca5bac40e3e3e7b288f1a5e1bc726e23ab3074
[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-2003 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 struct s_last_job last_job;
36
37 static void print_jobs_scheduled(UAContext *ua);
38 static void do_storage_status(UAContext *ua, STORE *store);
39 static void do_client_status(UAContext *ua, CLIENT *client);
40 static void do_director_status(UAContext *ua, char *cmd);
41 static void do_all_status(UAContext *ua, char *cmd);
42
43 /*
44  * status command
45  */
46 int status_cmd(UAContext *ua, char *cmd)
47 {
48    STORE *store;
49    CLIENT *client;
50    int item, i;
51
52    if (!open_db(ua)) {
53       return 1;
54    }
55    Dmsg1(20, "status:%s:\n", cmd);
56
57    for (i=1; i<ua->argc; i++) {
58       if (strcasecmp(ua->argk[i], _("all")) == 0) {
59          do_all_status(ua, cmd);
60          return 1;
61       } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
62                  strcasecmp(ua->argk[i], _("director")) == 0) {
63          do_director_status(ua, cmd);
64          return 1;
65       } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
66          client = get_client_resource(ua);
67          if (client) {
68             do_client_status(ua, client);
69          }
70          return 1;
71       } else {
72          store = get_storage_resource(ua, 0);
73          if (store) {
74             do_storage_status(ua, store);
75          }
76          return 1;
77       }
78    }
79    /* If no args, ask for status type */
80    if (ua->argc == 1) {                                    
81       start_prompt(ua, _("Status available for:\n"));
82       add_prompt(ua, _("Director"));
83       add_prompt(ua, _("Storage"));
84       add_prompt(ua, _("Client"));
85       add_prompt(ua, _("All"));
86       Dmsg0(20, "do_prompt: select daemon\n");
87       if ((item=do_prompt(ua, "",  _("Select daemon type for status"), cmd, MAX_NAME_LENGTH)) < 0) {
88          return 1;
89       }
90       Dmsg1(20, "item=%d\n", item);
91       switch (item) { 
92       case 0:                         /* Director */
93          do_director_status(ua, cmd);
94          break;
95       case 1:
96          store = select_storage_resource(ua);
97          if (store) {
98             do_storage_status(ua, store);
99          }
100          break;
101       case 2:
102          client = select_client_resource(ua);
103          if (client) {
104             do_client_status(ua, client);
105          }
106          break;
107       case 3:
108          do_all_status(ua, cmd);
109          break;
110       default:
111          break;
112       }
113    }
114    return 1;
115 }
116
117 static void do_all_status(UAContext *ua, char *cmd)
118 {
119    STORE *store, **unique_store;
120    CLIENT *client, **unique_client;
121    int i, j, found;
122
123    do_director_status(ua, cmd);
124
125    /* Count Storage items */
126    LockRes();
127    store = NULL;
128    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
129       { }
130    unique_store = (STORE **) malloc(i * sizeof(STORE));
131    /* Find Unique Storage address/port */         
132    store = (STORE *)GetNextRes(R_STORAGE, NULL);
133    i = 0;
134    unique_store[i++] = store;
135    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
136       found = 0;
137       for (j=0; j<i; j++) {
138          if (strcmp(unique_store[j]->address, store->address) == 0 &&
139              unique_store[j]->SDport == store->SDport) {
140             found = 1;
141             break;
142          }
143       }
144       if (!found) {
145          unique_store[i++] = store;
146          Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
147       }
148    }
149    UnlockRes();
150
151    /* Call each unique Storage daemon */
152    for (j=0; j<i; j++) {
153       do_storage_status(ua, unique_store[j]);
154    }
155    free(unique_store);
156
157    /* Count Client items */
158    LockRes();
159    client = NULL;
160    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
161       { }
162    unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
163    /* Find Unique Client address/port */         
164    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
165    i = 0;
166    unique_client[i++] = client;
167    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
168       found = 0;
169       for (j=0; j<i; j++) {
170          if (strcmp(unique_client[j]->address, client->address) == 0 &&
171              unique_client[j]->FDport == client->FDport) {
172             found = 1;
173             break;
174          }
175       }
176       if (!found) {
177          unique_client[i++] = client;
178          Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
179       }
180    }
181    UnlockRes();
182
183    /* Call each unique File daemon */
184    for (j=0; j<i; j++) {
185       do_client_status(ua, unique_client[j]);
186    }
187    free(unique_client);
188    
189 }
190
191 static void do_director_status(UAContext *ua, char *cmd)
192 {
193    JCR *jcr;
194    int njobs = 0;
195    char *msg;
196    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
197    int pool_mem = FALSE;
198
199    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
200             HOST_OS, DISTNAME, DISTVER);
201    bstrftime(dt, sizeof(dt), daemon_start_time);
202    bsendmsg(ua, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
203         last_job.NumJobs == 1 ? "" : "s");
204    if (last_job.NumJobs > 0) {
205       char termstat[30];
206
207       bstrftime(dt, sizeof(dt), last_job.end_time);
208       bsendmsg(ua, _("Last Job %s finished at %s\n"), last_job.Job, dt);
209       jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
210            
211       bsendmsg(ua, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
212            edit_uint64_with_commas(last_job.JobFiles, b1),
213            edit_uint64_with_commas(last_job.JobBytes, b2),
214            termstat);
215    }
216    lock_jcr_chain();
217    for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
218       if (jcr->JobId == 0) {      /* this is us */
219          bstrftime(dt, sizeof(dt), jcr->start_time);
220          bsendmsg(ua, _("Console connected at %s\n"), dt);
221          free_locked_jcr(jcr);
222          njobs--;
223          continue;
224       }
225       switch (jcr->JobStatus) {
226       case JS_Created:
227          msg = _("is waiting execution");
228          break;
229       case JS_Running:
230          msg = _("is running");
231          break;
232       case JS_Blocked:
233          msg = _("is blocked");
234          break;
235       case JS_Terminated:
236          msg = _("has terminated");
237          break;
238       case JS_ErrorTerminated:
239          msg = _("has erred");
240          break;
241       case JS_Canceled:
242          msg = _("has been canceled");
243          break;
244       case JS_WaitFD:
245          msg = (char *) get_pool_memory(PM_FNAME);
246          Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
247          pool_mem = TRUE;
248          break;
249       case JS_WaitSD:
250          msg = (char *) get_pool_memory(PM_FNAME);
251          Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
252          pool_mem = TRUE;
253          break;
254       case JS_WaitStoreRes:
255          msg = _("is waiting on max Storage jobs");
256          break;
257       case JS_WaitClientRes:
258          msg = _("is waiting on max Client jobs");
259          break;
260       case JS_WaitJobRes:
261          msg = _("is waiting on max Job jobs");
262          break;
263       case JS_WaitPriority:
264          msg = _("is waiting for higher priority jobs to finish");
265          break;
266       case JS_WaitMaxJobs:
267          msg = _("is waiting on max total jobs");
268          break;
269       case JS_WaitStartTime:
270          msg = _("is waiting for its start time");
271          break;
272
273
274       default:
275          msg = (char *) get_pool_memory(PM_FNAME);
276          Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
277          pool_mem = TRUE;
278          break;
279       }
280       /* 
281        * Now report Storage daemon status code 
282        */
283       switch (jcr->SDJobStatus) {
284       case JS_WaitMount:
285          if (pool_mem) {
286             free_pool_memory(msg);
287             pool_mem = FALSE;
288          }
289          msg = _("is waiting for a mount request");
290          break;
291       case JS_WaitMedia:
292          if (pool_mem) {
293             free_pool_memory(msg);
294             pool_mem = FALSE;
295          }
296          msg = _("is waiting for an appendable Volume");
297          break;
298       case JS_WaitFD:
299          if (!pool_mem) {
300             msg = (char *) get_pool_memory(PM_FNAME);
301             pool_mem = TRUE;
302          }
303          Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
304               jcr->client->hdr.name, jcr->store->hdr.name);
305          break;
306       }
307       bsendmsg(ua, _("JobId %d Job %s %s.\n"), jcr->JobId, jcr->Job, msg);
308       if (pool_mem) {
309          free_pool_memory(msg);
310          pool_mem = FALSE;
311       }
312       free_locked_jcr(jcr);
313    }
314    unlock_jcr_chain();
315
316    if (njobs == 0) {
317       bsendmsg(ua, _("No jobs are running.\n"));
318    }
319    print_jobs_scheduled(ua);
320    bsendmsg(ua, "====\n");
321 }
322
323 static void do_storage_status(UAContext *ua, STORE *store)
324 {
325    BSOCK *sd;
326
327    ua->jcr->store = store;
328    /* Try connecting for up to 15 seconds */
329    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
330       store->hdr.name, store->address, store->SDport);
331    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
332       bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
333          store->hdr.name);
334       if (ua->jcr->store_bsock) {
335          bnet_close(ua->jcr->store_bsock);
336          ua->jcr->store_bsock = NULL;
337       }         
338       return;
339    }
340    Dmsg0(20, _("Connected to storage daemon\n"));
341    sd = ua->jcr->store_bsock;
342    bnet_fsend(sd, "status");
343    while (bnet_recv(sd) >= 0) {
344       bsendmsg(ua, "%s", sd->msg);
345    }
346    bnet_sig(sd, BNET_TERMINATE);
347    bnet_close(sd);
348    ua->jcr->store_bsock = NULL;
349    return;  
350 }
351    
352 static void do_client_status(UAContext *ua, CLIENT *client)
353 {
354    BSOCK *fd;
355
356    /* Connect to File daemon */
357
358    ua->jcr->client = client;
359    /* Release any old dummy key */
360    if (ua->jcr->sd_auth_key) {
361       free(ua->jcr->sd_auth_key);
362    }
363    /* Create a new dummy SD auth key */
364    ua->jcr->sd_auth_key = bstrdup("dummy");
365
366    /* Try to connect for 15 seconds */
367    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
368       client->hdr.name, client->address, client->FDport);
369    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
370       bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
371          client->hdr.name);
372       if (ua->jcr->file_bsock) {
373          bnet_close(ua->jcr->file_bsock);
374          ua->jcr->file_bsock = NULL;
375       }         
376       return;
377    }
378    Dmsg0(20, _("Connected to file daemon\n"));
379    fd = ua->jcr->file_bsock;
380    bnet_fsend(fd, "status");
381    while (bnet_recv(fd) >= 0) {
382       bsendmsg(ua, "%s", fd->msg);
383    }
384    bnet_sig(fd, BNET_TERMINATE);
385    bnet_close(fd);
386    ua->jcr->file_bsock = NULL;
387
388    return;  
389 }
390
391 static void prt_runhdr(UAContext *ua)
392 {
393    bsendmsg(ua, _("\nScheduled Jobs:\n"));
394    bsendmsg(ua, _("Level          Type     Scheduled          Name               Volume\n"));
395    bsendmsg(ua, _("===============================================================================\n"));
396 }
397
398 static void prt_runtime(UAContext *ua, JOB *job, int level, time_t runtime, POOL *pool)
399 {
400    char dt[MAX_TIME_LENGTH];       
401    bool ok = false;
402    bool close_db = false;
403    JCR *jcr = ua->jcr;
404    MEDIA_DBR mr;
405    memset(&mr, 0, sizeof(mr));
406    if (job->JobType == JT_BACKUP) {
407       jcr->db = NULL;
408       ok = complete_jcr_for_job(jcr, job, pool);
409       if (jcr->db) {
410          close_db = true;             /* new db opened, remember to close it */
411       }
412       if (ok) {
413          ok = find_next_volume_for_append(jcr, &mr, 0);
414       }
415       if (!ok) {
416          bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
417       }
418    }
419    bstrftime(dt, sizeof(dt), runtime);
420    bsendmsg(ua, _("%-14s %-8s %-18s %-18s %s\n"), 
421       level_to_str(level), job_type_to_str(job->JobType), dt, job->hdr.name,
422       mr.VolumeName);
423    if (close_db) {
424       db_close_database(jcr, jcr->db);
425    }
426    jcr->db = ua->db;                  /* restore ua db to jcr */
427
428 }
429
430 /*          
431  * Find all jobs to be run this hour
432  * and the next hour.
433  */
434 static void print_jobs_scheduled(UAContext *ua)
435 {
436    time_t now, runtime, tomorrow;
437    RUN *run;
438    JOB *job;
439    SCHED *sched;
440    struct tm tm;
441    int mday, wday, month, wpos, tmday, twday, tmonth, twpos, i, hour;
442    int tod, tom;
443    int found;
444    int hdr_printed = FALSE;
445    int level;
446
447    Dmsg0(200, "enter find_runs()\n");
448
449    now = time(NULL);
450    localtime_r(&now, &tm);
451    mday = tm.tm_mday - 1;
452    wday = tm.tm_wday;
453    month = tm.tm_mon;
454    wpos = (tm.tm_mday - 1) / 7;
455
456    tomorrow = now + 60 * 60 * 24;
457    localtime_r(&tomorrow, &tm);
458    tmday = tm.tm_mday - 1;
459    twday = tm.tm_wday;
460    tmonth = tm.tm_mon;
461    twpos  = (tm.tm_mday - 1) / 7;
462
463    /* Loop through all jobs */
464    LockRes();
465    for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
466       level = job->level;   
467       sched = job->schedule;
468       if (sched == NULL) {            /* scheduled? */
469          continue;                    /* no, skip this job */
470       }
471       for (run=sched->run; run; run=run->next) {
472          if (run->level) {
473             level = run->level;
474          }
475          /* 
476           * Find runs in next 24 hours
477           */
478          tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) && 
479                 bit_is_set(month, run->month) && bit_is_set(wpos, run->wpos);
480
481          tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
482                 bit_is_set(tmonth, run->month) && bit_is_set(wpos, run->wpos);
483
484          Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
485          found = FALSE;
486          if (tod) {                   /* Jobs scheduled today (next 24 hours) */
487             /* find time (time_t) job is to be run */
488             localtime_r(&now, &tm);
489             hour = 0;
490             for (i=tm.tm_hour; i < 24; i++) {
491                if (bit_is_set(i, run->hour)) {
492                   tm.tm_hour = i;
493                   tm.tm_min = run->minute;
494                   tm.tm_sec = 0;
495                   runtime = mktime(&tm);
496                   if (runtime > now) {
497                      if (!hdr_printed) {
498                         hdr_printed = TRUE;
499                         prt_runhdr(ua);
500                      }
501                      prt_runtime(ua, job, level, runtime, run->pool);
502                      found = TRUE;
503                      break;
504                   }
505                }
506             }
507          }
508
509 //       Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
510          if (!found && tom) {            /* look at jobs scheduled tomorrow */
511             localtime_r(&tomorrow, &tm);
512             hour = 0;
513             for (i=0; i < 24; i++) {
514                if (bit_is_set(i, run->hour)) {
515                   hour = i;
516                   break;
517                }
518             }
519             tm.tm_hour = hour;
520             tm.tm_min = run->minute;
521             tm.tm_sec = 0;
522             runtime = mktime(&tm);
523             Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
524             if (runtime < tomorrow) {
525                if (!hdr_printed) {
526                   hdr_printed = TRUE;
527                   prt_runhdr(ua);
528                }
529                prt_runtime(ua, job, level, runtime, run->pool);
530             }
531          }
532       } /* end for loop over runs */ 
533    } /* end for loop over resources */
534    UnlockRes();
535    Dmsg0(200, "Leave find_runs()\n");
536 }