2 * This file handles the status command
4 * Kern Sibbald, May MMIII
10 Copyright (C) 2000-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Exported variables */
34 /* Imported variables */
35 extern BSOCK *filed_chan;
36 extern int r_first, r_last;
37 extern struct s_res resources[];
38 extern char my_name[];
39 extern time_t daemon_start_time;
40 extern int num_jobs_run;
43 /* Static variables */
44 static char qstatus[] = ".status %127s\n";
46 static char OKqstatus[] = "3000 OK .status\n";
47 static char DotStatusJob[] = "JobId=%d JobStatus=%c JobErrors=%d\n";
50 /* Forward referenced functions */
51 static void send_blocked_status(JCR *jcr, DEVICE *dev);
52 static void list_terminated_jobs(void *arg);
53 static void list_running_jobs(BSOCK *user);
54 static void sendit(const char *msg, int len, void *arg);
55 static const char *level_to_str(int level);
59 * Status command from Director
61 bool status_cmd(JCR *jcr)
65 BSOCK *user = jcr->dir_bsock;
66 char dt[MAX_TIME_LENGTH];
67 char b1[30], b2[30], b3[30];
70 bnet_fsend(user, "\n%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
71 HOST_OS, DISTNAME, DISTVER);
72 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
73 bnet_fsend(user, _("Daemon started %s, %d Job%s run since started.\n"), dt, num_jobs_run,
74 num_jobs_run == 1 ? "" : "s");
75 if (debug_level > 0) {
76 char b1[35], b2[35], b3[35], b4[35];
77 bnet_fsend(user, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
78 edit_uint64_with_commas(sm_bytes, b1),
79 edit_uint64_with_commas(sm_max_bytes, b2),
80 edit_uint64_with_commas(sm_buffers, b3),
81 edit_uint64_with_commas(sm_max_buffers, b4));
87 list_running_jobs(user);
90 * List terminated jobs
92 list_terminated_jobs(user);
97 bnet_fsend(user, _("\nDevice status:\n"));
99 foreach_res(device, R_DEVICE) {
101 if (dev && dev->is_open()) {
102 if (dev->is_labeled()) {
103 bnet_fsend(user, _("Device %s is mounted with Volume \"%s\"\n"),
104 dev->print_name(), dev->VolHdr.VolName);
106 bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"),
109 send_blocked_status(jcr, dev);
110 if (dev->can_append()) {
111 bpb = dev->VolCatInfo.VolCatBlocks;
115 bpb = dev->VolCatInfo.VolCatBytes / bpb;
116 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
117 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
118 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
119 edit_uint64_with_commas(bpb, b3));
120 } else { /* reading */
121 bpb = dev->VolCatInfo.VolCatReads;
125 if (dev->VolCatInfo.VolCatRBytes > 0) {
126 bpb = dev->VolCatInfo.VolCatRBytes / bpb;
130 bnet_fsend(user, _(" Total Bytes Read=%s Blocks Read=%s Bytes/block=%s\n"),
131 edit_uint64_with_commas(dev->VolCatInfo.VolCatRBytes, b1),
132 edit_uint64_with_commas(dev->VolCatInfo.VolCatReads, b2),
133 edit_uint64_with_commas(bpb, b3));
135 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
136 edit_uint64_with_commas(dev->file, b1),
137 edit_uint64_with_commas(dev->block_num, b2));
140 bnet_fsend(user, _("Archive \"%s\" is not open or does not exist.\n"), device->hdr.name);
141 send_blocked_status(jcr, dev);
147 if (debug_level > 0) {
148 bnet_fsend(user, "====\n\n");
149 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
151 bnet_fsend(user, "====\n\n");
154 list_spool_stats(user);
156 bnet_sig(user, BNET_EOD);
160 static void send_blocked_status(JCR *jcr, DEVICE *dev)
162 BSOCK *user = jcr->dir_bsock;
168 switch (dev->dev_blocked) {
170 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
172 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
173 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
175 case BST_WAITING_FOR_SYSOP:
176 if (jcr->JobStatus == JS_WaitMount) {
177 bnet_fsend(user, _(" Device is BLOCKED waiting for mount of volume \"%s\".\n"),
180 bnet_fsend(user, _(" Device is BLOCKED waiting for media.\n"));
183 case BST_DOING_ACQUIRE:
184 bnet_fsend(user, _(" Device is being initialized.\n"));
186 case BST_WRITING_LABEL:
187 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
192 if (debug_level > 1) {
193 bnet_fsend(user, _("Configured device capabilities:\n"));
194 bnet_fsend(user, "%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
195 bnet_fsend(user, "%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
196 bnet_fsend(user, "%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
197 bnet_fsend(user, "%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
198 bnet_fsend(user, "%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
199 bnet_fsend(user, "%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
200 bnet_fsend(user, "%sREM ", dev->capabilities & CAP_REM ? "" : "!");
201 bnet_fsend(user, "%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
202 bnet_fsend(user, "%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
203 bnet_fsend(user, "%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
204 bnet_fsend(user, "%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
205 bnet_fsend(user, "%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
206 bnet_fsend(user, "\n");
208 bnet_fsend(user, _("Device status:\n"));
209 bnet_fsend(user, "%sOPENED ", dev->is_open() ? "" : "!");
210 bnet_fsend(user, "%sTAPE ", dev->is_tape() ? "" : "!");
211 bnet_fsend(user, "%sLABEL ", dev->is_labeled() ? "" : "!");
212 bnet_fsend(user, "%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
213 bnet_fsend(user, "%sAPPEND ", dev->can_append() ? "" : "!");
214 bnet_fsend(user, "%sREAD ", dev->can_read() ? "" : "!");
215 bnet_fsend(user, "%sEOT ", dev->at_eot() ? "" : "!");
216 bnet_fsend(user, "%sWEOT ", dev->state & ST_WEOT ? "" : "!");
217 bnet_fsend(user, "%sEOF ", dev->at_eof() ? "" : "!");
218 bnet_fsend(user, "%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
219 bnet_fsend(user, "%sSHORT ", dev->state & ST_SHORT ? "" : "!");
220 bnet_fsend(user, "%sMOUNTED ", dev->state & ST_MOUNTED ? "" : "!");
221 bnet_fsend(user, "\n");
222 bnet_fsend(user, "num_writers=%d JobStatus=%c block=%d\nn", dev->num_writers,
223 jcr->JobStatus, dev->dev_blocked);
225 bnet_fsend(user, _("Device parameters:\n"));
226 bnet_fsend(user, "Archive name: %s Device name: %s\n", dev->archive_name(),
228 bnet_fsend(user, "File=%u block=%u\n", dev->file, dev->block_num);
229 bnet_fsend(user, "Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
234 static void list_running_jobs(BSOCK *user)
239 char JobName[MAX_NAME_LENGTH];
240 char b1[30], b2[30], b3[30];
242 bnet_fsend(user, _("\nRunning Jobs:\n"));
245 if (jcr->JobStatus == JS_WaitFD) {
246 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
247 job_type_to_str(jcr->JobType), jcr->Job);
249 if (jcr->dcr && jcr->dcr->device) {
250 bstrncpy(JobName, jcr->Job, sizeof(JobName));
251 /* There are three periods after the Job name */
253 for (int i=0; i<3; i++) {
254 if ((p=strrchr(JobName, '.')) != NULL) {
258 bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"),
259 job_level_to_str(jcr->JobLevel),
260 job_type_to_str(jcr->JobType),
263 jcr->dcr->VolumeName,
264 jcr->dcr->device->device_name);
265 sec = time(NULL) - jcr->run_time;
269 bps = jcr->JobBytes / sec;
270 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
271 edit_uint64_with_commas(jcr->JobFiles, b1),
272 edit_uint64_with_commas(jcr->JobBytes, b2),
273 edit_uint64_with_commas(bps, b3));
276 if (jcr->file_bsock) {
277 bnet_fsend(user, " FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n",
278 edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
279 jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
280 jcr->file_bsock->fd);
282 bnet_fsend(user, " FDSocket closed\n");
286 free_locked_jcr(jcr);
290 bnet_fsend(user, _("No Jobs running.\n"));
292 bnet_fsend(user, "====\n");
295 static void list_terminated_jobs(void *arg)
297 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
299 struct s_last_job *je;
302 if (last_jobs->size() == 0) {
303 msg = _("No Terminated Jobs.\n");
304 sendit(msg, strlen(msg), arg);
307 lock_last_jobs_list();
308 msg = _("\nTerminated Jobs:\n");
309 sendit(msg, strlen(msg), arg);
310 msg = _(" JobId Level Files Bytes Status Finished Name \n");
311 sendit(msg, strlen(msg), arg);
312 msg = _("======================================================================\n");
313 sendit(msg, strlen(msg), arg);
314 foreach_dlist(je, last_jobs) {
315 char JobName[MAX_NAME_LENGTH];
316 const char *termstat;
319 bstrftime_nc(dt, sizeof(dt), je->end_time);
320 switch (je->JobType) {
323 bstrncpy(level, " ", sizeof(level));
326 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
330 switch (je->JobStatus) {
332 termstat = "Created";
335 case JS_ErrorTerminated:
351 bstrncpy(JobName, je->Job, sizeof(JobName));
352 /* There are three periods after the Job name */
354 for (int i=0; i<3; i++) {
355 if ((p=strrchr(JobName, '.')) != NULL) {
359 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
362 edit_uint64_with_commas(je->JobFiles, b1),
363 edit_uint64_with_commas(je->JobBytes, b2),
366 sendit(buf, strlen(buf), arg);
368 sendit("====\n", 5, arg);
369 unlock_last_jobs_list();
373 * Convert Job Level into a string
375 static const char *level_to_str(int level)
386 str = _("Incremental");
389 str = _("Differential");
394 case L_VERIFY_CATALOG:
395 str = _("Verify Catalog");
398 str = _("Init Catalog");
400 case L_VERIFY_VOLUME_TO_CATALOG:
401 str = _("Volume to Catalog");
403 case L_VERIFY_DISK_TO_CATALOG:
404 str = _("Disk to Catalog");
413 str = _("Unknown Job Level");
422 static void sendit(const char *msg, int len, void *arg)
424 BSOCK *user = (BSOCK *)arg;
426 memcpy(user->msg, msg, len+1);
427 user->msglen = len+1;
432 * .status command from Director
434 bool qstatus_cmd(JCR *jcr)
436 BSOCK *dir = jcr->dir_bsock;
441 if (sscanf(dir->msg, qstatus, time.c_str()) != 1) {
442 pm_strcpy(jcr->errmsg, dir->msg);
443 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
444 bnet_fsend(dir, "3900 Bad .status command, missing argument.\n");
445 bnet_sig(dir, BNET_EOD);
450 if (strcmp(time.c_str(), "current") == 0) {
451 bnet_fsend(dir, OKqstatus, time.c_str());
454 if (njcr->JobId != 0) {
455 bnet_fsend(dir, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
457 free_locked_jcr(njcr);
461 else if (strcmp(time.c_str(), "last") == 0) {
462 bnet_fsend(dir, OKqstatus, time.c_str());
463 if ((last_jobs) && (last_jobs->size() > 0)) {
464 job = (s_last_job*)last_jobs->last();
465 bnet_fsend(dir, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
469 pm_strcpy(jcr->errmsg, dir->msg);
470 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
471 bnet_fsend(dir, "3900 Bad .status command, wrong argument.\n");
472 bnet_sig(dir, BNET_EOD);
475 bnet_sig(dir, BNET_EOD);