]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/status.c
Major part of Win32 BackupRead/Write done
[bacula/bacula] / bacula / src / filed / status.c
1 /*
2  *  Bacula File Daemon Status routines
3  *
4  *    Kern Sibbald, August MMI
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2003 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 "filed.h"
31
32 extern char my_name[];
33 extern struct s_last_job last_job;
34 extern time_t daemon_start_time;
35
36 #ifdef HAVE_CYGWIN
37 static int privs = 0;
38 #endif
39
40 /*
41  * General status generator
42  */
43 static void do_status(void sendit(char *msg, int len, void *sarg), void *arg) 
44 {
45    int sec, bps;
46    char *msg, b1[32], b2[32], b3[32];
47    int found, len;
48    JCR *njcr;
49    char dt[MAX_TIME_LENGTH];
50
51    msg = (char *)get_pool_memory(PM_MESSAGE);
52    found = 0;
53    len = Mmsg(&msg, "%s Version: " VERSION " (" BDATE ")\n", my_name);
54    sendit(msg, len, arg);
55    bstrftime(dt, sizeof(dt), daemon_start_time);
56    len = Mmsg(&msg, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
57         last_job.NumJobs == 1 ? "" : "s");
58    sendit(msg, len, arg);
59 #ifdef HAVE_CYGWIN
60    if (!privs) {
61       privs = get_backup_privileges(NULL, 1);
62    }
63    len = Mmsg(&msg, 
64       _("Priv 0x%x APIs=%sOPT,%sATP,%sLPV,%sGFAE,%sBR,%sBW,%sSPSP\n"), privs,
65       p_OpenProcessToken?"":"!",
66       p_AdjustTokenPrivileges?"":"!",
67       p_LookupPrivilegeValue?"":"!",
68       p_GetFileAttributesEx?"":"!",
69       p_BackupRead?"":"!",
70       p_BackupWrite?"":"!",
71       p_SetProcessShutdownParameters?"":"!");
72    sendit(msg, len, arg);
73 #endif
74
75    if (last_job.NumJobs > 0) {
76       char termstat[30];
77
78       bstrftime(dt, sizeof(dt), last_job.end_time);
79       len = Mmsg(&msg, _("Last Job %s finished at %s\n"), last_job.Job, dt);
80       sendit(msg, len, arg);
81
82       jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
83       len = Mmsg(&msg, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
84            edit_uint64_with_commas(last_job.JobFiles, b1),
85            edit_uint64_with_commas(last_job.JobBytes, b2),
86            termstat);
87       sendit(msg, len, arg);
88    }
89    Dmsg0(200, "Begin status jcr loop.\n");
90    lock_jcr_chain();
91    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
92       bstrftime(dt, sizeof(dt), njcr->start_time);
93       if (njcr->JobId == 0) {
94          len = Mmsg(&msg, _("Director connected at: %s\n"), dt);
95       } else {
96          len = Mmsg(&msg, _("JobId %d Job %s is running.\n"), 
97                     njcr->JobId, njcr->Job);
98          sendit(msg, len, arg);
99          len = Mmsg(&msg, _("    %s Job started: %s\n"), 
100                     job_type_to_str(njcr->JobType), dt);
101       }
102       sendit(msg, len, arg);
103       if (njcr->JobId == 0) {
104          free_locked_jcr(njcr);
105          continue;
106       }
107       sec = time(NULL) - njcr->start_time;
108       if (sec <= 0) {
109          sec = 1;
110       }
111       bps = njcr->JobBytes / sec;
112       len = Mmsg(&msg,  _("    Files=%s Bytes=%s Bytes/sec=%s\n"), 
113            edit_uint64_with_commas(njcr->JobFiles, b1),
114            edit_uint64_with_commas(njcr->JobBytes, b2),
115            edit_uint64_with_commas(bps, b3));
116       sendit(msg, len, arg);
117       len = Mmsg(&msg, _("    Files Examined=%s\n"), 
118            edit_uint64_with_commas(njcr->num_files_examined, b1));
119       sendit(msg, len, arg);
120       if (njcr->JobFiles > 0) {
121          P(njcr->mutex);
122          len = Mmsg(&msg, _("    Processing file: %s\n"), njcr->last_fname);
123          V(njcr->mutex);
124          sendit(msg, len, arg);
125       }
126
127       found = 1;
128       if (njcr->store_bsock) {
129          len = Mmsg(&msg, "    SDReadSeqNo=%" lld " fd=%d\n",
130              njcr->store_bsock->read_seqno, njcr->store_bsock->fd);
131          sendit(msg, len, arg);
132       } else {
133          len = Mmsg(&msg, _("    SDSocket closed.\n"));
134          sendit(msg, len, arg);
135       }
136       free_locked_jcr(njcr);
137    }
138    unlock_jcr_chain();
139    Dmsg0(200, "Begin status jcr loop.\n");
140    if (!found) {
141       len = Mmsg(&msg, _("No jobs running.\n"));
142       sendit(msg, len, arg);
143    }
144    free_pool_memory(msg);
145 }
146
147 /*
148  * Send to Director 
149  */
150 static void sendit(char *msg, int len, void *arg)
151 {
152    BSOCK *user = (BSOCK *)arg;
153
154    memcpy(user->msg, msg, len+1);
155    user->msglen = len+1;
156    bnet_send(user);
157 }
158                                    
159 /*
160  * Status command from Director
161  */
162 int status_cmd(JCR *jcr)
163 {
164    BSOCK *user = jcr->dir_bsock;
165
166    bnet_fsend(user, "\n");
167    do_status(sendit, (void *)user);
168    bnet_fsend(user, "====\n");
169
170    bnet_sig(user, BNET_EOD);
171    return 1;
172 }
173
174
175 #ifdef HAVE_CYGWIN
176 #include <windows.h>
177
178 static char buf[100];
179 int bacstat = 0;
180
181 struct s_win32_arg {
182    HWND hwnd;
183    int idlist;
184 };
185
186 /*
187  * Put message in Window List Box
188  */
189 static void win32_sendit(char *msg, int len, void *marg)
190 {
191    struct s_win32_arg *arg = (struct s_win32_arg *)marg;
192
193    if (len > 0) {
194       msg[len-1] = 0;                 /* eliminate newline */
195    }
196    SendDlgItemMessage(arg->hwnd, arg->idlist, LB_ADDSTRING, 0, (LONG)msg);
197    
198 }
199
200 void FillStatusBox(HWND hwnd, int idlist)
201 {
202    struct s_win32_arg arg;
203
204    arg.hwnd = hwnd;
205    arg.idlist = idlist;
206
207    /* Empty box */
208    for ( ; SendDlgItemMessage(hwnd, idlist, LB_DELETESTRING, 0, (LONG)0) > 0; )
209       { }
210    do_status(win32_sendit, (void *)&arg);
211 }
212
213 char *bac_status(int stat)
214 {
215    JCR *njcr;
216    char *termstat = _("Bacula Idle");
217
218    bacstat = 0;
219    if (last_job.NumJobs > 0) {
220       switch (last_job.JobStatus) {
221         case JS_ErrorTerminated:
222             bacstat = -1;
223             termstat = _("Last Job Erred");
224             break;
225         default:
226             break;
227       }
228    }
229    Dmsg0(200, "Begin bac_status jcr loop.\n");
230    lock_jcr_chain();
231    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
232       if (njcr->JobId != 0) {
233          bacstat = 1;
234          termstat = _("Bacula Running");
235          free_locked_jcr(njcr);
236          break;
237       }
238       free_locked_jcr(njcr);
239    }
240    unlock_jcr_chain();
241    Dmsg0(200, "End bac_status jcr loop.\n");
242    strcpy(buf, termstat);
243    return buf;
244 }
245
246 #endif /* HAVE_CYGWIN */