]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/status.h
crypto: remove most of OpenSSL initcallbacks for 1.1
[bacula/bacula] / bacula / src / lib / status.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Status packet definition that is used in both the SD and FD. It
21  *    permits Win32 to call output_status() and get the output back
22  *    at the callback address line by line, and for Linux code,
23  *    the output can be sent directly to a BSOCK.
24  *
25  *     Kern Sibbald, March MMVII
26  *
27  */
28
29 #ifndef __STATUS_H_
30 #define __STATUS_H_
31
32 /*
33  * Packet to send to output_status()
34  */
35 class STATUS_PKT {
36 public:
37   BSOCK *bs;                       /* used on Unix machines */
38   void *context;                   /* Win32 */
39   void (*callback)(const char *msg, int len, void *context);  /* Win32 */
40   char api_opts[MAX_NAME_LENGTH];
41   int  api;                        /* set if we want API output, with api level */
42
43   /* Methods */
44   STATUS_PKT() { memset(this, 0, sizeof(STATUS_PKT)); };
45   ~STATUS_PKT() { };
46 };
47
48 extern void output_status(STATUS_PKT *sp);
49
50 /*
51  * Send to bsock (Director or Console)
52  */
53 static void sendit(const char *msg, int len, STATUS_PKT *sp)
54 {
55    if (sp->bs) {
56       BSOCK *user = sp->bs;
57       user->msg = check_pool_memory_size(user->msg, len+1);
58       memcpy(user->msg, msg, len+1);
59       user->msglen = len+1;
60       user->send();
61    } else {
62       sp->callback(msg, len, sp->context);
63    }
64 }
65
66 #ifndef STATUS_FUNCTIONS
67 #define STATUS_FUNCTIONS
68
69 /* common to SD/FD */
70 static void list_terminated_jobs(STATUS_PKT *sp)
71 {
72    OutputWriter ow(sp->api_opts);
73    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
74    char level[10];
75    struct s_last_job *je;
76    const char *msg;
77    char *p;
78
79    msg =  _("\nTerminated Jobs:\n");
80    if (!sp->api) sendit(msg, strlen(msg), sp);
81    if (last_jobs->size() == 0) {
82       if (!sp->api) sendit("====\n", 5, sp);
83       return;
84    }
85    lock_last_jobs_list();
86    msg =  _(" JobId  Level      Files    Bytes   Status   Finished        Name \n");
87    if (!sp->api) sendit(msg, strlen(msg), sp);
88    msg =  _("===================================================================\n");
89    if (!sp->api) sendit(msg, strlen(msg), sp);
90    if (sp->api > 1) {
91       p = ow.start_group("terminated");
92       sendit(p, strlen(p), sp);
93    }
94    foreach_dlist(je, last_jobs) {
95       char JobName[MAX_NAME_LENGTH];
96       const char *termstat;
97       char buf[1000];
98
99       bstrftime_nc(dt, sizeof(dt), je->end_time);
100       switch (je->JobType) {
101       case JT_ADMIN:
102          bstrncpy(level, "Admn", sizeof(level));
103          break;
104       case JT_RESTORE:
105          bstrncpy(level, "Rest", sizeof(level));
106          break;
107       default:
108          bstrncpy(level, job_level_to_str(je->JobLevel), sizeof(level));
109          level[4] = 0;
110          break;
111       }
112       switch (je->JobStatus) {
113       case JS_Created:
114          termstat = _("Created");
115          break;
116       case JS_FatalError:
117       case JS_ErrorTerminated:
118          termstat = _("Error");
119          break;
120       case JS_Differences:
121          termstat = _("Diffs");
122          break;
123       case JS_Canceled:
124          termstat = _("Cancel");
125          break;
126       case JS_Terminated:
127          termstat = _("OK");
128          break;
129       case JS_Warnings:
130          termstat = _("OK -- with warnings");
131          break;
132       case JS_Incomplete:
133          termstat = _("Incomplete");
134          break;
135       default:
136          termstat = _("Other");
137          break;
138       }
139       bstrncpy(JobName, je->Job, sizeof(JobName));
140       /* There are three periods after the Job name */
141       char *p;
142       for (int i=0; i<3; i++) {
143          if ((p=strrchr(JobName, '.')) != NULL) {
144             *p = 0;
145          }
146       }
147       if (sp->api == 1) {
148          bsnprintf(buf, sizeof(buf), _("%6d\t%-7s\t%8s\t%10s\t%-7s\t%-8s\t%s\n"),
149             je->JobId,
150             level,
151             edit_uint64_with_commas(je->JobFiles, b1),
152             edit_uint64_with_suffix(je->JobBytes, b2),
153             termstat,
154             dt, JobName);
155
156       } else if (sp->api > 1) {
157          p = ow.get_output(OT_CLEAR,
158                            OT_START_OBJ,
159                            OT_INT,     "jobid",     je->JobId,
160                            OT_JOBLEVEL,"level",     je->JobLevel,
161                            OT_JOBTYPE, "type",      je->JobType,
162                            OT_JOBSTATUS,"status",    je->JobStatus,
163                            OT_STRING,  "status_desc",termstat,
164                            OT_SIZE,    "jobbytes",  je->JobBytes,
165                            OT_INT32,   "jobfiles",  je->JobFiles,
166                            OT_STRING,  "job",       je->Job,
167                            OT_STRING,  "name",      JobName,
168                            OT_UTIME,   "starttime", je->start_time,
169                            OT_UTIME,   "endtime",   je->end_time,
170                            OT_INT,     "errors",    je->Errors,
171                            OT_END_OBJ,
172                            OT_END);
173          sendit(p, strlen(p), sp);
174
175
176       } else {
177          bsnprintf(buf, sizeof(buf), _("%6d  %-7s %8s %10s  %-7s  %-8s %s\n"),
178             je->JobId,
179             level,
180             edit_uint64_with_commas(je->JobFiles, b1),
181             edit_uint64_with_suffix(je->JobBytes, b2),
182             termstat,
183             dt, JobName);
184       }
185       sendit(buf, strlen(buf), sp);
186    }
187    unlock_last_jobs_list();
188    if (!sp->api) {
189       sendit("====\n", 5, sp);
190    } else if (sp->api > 1) {
191       p = ow.end_group(false);
192       sendit(p, strlen(p), sp);
193    }
194 }
195
196 #if defined(HAVE_WIN32)
197 int bacstat = 0;
198
199 #ifdef FILE_DAEMON
200 # define BAC_COMPONENT "Client"
201 #else
202 # define BAC_COMPONENT "Storage"
203 #endif
204
205 /* Return a one line status for the tray monitor */
206 char *bac_status(char *buf, int buf_len)
207 {
208    JCR *njcr;
209    const char *termstat = _("Bacula " BAC_COMPONENT ": Idle");
210    struct s_last_job *job;
211    int stat = 0;                      /* Idle */
212
213    if (!last_jobs) {
214       goto done;
215    }
216    Dmsg0(1000, "Begin bac_status jcr loop.\n");
217    foreach_jcr(njcr) {
218       if (njcr->JobId != 0) {
219          stat = JS_Running;
220          termstat = _("Bacula " BAC_COMPONENT ": Running");
221          break;
222       }
223    }
224    endeach_jcr(njcr);
225
226    if (stat != 0) {
227       goto done;
228    }
229    if (last_jobs->size() > 0) {
230       job = (struct s_last_job *)last_jobs->last();
231       stat = job->JobStatus;
232       switch (job->JobStatus) {
233       case JS_Canceled:
234          termstat = _("Bacula " BAC_COMPONENT ": Last Job Canceled");
235          break;
236       case JS_ErrorTerminated:
237       case JS_FatalError:
238          termstat = _("Bacula " BAC_COMPONENT ": Last Job Failed");
239          break;
240       default:
241          if (job->Errors) {
242             termstat = _("Bacula " BAC_COMPONENT ": Last Job had Warnings");
243          }
244          break;
245       }
246    }
247    Dmsg0(1000, "End bac_status jcr loop.\n");
248 done:
249    bacstat = stat;
250    if (buf) {
251       bstrncpy(buf, termstat, buf_len);
252    }
253    return buf;
254 }
255
256 #endif /* HAVE_WIN32 */
257
258 #endif  /* ! STATUS_FUNCTIONS */
259
260 #endif