3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
32 extern char my_name[];
33 extern time_t daemon_start_time;
34 extern struct s_last_job last_job;
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);
45 int statuscmd(UAContext *ua, char *cmd)
54 Dmsg1(20, "status:%s:\n", cmd);
56 for (i=1; i<ua->argc; i++) {
57 if (strcasecmp(ua->argk[i], _("all")) == 0) {
58 do_all_status(ua, cmd);
60 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
61 strcasecmp(ua->argk[i], _("director")) == 0) {
62 do_director_status(ua, cmd);
64 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
65 client = get_client_resource(ua);
67 do_client_status(ua, client);
71 store = get_storage_resource(ua, cmd);
73 do_storage_status(ua, store);
78 /* If no args, ask for status type */
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) {
89 Dmsg1(20, "item=%d\n", item);
91 case 0: /* Director */
92 do_director_status(ua, cmd);
95 store = select_storage_resource(ua);
97 do_storage_status(ua, store);
101 client = select_client_resource(ua);
103 do_client_status(ua, client);
107 do_all_status(ua, cmd);
116 static void do_all_status(UAContext *ua, char *cmd)
118 STORE *store, **unique_store;
119 CLIENT *client, **unique_client;
122 do_director_status(ua, cmd);
124 /* Count Storage items */
127 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
129 unique_store = (STORE **) malloc(i * sizeof(STORE));
130 /* Find Unique Storage address/port */
131 store = (STORE *)GetNextRes(R_STORAGE, NULL);
133 unique_store[i++] = store;
134 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
136 for (j=0; j<i; j++) {
137 if (strcmp(unique_store[j]->address, store->address) == 0 &&
138 unique_store[j]->SDport == store->SDport) {
144 unique_store[i++] = store;
145 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
150 /* Call each unique Storage daemon */
151 for (j=0; j<i; j++) {
152 do_storage_status(ua, unique_store[j]);
156 /* Count Client items */
159 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
161 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
162 /* Find Unique Client address/port */
163 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
165 unique_client[i++] = client;
166 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
168 for (j=0; j<i; j++) {
169 if (strcmp(unique_client[j]->address, client->address) == 0 &&
170 unique_client[j]->FDport == client->FDport) {
176 unique_client[i++] = client;
177 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
182 /* Call each unique File daemon */
183 for (j=0; j<i; j++) {
184 do_client_status(ua, unique_client[j]);
190 static void do_director_status(UAContext *ua, char *cmd)
195 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
196 int pool_mem = FALSE;
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];
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) {
212 case JS_ErrorTerminated:
213 termstat = _("Error");
216 jstat[0] = last_job.JobStatus;
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),
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);
236 switch (jcr->JobStatus) {
238 msg = _("is waiting execution");
241 msg = _("is running");
244 msg = _("is blocked");
247 msg = _("has terminated");
249 case JS_ErrorTerminated:
250 msg = _("has erred");
253 msg = _("has been canceled");
256 msg = (char *) get_pool_memory(PM_FNAME);
257 Mmsg(&msg, _("is waiting on Client %s"), jcr->client->hdr.name);
261 msg = (char *) get_pool_memory(PM_FNAME);
262 Mmsg(&msg, _("is waiting on Storage %s"), jcr->store->hdr.name);
266 msg = (char *) get_pool_memory(PM_FNAME);
267 Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
271 switch (jcr->SDJobStatus) {
274 free_pool_memory(msg);
277 msg = _("is waiting for a mount request");
281 free_pool_memory(msg);
284 msg = _("is waiting for an appendable Volume");
288 msg = (char *) get_pool_memory(PM_FNAME);
291 Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
292 jcr->client->hdr.name, jcr->store->hdr.name);
296 bsendmsg(ua, _("JobId %d Job %s %s.\n"), jcr->JobId, jcr->Job, msg);
298 free_pool_memory(msg);
301 free_locked_jcr(jcr);
306 bsendmsg(ua, _("No jobs are running.\n"));
308 print_jobs_scheduled(ua);
309 bsendmsg(ua, "====\n");
312 static void do_storage_status(UAContext *ua, STORE *store)
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"),
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);
331 bnet_sig(sd, BNET_TERMINATE);
333 ua->jcr->store_bsock = NULL;
337 static void do_client_status(UAContext *ua, CLIENT *client)
341 /* Connect to File daemon */
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"),
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);
358 bnet_sig(fd, BNET_TERMINATE);
360 ua->jcr->file_bsock = NULL;
365 static void prt_runtime(UAContext *ua, JOB *job, time_t runtime)
367 char dt[MAX_TIME_LENGTH], *type;
369 bstrftime(dt, sizeof(dt), runtime);
370 switch (job->JobType) {
381 type = _("Unknown type of");
384 bsendmsg(ua, _("%s job \"%s\" scheduled for %s\n"), type, job->hdr.name, dt);
388 * Find all jobs to be run this hour
391 static void print_jobs_scheduled(UAContext *ua)
393 time_t now, runtime, tomorrow;
398 int mday, wday, month, tmday, twday, tmonth, i, hour;
402 Dmsg0(200, "enter find_runs()\n");
405 localtime_r(&now, &tm);
406 mday = tm.tm_mday - 1;
410 tomorrow = now + 60 * 60 * 24;
411 localtime_r(&tomorrow, &tm);
412 tmday = tm.tm_mday - 1;
416 /* Loop through all jobs */
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 */
423 for (run=sched->run; run; run=run->next) {
425 * Find runs in next 24 hours
427 tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) &&
428 bit_is_set(month, run->month);
430 tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
431 bit_is_set(tmonth, run->month);
433 Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
436 /* find time (time_t) job is to be run */
437 localtime_r(&now, &tm);
439 for (i=tm.tm_hour; i < 24; i++) {
440 if (bit_is_set(i, run->hour)) {
442 tm.tm_min = run->minute;
444 runtime = mktime(&tm);
446 prt_runtime(ua, job, runtime);
454 Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
456 localtime_r(&tomorrow, &tm);
458 for (i=0; i < 24; i++) {
459 if (bit_is_set(i, run->hour)) {
465 tm.tm_min = run->minute;
467 runtime = mktime(&tm);
468 Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
469 if (runtime < tomorrow) {
470 prt_runtime(ua, job, runtime);
476 Dmsg0(200, "Leave find_runs()\n");