2 * Bacula File Daemon Status routines
4 * Kern Sibbald, August MMI
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 extern char my_name[];
33 extern int num_jobs_run;
34 extern time_t daemon_start_time;
36 /* Forward referenced functions */
37 static void list_terminated_jobs(void sendit(const char *msg, int len, void *sarg), void *arg);
38 static void bsock_sendit(const char *msg, int len, void *arg);
39 static const char *level_to_str(int level);
42 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
47 * General status generator
49 static void do_status(void sendit(const char *msg, int len, void *sarg), void *arg)
52 char *msg, b1[32], b2[32], b3[32], b4[32];
55 char dt[MAX_TIME_LENGTH];
57 msg = (char *)get_pool_memory(PM_MESSAGE);
59 len = Mmsg(&msg, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
60 HOST_OS, DISTNAME, DISTVER);
61 sendit(msg, len, arg);
62 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
63 len = Mmsg(&msg, _("Daemon started %s, %d Job%s run since started.\n"),
64 dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
65 sendit(msg, len, arg);
66 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
67 if (debug_level > 0) {
69 privs = enable_backup_privileges(NULL, 1);
72 _(" Priv 0x%x APIs=%sOPT,%sATP,%sLPV,%sGFAE,%sBR,%sBW,%sSPSP\n"), privs,
73 p_OpenProcessToken?"":"!",
74 p_AdjustTokenPrivileges?"":"!",
75 p_LookupPrivilegeValue?"":"!",
76 p_GetFileAttributesEx?"":"!",
79 p_SetProcessShutdownParameters?"":"!");
80 sendit(msg, len, arg);
83 if (debug_level > 0) {
84 len = Mmsg(&msg, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
85 edit_uint64_with_commas(sm_bytes, b1),
86 edit_uint64_with_commas(sm_max_bytes, b2),
87 edit_uint64_with_commas(sm_buffers, b3),
88 edit_uint64_with_commas(sm_max_buffers, b4));
89 sendit(msg, len, arg);
91 if (debug_level > 0) {
92 len = Mmsg(&msg, _(" Sizeof: off_t=%d size_t=%d\n"), sizeof(off_t),
94 sendit(msg, len, arg);
97 list_terminated_jobs(sendit, arg);
102 Dmsg0(1000, "Begin status jcr loop.\n");
103 len = Mmsg(&msg, _("Running Jobs:\n"));
104 sendit(msg, len, arg);
107 bstrftime_nc(dt, sizeof(dt), njcr->start_time);
108 if (njcr->JobId == 0) {
109 len = Mmsg(&msg, _("Director connected at: %s\n"), dt);
111 len = Mmsg(&msg, _("JobId %d Job %s is running.\n"),
112 njcr->JobId, njcr->Job);
113 sendit(msg, len, arg);
114 len = Mmsg(&msg, _(" %s Job started: %s\n"),
115 job_type_to_str(njcr->JobType), dt);
117 sendit(msg, len, arg);
118 if (njcr->JobId == 0) {
119 free_locked_jcr(njcr);
122 sec = time(NULL) - njcr->start_time;
126 bps = (int)(njcr->JobBytes / sec);
127 len = Mmsg(&msg, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
128 edit_uint64_with_commas(njcr->JobFiles, b1),
129 edit_uint64_with_commas(njcr->JobBytes, b2),
130 edit_uint64_with_commas(bps, b3));
131 sendit(msg, len, arg);
132 len = Mmsg(&msg, _(" Files Examined=%s\n"),
133 edit_uint64_with_commas(njcr->num_files_examined, b1));
134 sendit(msg, len, arg);
135 if (njcr->JobFiles > 0) {
137 len = Mmsg(&msg, _(" Processing file: %s\n"), njcr->last_fname);
139 sendit(msg, len, arg);
143 if (njcr->store_bsock) {
144 len = Mmsg(&msg, " SDReadSeqNo=%" lld " fd=%d\n",
145 njcr->store_bsock->read_seqno, njcr->store_bsock->fd);
146 sendit(msg, len, arg);
148 len = Mmsg(&msg, _(" SDSocket closed.\n"));
149 sendit(msg, len, arg);
151 free_locked_jcr(njcr);
154 Dmsg0(1000, "Begin status jcr loop.\n");
156 len = Mmsg(&msg, _("No Jobs running.\n"));
157 sendit(msg, len, arg);
159 len = Mmsg(&msg, _("====\n"));
160 sendit(msg, len, arg);
161 free_pool_memory(msg);
164 static void list_terminated_jobs(void sendit(const char *msg, int len, void *sarg), void *arg)
166 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
168 struct s_last_job *je;
171 if (last_jobs->size() == 0) {
172 msg = _("No Terminated Jobs.\n");
173 sendit(msg, strlen(msg), arg);
176 lock_last_jobs_list();
177 sendit("\n", 1, arg); /* send separately */
178 msg = _("Terminated Jobs:\n");
179 sendit(msg, strlen(msg), arg);
180 msg = _(" JobId Level Files Bytes Status Finished Name \n");
181 sendit(msg, strlen(msg), arg);
182 msg = _("======================================================================\n");
183 sendit(msg, strlen(msg), arg);
184 foreach_dlist(je, last_jobs) {
185 char JobName[MAX_NAME_LENGTH];
186 const char *termstat;
189 bstrftime_nc(dt, sizeof(dt), je->end_time);
190 switch (je->JobType) {
193 bstrncpy(level, " ", sizeof(level));
196 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
200 switch (je->JobStatus) {
202 termstat = "Created";
205 case JS_ErrorTerminated:
221 bstrncpy(JobName, je->Job, sizeof(JobName));
222 /* There are three periods after the Job name */
224 for (int i=0; i<3; i++) {
225 if ((p=strrchr(JobName, '.')) != NULL) {
229 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
232 edit_uint64_with_commas(je->JobFiles, b1),
233 edit_uint64_with_commas(je->JobBytes, b2),
236 sendit(buf, strlen(buf), arg);
238 sendit("====\n", 5, arg);
239 unlock_last_jobs_list();
244 * Send to bsock (Director or Console)
246 static void bsock_sendit(const char *msg, int len, void *arg)
248 BSOCK *user = (BSOCK *)arg;
250 user->msg = check_pool_memory_size(user->msg, len+1);
251 memcpy(user->msg, msg, len+1);
252 user->msglen = len+1;
257 * Status command from Director
259 int status_cmd(JCR *jcr)
261 BSOCK *user = jcr->dir_bsock;
263 bnet_fsend(user, "\n");
264 do_status(bsock_sendit, (void *)user);
266 bnet_sig(user, BNET_EOD);
272 * Convert Job Level into a string
274 static const char *level_to_str(int level)
285 str = _("Incremental");
288 str = _("Differential");
293 case L_VERIFY_CATALOG:
294 str = _("Verify Catalog");
297 str = _("Init Catalog");
299 case L_VERIFY_VOLUME_TO_CATALOG:
300 str = _("Volume to Catalog");
302 case L_VERIFY_DISK_TO_CATALOG:
303 str = _("Disk to Catalog");
312 str = _("Unknown Job Level");
319 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
330 * Put message in Window List Box
332 static void win32_sendit(const char *msg, int len, void *marg)
334 struct s_win32_arg *arg = (struct s_win32_arg *)marg;
336 if (len > 0 && msg[len-1] == '\n') {
337 // when compiling with visual studio some strings are read-only
338 // and cause access violations. So we creat a tmp copy.
339 char *_msg = (char *)alloca(len);
340 bstrncpy(_msg, msg, len);
343 SendDlgItemMessage(arg->hwnd, arg->idlist, LB_ADDSTRING, 0, (LONG)msg);
347 void FillStatusBox(HWND hwnd, int idlist)
349 struct s_win32_arg arg;
355 for ( ; SendDlgItemMessage(hwnd, idlist, LB_DELETESTRING, 0, (LONG)0) > 0; )
357 do_status(win32_sendit, (void *)&arg);
360 char *bac_status(char *buf, int buf_len)
363 const char *termstat = _("Bacula Idle");
364 struct s_last_job *job;
365 int stat = 0; /* Idle */
370 Dmsg0(1000, "Begin bac_status jcr loop.\n");
373 if (njcr->JobId != 0) {
375 termstat = _("Bacula Running");
376 free_locked_jcr(njcr);
379 free_locked_jcr(njcr);
385 if (last_jobs->size() > 0) {
386 job = (struct s_last_job *)last_jobs->last();
387 stat = job->JobStatus;
388 switch (job->JobStatus) {
390 termstat = _("Last Job Canceled");
392 case JS_ErrorTerminated:
394 termstat = _("Last Job Failed");
398 termstat = _("Last Job had Warnings");
403 Dmsg0(1000, "End bac_status jcr loop.\n");
407 bstrncpy(buf, termstat, buf_len);
412 #endif /* HAVE_CYGWIN */