2 * This file handles the status command
4 * Kern Sibbald, May MMIII
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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 %s\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 int 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) {
100 for (dev=device->dev; dev; dev=dev->next) {
101 if (dev_state(dev, ST_OPENED)) {
102 if (dev_state(dev, ST_LABEL)) {
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_state(dev, ST_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, _("Device \"%s\" is not open.\n"), dev_name(dev));
140 send_blocked_status(jcr, dev);
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");
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;
163 switch (dev->dev_blocked) {
165 bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
167 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
168 bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
170 case BST_WAITING_FOR_SYSOP:
171 if (jcr->JobStatus == JS_WaitMount) {
172 bnet_fsend(user, _(" Device is BLOCKED waiting for mount of volume \"%s\".\n"),
175 bnet_fsend(user, _(" Device is BLOCKED waiting for appendable media.\n"));
178 case BST_DOING_ACQUIRE:
179 bnet_fsend(user, _(" Device is being initialized.\n"));
181 case BST_WRITING_LABEL:
182 bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
187 if (debug_level > 1) {
188 bnet_fsend(user, _("Configured device capabilities:\n"));
189 bnet_fsend(user, "%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
190 bnet_fsend(user, "%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
191 bnet_fsend(user, "%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
192 bnet_fsend(user, "%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
193 bnet_fsend(user, "%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
194 bnet_fsend(user, "%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
195 bnet_fsend(user, "%sREM ", dev->capabilities & CAP_REM ? "" : "!");
196 bnet_fsend(user, "%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
197 bnet_fsend(user, "%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
198 bnet_fsend(user, "%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
199 bnet_fsend(user, "%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
200 bnet_fsend(user, "%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
201 bnet_fsend(user, "\n");
203 bnet_fsend(user, _("Device status:\n"));
204 bnet_fsend(user, "%sOPENED ", dev->state & ST_OPENED ? "" : "!");
205 bnet_fsend(user, "%sTAPE ", dev->state & ST_TAPE ? "" : "!");
206 bnet_fsend(user, "%sLABEL ", dev->state & ST_LABEL ? "" : "!");
207 bnet_fsend(user, "%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
208 bnet_fsend(user, "%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
209 bnet_fsend(user, "%sREAD ", dev->state & ST_READ ? "" : "!");
210 bnet_fsend(user, "%sEOT ", dev->state & ST_EOT ? "" : "!");
211 bnet_fsend(user, "%sWEOT ", dev->state & ST_WEOT ? "" : "!");
212 bnet_fsend(user, "%sEOF ", dev->state & ST_EOF ? "" : "!");
213 bnet_fsend(user, "%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
214 bnet_fsend(user, "%sSHORT ", dev->state & ST_SHORT ? "" : "!");
215 bnet_fsend(user, "\n");
217 bnet_fsend(user, _("Device parameters:\n"));
218 bnet_fsend(user, "Device name: %s\n", dev->dev_name);
219 bnet_fsend(user, "File=%u block=%u\n", dev->file, dev->block_num);
220 bnet_fsend(user, "Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
225 static void list_running_jobs(BSOCK *user)
230 char JobName[MAX_NAME_LENGTH];
231 char b1[30], b2[30], b3[30];
233 bnet_fsend(user, _("\nRunning Jobs:\n"));
236 if (jcr->JobStatus == JS_WaitFD) {
237 bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
238 job_type_to_str(jcr->JobType), jcr->Job);
241 bstrncpy(JobName, jcr->Job, sizeof(JobName));
242 /* There are three periods after the Job name */
244 for (int i=0; i<3; i++) {
245 if ((p=strrchr(JobName, '.')) != NULL) {
249 bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"),
250 job_level_to_str(jcr->JobLevel),
251 job_type_to_str(jcr->JobType),
255 jcr->device->device_name);
256 sec = time(NULL) - jcr->run_time;
260 bps = jcr->JobBytes / sec;
261 bnet_fsend(user, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
262 edit_uint64_with_commas(jcr->JobFiles, b1),
263 edit_uint64_with_commas(jcr->JobBytes, b2),
264 edit_uint64_with_commas(bps, b3));
267 if (jcr->file_bsock) {
268 bnet_fsend(user, " FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n",
269 edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
270 jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
271 jcr->file_bsock->fd);
273 bnet_fsend(user, " FDSocket closed\n");
277 free_locked_jcr(jcr);
281 bnet_fsend(user, _("No Jobs running.\n"));
283 bnet_fsend(user, "====\n");
286 static void list_terminated_jobs(void *arg)
288 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
290 struct s_last_job *je;
293 if (last_jobs->size() == 0) {
294 msg = _("No Terminated Jobs.\n");
295 sendit(msg, strlen(msg), arg);
298 lock_last_jobs_list();
299 msg = _("\nTerminated Jobs:\n");
300 sendit(msg, strlen(msg), arg);
301 msg = _(" JobId Level Files Bytes Status Finished Name \n");
302 sendit(msg, strlen(msg), arg);
303 msg = _("======================================================================\n");
304 sendit(msg, strlen(msg), arg);
305 foreach_dlist(je, last_jobs) {
306 char JobName[MAX_NAME_LENGTH];
307 const char *termstat;
310 bstrftime_nc(dt, sizeof(dt), je->end_time);
311 switch (je->JobType) {
314 bstrncpy(level, " ", sizeof(level));
317 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
321 switch (je->JobStatus) {
323 termstat = "Created";
326 case JS_ErrorTerminated:
342 bstrncpy(JobName, je->Job, sizeof(JobName));
343 /* There are three periods after the Job name */
345 for (int i=0; i<3; i++) {
346 if ((p=strrchr(JobName, '.')) != NULL) {
350 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
353 edit_uint64_with_commas(je->JobFiles, b1),
354 edit_uint64_with_commas(je->JobBytes, b2),
357 sendit(buf, strlen(buf), arg);
359 sendit("====\n", 5, arg);
360 unlock_last_jobs_list();
364 * Convert Job Level into a string
366 static const char *level_to_str(int level)
377 str = _("Incremental");
380 str = _("Differential");
385 case L_VERIFY_CATALOG:
386 str = _("Verify Catalog");
389 str = _("Init Catalog");
391 case L_VERIFY_VOLUME_TO_CATALOG:
392 str = _("Volume to Catalog");
394 case L_VERIFY_DISK_TO_CATALOG:
395 str = _("Disk to Catalog");
404 str = _("Unknown Job Level");
413 static void sendit(const char *msg, int len, void *arg)
415 BSOCK *user = (BSOCK *)arg;
417 memcpy(user->msg, msg, len+1);
418 user->msglen = len+1;
423 * .status command from Director
425 int qstatus_cmd(JCR *jcr)
427 BSOCK *dir = jcr->dir_bsock;
428 char time[dir->msglen+1];
432 if (sscanf(dir->msg, qstatus, time) != 1) {
433 pm_strcpy(&jcr->errmsg, dir->msg);
434 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
435 bnet_fsend(dir, "3900 Bad .status command, missing argument.\n");
436 bnet_sig(dir, BNET_EOD);
441 if (strcmp(time, "current") == 0) {
442 bnet_fsend(dir, OKqstatus, time);
445 if (njcr->JobId != 0) {
446 bnet_fsend(dir, DotStatusJob, njcr->JobId, njcr->JobStatus, njcr->JobErrors);
448 free_locked_jcr(njcr);
452 else if (strcmp(time, "last") == 0) {
453 bnet_fsend(dir, OKqstatus, time);
454 if ((last_jobs) && (last_jobs->size() > 0)) {
455 job = (s_last_job*)last_jobs->last();
456 bnet_fsend(dir, DotStatusJob, job->JobId, job->JobStatus, job->Errors);
460 pm_strcpy(&jcr->errmsg, dir->msg);
461 Jmsg1(jcr, M_FATAL, 0, _("Bad .status command: %s\n"), jcr->errmsg);
462 bnet_fsend(dir, "3900 Bad .status command, wrong argument.\n");
463 bnet_sig(dir, BNET_EOD);
467 bnet_sig(dir, BNET_EOD);