2 * Bacula File Daemon Status routines
4 * Kern Sibbald, August MMI
10 Copyright (C) 2000-2003 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 extern uint64_t sm_max_bytes;
37 extern uint64_t sm_bytes;
38 extern uint32_t sm_max_buffers;
39 extern uint32_t sm_buffers;
42 /* Forward referenced functions */
43 static void list_terminated_jobs(void sendit(const char *msg, int len, void *sarg), void *arg);
44 static void bsock_sendit(const char *msg, int len, void *arg);
45 static const char *level_to_str(int level);
48 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
53 * General status generator
55 static void do_status(void sendit(const char *msg, int len, void *sarg), void *arg)
58 char *msg, b1[32], b2[32], b3[32], b4[32];
61 char dt[MAX_TIME_LENGTH];
63 msg = (char *)get_pool_memory(PM_MESSAGE);
65 len = Mmsg(&msg, "%s Version: " VERSION " (" BDATE ") %s %s %s\n", my_name,
66 HOST_OS, DISTNAME, DISTVER);
67 sendit(msg, len, arg);
68 bstrftime_nc(dt, sizeof(dt), daemon_start_time);
69 len = Mmsg(&msg, _("Daemon started %s, %d Job%s run since started.\n"),
70 dt, num_jobs_run, num_jobs_run == 1 ? "" : "s");
71 sendit(msg, len, arg);
72 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
73 if (debug_level > 0) {
75 privs = enable_backup_privileges(NULL, 1);
78 _(" Priv 0x%x APIs=%sOPT,%sATP,%sLPV,%sGFAE,%sBR,%sBW,%sSPSP\n"), privs,
79 p_OpenProcessToken?"":"!",
80 p_AdjustTokenPrivileges?"":"!",
81 p_LookupPrivilegeValue?"":"!",
82 p_GetFileAttributesEx?"":"!",
85 p_SetProcessShutdownParameters?"":"!");
86 sendit(msg, len, arg);
90 if (debug_level > 0) {
91 len = Mmsg(&msg, _(" Heap: bytes=%s max_bytes=%s bufs=%s max_bufs=%s\n"),
92 edit_uint64_with_commas(sm_bytes, b1),
93 edit_uint64_with_commas(sm_max_bytes, b2),
94 edit_uint64_with_commas(sm_buffers, b3),
95 edit_uint64_with_commas(sm_max_buffers, b4));
96 sendit(msg, len, arg);
99 if (debug_level > 0) {
100 len = Mmsg(&msg, _(" Sizeof: off_t=%d size_t=%d\n"), sizeof(off_t),
102 sendit(msg, len, arg);
105 list_terminated_jobs(sendit, arg);
110 Dmsg0(1000, "Begin status jcr loop.\n");
111 len = Mmsg(&msg, _("Running Jobs:\n"));
112 sendit(msg, len, arg);
115 bstrftime_nc(dt, sizeof(dt), njcr->start_time);
116 if (njcr->JobId == 0) {
117 len = Mmsg(&msg, _("Director connected at: %s\n"), dt);
119 len = Mmsg(&msg, _("JobId %d Job %s is running.\n"),
120 njcr->JobId, njcr->Job);
121 sendit(msg, len, arg);
122 len = Mmsg(&msg, _(" %s Job started: %s\n"),
123 job_type_to_str(njcr->JobType), dt);
125 sendit(msg, len, arg);
126 if (njcr->JobId == 0) {
127 free_locked_jcr(njcr);
130 sec = time(NULL) - njcr->start_time;
134 bps = (int)(njcr->JobBytes / sec);
135 len = Mmsg(&msg, _(" Files=%s Bytes=%s Bytes/sec=%s\n"),
136 edit_uint64_with_commas(njcr->JobFiles, b1),
137 edit_uint64_with_commas(njcr->JobBytes, b2),
138 edit_uint64_with_commas(bps, b3));
139 sendit(msg, len, arg);
140 len = Mmsg(&msg, _(" Files Examined=%s\n"),
141 edit_uint64_with_commas(njcr->num_files_examined, b1));
142 sendit(msg, len, arg);
143 if (njcr->JobFiles > 0) {
145 len = Mmsg(&msg, _(" Processing file: %s\n"), njcr->last_fname);
147 sendit(msg, len, arg);
151 if (njcr->store_bsock) {
152 len = Mmsg(&msg, " SDReadSeqNo=%" lld " fd=%d\n",
153 njcr->store_bsock->read_seqno, njcr->store_bsock->fd);
154 sendit(msg, len, arg);
156 len = Mmsg(&msg, _(" SDSocket closed.\n"));
157 sendit(msg, len, arg);
159 free_locked_jcr(njcr);
162 Dmsg0(1000, "Begin status jcr loop.\n");
164 len = Mmsg(&msg, _("No Jobs running.\n"));
165 sendit(msg, len, arg);
167 len = Mmsg(&msg, _("====\n"));
168 sendit(msg, len, arg);
169 free_pool_memory(msg);
172 static void list_terminated_jobs(void sendit(const char *msg, int len, void *sarg), void *arg)
174 char dt[MAX_TIME_LENGTH], b1[30], b2[30];
176 struct s_last_job *je;
179 if (last_jobs->size() == 0) {
180 msg = _("No Terminated Jobs.\n");
181 sendit(msg, strlen(msg), arg);
184 lock_last_jobs_list();
185 sendit("\n", 1, arg); /* send separately */
186 msg = _("Terminated Jobs:\n");
187 sendit(msg, strlen(msg), arg);
188 msg = _(" JobId Level Files Bytes Status Finished Name \n");
189 sendit(msg, strlen(msg), arg);
190 msg = _("======================================================================\n");
191 sendit(msg, strlen(msg), arg);
192 foreach_dlist(je, last_jobs) {
193 char JobName[MAX_NAME_LENGTH];
194 const char *termstat;
197 bstrftime_nc(dt, sizeof(dt), je->end_time);
198 switch (je->JobType) {
201 bstrncpy(level, " ", sizeof(level));
204 bstrncpy(level, level_to_str(je->JobLevel), sizeof(level));
208 switch (je->JobStatus) {
210 termstat = "Created";
213 case JS_ErrorTerminated:
229 bstrncpy(JobName, je->Job, sizeof(JobName));
230 /* There are three periods after the Job name */
232 for (int i=0; i<3; i++) {
233 if ((p=strrchr(JobName, '.')) != NULL) {
237 bsnprintf(buf, sizeof(buf), _("%6d %-6s %8s %14s %-7s %-8s %s\n"),
240 edit_uint64_with_commas(je->JobFiles, b1),
241 edit_uint64_with_commas(je->JobBytes, b2),
244 sendit(buf, strlen(buf), arg);
246 sendit("====\n", 5, arg);
247 unlock_last_jobs_list();
252 * Send to bsock (Director or Console)
254 static void bsock_sendit(const char *msg, int len, void *arg)
256 BSOCK *user = (BSOCK *)arg;
258 user->msg = check_pool_memory_size(user->msg, len+1);
259 memcpy(user->msg, msg, len+1);
260 user->msglen = len+1;
265 * Status command from Director
267 int status_cmd(JCR *jcr)
269 BSOCK *user = jcr->dir_bsock;
271 bnet_fsend(user, "\n");
272 do_status(bsock_sendit, (void *)user);
274 bnet_sig(user, BNET_EOD);
280 * Convert Job Level into a string
282 static const char *level_to_str(int level)
293 str = _("Incremental");
296 str = _("Differential");
301 case L_VERIFY_CATALOG:
302 str = _("Verify Catalog");
305 str = _("Init Catalog");
307 case L_VERIFY_VOLUME_TO_CATALOG:
308 str = _("Volume to Catalog");
310 case L_VERIFY_DISK_TO_CATALOG:
311 str = _("Disk to Catalog");
320 str = _("Unknown Job Level");
327 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
338 * Put message in Window List Box
340 static void win32_sendit(const char *msg, int len, void *marg)
342 struct s_win32_arg *arg = (struct s_win32_arg *)marg;
344 if (len > 0 && msg[len-1] == '\n') {
345 // when compiling with visual studio some strings are read-only
346 // and cause access violations. So we creat a tmp copy.
347 char *_msg = (char *)alloca(len);
348 bstrncpy(_msg, msg, len);
351 SendDlgItemMessage(arg->hwnd, arg->idlist, LB_ADDSTRING, 0, (LONG)msg);
355 void FillStatusBox(HWND hwnd, int idlist)
357 struct s_win32_arg arg;
363 for ( ; SendDlgItemMessage(hwnd, idlist, LB_DELETESTRING, 0, (LONG)0) > 0; )
365 do_status(win32_sendit, (void *)&arg);
368 char *bac_status(char *buf, int buf_len)
371 const char *termstat = _("Bacula Idle");
372 struct s_last_job *job;
373 int stat = 0; /* Idle */
378 Dmsg0(1000, "Begin bac_status jcr loop.\n");
381 if (njcr->JobId != 0) {
383 termstat = _("Bacula Running");
384 free_locked_jcr(njcr);
387 free_locked_jcr(njcr);
393 if (last_jobs->size() > 0) {
394 job = (struct s_last_job *)last_jobs->last();
395 stat = job->JobStatus;
396 switch (job->JobStatus) {
398 termstat = _("Last Job Canceled");
400 case JS_ErrorTerminated:
402 termstat = _("Last Job Failed");
406 termstat = _("Last Job had Warnings");
411 Dmsg0(1000, "End bac_status jcr loop.\n");
415 bstrncpy(buf, termstat, buf_len);
420 #endif /* HAVE_CYGWIN */