3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
33 extern char my_name[];
34 extern time_t daemon_start_time;
35 extern struct s_last_job last_job;
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);
46 int status_cmd(UAContext *ua, char *cmd)
55 Dmsg1(20, "status:%s:\n", cmd);
57 for (i=1; i<ua->argc; i++) {
58 if (strcasecmp(ua->argk[i], _("all")) == 0) {
59 do_all_status(ua, cmd);
61 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
62 strcasecmp(ua->argk[i], _("director")) == 0) {
63 do_director_status(ua, cmd);
65 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
66 client = get_client_resource(ua);
68 do_client_status(ua, client);
72 store = get_storage_resource(ua, 0);
74 do_storage_status(ua, store);
79 /* If no args, ask for status type */
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) {
90 Dmsg1(20, "item=%d\n", item);
92 case 0: /* Director */
93 do_director_status(ua, cmd);
96 store = select_storage_resource(ua);
98 do_storage_status(ua, store);
102 client = select_client_resource(ua);
104 do_client_status(ua, client);
108 do_all_status(ua, cmd);
117 static void do_all_status(UAContext *ua, char *cmd)
119 STORE *store, **unique_store;
120 CLIENT *client, **unique_client;
123 do_director_status(ua, cmd);
125 /* Count Storage items */
128 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
130 unique_store = (STORE **) malloc(i * sizeof(STORE));
131 /* Find Unique Storage address/port */
132 store = (STORE *)GetNextRes(R_STORAGE, NULL);
134 unique_store[i++] = store;
135 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
137 for (j=0; j<i; j++) {
138 if (strcmp(unique_store[j]->address, store->address) == 0 &&
139 unique_store[j]->SDport == store->SDport) {
145 unique_store[i++] = store;
146 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
151 /* Call each unique Storage daemon */
152 for (j=0; j<i; j++) {
153 do_storage_status(ua, unique_store[j]);
157 /* Count Client items */
160 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
162 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
163 /* Find Unique Client address/port */
164 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
166 unique_client[i++] = client;
167 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
169 for (j=0; j<i; j++) {
170 if (strcmp(unique_client[j]->address, client->address) == 0 &&
171 unique_client[j]->FDport == client->FDport) {
177 unique_client[i++] = client;
178 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
183 /* Call each unique File daemon */
184 for (j=0; j<i; j++) {
185 do_client_status(ua, unique_client[j]);
191 static void do_director_status(UAContext *ua, char *cmd)
196 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
197 int pool_mem = FALSE;
199 Dmsg0(200, "Doing status\n");
200 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
201 HOST_OS, DISTNAME, DISTVER);
202 bstrftime(dt, sizeof(dt), daemon_start_time);
203 bsendmsg(ua, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
204 last_job.NumJobs == 1 ? "" : "s");
205 if (last_job.NumJobs > 0) {
208 bstrftime(dt, sizeof(dt), last_job.end_time);
209 bsendmsg(ua, _("Last Job %s finished at %s\n"), last_job.Job, dt);
210 jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
212 bsendmsg(ua, _(" Files=%s Bytes=%s Termination Status=%s\n"),
213 edit_uint64_with_commas(last_job.JobFiles, b1),
214 edit_uint64_with_commas(last_job.JobBytes, b2),
218 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
219 if (jcr->JobId == 0) { /* this is us */
220 bstrftime(dt, sizeof(dt), jcr->start_time);
221 bsendmsg(ua, _("Console connected at %s\n"), dt);
222 free_locked_jcr(jcr);
226 switch (jcr->JobStatus) {
228 msg = _("is waiting execution");
231 msg = _("is running");
234 msg = _("is blocked");
237 msg = _("has terminated");
239 case JS_ErrorTerminated:
240 msg = _("has erred");
243 msg = _("has been canceled");
246 msg = (char *) get_pool_memory(PM_FNAME);
247 Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
251 msg = (char *) get_pool_memory(PM_FNAME);
252 Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
255 case JS_WaitStoreRes:
256 msg = _("is waiting on max Storage jobs");
258 case JS_WaitClientRes:
259 msg = _("is waiting on max Client jobs");
262 msg = _("is waiting on max Job jobs");
264 case JS_WaitPriority:
265 msg = _("is waiting for higher priority jobs to finish");
268 msg = _("is waiting on max total jobs");
270 case JS_WaitStartTime:
271 msg = _("is waiting for its start time");
276 msg = (char *) get_pool_memory(PM_FNAME);
277 Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
281 switch (jcr->SDJobStatus) {
284 free_pool_memory(msg);
287 msg = _("is waiting for a mount request");
291 free_pool_memory(msg);
294 msg = _("is waiting for an appendable Volume");
298 msg = (char *) get_pool_memory(PM_FNAME);
301 Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
302 jcr->client->hdr.name, jcr->store->hdr.name);
306 bsendmsg(ua, _("JobId %d Job %s %s.\n"), jcr->JobId, jcr->Job, msg);
308 free_pool_memory(msg);
311 free_locked_jcr(jcr);
316 bsendmsg(ua, _("No jobs are running.\n"));
318 print_jobs_scheduled(ua);
319 bsendmsg(ua, "====\n");
322 static void do_storage_status(UAContext *ua, STORE *store)
326 ua->jcr->store = store;
327 /* Try connecting for up to 15 seconds */
328 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
329 store->hdr.name, store->address, store->SDport);
330 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
331 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
333 if (ua->jcr->store_bsock) {
334 bnet_close(ua->jcr->store_bsock);
335 ua->jcr->store_bsock = NULL;
339 Dmsg0(20, _("Connected to storage daemon\n"));
340 sd = ua->jcr->store_bsock;
341 bnet_fsend(sd, "status");
342 while (bnet_recv(sd) >= 0) {
343 bsendmsg(ua, "%s", sd->msg);
345 bnet_sig(sd, BNET_TERMINATE);
347 ua->jcr->store_bsock = NULL;
351 static void do_client_status(UAContext *ua, CLIENT *client)
355 /* Connect to File daemon */
357 ua->jcr->client = client;
358 /* Release any old dummy key */
359 if (ua->jcr->sd_auth_key) {
360 free(ua->jcr->sd_auth_key);
362 /* Create a new dummy SD auth key */
363 ua->jcr->sd_auth_key = bstrdup("dummy");
365 /* Try to connect for 15 seconds */
366 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
367 client->hdr.name, client->address, client->FDport);
368 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
369 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
371 if (ua->jcr->file_bsock) {
372 bnet_close(ua->jcr->file_bsock);
373 ua->jcr->file_bsock = NULL;
377 Dmsg0(20, _("Connected to file daemon\n"));
378 fd = ua->jcr->file_bsock;
379 bnet_fsend(fd, "status");
380 while (bnet_recv(fd) >= 0) {
381 bsendmsg(ua, "%s", fd->msg);
383 bnet_sig(fd, BNET_TERMINATE);
385 ua->jcr->file_bsock = NULL;
390 static void prt_runhdr(UAContext *ua)
392 bsendmsg(ua, _("Level Type Scheduled Name\n"));
393 bsendmsg(ua, _("=================================================================\n"));
396 static void prt_runtime(UAContext *ua, JOB *job, int level, time_t runtime)
398 char dt[MAX_TIME_LENGTH];
400 bstrftime(dt, sizeof(dt), runtime);
401 bsendmsg(ua, _("%-14s %-8s %-18s %s\n"),
402 level_to_str(level), job_type_to_str(job->JobType), dt, job->hdr.name);
406 * Find all jobs to be run this hour
409 static void print_jobs_scheduled(UAContext *ua)
411 time_t now, runtime, tomorrow;
416 int mday, wday, month, wpos, tmday, twday, tmonth, twpos, i, hour;
419 int hdr_printed = FALSE;
422 Dmsg0(200, "enter find_runs()\n");
425 localtime_r(&now, &tm);
426 mday = tm.tm_mday - 1;
429 wpos = (tm.tm_mday - 1) / 7;
431 tomorrow = now + 60 * 60 * 24;
432 localtime_r(&tomorrow, &tm);
433 tmday = tm.tm_mday - 1;
436 twpos = (tm.tm_mday - 1) / 7;
438 /* Loop through all jobs */
440 for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
442 sched = job->schedule;
443 if (sched == NULL) { /* scheduled? */
444 continue; /* no, skip this job */
446 for (run=sched->run; run; run=run->next) {
451 * Find runs in next 24 hours
453 tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) &&
454 bit_is_set(month, run->month) && bit_is_set(wpos, run->wpos);
456 tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
457 bit_is_set(tmonth, run->month) && bit_is_set(wpos, run->wpos);
459 Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
461 if (tod) { /* Jobs scheduled today (next 24 hours) */
462 /* find time (time_t) job is to be run */
463 localtime_r(&now, &tm);
465 for (i=tm.tm_hour; i < 24; i++) {
466 if (bit_is_set(i, run->hour)) {
468 tm.tm_min = run->minute;
470 runtime = mktime(&tm);
476 prt_runtime(ua, job, level, runtime);
484 // Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
485 if (!found && tom) { /* look at jobs scheduled tomorrow */
486 localtime_r(&tomorrow, &tm);
488 for (i=0; i < 24; i++) {
489 if (bit_is_set(i, run->hour)) {
495 tm.tm_min = run->minute;
497 runtime = mktime(&tm);
498 Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
499 if (runtime < tomorrow) {
504 prt_runtime(ua, job, level, runtime);
507 } /* end for loop over runs */
508 } /* end for loop over resources */
510 Dmsg0(200, "Leave find_runs()\n");