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)
66 BSOCK *user = jcr->dir_bsock;
67 char dt[MAX_TIME_LENGTH];
68 char b1[30], b2[30], b3[30];
71 bnet_fsend(user, "\n%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
72 HOST_OS, DISTNAME, DISTVER);
73 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
74 bnet_fsend(user, _("Daemon started %s, %d Job%s run since started.\n"), dt, num_jobs_run,
75 num_jobs_run == 1 ? "" : "s");
76 if (debug_level > 0) {
77 char b1[35], b2[35], b3[35], b4[35];
78 bnet_fsend(user, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
79 edit_uint64_with_commas(sm_bytes, b1),
80 edit_uint64_with_commas(sm_max_bytes, b2),
81 edit_uint64_with_commas(sm_buffers, b3),
82 edit_uint64_with_commas(sm_max_buffers, b4));
88 list_running_jobs(user);
91 * List terminated jobs
93 list_terminated_jobs(user);
98 bnet_fsend(user, _("\nDevice status:\n"));
100 foreach_res(changer, R_AUTOCHANGER) {
101 bnet_fsend(user, _("Autochanger \"%s\" with devices:\n"),
103 foreach_alist(device, changer->device) {
105 bnet_fsend(user, " %s\n", device->dev->print_name());
107 bnet_fsend(user, " %s\n", device->hdr.name);
111 foreach_res(device, R_DEVICE) {
113 if (dev && dev->is_open()) {
114 if (dev->is_labeled()) {
115 bnet_fsend(user, _("Device %s is mounted with Volume \"%s\"\n"),
116 dev->print_name(), dev->VolHdr.VolName);
118 bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"),
121 send_blocked_status(jcr, dev);
122 if (dev->can_append()) {
123 bpb = dev->VolCatInfo.VolCatBlocks;
127 bpb = dev->VolCatInfo.VolCatBytes / bpb;
128 bnet_fsend(user, _(" Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
129 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
130 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
131 edit_uint64_with_commas(bpb, b3));
132 } else { /* reading */
133 bpb = dev->VolCatInfo.VolCatReads;
137 if (dev->VolCatInfo.VolCatRBytes > 0) {
138 bpb = dev->VolCatInfo.VolCatRBytes / bpb;
142 bnet_fsend(user, _(" Total Bytes Read=%s Blocks Read=%s Bytes/block=%s\n"),
143 edit_uint64_with_commas(dev->VolCatInfo.VolCatRBytes, b1),
144 edit_uint64_with_commas(dev->VolCatInfo.VolCatReads, b2),
145 edit_uint64_with_commas(bpb, b3));
147 bnet_fsend(user, _(" Positioned at File=%s Block=%s\n"),
148 edit_uint64_with_commas(dev->file, b1),
149 edit_uint64_with_commas(dev->block_num, b2));
152 bnet_fsend(user, _("Archive \"%s\" is not open or does not exist.\n"), device->hdr.name);
153 send_blocked_status(jcr, dev);
159 if (debug_level > 0) {
160 bnet_fsend(user, "====\n\n");
161 dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
163 bnet_fsend(user, "====\n\n");
166 list_spool_stats(user);
168 bnet_sig(user, BNET_EOD);
172 static void send_blocked_status(JCR *jcr, DEVICE *dev)
174 BSOCK *user = jcr->dir_bsock;
180 switch (dev->dev_blocked) {
182 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
184 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
185 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
187 case BST_WAITING_FOR_SYSOP:
188 if (jcr->JobStatus == JS_WaitMount) {
189 bnet_fsend(user, _(" Device is BLOCKED waiting for mount of volume \"%s\".\n"),
192 bnet_fsend(user, _(" Device is BLOCKED waiting for media.\n"));
195 case BST_DOING_ACQUIRE:
196 bnet_fsend(user, _(" Device is being initialized.\n"));
198 case BST_WRITING_LABEL:
199 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
204 if (debug_level > 1) {
205 bnet_fsend(user, _("Configured device capabilities:\n"));
206 bnet_fsend(user, "%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
207 bnet_fsend(user, "%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
208 bnet_fsend(user, "%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
209 bnet_fsend(user, "%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
210 bnet_fsend(user, "%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
211 bnet_fsend(user, "%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
212 bnet_fsend(user, "%sREM ", dev->capabilities & CAP_REM ? "" : "!");
213 bnet_fsend(user, "%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
214 bnet_fsend(user, "%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
215 bnet_fsend(user, "%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
216 bnet_fsend(user, "%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
217 bnet_fsend(user, "%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
218 bnet_fsend(user, "\n");
220 bnet_fsend(user, _("Device status:\n"));
221 bnet_fsend(user, "%sOPENED ", dev->is_open() ? "" : "!");
222 bnet_fsend(user, "%sTAPE ", dev->is_tape() ? "" : "!");
223 bnet_fsend(user, "%sLABEL ", dev->is_labeled() ? "" : "!");
224 bnet_fsend(user, "%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
225 bnet_fsend(user, "%sAPPEND ", dev->can_append() ? "" : "!");
226 bnet_fsend(user, "%sREAD ", dev->can_read() ? "" : "!");
227 bnet_fsend(user, "%sEOT ", dev->at_eot() ? "" : "!");
228 bnet_fsend(user, "%sWEOT ", dev->state & ST_WEOT ? "" : "!");
229 bnet_fsend(user, "%sEOF ", dev->at_eof() ? "" : "!");
230 bnet_fsend(user, "%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
231 bnet_fsend(user, "%sSHORT ", dev->state & ST_SHORT ? "" : "!");
232 bnet_fsend(user, "%sMOUNTED ", dev->state & ST_MOUNTED ? "" : "!");
233 bnet_fsend(user, "\n");
234 bnet_fsend(user, "num_writers=%d JobStatus=%c block=%d\nn", dev->num_writers,
235 jcr->JobStatus, dev->dev_blocked);
237 bnet_fsend(user, _("Device parameters:\n"));
238 bnet_fsend(user, "Archive name: %s Device name: %s\n", dev->archive_name(),
240 bnet_fsend(user, "File=%u block=%u\n", dev->file, dev->block_num);
241 bnet_fsend(user, "Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
246 static void list_running_jobs(BSOCK *user)
251 char JobName[MAX_NAME_LENGTH];
252 char b1[30], b2[30], b3[30];
254 bnet_fsend(user, _("\nRunning Jobs:\n"));
256 if (jcr->JobStatus == JS_WaitFD) {
257 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
258 job_type_to_str(jcr->JobType), jcr->Job);
260 if (jcr->dcr && jcr->dcr->device) {
261 bstrncpy(JobName, jcr->Job, sizeof(JobName));
262 /* There are three periods after the Job name */
264 for (int i=0; i<3; i++) {
265 if ((p=strrchr(JobName, '.')) != NULL) {
269 bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"),
270 job_level_to_str(jcr->JobLevel),
271 job_type_to_str(jcr->JobType),
274 jcr->dcr->VolumeName,
275 jcr->dcr->device->device_name);
276 sec = time(NULL) - jcr->run_time;
280 bps = jcr->JobBytes / sec;
281 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
282 edit_uint64_with_commas(jcr->JobFiles, b1),
283 edit_uint64_with_commas(jcr->JobBytes, b2),
284 edit_uint64_with_commas(bps, b3));
287 if (jcr->file_bsock) {
288 bnet_fsend(user, " FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n",
289 edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
290 jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
291 jcr->file_bsock->fd);
293 bnet_fsend(user, " FDSocket closed\n");
300 bnet_fsend(user, _("No Jobs running.\n"));
302 bnet_fsend(user, "====\n");
305 static void list_terminated_jobs(void *arg)
307 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
309 struct s_last_job *je;
312 if (last_jobs->size() == 0) {
313 msg = _("No Terminated Jobs.\n");
314 sendit(msg, strlen(msg), arg);
317 lock_last_jobs_list();
318 msg = _("\nTerminated Jobs:\n");
319 sendit(msg, strlen(msg), arg);
320 msg = _(" JobId Level Files Bytes Status Finished Name \n");
321 sendit(msg, strlen(msg), arg);
322 msg = _("======================================================================\n");
323 sendit(msg, strlen(msg), arg);
324 foreach_dlist(je, last_jobs) {
325 char JobName[MAX_NAME_LENGTH];
326 const char *termstat;
329 bstrftime_nc(dt, sizeof(dt), je->end_time);
330 switch (je->JobType) {
333 bstrncpy(level, " ", sizeof(level));
336 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
340 switch (je->JobStatus) {
342 termstat = "Created";
345 case JS_ErrorTerminated:
361 bstrncpy(JobName, je->Job, sizeof(JobName));
362 /* There are three periods after the Job name */
364 for (int i=0; i<3; i++) {
365 if ((p=strrchr(JobName, '.')) != NULL) {
369 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
372 edit_uint64_with_commas(je->JobFiles, b1),
373 edit_uint64_with_commas(je->JobBytes, b2),
376 sendit(buf, strlen(buf), arg);
378 sendit("====\n", 5, arg);
379 unlock_last_jobs_list();
383 * Convert Job Level into a string
385 static const char *level_to_str(int level)
396 str = _("Incremental");
399 str = _("Differential");
404 case L_VERIFY_CATALOG:
405 str = _("Verify Catalog");
408 str = _("Init Catalog");
410 case L_VERIFY_VOLUME_TO_CATALOG:
411 str = _("Volume to Catalog");
413 case L_VERIFY_DISK_TO_CATALOG:
414 str = _("Disk to Catalog");
423 str = _("Unknown Job Level");
432 static void sendit(const char *msg, int len, void *arg)
434 BSOCK *user = (BSOCK *)arg;
436 memcpy(user->msg, msg, len+1);
437 user->msglen = len+1;
442 * .status command from Director
444 bool qstatus_cmd(JCR *jcr)
446 BSOCK *dir = jcr->dir_bsock;
451 if (sscanf(dir->msg, qstatus, time.c_str()) != 1) {
452 pm_strcpy(jcr->errmsg, dir->msg);
453 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
454 bnet_fsend(dir, "3900 Bad .status command, missing argument.\n");
455 bnet_sig(dir, BNET_EOD);
460 if (strcmp(time.c_str(), "current") == 0) {
461 bnet_fsend(dir, OKqstatus, time.c_str());
463 if (njcr->JobId != 0) {
464 bnet_fsend(dir, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
469 else if (strcmp(time.c_str(), "last") == 0) {
470 bnet_fsend(dir, OKqstatus, time.c_str());
471 if ((last_jobs) && (last_jobs->size() > 0)) {
472 job = (s_last_job*)last_jobs->last();
473 bnet_fsend(dir, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
477 pm_strcpy(jcr->errmsg, dir->msg);
478 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
479 bnet_fsend(dir, "3900 Bad .status command, wrong argument.\n");
480 bnet_sig(dir, BNET_EOD);
483 bnet_sig(dir, BNET_EOD);