]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/status.h
Backport from BEE
[bacula/bacula] / bacula / src / lib / status.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2008-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    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    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  Status packet definition that is used in both the SD and FD. It
18  *    permits Win32 to call output_status() and get the output back
19  *    at the callback address line by line, and for Linux code,
20  *    the output can be sent directly to a BSOCK.
21  *
22  *     Kern Sibbald, March MMVII
23  *
24  *   Version $Id: $
25  *
26  */
27
28 #ifndef __STATUS_H_
29 #define __STATUS_H_
30
31 /*
32  * Packet to send to output_status()
33  */
34 class STATUS_PKT {
35 public:
36   BSOCK *bs;                       /* used on Unix machines */
37   void *context;                   /* Win32 */
38   void (*callback)(const char *msg, int len, void *context);  /* Win32 */
39   char api_opts[MAX_NAME_LENGTH];
40   int  api;                        /* set if we want API output, with api level */
41
42   /* Methods */
43   STATUS_PKT() { memset(this, 0, sizeof(STATUS_PKT)); };
44   ~STATUS_PKT() { };
45 };
46
47 extern void output_status(STATUS_PKT *sp);
48
49 /*
50  * Send to bsock (Director or Console)
51  */
52 static void sendit(const char *msg, int len, STATUS_PKT *sp)
53 {
54    if (sp->bs) {
55       BSOCK *user = sp->bs;
56       user->msg = check_pool_memory_size(user->msg, len+1);
57       memcpy(user->msg, msg, len+1);
58       user->msglen = len+1;
59       user->send();
60    } else {
61       sp->callback(msg, len, sp->context);
62    }
63 }
64
65 /* common to SD/FD */
66 static void list_terminated_jobs(STATUS_PKT *sp)
67 {
68    char dt[MAX_TIME_LENGTH], b1[30], b2[30];
69    char level[10];
70    struct s_last_job *je;
71    const char *msg;
72
73    msg =  _("\nTerminated Jobs:\n");
74    if (!sp->api) sendit(msg, strlen(msg), sp);
75    if (last_jobs->size() == 0) {
76       if (!sp->api) sendit("====\n", 5, sp);
77       return;
78    }
79    lock_last_jobs_list();
80    msg =  _(" JobId  Level    Files      Bytes   Status   Finished        Name \n");
81    if (!sp->api) sendit(msg, strlen(msg), sp);
82    msg =  _("===================================================================\n");
83    if (!sp->api) sendit(msg, strlen(msg), sp);
84    foreach_dlist(je, last_jobs) {
85       char JobName[MAX_NAME_LENGTH];
86       const char *termstat;
87       char buf[1000];
88
89       bstrftime_nc(dt, sizeof(dt), je->end_time);
90       switch (je->JobType) {
91       case JT_ADMIN:
92       case JT_RESTORE:
93          bstrncpy(level, "    ", sizeof(level));
94          break;
95       default:
96          bstrncpy(level, job_level_to_str(je->JobLevel), sizeof(level));
97          level[4] = 0;
98          break;
99       }
100       switch (je->JobStatus) {
101       case JS_Created:
102          termstat = _("Created");
103          break;
104       case JS_FatalError:
105       case JS_ErrorTerminated:
106          termstat = _("Error");
107          break;
108       case JS_Differences:
109          termstat = _("Diffs");
110          break;
111       case JS_Canceled:
112          termstat = _("Cancel");
113          break;
114       case JS_Terminated:
115          termstat = _("OK");
116          break;
117       case JS_Warnings:
118          termstat = _("OK -- with warnings");
119          break;
120          break;
121       default:
122          termstat = _("Other");
123          break;
124       }
125       bstrncpy(JobName, je->Job, sizeof(JobName));
126       /* There are three periods after the Job name */
127       char *p;
128       for (int i=0; i<3; i++) {
129          if ((p=strrchr(JobName, '.')) != NULL) {
130             *p = 0;
131          }
132       }
133       if (sp->api == 1) {
134          bsnprintf(buf, sizeof(buf), _("%6d\t%-6s\t%8s\t%10s\t%-7s\t%-8s\t%s\n"),
135             je->JobId,
136             level,
137             edit_uint64_with_commas(je->JobFiles, b1),
138             edit_uint64_with_suffix(je->JobBytes, b2),
139             termstat,
140             dt, JobName);
141       } else {
142          bsnprintf(buf, sizeof(buf), _("%6d  %-6s %8s %10s  %-7s  %-8s %s\n"),
143             je->JobId,
144             level,
145             edit_uint64_with_commas(je->JobFiles, b1),
146             edit_uint64_with_suffix(je->JobBytes, b2),
147             termstat,
148             dt, JobName);
149       }
150       sendit(buf, strlen(buf), sp);
151    }
152    unlock_last_jobs_list();
153    if (!sp->api) {
154       sendit("====\n", 5, sp);
155    }
156 }
157
158 #if defined(HAVE_WIN32)
159 int bacstat = 0;
160
161 #ifdef FILE_DAEMON
162 # define BAC_COMPONENT "Client"
163 #else
164 # define BAC_COMPONENT "Storage"
165 #endif
166
167 /* Return a one line status for the tray monitor */
168 char *bac_status(char *buf, int buf_len)
169 {
170    JCR *njcr;
171    const char *termstat = _("Bacula " BAC_COMPONENT ": Idle");
172    struct s_last_job *job;
173    int stat = 0;                      /* Idle */
174
175    if (!last_jobs) {
176       goto done;
177    }
178    Dmsg0(1000, "Begin bac_status jcr loop.\n");
179    foreach_jcr(njcr) {
180       if (njcr->JobId != 0) {
181          stat = JS_Running;
182          termstat = _("Bacula " BAC_COMPONENT ": Running");
183          break;
184       }
185    }
186    endeach_jcr(njcr);
187
188    if (stat != 0) {
189       goto done;
190    }
191    if (last_jobs->size() > 0) {
192       job = (struct s_last_job *)last_jobs->last();
193       stat = job->JobStatus;
194       switch (job->JobStatus) {
195       case JS_Canceled:
196          termstat = _("Bacula " BAC_COMPONENT ": Last Job Canceled");
197          break;
198       case JS_ErrorTerminated:
199       case JS_FatalError:
200          termstat = _("Bacula " BAC_COMPONENT ": Last Job Failed");
201          break;
202       default:
203          if (job->Errors) {
204             termstat = _("Bacula " BAC_COMPONENT ": Last Job had Warnings");
205          }
206          break;
207       }
208    }
209    Dmsg0(1000, "End bac_status jcr loop.\n");
210 done:
211    bacstat = stat;
212    if (buf) {
213       bstrncpy(buf, termstat, buf_len);
214    }
215    return buf;
216 }
217
218 #endif /* HAVE_WIN32 */
219
220
221 #endif