]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/status.c
restore cmd + misc -- see kes04Aug02
[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, 2001, 2002 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 /*
37  * General status generator
38  */
39 static void do_status(void sendit(char *msg, int len, void *sarg), void *arg) 
40 {
41    int sec, bps;
42    char *msg, b1[32], b2[32], b3[32];
43    int found, len;
44    JCR *njcr;
45    char dt[MAX_TIME_LENGTH];
46
47    msg = (char *)get_pool_memory(PM_MESSAGE);
48    found = 0;
49    len = Mmsg(&msg, "%s Version: " VERSION " (" DATE ")\n", my_name);
50    sendit(msg, len, arg);
51    bstrftime(dt, sizeof(dt), daemon_start_time);
52    len = Mmsg(&msg, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
53         last_job.NumJobs == 1 ? "" : "s");
54    sendit(msg, len, arg);
55    if (last_job.NumJobs > 0) {
56       char termstat[30];
57
58       bstrftime(dt, sizeof(dt), last_job.end_time);
59       len = Mmsg(&msg, _("Last Job %s finished at %s\n"), last_job.Job, dt);
60       sendit(msg, len, arg);
61
62       jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
63       len = Mmsg(&msg, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
64            edit_uint64_with_commas(last_job.JobFiles, b1),
65            edit_uint64_with_commas(last_job.JobBytes, b2),
66            termstat);
67       sendit(msg, len, arg);
68    }
69    lock_jcr_chain();
70    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
71       bstrftime(dt, sizeof(dt), njcr->start_time);
72       if (njcr->JobId == 0) {
73          len = Mmsg(&msg, _("Director connected at: %s\n"), dt);
74       } else {
75          len = Mmsg(&msg, _("JobId %d Job %s is running.\n    %s Job started: %s\n"), 
76              njcr->JobId, njcr->Job, job_type_to_str(njcr->JobType), 
77              dt);
78 //           job_level_to_str(njcr->JobLevel), dt);
79       }
80       sendit(msg, len, arg);
81       if (njcr->JobId == 0) {
82          free_locked_jcr(njcr);
83          continue;
84       }
85       sec = time(NULL) - njcr->start_time;
86       if (sec <= 0) {
87          sec = 1;
88       }
89       bps = njcr->JobBytes / sec;
90       len = Mmsg(&msg,  _("    Files=%s Bytes=%s Bytes/sec=%s\n"), 
91            edit_uint64_with_commas(njcr->JobFiles, b1),
92            edit_uint64_with_commas(njcr->JobBytes, b2),
93            edit_uint64_with_commas(bps, b3));
94       sendit(msg, len, arg);
95       len = Mmsg(&msg, _("    Files Examined=%s\n"), 
96            edit_uint64_with_commas(njcr->num_files_examined, b1));
97       sendit(msg, len, arg);
98       if (njcr->JobFiles > 0) {
99          len = Mmsg(&msg, _("    Processing file: %s\n"), njcr->last_fname);
100          sendit(msg, len, arg);
101       }
102
103       found = 1;
104       if (njcr->store_bsock) {
105          len = Mmsg(&msg, "    SDReadSeqNo=%" lld " fd=%d\n",
106              njcr->store_bsock->read_seqno, njcr->store_bsock->fd);
107          sendit(msg, len, arg);
108       } else {
109          len = Mmsg(&msg, _("    SDSocket closed.\n"));
110          sendit(msg, len, arg);
111       }
112       free_locked_jcr(njcr);
113    }
114    unlock_jcr_chain();
115    if (!found) {
116       len = Mmsg(&msg, _("No jobs running.\n"));
117       sendit(msg, len, arg);
118    }
119    free_pool_memory(msg);
120 }
121
122 /*
123  * Send to Director 
124  */
125 static void sendit(char *msg, int len, void *arg)
126 {
127    BSOCK *user = (BSOCK *)arg;
128
129    memcpy(user->msg, msg, len+1);
130    user->msglen = len+1;
131    bnet_send(user);
132 }
133                                    
134 /*
135  * Status command from Director
136  */
137 int status_cmd(JCR *jcr)
138 {
139    BSOCK *user = jcr->dir_bsock;
140
141    bnet_fsend(user, "\n");
142    do_status(sendit, (void *)user);
143    bnet_fsend(user, "====\n");
144
145    bnet_sig(user, BNET_EOD);
146    return 1;
147 }
148
149
150 #ifdef HAVE_CYGWIN
151 #include <windows.h>
152
153 static char buf[100];
154 int bacstat = 0;
155
156 struct s_win32_arg {
157    HWND hwnd;
158    int idlist;
159 };
160
161 /*
162  * Put message in Window List Box
163  */
164 static void win32_sendit(char *msg, int len, void *marg)
165 {
166    struct s_win32_arg *arg = (struct s_win32_arg *)marg;
167
168    if (len > 0) {
169       msg[len-1] = 0;                 /* eliminate newline */
170    }
171    SendDlgItemMessage(arg->hwnd, arg->idlist, LB_ADDSTRING, 0, (LONG)msg);
172    
173 }
174
175 void FillStatusBox(HWND hwnd, int idlist)
176 {
177    struct s_win32_arg arg;
178
179    arg.hwnd = hwnd;
180    arg.idlist = idlist;
181
182    /* Empty box */
183    for ( ; SendDlgItemMessage(hwnd, idlist, LB_DELETESTRING, 0, (LONG)0) > 0; )
184       { }
185    do_status(win32_sendit, (void *)&arg);
186 }
187
188 char *bac_status(int stat)
189 {
190    JCR *njcr;
191    char *termstat = _("Bacula Idle");
192
193    bacstat = 0;
194    if (last_job.NumJobs > 0) {
195       switch (last_job.JobStatus) {
196         case JS_ErrorTerminated:
197             bacstat = -1;
198             termstat = _("Last Job Erred");
199             break;
200         default:
201             break;
202       }
203    }
204    lock_jcr_chain();
205    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
206       if (njcr->JobId != 0) {
207          bacstat = 1;
208          termstat = _("Bacula Running");
209          free_locked_jcr(njcr);
210          break;
211       }
212       free_locked_jcr(njcr);
213    }
214    unlock_jcr_chain();
215    strcpy(buf, termstat);
216    return buf;
217 }
218
219 #endif /* HAVE_CYGWIN */