]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/status.c
Eliminate zombie job in SD
[bacula/bacula] / bacula / src / stored / status.c
1 /*
2  *  This file handles the status command
3  *
4  *     Kern Sibbald, May MMIII
5  *
6  *   Version $Id$
7  *  
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "stored.h"
31
32 /* Exported variables */
33
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;
41
42 /* Static variables */
43
44
45 /* Forward referenced functions */
46 static void send_blocked_status(JCR *jcr, DEVICE *dev);
47 static void list_terminated_jobs(void *arg);
48 static void list_running_jobs(BSOCK *user);
49 static void sendit(char *msg, int len, void *arg);
50 static char *level_to_str(int level);
51
52
53 /*
54  * Status command from Director
55  */
56 int status_cmd(JCR *jcr)
57 {
58    DEVRES *device;
59    DEVICE *dev;
60    BSOCK *user = jcr->dir_bsock;
61    char dt[MAX_TIME_LENGTH];
62    char b1[30], b2[30], b3[30];
63    int bpb;
64
65    bnet_fsend(user, "\n%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
66               HOST_OS, DISTNAME, DISTVER);
67    bstrftime_nc(dt, sizeof(dt), daemon_start_time);
68    bnet_fsend(user, _("Daemon started %s, %d Job%s run since started.\n"), dt, num_jobs_run,
69         num_jobs_run == 1 ? "" : "s");
70
71    /*
72     * List running jobs
73     */
74    list_running_jobs(user);
75
76    /*
77     * List terminated jobs
78     */
79    list_terminated_jobs(user);
80
81    /*
82     * List devices
83     */
84    bnet_fsend(user, _("\nDevice status:\n"));
85    LockRes();
86    foreach_res(device, R_DEVICE) {
87       for (dev=device->dev; dev; dev=dev->next) {
88          if (dev_state(dev, ST_OPENED)) {
89             if (dev_state(dev, ST_LABEL)) {
90                bnet_fsend(user, _("Device \"%s\" is mounted with Volume \"%s\"\n"), 
91                   dev_name(dev), dev->VolHdr.VolName);
92             } else {
93                bnet_fsend(user, _("Device \"%s\" open but no Bacula volume is mounted.\n"), dev_name(dev));
94             }
95             send_blocked_status(jcr, dev);
96             if (dev_state(dev, ST_APPEND)) {
97                bpb = dev->VolCatInfo.VolCatBlocks;
98                if (bpb <= 0) {
99                   bpb = 1;
100                }
101                bpb = dev->VolCatInfo.VolCatBytes / bpb;
102                bnet_fsend(user, _("    Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
103                   edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
104                   edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2), 
105                   edit_uint64_with_commas(bpb, b3));
106             } else {  /* reading */
107                bpb = dev->VolCatInfo.VolCatReads;
108                if (bpb <= 0) {
109                   bpb = 1;
110                }
111                if (dev->VolCatInfo.VolCatRBytes > 0) {
112                   bpb = dev->VolCatInfo.VolCatRBytes / bpb;
113                } else {
114                   bpb = 0;
115                }
116                bnet_fsend(user, _("    Total Bytes Read=%s Blocks Read=%s Bytes/block=%s\n"),
117                   edit_uint64_with_commas(dev->VolCatInfo.VolCatRBytes, b1),
118                   edit_uint64_with_commas(dev->VolCatInfo.VolCatReads, b2), 
119                   edit_uint64_with_commas(bpb, b3));
120             }
121             bnet_fsend(user, _("    Positioned at File=%s Block=%s\n"), 
122                edit_uint64_with_commas(dev->file, b1),
123                edit_uint64_with_commas(dev->block_num, b2));
124
125          } else {
126             bnet_fsend(user, _("Device \"%s\" is not open.\n"), dev_name(dev));
127             send_blocked_status(jcr, dev);
128          }
129       }
130    }
131    UnlockRes();
132
133
134 #ifdef xfull_status
135    bnet_fsend(user, "\n\n");
136    dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
137 #endif
138    bnet_fsend(user, "====\n\n");
139
140    list_spool_stats(user);
141
142    bnet_sig(user, BNET_EOD);
143    return 1;
144 }
145
146 static void send_blocked_status(JCR *jcr, DEVICE *dev) 
147 {
148    BSOCK *user = jcr->dir_bsock;
149
150    switch (dev->dev_blocked) {
151    case BST_UNMOUNTED:
152       bnet_fsend(user, _("    Device is BLOCKED. User unmounted.\n"));
153       break;
154    case BST_UNMOUNTED_WAITING_FOR_SYSOP:
155       bnet_fsend(user, _("    Device is BLOCKED. User unmounted during wait for media/mount.\n"));
156       break;
157    case BST_WAITING_FOR_SYSOP:
158       if (jcr->JobStatus == JS_WaitMount) {
159          bnet_fsend(user, _("    Device is BLOCKED waiting for mount.\n"));
160       } else {
161          bnet_fsend(user, _("    Device is BLOCKED waiting for appendable media.\n"));
162       }
163       break;
164    case BST_DOING_ACQUIRE:
165       bnet_fsend(user, _("    Device is being initialized.\n"));
166       break;
167    case BST_WRITING_LABEL:
168       bnet_fsend(user, _("    Device is blocked labeling a Volume.\n"));
169       break;
170    default:
171       break;
172    }
173    if (debug_level > 1) {
174       bnet_fsend(user, _("Configured device capabilities:\n"));
175       bnet_fsend(user, "%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
176       bnet_fsend(user, "%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
177       bnet_fsend(user, "%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
178       bnet_fsend(user, "%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
179       bnet_fsend(user, "%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
180       bnet_fsend(user, "%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
181       bnet_fsend(user, "%sREM ", dev->capabilities & CAP_REM ? "" : "!");
182       bnet_fsend(user, "%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
183       bnet_fsend(user, "%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
184       bnet_fsend(user, "%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
185       bnet_fsend(user, "%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
186       bnet_fsend(user, "%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
187       bnet_fsend(user, "\n");
188
189       bnet_fsend(user, _("Device status:\n"));
190       bnet_fsend(user, "%sOPENED ", dev->state & ST_OPENED ? "" : "!");
191       bnet_fsend(user, "%sTAPE ", dev->state & ST_TAPE ? "" : "!");
192       bnet_fsend(user, "%sLABEL ", dev->state & ST_LABEL ? "" : "!");
193       bnet_fsend(user, "%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
194       bnet_fsend(user, "%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
195       bnet_fsend(user, "%sREAD ", dev->state & ST_READ ? "" : "!");
196       bnet_fsend(user, "%sEOT ", dev->state & ST_EOT ? "" : "!");
197       bnet_fsend(user, "%sWEOT ", dev->state & ST_WEOT ? "" : "!");
198       bnet_fsend(user, "%sEOF ", dev->state & ST_EOF ? "" : "!");
199       bnet_fsend(user, "%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
200       bnet_fsend(user, "%sSHORT ", dev->state & ST_SHORT ? "" : "!");
201       bnet_fsend(user, "\n");
202
203       bnet_fsend(user, _("Device parameters:\n"));
204       bnet_fsend(user, "Device name: %s\n", dev->dev_name);
205       bnet_fsend(user, "File=%u block=%u\n", dev->file, dev->block_num);
206       bnet_fsend(user, "Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
207    }
208
209 }
210
211 static void list_running_jobs(BSOCK *user)
212 {
213    bool found = false;
214    int bps, sec;
215    JCR *jcr;
216    char JobName[MAX_NAME_LENGTH];
217    char b1[30], b2[30], b3[30];
218    
219    bnet_fsend(user, _("\nRunning Jobs:\n"));
220    lock_jcr_chain();
221    foreach_jcr(jcr) {
222       if (jcr->JobStatus == JS_WaitFD) {
223          bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
224             job_type_to_str(jcr->JobType), jcr->Job);
225       }
226       if (jcr->device) {
227          bstrncpy(JobName, jcr->Job, sizeof(JobName));
228          /* There are three periods after the Job name */
229          char *p;
230          for (int i=0; i<3; i++) {
231             if ((p=strrchr(JobName, '.')) != NULL) {
232                *p = 0;
233             }
234          }
235          bnet_fsend(user, _("%s %s job %s JobId=%d Volume=\"%s\" device=\"%s\"\n"), 
236                    job_level_to_str(jcr->JobLevel),
237                    job_type_to_str(jcr->JobType),
238                    JobName,
239                    jcr->JobId,
240                    jcr->VolumeName,
241                    jcr->device->device_name);
242          sec = time(NULL) - jcr->run_time;
243          if (sec <= 0) {
244             sec = 1;
245          }
246          bps = jcr->JobBytes / sec;
247          bnet_fsend(user, _("    Files=%s Bytes=%s Bytes/sec=%s\n"), 
248             edit_uint64_with_commas(jcr->JobFiles, b1),
249             edit_uint64_with_commas(jcr->JobBytes, b2),
250             edit_uint64_with_commas(bps, b3));
251          found = true;
252 #ifdef DEBUG
253          if (jcr->file_bsock) {
254             bnet_fsend(user, "    FDReadSeqNo=%s in_msg=%u out_msg=%d fd=%d\n", 
255                edit_uint64_with_commas(jcr->file_bsock->read_seqno, b1),
256                jcr->file_bsock->in_msg_no, jcr->file_bsock->out_msg_no,
257                jcr->file_bsock->fd);
258          } else {
259             bnet_fsend(user, "    FDSocket closed\n");
260          }
261 #endif
262       }
263       free_locked_jcr(jcr);
264    }
265    unlock_jcr_chain();
266    if (!found) {
267       bnet_fsend(user, _("No Jobs running.\n"));
268    }
269    bnet_fsend(user, "====\n");
270 }
271
272 static void list_terminated_jobs(void *arg)
273 {
274    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
275    char level[10];
276    struct s_last_job *je;
277    char *msg;
278
279    if (last_jobs->size() == 0) {
280       msg = _("No Terminated Jobs.\n"); 
281       sendit(msg, strlen(msg), arg);
282       return;
283    }
284    lock_last_jobs_list();
285    msg =  _("\nTerminated Jobs:\n"); 
286    sendit(msg, strlen(msg), arg);
287    msg =  _(" JobId  Level   Files          Bytes Status   Finished        Name \n");
288    sendit(msg, strlen(msg), arg);
289    msg = _("======================================================================\n"); 
290    sendit(msg, strlen(msg), arg);
291    foreach_dlist(je, last_jobs) {
292       char JobName[MAX_NAME_LENGTH];
293       char *termstat;
294       char buf[1000];
295
296       bstrftime_nc(dt, sizeof(dt), je->end_time);
297       switch (je->JobType) {
298       case JT_ADMIN:
299       case JT_RESTORE:
300          bstrncpy(level, "    ", sizeof(level));
301          break;
302       default:
303          bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
304          level[4] = 0;
305          break;
306       }
307       switch (je->JobStatus) {
308       case JS_Created:
309          termstat = "Created";
310          break;
311       case JS_FatalError:
312       case JS_ErrorTerminated:
313          termstat = "Error";
314          break;
315       case JS_Differences:
316          termstat = "Diffs";
317          break;
318       case JS_Canceled:
319          termstat = "Cancel";
320          break;
321       case JS_Terminated:
322          termstat = "OK";
323          break;
324       default:
325          termstat = "Other";
326          break;
327       }
328       bstrncpy(JobName, je->Job, sizeof(JobName));
329       /* There are three periods after the Job name */
330       char *p;
331       for (int i=0; i<3; i++) {
332          if ((p=strrchr(JobName, '.')) != NULL) {
333             *p = 0;
334          }
335       }
336       bsnprintf(buf, sizeof(buf), _("%6d  %-6s %8s %14s %-7s  %-8s %s\n"), 
337          je->JobId,
338          level, 
339          edit_uint64_with_commas(je->JobFiles, b1),
340          edit_uint64_with_commas(je->JobBytes, b2), 
341          termstat,
342          dt, JobName);
343       sendit(buf, strlen(buf), arg);
344    }
345    sendit("====\n", 5, arg);
346    unlock_last_jobs_list();
347 }
348
349 /*
350  * Convert Job Level into a string
351  */
352 static char *level_to_str(int level) 
353 {
354    char *str;
355
356    switch (level) {
357    case L_BASE:
358       str = _("Base");
359    case L_FULL:
360       str = _("Full");
361       break;
362    case L_INCREMENTAL:
363       str = _("Incremental");
364       break;
365    case L_DIFFERENTIAL:
366       str = _("Differential");
367       break;
368    case L_SINCE:
369       str = _("Since");
370       break;
371    case L_VERIFY_CATALOG:
372       str = _("Verify Catalog");
373       break;
374    case L_VERIFY_INIT:
375       str = _("Init Catalog");
376       break;
377    case L_VERIFY_VOLUME_TO_CATALOG:
378       str = _("Volume to Catalog");
379       break;
380    case L_VERIFY_DISK_TO_CATALOG:
381       str = _("Disk to Catalog");
382       break;
383    case L_VERIFY_DATA:
384       str = _("Data");
385       break;
386    case L_NONE:
387       str = " ";
388       break;
389    default:
390       str = _("Unknown Job Level");
391       break;
392    }
393    return str;
394 }
395
396 /*
397  * Send to Director 
398  */
399 static void sendit(char *msg, int len, void *arg)
400 {
401    BSOCK *user = (BSOCK *)arg;
402
403    memcpy(user->msg, msg, len+1);
404    user->msglen = len+1;
405    bnet_send(user);
406 }