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 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, char *cmd);
43 static void do_all_status(UAContext *ua, char *cmd);
48 int status_cmd(UAContext *ua, char *cmd)
57 Dmsg1(20, "status:%s:\n", cmd);
59 for (i=1; i<ua->argc; i++) {
60 if (strcasecmp(ua->argk[i], _("all")) == 0) {
61 do_all_status(ua, cmd);
63 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
64 strcasecmp(ua->argk[i], _("director")) == 0) {
65 do_director_status(ua, cmd);
67 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
68 client = get_client_resource(ua);
70 do_client_status(ua, client);
74 store = get_storage_resource(ua, 0);
76 do_storage_status(ua, store);
81 /* If no args, ask for status type */
83 start_prompt(ua, _("Status available for:\n"));
84 add_prompt(ua, _("Director"));
85 add_prompt(ua, _("Storage"));
86 add_prompt(ua, _("Client"));
87 add_prompt(ua, _("All"));
88 Dmsg0(20, "do_prompt: select daemon\n");
89 if ((item=do_prompt(ua, "", _("Select daemon type for status"), cmd, MAX_NAME_LENGTH)) < 0) {
92 Dmsg1(20, "item=%d\n", item);
94 case 0: /* Director */
95 do_director_status(ua, cmd);
98 store = select_storage_resource(ua);
100 do_storage_status(ua, store);
104 client = select_client_resource(ua);
106 do_client_status(ua, client);
110 do_all_status(ua, cmd);
119 static void do_all_status(UAContext *ua, char *cmd)
121 STORE *store, **unique_store;
122 CLIENT *client, **unique_client;
125 do_director_status(ua, cmd);
127 /* Count Storage items */
130 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
132 unique_store = (STORE **) malloc(i * sizeof(STORE));
133 /* Find Unique Storage address/port */
134 store = (STORE *)GetNextRes(R_STORAGE, NULL);
136 unique_store[i++] = store;
137 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
139 for (j=0; j<i; j++) {
140 if (strcmp(unique_store[j]->address, store->address) == 0 &&
141 unique_store[j]->SDport == store->SDport) {
147 unique_store[i++] = store;
148 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
153 /* Call each unique Storage daemon */
154 for (j=0; j<i; j++) {
155 do_storage_status(ua, unique_store[j]);
159 /* Count Client items */
162 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
164 unique_client = (CLIENT **)malloc(i * sizeof(CLIENT));
165 /* Find Unique Client address/port */
166 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
168 unique_client[i++] = client;
169 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
171 for (j=0; j<i; j++) {
172 if (strcmp(unique_client[j]->address, client->address) == 0 &&
173 unique_client[j]->FDport == client->FDport) {
179 unique_client[i++] = client;
180 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
185 /* Call each unique File daemon */
186 for (j=0; j<i; j++) {
187 do_client_status(ua, unique_client[j]);
193 static void do_director_status(UAContext *ua, char *cmd)
195 char dt[MAX_TIME_LENGTH];
197 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
198 HOST_OS, DISTNAME, DISTVER);
199 bstrftime(dt, sizeof(dt), daemon_start_time);
200 strcpy(dt+7, dt+9); /* cut century */
201 bsendmsg(ua, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
202 last_job.NumJobs == 1 ? "" : "s");
204 * List scheduled Jobs
206 list_scheduled_jobs(ua);
211 list_running_jobs(ua);
214 * List terminated jobs
216 list_terminated_jobs(ua);
217 bsendmsg(ua, "====\n");
220 static void do_storage_status(UAContext *ua, STORE *store)
224 ua->jcr->store = store;
225 /* Try connecting for up to 15 seconds */
226 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
227 store->hdr.name, store->address, store->SDport);
228 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
229 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
231 if (ua->jcr->store_bsock) {
232 bnet_close(ua->jcr->store_bsock);
233 ua->jcr->store_bsock = NULL;
237 Dmsg0(20, _("Connected to storage daemon\n"));
238 sd = ua->jcr->store_bsock;
239 bnet_fsend(sd, "status");
240 while (bnet_recv(sd) >= 0) {
241 bsendmsg(ua, "%s", sd->msg);
243 bnet_sig(sd, BNET_TERMINATE);
245 ua->jcr->store_bsock = NULL;
249 static void do_client_status(UAContext *ua, CLIENT *client)
253 /* Connect to File daemon */
255 ua->jcr->client = client;
256 /* Release any old dummy key */
257 if (ua->jcr->sd_auth_key) {
258 free(ua->jcr->sd_auth_key);
260 /* Create a new dummy SD auth key */
261 ua->jcr->sd_auth_key = bstrdup("dummy");
263 /* Try to connect for 15 seconds */
264 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
265 client->hdr.name, client->address, client->FDport);
266 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
267 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
269 if (ua->jcr->file_bsock) {
270 bnet_close(ua->jcr->file_bsock);
271 ua->jcr->file_bsock = NULL;
275 Dmsg0(20, _("Connected to file daemon\n"));
276 fd = ua->jcr->file_bsock;
277 bnet_fsend(fd, "status");
278 while (bnet_recv(fd) >= 0) {
279 bsendmsg(ua, "%s", fd->msg);
281 bnet_sig(fd, BNET_TERMINATE);
283 ua->jcr->file_bsock = NULL;
288 static void prt_runhdr(UAContext *ua)
290 bsendmsg(ua, _("\nScheduled Jobs:\n"));
291 bsendmsg(ua, _("Level Type Scheduled Name Volume\n"));
292 bsendmsg(ua, _("===============================================================================\n"));
295 static void prt_runtime(UAContext *ua, JOB *job, int level, time_t runtime, POOL *pool)
297 char dt[MAX_TIME_LENGTH];
300 bool close_db = false;
303 memset(&mr, 0, sizeof(mr));
304 if (job->JobType == JT_BACKUP) {
306 ok = complete_jcr_for_job(jcr, job, pool);
308 close_db = true; /* new db opened, remember to close it */
311 ok = find_next_volume_for_append(jcr, &mr, 0);
314 bstrncpy(mr.VolumeName, "*unknown*", sizeof(mr.VolumeName));
317 bstrftime(dt, sizeof(dt), runtime);
318 strcpy(dt+7, dt+9); /* cut century */
319 switch (job->JobType) {
325 level_ptr = level_to_str(level);
328 bsendmsg(ua, _("%-14s %-8s %-18s %-18s %s\n"),
329 level_ptr, job_type_to_str(job->JobType), dt, job->hdr.name, mr.VolumeName);
331 db_close_database(jcr, jcr->db);
333 jcr->db = ua->db; /* restore ua db to jcr */
338 * Find all jobs to be run in roughly the
341 static void list_scheduled_jobs(UAContext *ua)
346 int level, num_jobs = 0;
347 bool hdr_printed = false;
349 Dmsg0(200, "enter find_runs()\n");
351 /* Loop through all jobs */
353 for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
354 for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
363 prt_runtime(ua, job, level, runtime, run->pool);
367 } /* end for loop over resources */
370 bsendmsg(ua, _("No Scheduled Jobs.\n"));
374 Dmsg0(200, "Leave find_runs()\n");
377 static void list_running_jobs(UAContext *ua)
382 char dt[MAX_TIME_LENGTH];
384 bool pool_mem = false;
387 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
388 if (jcr->JobId == 0) { /* this is us */
389 /* this is a console or other control job. We only show console
390 * jobs in the status output.
392 if (jcr->JobType == JT_CONSOLE) {
393 bstrftime(dt, sizeof(dt), jcr->start_time);
394 strcpy(dt+7, dt+9); /* cut century */
395 bsendmsg(ua, _("Console connected at %s\n"), dt);
399 free_locked_jcr(jcr);
403 bsendmsg(ua, _("No Running Jobs.\n"));
407 bsendmsg(ua, _("\nRunning Jobs:\n"));
408 bsendmsg(ua, _("Level JobId Job Status\n"));
409 bsendmsg(ua, _("====================================================================\n"));
410 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
411 if (jcr->JobId == 0) { /* this is us */
413 free_locked_jcr(jcr);
416 switch (jcr->JobStatus) {
418 msg = _("is waiting execution");
421 msg = _("is running");
424 msg = _("is blocked");
427 msg = _("has terminated");
429 case JS_ErrorTerminated:
430 msg = _("has erred");
433 msg = _("has errors");
436 msg = _("has a fatal error");
439 msg = _("has verify differences");
442 msg = _("has been canceled");
445 msg = (char *) get_pool_memory(PM_FNAME);
446 Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
450 msg = (char *) get_pool_memory(PM_FNAME);
451 Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
454 case JS_WaitStoreRes:
455 msg = _("is waiting on max Storage jobs");
457 case JS_WaitClientRes:
458 msg = _("is waiting on max Client jobs");
461 msg = _("is waiting on max Job jobs");
464 msg = _("is waiting on max total jobs");
466 case JS_WaitStartTime:
467 msg = _("is waiting for its start time");
469 case JS_WaitPriority:
470 msg = _("is waiting for higher priority jobs to finish");
474 msg = (char *) get_pool_memory(PM_FNAME);
475 Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
480 * Now report Storage daemon status code
482 switch (jcr->SDJobStatus) {
485 free_pool_memory(msg);
488 msg = _("is waiting for a mount request");
492 free_pool_memory(msg);
495 msg = _("is waiting for an appendable Volume");
499 msg = (char *) get_pool_memory(PM_FNAME);
502 Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
503 jcr->client->hdr.name, jcr->store->hdr.name);
506 switch (jcr->JobType) {
509 bstrncpy(level, " ", sizeof(level));
512 bstrncpy(level, level_to_str(jcr->JobLevel), sizeof(level));
517 bsendmsg(ua, _("%-4s %6d %-20s %s\n"),
524 free_pool_memory(msg);
527 free_locked_jcr(jcr);
534 static void list_terminated_jobs(UAContext *ua)
536 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
539 if (last_jobs->empty()) {
540 bsendmsg(ua, _("No Terminated Jobs.\n"));
543 lock_last_jobs_list();
544 struct s_last_job *je;
545 bsendmsg(ua, _("\nTerminated Jobs:\n"));
546 bsendmsg(ua, _(" JobId Level Files Bytes Status Finished Name \n"));
547 bsendmsg(ua, _("======================================================================\n"));
548 for (je=NULL; (je=(s_last_job *)last_jobs->next(je)); ) {
549 char JobName[MAX_NAME_LENGTH];
552 bstrftime(dt, sizeof(dt), je->end_time);
553 strcpy(dt+7, dt+9); /* cut century */
554 switch (je->JobType) {
557 bstrncpy(level, " ", sizeof(level));
560 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
564 switch (je->JobStatus) {
566 termstat = "Created";
569 case JS_ErrorTerminated:
585 bstrncpy(JobName, je->Job, sizeof(JobName));
586 /* There are three periods after the Job name */
588 for (int i=0; i<3; i++) {
589 if ((p=strrchr(JobName, '.')) != NULL) {
593 bsendmsg(ua, _("%6d %-4s %8s %14s %-7s %-8s %s\n"),
596 edit_uint64_with_commas(je->JobFiles, b1),
597 edit_uint64_with_commas(je->JobBytes, b2),
602 unlock_last_jobs_list();