]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_status.c
a05ee54b1cb04c10d879366cffe1565aeda70147
[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
8 /*
9    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "dird.h"
30 #include "ua.h"
31
32 extern char my_name[];
33 extern time_t daemon_start_time;
34 extern struct s_last_job last_job;
35
36 static void print_jobs_scheduled(UAContext *ua);
37 static void do_storage_status(UAContext *ua, STORE *store);
38 static void do_client_status(UAContext *ua, CLIENT *client);
39 static void do_director_status(UAContext *ua, char *cmd);
40 static void do_all_status(UAContext *ua, char *cmd);
41
42 /*
43  * status command
44  */
45 int statuscmd(UAContext *ua, char *cmd)
46 {
47    STORE *store;
48    CLIENT *client;
49    int item, i;
50
51    if (!open_db(ua)) {
52       return 1;
53    }
54    Dmsg1(20, "status:%s:\n", cmd);
55
56    for (i=1; i<ua->argc; i++) {
57       if (strcasecmp(ua->argk[i], _("all")) == 0) {
58          do_all_status(ua, cmd);
59          return 1;
60       } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
61                  strcasecmp(ua->argk[i], _("director")) == 0) {
62          do_director_status(ua, cmd);
63          return 1;
64       } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
65          client = get_client_resource(ua);
66          if (client) {
67             do_client_status(ua, client);
68          }
69          return 1;
70       } else {
71          store = get_storage_resource(ua, cmd);
72          if (store) {
73             do_storage_status(ua, store);
74          }
75          return 1;
76       }
77    }
78    /* If no args, ask for status type */
79    if (ua->argc == 1) {                                    
80       start_prompt(ua, _("Status available for:\n"));
81       add_prompt(ua, _("Director"));
82       add_prompt(ua, _("Storage"));
83       add_prompt(ua, _("Client"));
84       add_prompt(ua, _("All"));
85       Dmsg0(20, "do_prompt: select daemon\n");
86       if ((item=do_prompt(ua, _("Select daemon type for status"), cmd)) < 0) {
87          return 1;
88       }
89       Dmsg1(20, "item=%d\n", item);
90       switch (item) { 
91       case 0:                         /* Director */
92          do_director_status(ua, cmd);
93          break;
94       case 1:
95          store = select_storage_resource(ua);
96          if (store) {
97             do_storage_status(ua, store);
98          }
99          break;
100       case 2:
101          client = select_client_resource(ua);
102          if (client) {
103             do_client_status(ua, client);
104          }
105          break;
106       case 3:
107          do_all_status(ua, cmd);
108          break;
109       default:
110          break;
111       }
112    }
113    return 1;
114 }
115
116 static void do_all_status(UAContext *ua, char *cmd)
117 {
118    STORE *store, **unique_store;
119    CLIENT *client, **unique_client;
120    int i, j, found;
121
122    do_director_status(ua, cmd);
123
124    /* Count Storage items */
125    LockRes();
126    store = NULL;
127    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
128       { }
129    unique_store = (STORE **) malloc(i * sizeof(STORE));
130    /* Find Unique Storage address/port */         
131    store = (STORE *)GetNextRes(R_STORAGE, NULL);
132    i = 0;
133    unique_store[i++] = store;
134    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
135       found = 0;
136       for (j=0; j<i; j++) {
137          if (strcmp(unique_store[j]->address, store->address) == 0 &&
138              unique_store[j]->SDport == store->SDport) {
139             found = 1;
140             break;
141          }
142       }
143       if (!found) {
144          unique_store[i++] = store;
145          Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
146       }
147    }
148    UnlockRes();
149
150    /* Call each unique Storage daemon */
151    for (j=0; j<i; j++) {
152       do_storage_status(ua, unique_store[j]);
153    }
154    free(unique_store);
155
156    /* Count Client items */
157    LockRes();
158    client = NULL;
159    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
160       { }
161    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
162    /* Find Unique Client address/port */         
163    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
164    i = 0;
165    unique_client[i++] = client;
166    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
167       found = 0;
168       for (j=0; j<i; j++) {
169          if (strcmp(unique_client[j]->address, client->address) == 0 &&
170              unique_client[j]->FDport == client->FDport) {
171             found = 1;
172             break;
173          }
174       }
175       if (!found) {
176          unique_client[i++] = client;
177          Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
178       }
179    }
180    UnlockRes();
181
182    /* Call each unique File daemon */
183    for (j=0; j<i; j++) {
184       do_client_status(ua, unique_client[j]);
185    }
186    free(unique_client);
187    
188 }
189
190 static void do_director_status(UAContext *ua, char *cmd)
191 {
192    JCR *jcr;
193    int njobs = 0;
194    char *msg;
195    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
196    int pool_mem = FALSE;
197
198    Dmsg0(200, "Doing status\n");
199    bsendmsg(ua, "%s Version: " VERSION " (" DATE ")\n", my_name);
200    bstrftime(dt, sizeof(dt), daemon_start_time);
201    bsendmsg(ua, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
202         last_job.NumJobs == 1 ? "" : "s");
203    if (last_job.NumJobs > 0) {
204       char *termstat, jstat[2];
205
206       bstrftime(dt, sizeof(dt), last_job.end_time);
207       bsendmsg(ua, _("Last Job %s finished at %s\n"), last_job.Job, dt);
208       switch (last_job.JobStatus) {
209          case JS_Terminated:
210             termstat = _("OK");
211             break;
212         case JS_ErrorTerminated:
213             termstat = _("Error");
214             break;
215         default:
216             jstat[0] = last_job.JobStatus;
217             jstat[1] = 0;
218             termstat = jstat;
219             break;
220       }
221            
222       bsendmsg(ua, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
223            edit_uint_with_commas(last_job.JobFiles, b1),
224            edit_uint_with_commas(last_job.JobBytes, b2),
225            termstat);
226    }
227    lock_jcr_chain();
228    for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
229       if (jcr->JobId == 0) {      /* this is us */
230          bstrftime(dt, sizeof(dt), jcr->start_time);
231          bsendmsg(ua, _("Console connected at %s\n"), dt);
232          free_locked_jcr(jcr);
233          njobs--;
234          continue;
235       }
236       switch (jcr->JobStatus) {
237          case JS_Created:
238             msg = _("is waiting execution");
239             break;
240          case JS_Running:
241             msg = _("is running");
242             break;
243          case JS_Blocked:
244             msg = _("is blocked");
245             break;
246          case JS_Terminated:
247             msg = _("has terminated");
248             break;
249          case JS_ErrorTerminated:
250             msg = _("has erred");
251             break;
252          case JS_Cancelled:
253             msg = _("has been canceled");
254             break;
255          case JS_WaitFD:
256             msg = (char *) get_pool_memory(PM_FNAME);
257             Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
258             pool_mem = TRUE;
259             break;
260          case JS_WaitSD:
261             msg = (char *) get_pool_memory(PM_FNAME);
262             Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
263             pool_mem = TRUE;
264             break;
265          default:
266             msg = (char *) get_pool_memory(PM_FNAME);
267             Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
268             pool_mem = TRUE;
269             break;
270       }
271       switch (jcr->SDJobStatus) {
272          case JS_WaitMount:
273             if (pool_mem) {
274                free_pool_memory(msg);
275                pool_mem = FALSE;
276             }
277             msg = _("is waiting for a mount request");
278             break;
279          case JS_WaitMedia:
280             if (pool_mem) {
281                free_pool_memory(msg);
282                pool_mem = FALSE;
283             }
284             msg = _("is waiting for an appendable Volume");
285             break;
286          case JS_WaitFD:
287             if (!pool_mem) {
288                msg = (char *) get_pool_memory(PM_FNAME);
289                pool_mem = TRUE;
290             }
291             Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
292                  jcr->client->hdr.name, jcr->store->hdr.name);
293             break;
294
295       }
296       bsendmsg(ua, _("JobId %d Job %s %s.\n"), jcr->JobId, jcr->Job, msg);
297       if (pool_mem) {
298          free_pool_memory(msg);
299          pool_mem = FALSE;
300       }
301       free_locked_jcr(jcr);
302    }
303    unlock_jcr_chain();
304
305    if (njobs == 0) {
306       bsendmsg(ua, _("No jobs are running.\n"));
307    }
308    print_jobs_scheduled(ua);
309    bsendmsg(ua, "====\n");
310 }
311
312 static void do_storage_status(UAContext *ua, STORE *store)
313 {
314    BSOCK *sd;
315
316    ua->jcr->store = store;
317    /* Try connecting for up to 15 seconds */
318    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
319       store->hdr.name, store->address, store->SDport);
320    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
321       bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
322          store->hdr.name);
323       return;
324    }
325    Dmsg0(20, _("Connected to storage daemon\n"));
326    sd = ua->jcr->store_bsock;
327    bnet_fsend(sd, "status");
328    while (bnet_recv(sd) > 0) {
329       bsendmsg(ua, "%s", sd->msg);
330    }
331    bnet_sig(sd, BNET_TERMINATE);
332    bnet_close(sd);
333    ua->jcr->store_bsock = NULL;
334    return;  
335 }
336    
337 static void do_client_status(UAContext *ua, CLIENT *client)
338 {
339    BSOCK *fd;
340
341    /* Connect to File daemon */
342
343    ua->jcr->client = client;
344    /* Try to connect for 15 seconds */
345    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
346       client->hdr.name, client->address, client->FDport);
347    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
348       bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
349          client->hdr.name);
350       return;
351    }
352    Dmsg0(20, _("Connected to file daemon\n"));
353    fd = ua->jcr->file_bsock;
354    bnet_fsend(fd, "status");
355    while (bnet_recv(fd) > 0) {
356       bsendmsg(ua, "%s", fd->msg);
357    }
358    bnet_sig(fd, BNET_TERMINATE);
359    bnet_close(fd);
360    ua->jcr->file_bsock = NULL;
361
362    return;  
363 }
364
365 static void prt_runtime(UAContext *ua, JOB *job, time_t runtime)
366 {
367    char dt[MAX_TIME_LENGTH], *type;
368
369    bstrftime(dt, sizeof(dt), runtime);
370    switch (job->JobType) {
371       case JT_BACKUP:
372          type = _("Backup");
373          break;
374       case JT_VERIFY:
375          type = _("Verify");
376          break;
377       case JT_RESTORE:
378          type = _("Restore");
379          break;
380       default:
381          type = _("Unknown type of");
382          break;
383    }
384    bsendmsg(ua, _("%s job \"%s\" scheduled for %s\n"), type, job->hdr.name, dt);
385 }
386
387 /*          
388  * Find all jobs to be run this hour
389  * and the next hour.
390  */
391 static void print_jobs_scheduled(UAContext *ua)
392 {
393    time_t now, runtime, tomorrow;
394    RUN *run;
395    JOB *job;
396    SCHED *sched;
397    struct tm tm;
398    int mday, wday, month, tmday, twday, tmonth, i, hour;
399    int tod, tom;
400    int found;
401
402    Dmsg0(200, "enter find_runs()\n");
403
404    now = time(NULL);
405    localtime_r(&now, &tm);
406    mday = tm.tm_mday - 1;
407    wday = tm.tm_wday;
408    month = tm.tm_mon;
409
410    tomorrow = now + 60 * 60 * 24;
411    localtime_r(&tomorrow, &tm);
412    tmday = tm.tm_mday - 1;
413    twday = tm.tm_wday;
414    tmonth = tm.tm_mon;
415
416    /* Loop through all jobs */
417    LockRes();
418    for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
419       sched = job->schedule;
420       if (sched == NULL) {            /* scheduled? */
421          continue;                    /* no, skip this job */
422       }
423       for (run=sched->run; run; run=run->next) {
424          /* 
425           * Find runs in next 24 hours
426           */
427          tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) && 
428                 bit_is_set(month, run->month);
429
430          tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
431                 bit_is_set(tmonth, run->month);
432
433          Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
434          found = FALSE;
435          if (tod) {
436             /* find time (time_t) job is to be run */
437             localtime_r(&now, &tm);
438             hour = 0;
439             for (i=tm.tm_hour; i < 24; i++) {
440                if (bit_is_set(i, run->hour)) {
441                   tm.tm_hour = i;
442                   tm.tm_min = run->minute;
443                   tm.tm_sec = 0;
444                   runtime = mktime(&tm);
445                   if (runtime > now) {
446                      prt_runtime(ua, job, runtime);
447                      found = TRUE;
448                      break;
449                   }
450                }
451             }
452          }
453
454         Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
455         if (!found && tom) {
456             localtime_r(&tomorrow, &tm);
457             hour = 0;
458             for (i=0; i < 24; i++) {
459                if (bit_is_set(i, run->hour)) {
460                   hour = i;
461                   break;
462                }
463             }
464             tm.tm_hour = hour;
465             tm.tm_min = run->minute;
466             tm.tm_sec = 0;
467             runtime = mktime(&tm);
468             Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
469             if (runtime < tomorrow) {
470                prt_runtime(ua, job, runtime);
471             }
472          }
473       }  
474    }
475    UnlockRes();
476    Dmsg0(200, "Leave find_runs()\n");
477 }