3 * Bacula Director -- User Agent Status Command
5 * Kern Sibbald, August MMI
11 Copyright (C) 2000, 2001, 2002 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,
34 extern char my_name[];
35 extern time_t daemon_start_time;
36 extern struct s_last_job last_job;
38 static void print_jobs_scheduled(UAContext *ua);
39 static void do_storage_status(UAContext *ua, STORE *store);
40 static void do_client_status(UAContext *ua, CLIENT *client);
41 static void do_director_status(UAContext *ua, char *cmd);
42 static void do_all_status(UAContext *ua, char *cmd);
47 int statuscmd(UAContext *ua, char *cmd)
56 Dmsg1(20, "status:%s:\n", cmd);
58 for (i=1; i<ua->argc; i++) {
59 if (strcasecmp(ua->argk[i], _("all")) == 0) {
60 do_all_status(ua, cmd);
62 } else if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
63 strcasecmp(ua->argk[i], _("director")) == 0) {
64 do_director_status(ua, cmd);
66 } else if (strcasecmp(ua->argk[i], _("client")) == 0) {
67 client = get_client_resource(ua);
69 do_client_status(ua, client);
73 store = get_storage_resource(ua, cmd);
75 do_storage_status(ua, store);
80 /* If no args, ask for status type */
82 start_prompt(ua, _("Status available for:\n"));
83 add_prompt(ua, _("Director"));
84 add_prompt(ua, _("Storage"));
85 add_prompt(ua, _("Client"));
86 add_prompt(ua, _("All"));
87 Dmsg0(20, "do_prompt: select daemon\n");
88 if ((item=do_prompt(ua, _("Select daemon type for status"), cmd)) < 0) {
91 Dmsg1(20, "item=%d\n", item);
93 case 0: /* Director */
94 do_director_status(ua, cmd);
97 store = select_storage_resource(ua);
99 do_storage_status(ua, store);
103 client = select_client_resource(ua);
105 do_client_status(ua, client);
109 do_all_status(ua, cmd);
118 static void do_all_status(UAContext *ua, char *cmd)
120 STORE *store, **unique_store;
121 CLIENT *client, **unique_client;
124 do_director_status(ua, cmd);
126 /* Count Storage items */
129 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
131 unique_store = (STORE **) malloc(i * sizeof(STORE));
132 /* Find Unique Storage address/port */
133 store = (STORE *)GetNextRes(R_STORAGE, NULL);
135 unique_store[i++] = store;
136 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
138 for (j=0; j<i; j++) {
139 if (strcmp(unique_store[j]->address, store->address) == 0 &&
140 unique_store[j]->SDport == store->SDport) {
146 unique_store[i++] = store;
147 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
152 /* Call each unique Storage daemon */
153 for (j=0; j<i; j++) {
154 do_storage_status(ua, unique_store[j]);
158 /* Count Client items */
161 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
163 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
164 /* Find Unique Client address/port */
165 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
167 unique_client[i++] = client;
168 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
170 for (j=0; j<i; j++) {
171 if (strcmp(unique_client[j]->address, client->address) == 0 &&
172 unique_client[j]->FDport == client->FDport) {
178 unique_client[i++] = client;
179 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
184 /* Call each unique File daemon */
185 for (j=0; j<i; j++) {
186 do_client_status(ua, unique_client[j]);
192 static void do_director_status(UAContext *ua, char *cmd)
197 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
198 int pool_mem = FALSE;
200 Dmsg0(200, "Doing status\n");
201 bsendmsg(ua, "%s Version: " VERSION " (" DATE ")\n", my_name);
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);
256 msg = (char *) get_pool_memory(PM_FNAME);
257 Mmsg(&msg, _("is in unknown state %c"), jcr->JobStatus);
261 switch (jcr->SDJobStatus) {
264 free_pool_memory(msg);
267 msg = _("is waiting for a mount request");
271 free_pool_memory(msg);
274 msg = _("is waiting for an appendable Volume");
278 msg = (char *) get_pool_memory(PM_FNAME);
281 Mmsg(&msg, _("is waiting for Client %s to connect to Storage %s"),
282 jcr->client->hdr.name, jcr->store->hdr.name);
286 bsendmsg(ua, _("JobId %d Job %s %s.\n"), jcr->JobId, jcr->Job, msg);
288 free_pool_memory(msg);
291 free_locked_jcr(jcr);
296 bsendmsg(ua, _("No jobs are running.\n"));
298 print_jobs_scheduled(ua);
299 bsendmsg(ua, "====\n");
302 static void do_storage_status(UAContext *ua, STORE *store)
306 ua->jcr->store = store;
307 /* Try connecting for up to 15 seconds */
308 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
309 store->hdr.name, store->address, store->SDport);
310 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
311 bsendmsg(ua, _("\nFailed to connect to Storage daemon %s.\n====\n"),
315 Dmsg0(20, _("Connected to storage daemon\n"));
316 sd = ua->jcr->store_bsock;
317 bnet_fsend(sd, "status");
318 while (bnet_recv(sd) >= 0) {
319 bsendmsg(ua, "%s", sd->msg);
321 bnet_sig(sd, BNET_TERMINATE);
323 ua->jcr->store_bsock = NULL;
327 static void do_client_status(UAContext *ua, CLIENT *client)
331 /* Connect to File daemon */
333 ua->jcr->client = client;
334 /* Try to connect for 15 seconds */
335 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
336 client->hdr.name, client->address, client->FDport);
337 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
338 bsendmsg(ua, _("Failed to connect to Client %s.\n====\n"),
342 Dmsg0(20, _("Connected to file daemon\n"));
343 fd = ua->jcr->file_bsock;
344 bnet_fsend(fd, "status");
345 while (bnet_recv(fd) >= 0) {
346 bsendmsg(ua, "%s", fd->msg);
348 bnet_sig(fd, BNET_TERMINATE);
350 ua->jcr->file_bsock = NULL;
355 static void prt_runtime(UAContext *ua, JOB *job, time_t runtime)
357 char dt[MAX_TIME_LENGTH];
359 bstrftime(dt, sizeof(dt), runtime);
360 bsendmsg(ua, _("%s job \"%s\" scheduled for %s\n"),
361 job_type_to_str(job->JobType), job->hdr.name, dt);
365 * Find all jobs to be run this hour
368 static void print_jobs_scheduled(UAContext *ua)
370 time_t now, runtime, tomorrow;
375 int mday, wday, month, tmday, twday, tmonth, i, hour;
379 Dmsg0(200, "enter find_runs()\n");
382 localtime_r(&now, &tm);
383 mday = tm.tm_mday - 1;
387 tomorrow = now + 60 * 60 * 24;
388 localtime_r(&tomorrow, &tm);
389 tmday = tm.tm_mday - 1;
393 /* Loop through all jobs */
395 for (job=NULL; (job=(JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
396 sched = job->schedule;
397 if (sched == NULL) { /* scheduled? */
398 continue; /* no, skip this job */
400 for (run=sched->run; run; run=run->next) {
402 * Find runs in next 24 hours
404 tod = (bit_is_set(mday, run->mday) || bit_is_set(wday, run->wday)) &&
405 bit_is_set(month, run->month);
407 tom = (bit_is_set(tmday, run->mday) || bit_is_set(twday, run->wday)) &&
408 bit_is_set(tmonth, run->month);
410 Dmsg2(200, "tod=%d tom=%d\n", tod, tom);
413 /* find time (time_t) job is to be run */
414 localtime_r(&now, &tm);
416 for (i=tm.tm_hour; i < 24; i++) {
417 if (bit_is_set(i, run->hour)) {
419 tm.tm_min = run->minute;
421 runtime = mktime(&tm);
423 prt_runtime(ua, job, runtime);
431 // Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
433 localtime_r(&tomorrow, &tm);
435 for (i=0; i < 24; i++) {
436 if (bit_is_set(i, run->hour)) {
442 tm.tm_min = run->minute;
444 runtime = mktime(&tm);
445 Dmsg2(200, "truntime=%d now=%d\n", runtime, now);
446 if (runtime < tomorrow) {
447 prt_runtime(ua, job, runtime);
453 Dmsg0(200, "Leave find_runs()\n");