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"));
257 if (jcr->JobStatus == JS_WaitFD) {
258 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
259 job_type_to_str(jcr->JobType), jcr->Job);
261 if (jcr->dcr && jcr->dcr->device) {
262 bstrncpy(JobName, jcr->Job, sizeof(JobName));
263 /* There are three periods after the Job name */
265 for (int i=0; i<3; i++) {
266 if ((p=strrchr(JobName, '.')) != NULL) {
270 bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"),
271 job_level_to_str(jcr->JobLevel),
272 job_type_to_str(jcr->JobType),
275 jcr->dcr->VolumeName,
276 jcr->dcr->device->device_name);
277 sec = time(NULL) - jcr->run_time;
281 bps = jcr->JobBytes / sec;
282 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
283 edit_uint64_with_commas(jcr->JobFiles, b1),
284 edit_uint64_with_commas(jcr->JobBytes, b2),
285 edit_uint64_with_commas(bps, b3));
288 if (jcr->file_bsock) {
289 bnet_fsend(user, " FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n",
290 edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
291 jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
292 jcr->file_bsock->fd);
294 bnet_fsend(user, " FDSocket closed\n");
298 free_locked_jcr(jcr);
302 bnet_fsend(user, _("No Jobs running.\n"));
304 bnet_fsend(user, "====\n");
307 static void list_terminated_jobs(void *arg)
309 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
311 struct s_last_job *je;
314 if (last_jobs->size() == 0) {
315 msg = _("No Terminated Jobs.\n");
316 sendit(msg, strlen(msg), arg);
319 lock_last_jobs_list();
320 msg = _("\nTerminated Jobs:\n");
321 sendit(msg, strlen(msg), arg);
322 msg = _(" JobId Level Files Bytes Status Finished Name \n");
323 sendit(msg, strlen(msg), arg);
324 msg = _("======================================================================\n");
325 sendit(msg, strlen(msg), arg);
326 foreach_dlist(je, last_jobs) {
327 char JobName[MAX_NAME_LENGTH];
328 const char *termstat;
331 bstrftime_nc(dt, sizeof(dt), je->end_time);
332 switch (je->JobType) {
335 bstrncpy(level, " ", sizeof(level));
338 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
342 switch (je->JobStatus) {
344 termstat = "Created";
347 case JS_ErrorTerminated:
363 bstrncpy(JobName, je->Job, sizeof(JobName));
364 /* There are three periods after the Job name */
366 for (int i=0; i<3; i++) {
367 if ((p=strrchr(JobName, '.')) != NULL) {
371 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
374 edit_uint64_with_commas(je->JobFiles, b1),
375 edit_uint64_with_commas(je->JobBytes, b2),
378 sendit(buf, strlen(buf), arg);
380 sendit("====\n", 5, arg);
381 unlock_last_jobs_list();
385 * Convert Job Level into a string
387 static const char *level_to_str(int level)
398 str = _("Incremental");
401 str = _("Differential");
406 case L_VERIFY_CATALOG:
407 str = _("Verify Catalog");
410 str = _("Init Catalog");
412 case L_VERIFY_VOLUME_TO_CATALOG:
413 str = _("Volume to Catalog");
415 case L_VERIFY_DISK_TO_CATALOG:
416 str = _("Disk to Catalog");
425 str = _("Unknown Job Level");
434 static void sendit(const char *msg, int len, void *arg)
436 BSOCK *user = (BSOCK *)arg;
438 memcpy(user->msg, msg, len+1);
439 user->msglen = len+1;
444 * .status command from Director
446 bool qstatus_cmd(JCR *jcr)
448 BSOCK *dir = jcr->dir_bsock;
453 if (sscanf(dir->msg, qstatus, time.c_str()) != 1) {
454 pm_strcpy(jcr->errmsg, dir->msg);
455 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
456 bnet_fsend(dir, "3900 Bad .status command, missing argument.\n");
457 bnet_sig(dir, BNET_EOD);
462 if (strcmp(time.c_str(), "current") == 0) {
463 bnet_fsend(dir, OKqstatus, time.c_str());
466 if (njcr->JobId != 0) {
467 bnet_fsend(dir, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
469 free_locked_jcr(njcr);
473 else if (strcmp(time.c_str(), "last") == 0) {
474 bnet_fsend(dir, OKqstatus, time.c_str());
475 if ((last_jobs) && (last_jobs->size() > 0)) {
476 job = (s_last_job*)last_jobs->last();
477 bnet_fsend(dir, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
481 pm_strcpy(jcr->errmsg, dir->msg);
482 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
483 bnet_fsend(dir, "3900 Bad .status command, wrong argument.\n");
484 bnet_sig(dir, BNET_EOD);
487 bnet_sig(dir, BNET_EOD);