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_name(dev), dev->VolHdr.VolName);
106 bnet_fsend(user, _("Device \"%s\" open but no Bacula volume is mounted.\n"), dev_name(dev));
108 send_blocked_status(jcr, dev);
109 if (dev->can_append()) {
110 bpb = dev->VolCatInfo.VolCatBlocks;
114 bpb = dev->VolCatInfo.VolCatBytes / bpb;
115 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
116 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
117 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
118 edit_uint64_with_commas(bpb, b3));
119 } else { /* reading */
120 bpb = dev->VolCatInfo.VolCatReads;
124 if (dev->VolCatInfo.VolCatRBytes > 0) {
125 bpb = dev->VolCatInfo.VolCatRBytes / bpb;
129 bnet_fsend(user, _(" Total Bytes Read=%s Blocks Read=%s Bytes/block=%s\n"),
130 edit_uint64_with_commas(dev->VolCatInfo.VolCatRBytes, b1),
131 edit_uint64_with_commas(dev->VolCatInfo.VolCatReads, b2),
132 edit_uint64_with_commas(bpb, b3));
134 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
135 edit_uint64_with_commas(dev->file, b1),
136 edit_uint64_with_commas(dev->block_num, b2));
139 bnet_fsend(user, _("Archive \"%s\" is not open or does not exist.\n"), device->hdr.name);
140 send_blocked_status(jcr, dev);
146 if (debug_level > 0) {
147 bnet_fsend(user, "====\n\n");
148 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
150 bnet_fsend(user, "====\n\n");
153 list_spool_stats(user);
155 bnet_sig(user, BNET_EOD);
159 static void send_blocked_status(JCR *jcr, DEVICE *dev)
161 BSOCK *user = jcr->dir_bsock;
167 switch (dev->dev_blocked) {
169 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
171 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
172 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
174 case BST_WAITING_FOR_SYSOP:
175 if (jcr->JobStatus == JS_WaitMount) {
176 bnet_fsend(user, _(" Device is BLOCKED waiting for mount of volume \"%s\".\n"),
179 bnet_fsend(user, _(" Device is BLOCKED waiting for appendable media.\n"));
182 case BST_DOING_ACQUIRE:
183 bnet_fsend(user, _(" Device is being initialized.\n"));
185 case BST_WRITING_LABEL:
186 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
191 if (debug_level > 1) {
192 bnet_fsend(user, _("Configured device capabilities:\n"));
193 bnet_fsend(user, "%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
194 bnet_fsend(user, "%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
195 bnet_fsend(user, "%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
196 bnet_fsend(user, "%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
197 bnet_fsend(user, "%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
198 bnet_fsend(user, "%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
199 bnet_fsend(user, "%sREM ", dev->capabilities & CAP_REM ? "" : "!");
200 bnet_fsend(user, "%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
201 bnet_fsend(user, "%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
202 bnet_fsend(user, "%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
203 bnet_fsend(user, "%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
204 bnet_fsend(user, "%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
205 bnet_fsend(user, "\n");
207 bnet_fsend(user, _("Device status:\n"));
208 bnet_fsend(user, "%sOPENED ", dev->is_open() ? "" : "!");
209 bnet_fsend(user, "%sTAPE ", dev->is_tape() ? "" : "!");
210 bnet_fsend(user, "%sLABEL ", dev->is_labeled() ? "" : "!");
211 bnet_fsend(user, "%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
212 bnet_fsend(user, "%sAPPEND ", dev->can_append() ? "" : "!");
213 bnet_fsend(user, "%sREAD ", dev->can_read() ? "" : "!");
214 bnet_fsend(user, "%sEOT ", dev->at_eot() ? "" : "!");
215 bnet_fsend(user, "%sWEOT ", dev->state & ST_WEOT ? "" : "!");
216 bnet_fsend(user, "%sEOF ", dev->at_eof() ? "" : "!");
217 bnet_fsend(user, "%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
218 bnet_fsend(user, "%sSHORT ", dev->state & ST_SHORT ? "" : "!");
219 bnet_fsend(user, "%sMOUNTED ", dev->state & ST_MOUNTED ? "" : "!");
220 bnet_fsend(user, "\n");
222 bnet_fsend(user, _("Device parameters:\n"));
223 bnet_fsend(user, "Archive name: %s Device name: %s\n", dev->dev_name,
224 dev->device->hdr.name);
225 bnet_fsend(user, "File=%u block=%u\n", dev->file, dev->block_num);
226 bnet_fsend(user, "Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
231 static void list_running_jobs(BSOCK *user)
236 char JobName[MAX_NAME_LENGTH];
237 char b1[30], b2[30], b3[30];
239 bnet_fsend(user, _("\nRunning Jobs:\n"));
242 if (jcr->JobStatus == JS_WaitFD) {
243 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
244 job_type_to_str(jcr->JobType), jcr->Job);
246 if (jcr->dcr && jcr->dcr->device) {
247 bstrncpy(JobName, jcr->Job, sizeof(JobName));
248 /* There are three periods after the Job name */
250 for (int i=0; i<3; i++) {
251 if ((p=strrchr(JobName, '.')) != NULL) {
255 bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"),
256 job_level_to_str(jcr->JobLevel),
257 job_type_to_str(jcr->JobType),
260 jcr->dcr->VolumeName,
261 jcr->dcr->device->device_name);
262 sec = time(NULL) - jcr->run_time;
266 bps = jcr->JobBytes / sec;
267 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
268 edit_uint64_with_commas(jcr->JobFiles, b1),
269 edit_uint64_with_commas(jcr->JobBytes, b2),
270 edit_uint64_with_commas(bps, b3));
273 if (jcr->file_bsock) {
274 bnet_fsend(user, " FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n",
275 edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
276 jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
277 jcr->file_bsock->fd);
279 bnet_fsend(user, " FDSocket closed\n");
283 free_locked_jcr(jcr);
287 bnet_fsend(user, _("No Jobs running.\n"));
289 bnet_fsend(user, "====\n");
292 static void list_terminated_jobs(void *arg)
294 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
296 struct s_last_job *je;
299 if (last_jobs->size() == 0) {
300 msg = _("No Terminated Jobs.\n");
301 sendit(msg, strlen(msg), arg);
304 lock_last_jobs_list();
305 msg = _("\nTerminated Jobs:\n");
306 sendit(msg, strlen(msg), arg);
307 msg = _(" JobId Level Files Bytes Status Finished Name \n");
308 sendit(msg, strlen(msg), arg);
309 msg = _("======================================================================\n");
310 sendit(msg, strlen(msg), arg);
311 foreach_dlist(je, last_jobs) {
312 char JobName[MAX_NAME_LENGTH];
313 const char *termstat;
316 bstrftime_nc(dt, sizeof(dt), je->end_time);
317 switch (je->JobType) {
320 bstrncpy(level, " ", sizeof(level));
323 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
327 switch (je->JobStatus) {
329 termstat = "Created";
332 case JS_ErrorTerminated:
348 bstrncpy(JobName, je->Job, sizeof(JobName));
349 /* There are three periods after the Job name */
351 for (int i=0; i<3; i++) {
352 if ((p=strrchr(JobName, '.')) != NULL) {
356 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
359 edit_uint64_with_commas(je->JobFiles, b1),
360 edit_uint64_with_commas(je->JobBytes, b2),
363 sendit(buf, strlen(buf), arg);
365 sendit("====\n", 5, arg);
366 unlock_last_jobs_list();
370 * Convert Job Level into a string
372 static const char *level_to_str(int level)
383 str = _("Incremental");
386 str = _("Differential");
391 case L_VERIFY_CATALOG:
392 str = _("Verify Catalog");
395 str = _("Init Catalog");
397 case L_VERIFY_VOLUME_TO_CATALOG:
398 str = _("Volume to Catalog");
400 case L_VERIFY_DISK_TO_CATALOG:
401 str = _("Disk to Catalog");
410 str = _("Unknown Job Level");
419 static void sendit(const char *msg, int len, void *arg)
421 BSOCK *user = (BSOCK *)arg;
423 memcpy(user->msg, msg, len+1);
424 user->msglen = len+1;
429 * .status command from Director
431 bool qstatus_cmd(JCR *jcr)
433 BSOCK *dir = jcr->dir_bsock;
438 if (sscanf(dir->msg, qstatus, time.c_str()) != 1) {
439 pm_strcpy(jcr->errmsg, dir->msg);
440 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
441 bnet_fsend(dir, "3900 Bad .status command, missing argument.\n");
442 bnet_sig(dir, BNET_EOD);
447 if (strcmp(time.c_str(), "current") == 0) {
448 bnet_fsend(dir, OKqstatus, time.c_str());
451 if (njcr->JobId != 0) {
452 bnet_fsend(dir, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
454 free_locked_jcr(njcr);
458 else if (strcmp(time.c_str(), "last") == 0) {
459 bnet_fsend(dir, OKqstatus, time.c_str());
460 if ((last_jobs) && (last_jobs->size() > 0)) {
461 job = (s_last_job*)last_jobs->last();
462 bnet_fsend(dir, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
466 pm_strcpy(jcr->errmsg, dir->msg);
467 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
468 bnet_fsend(dir, "3900 Bad .status command, wrong argument.\n");
469 bnet_sig(dir, BNET_EOD);
472 bnet_sig(dir, BNET_EOD);