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