Projet

Général

Profil

check_bareos.patch

Emmanuel GARETTE, 09/09/2015 11:48

Télécharger (27,2 ko)

Voir les différences:

check_bareos/Makefile.in
1
#
2
#  Version $Id: Makefile.in,v 1.4 2004/09/25 10:19:59 nboichat Exp $
3
#
4
@MCOMMON@
5

  
6
srcdir = ../src
7

  
8
# one up
9
basedir = ../src/include
10
# top dir
11
topdir = ..
12
# this dir relative to top dir
13
thisdir = ../check_bareos
14

  
15
DEBUG=@DEBUG@
16

  
17
first_rule: all
18
dummy:
19

  
20
#
21
CHECKSRCS = check_bareos.c authenticate.c
22
CHECKOBJS = check_bareos.o authenticate.o
23

  
24
CHECK_CPPFLAGS=
25
CHECK_LDFLAGS=
26

  
27
.SUFFIXES:	.c .o
28
.PHONY:
29
.DONTCARE:
30

  
31
# inference rules
32
.c.o:
33
	@echo "Compiling $<"
34
	$(NO_ECHO) $(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) $(CHECK_CPPFLAGS) \
35
 -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CXXFLAGS) $<
36
#-------------------------------------------------------------------------
37
all: Makefile check_bareos
38
	@echo "==== Make of check_bareos is good ===="
39
	@echo " "
40

  
41
check_bareos: Makefile $(CHECKOBJS) $(srcdir)/lib/libbareos$(DEFAULT_ARCHIVE_TYPE)
42
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) $(CHECK_LDFLAGS) -L$(srcdir)/lib -o $@ \
43
	  $(CHECKOBJS) $(DLIB) -lbareos -lm $(LIBS) $(OPENSSL_LIBS)
44

  
45

  
46
Makefile: $(srcdir)/Makefile.in $(topdir)/config.status
47
	cd $(topdir) \
48
	  && CONFIG_FILES=$(thisdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
49

  
50
libtool-clean:
51
	$(RMF) -r .libs _libs
52

  
53
clean:
54
	@$(RMF) check_bareos core core.* a.out *.o *.bak *~ *.intpro *.extpro 1 2 3
55

  
56
realclean: clean
57
	@$(RMF) tags
58

  
59
distclean: realclean
60
	if test $(srcdir) = .; then $(MAKE) realclean; fi
61
	(cd $(srcdir); $(RMF) Makefile)
62

  
63
devclean: realclean
64
	if test $(srcdir) = .; then $(MAKE) realclean; fi
65
	(cd $(srcdir); $(RMF) Makefile)
66

  
67
install: all
68
	$(INSTALL_PROGRAM) check_bareos $(DESTDIR)$(sbindir)/check_bareos
69

  
70
uninstall:
71
	(cd $(DESTDIR)$(sbindir); $(RMF) check_bareos)
72

  
73

  
74

  
75
# Semi-automatic generation of dependencies:
76
# Use gcc -MM because X11 `makedepend' doesn't work on all systems
77
# and it also includes system headers.
78
# `semi'-automatic since dependencies are generated at distribution time.
79

  
80
depend:
81
	@$(MV) Makefile Makefile.bak
82
	@$(SED) "/^# DO NOT DELETE:/,$$ d" Makefile.bak > Makefile
83
	@$(ECHO) "# DO NOT DELETE: nice dependency list follows" >> Makefile
84
	@$(CXX) -S -M $(CPPFLAGS) $(CHECK_CPPFLAGS) -I$(srcdir) -I$(basedir) *.c >> Makefile
85
	@if test -f Makefile ; then \
86
	    $(RMF) Makefile.bak; \
87
	else \
88
	   $(MV) Makefile.bak Makefile; \
89
	   echo -e "Something went wrong\n\a"; \
90
	fi
91

  
92
# -----------------------------------------------------------------------
93
# DO NOT DELETE: nice dependency list follows
check_bareos/README
1
# It's more or less untested, though.
2
#  Submitted by Arno Lehmann <al@its-lehmann.de>
3
#
4

  
5
run ./configure with your usual options at the toplevel. Afterwards, use
6
'make' to create your binaries.
7

  
8
If you encounter problems, you'll have to setup the Bacula source manually.
9

  
10
Note that check_bareos does not support TLS and is an unsupported add-on to
11
Bacula. Even if it's not part of the core Bacula programs, questions can be
12
asked at the bareos-users mailing list.
13

  
14
Bacula is a Trademark of Kern Sibbald. Bacula and the accompanying programs
15
are open source. See the LICENSE file for more information.
check_bareos/authenticate.c
1
/*
2
 *
3
 *   Bacula authentication. Provides authentication with
4
 *     File and Storage daemons.
5
 *
6
 *     Nicolas Boichat, August MMIV
7
 *
8
 *    This routine runs as a thread and must be thread reentrant.
9
 *
10
 *  Basic tasks done here:
11
 *
12
 */
13
/*
14
   Bacula® - The Network Backup Solution
15

  
16
   Copyright (C) 2004-2010 Free Software Foundation Europe e.V.
17

  
18
   The main author of Bacula is Kern Sibbald, with contributions from
19
   many others, a complete list can be found in the file AUTHORS.
20
   This program is Free Software; you can redistribute it and/or
21
   modify it under the terms of version three of the GNU Affero General Public
22
   License as published by the Free Software Foundation plus additions
23
   that are listed in the file LICENSE.
24

  
25
   This program is distributed in the hope that it will be useful, but
26
   WITHOUT ANY WARRANTY; without even the implied warranty of
27
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28
   General Public License for more details.
29

  
30
   You should have received a copy of the GNU Affero General Public License
31
   along with this program; if not, write to the Free Software
32
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33
   02110-1301, USA.
34

  
35
   Bacula® is a registered trademark of John Walker.
36
   The licensor of Bacula is the Free Software Foundation Europe
37
   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
38
   Switzerland, email:ftf@fsfeurope.org.
39
*/
40

  
41
#include "bareos.h"
42
#include "check_bareos.h"
43

  
44
void senditf(const char *fmt, ...);
45
void sendit(const char *buf);
46

  
47
/* Commands sent to Director */
48
static char DIRhello[]    = "Hello %s calling\n";
49

  
50
/* Response from Director */
51
static char DIROKhello[]   = "1000 OK:";
52

  
53
/* Commands sent to Storage daemon and File daemon and received
54
 *  from the User Agent */
55
static char SDFDhello[]    = "Hello Director %s calling\n";
56

  
57
/* Response from SD */
58
static char SDOKhello[]   = "3000 OK Hello\n";
59
/* Response from FD */
60
static char FDOKhello[] = "2000 OK Hello";
61

  
62
/* Forward referenced functions */
63

  
64
/*
65
 * Authenticate Director
66
 */
67
int authenticate_director(BSOCK *dir, char *dirname, char *password)
68
{
69
   int tls_local_need = BNET_TLS_NONE;
70
   int tls_remote_need = BNET_TLS_NONE;
71
   bool compatible = true;
72
   char bashed_name[MAX_NAME_LENGTH];
73

  
74
   bstrncpy(bashed_name, dirname, sizeof(bashed_name));
75
   bash_spaces(bashed_name);
76

  
77
   /* Timeout Hello after 5 mins */
78
   btimer_t *tid = start_bsock_timer(dir, 60 * 5);
79
   bnet_fsend(dir, DIRhello, bashed_name);
80

  
81
   if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
82
       !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
83
      stop_bsock_timer(tid);
84
      return 0;
85
   }
86

  
87
   Dmsg1(6, ">dird: %s", dir->msg);
88
   if (bnet_recv(dir) <= 0) {
89
      stop_bsock_timer(tid);
90
      return 0;
91
   }
92
   Dmsg1(10, "<dird: %s", dir->msg);
93
   stop_bsock_timer(tid);
94
   if (!bstrncmp(dir->msg, DIROKhello, sizeof(DIROKhello)-1)) {
95
      return 0;
96
   }
97
   return 1;
98
}
99

  
100
/*
101
 * Authenticate Storage daemon connection
102
 */
103
int authenticate_storage_daemon(BSOCK *sd, char *sdname, char* password)
104
{
105
   char dirname[MAX_NAME_LENGTH];
106
   int tls_local_need = BNET_TLS_NONE;
107
   int tls_remote_need = BNET_TLS_NONE;
108
   bool compatible = true;
109

  
110
   /*
111
    * Send my name to the Storage daemon then do authentication
112
    */
113
   bstrncpy(dirname, sdname, sizeof(dirname));
114
   bash_spaces(dirname);
115
   /* Timeout Hello after 5 mins */
116
   btimer_t *tid = start_bsock_timer(sd, 60 * 5);
117
   if (!bnet_fsend(sd, SDFDhello, dirname)) {
118
      stop_bsock_timer(tid);
119
      return 0;
120
   }
121
   if (!cram_md5_respond(sd, password, &tls_remote_need, &compatible) ||
122
       !cram_md5_challenge(sd, password, tls_local_need, compatible)) {
123
      stop_bsock_timer(tid);
124
      return 0;
125
   }
126
   Dmsg1(116, ">stored: %s", sd->msg);
127
   if (bnet_recv(sd) <= 0) {
128
      stop_bsock_timer(tid);
129
      return 0;
130
   }
131
   Dmsg1(110, "<stored: %s", sd->msg);
132
   stop_bsock_timer(tid);
133
   if (!bstrncmp(sd->msg, SDOKhello, sizeof(SDOKhello))) {
134
      return 0;
135
   }
136
   return 1;
137
}
138

  
139
/*
140
 * Authenticate File daemon connection
141
 */
142
int authenticate_file_daemon(BSOCK *fd, char *fdname, char *password)
143
{
144
   char dirname[MAX_NAME_LENGTH];
145
   int tls_local_need = BNET_TLS_NONE;
146
   int tls_remote_need = BNET_TLS_NONE;
147
   bool compatible = true;
148

  
149
   /*
150
    * Send my name to the File daemon then do authentication
151
    */
152
   bstrncpy(dirname, fdname, sizeof(dirname));
153
   bash_spaces(dirname);
154
   /* Timeout Hello after 5 mins */
155
   btimer_t *tid = start_bsock_timer(fd, 60 * 5);
156
   if (!bnet_fsend(fd, SDFDhello, dirname)) {
157
      stop_bsock_timer(tid);
158
      return 0;
159
   }
160
   if (!cram_md5_respond(fd, password, &tls_remote_need, &compatible) ||
161
       !cram_md5_challenge(fd, password, tls_local_need, compatible)) {
162
      stop_bsock_timer(tid);
163
      return 0;
164
   }
165
   Dmsg1(116, ">filed: %s", fd->msg);
166
   if (bnet_recv(fd) <= 0) {
167
      stop_bsock_timer(tid);
168
      return 0;
169
   }
170
   Dmsg1(110, "<stored: %s", fd->msg);
171
   stop_bsock_timer(tid);
172
   if (!bstrncmp(fd->msg, FDOKhello, strlen(FDOKhello))) {
173
      return 0;
174
   }
175
   return 1;
176
}
check_bareos/check_bareos.c
1
/*
2
 *
3
 *   Nagios Plugin check_bareos
4
 *
5
 *     Christian Masopust, (c)2005
6
 *
7
 *     Version $Id: check_bareos.c,v 1.0 2005/02/25
8
 */
9

  
10
/*
11
   Copyright (C) 2005 Christian Masopust
12

  
13
   This library is free software; you can redistribute it and/or
14
   modify it under the terms of the GNU Lesser General Public
15
   License as published by the Free Software Foundation; either
16
   version 2.1 of the License, or (at your option) any later version.
17

  
18
   This library is distributed in the hope that it will be useful,
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
   Lesser General Public License for more details.
22

  
23
   You should have received a copy of the GNU Lesser General Public
24
   License along with this library; if not, write to the Free
25
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26
   MA 02111-1307, USA.
27

  
28
 */
29

  
30
#include "bareos.h"
31
#include "check_bareos.h"
32

  
33
#define STATE_OK 0
34
#define STATE_WARNING 1
35
#define STATE_CRITICAL 2
36
#define STATE_UNKNOWN 3
37

  
38

  
39
/* Imported functions */
40
int authenticate_director(BSOCK *s, char *dirname, char *password);
41
int authenticate_file_daemon(BSOCK *s, char *fdname, char *password);
42
int authenticate_storage_daemon(BSOCK *s, char* sdname, char *password);
43

  
44
/* Forward referenced functions */
45
void writecmd(monitoritem* item, const char* command);
46
int docmd(monitoritem* item, const char* command, char *answer);
47

  
48
/* Static variables */
49
static monitoritem mitem;
50

  
51
/* Data received from DIR/FD/SD */
52
static char OKqstatus[]   = "%c000 OK .status\n";
53

  
54

  
55

  
56
static void usage()
57
{
58
   fprintf(stderr, _(
59
"Copyright (C) 2005 Christian Masopust\n"
60
"Written by Christian Masopust (2005)\n"
61
"\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
62
"Usage: check_bareos [-d debug_level] -H host -D daemon -M name -P port\n"
63
"       -H <host>     hostname where daemon runs\n"
64
"       -D <daemon>   which daemon to check: dir|sd|fd\n"
65
"       -M <name>     name of monitor (as in bareos-*.conf)\n"
66
"       -K <md5-hash> password for access to daemon\n"
67
"       -P <port>     port where daemon listens\n"
68
"       -dnn          set debug level to nn\n"
69
"       -?            print this message.\n"
70
"\n"), HOST_OS, DISTNAME, DISTVER);
71
}
72

  
73

  
74
/*********************************************************************
75
 *
76
 *	   Main Bacula Tray Monitor -- User Interface Program
77
 *
78
 */
79
int main(int argc, char *argv[])
80
{
81
   int ch;
82
   DIRRES s_dird;
83
   CLIENTRES s_filed;
84
   STORERES s_stored;
85

  
86
   char host[250];
87
   char daemon[20];
88
   char monitorname[100];
89
   char pw[200];
90
   int port = 0;
91

  
92
   char answer[1024];
93
   int retcode = STATE_UNKNOWN;
94

  
95
   unsigned int i, j;
96
   MD5_CTX md5c;
97
   unsigned char signature[16];
98

  
99

  
100
   struct sigaction sigignore;
101
   sigignore.sa_flags = 0;
102
   sigignore.sa_handler = SIG_IGN;
103
   sigfillset(&sigignore.sa_mask);
104
   sigaction(SIGPIPE, &sigignore, NULL);
105

  
106
   strcpy (pw, "");
107

  
108
   init_stack_dump();
109
   my_name_is(argc, argv, "check_bareos");
110
   textdomain("bareos");
111
   init_msg(NULL, NULL);
112

  
113
   while ((ch = getopt(argc, argv, "H:D:M:P:K:d:h?")) != -1) {
114

  
115
      switch (ch) {
116

  
117
		  case 'H':
118
		  	strcpy (host, optarg);
119
		  	break;
120

  
121
		  case 'D':
122
		  	strcpy (daemon, optarg);
123
		  	break;
124

  
125
		  case 'M':
126
		  	strcpy (monitorname, optarg);
127
		  	break;
128

  
129
		  case 'P':
130
		  	port = atoi(optarg);
131
		  	break;
132

  
133
		  case 'K':
134
		  	strcpy (pw, optarg);
135
		  	break;
136

  
137
		  case 'd':
138
		 	debug_level = atoi(optarg);
139
		 	if (debug_level <= 0) {
140
		 	   debug_level = 1;
141
		 	}
142
		 	break;
143

  
144
      	  case 'h':
145
      	  case '?':
146
      	  default:
147
			 usage();
148
			 exit(1);
149
      }
150
   }
151
   argc -= optind;
152
   //argv += optind;
153

  
154
   if (argc) {
155
      usage();
156
      exit(STATE_UNKNOWN);
157
   }
158

  
159
   lmgr_init_thread();
160

  
161
   char sig[100];
162
   MD5_Init(&md5c);
163
   MD5_Update(&md5c, (unsigned char *) pw, strlen(pw));
164
   MD5_Final(signature, &md5c);
165
   for (i = j = 0; i < sizeof(signature); i++) {
166
      sprintf(&sig[j], "%02x", signature[i]);
167
      j += 2;
168
   }
169

  
170

  
171
   /* director ?  */
172
   if (bstrcmp(daemon, "dir")) {
173

  
174
	   if (port != 0)
175
	   	 s_dird.DIRport = port;
176
	   else
177
	   	 s_dird.DIRport = 9101;
178

  
179
	   s_dird.address  = host;
180
	   s_dird.password = sig;
181
	   s_dird.hdr.name = monitorname;
182

  
183
	   mitem.type = R_DIRECTOR;
184
	   mitem.resource = &s_dird;
185
	   mitem.D_sock = NULL;
186

  
187
   } else if (bstrcmp(daemon, "sd")) {
188

  
189
	   if (port != 0)
190
	   	 s_stored.SDport = port;
191
	   else
192
	   	 s_stored.SDport = 9103;
193

  
194
	   s_stored.address = host;
195
	   s_stored.password = sig;
196
	   s_stored.hdr.name = monitorname;
197

  
198
	   mitem.type = R_STORAGE;
199
	   mitem.resource = &s_stored;
200
	   mitem.D_sock = NULL;
201

  
202
   } else if (bstrcmp(daemon, "fd")) {
203

  
204
	   if (port != 0)
205
	   	 s_filed.FDport = port;
206
	   else
207
	   	 s_filed.FDport = 9102;
208

  
209
	   s_filed.address = host;
210
	   s_filed.password = sig;
211
	   s_filed.hdr.name = monitorname;
212

  
213
	   mitem.type = R_CLIENT;
214
	   mitem.resource = &s_filed;
215
	   mitem.D_sock = NULL;
216

  
217
   } else {
218

  
219
	   usage();
220
	   exit(1);
221
   }
222

  
223

  
224
   if (mitem.type == R_DIRECTOR)
225
	   retcode = docmd(&mitem, ".status dir current\n", answer);
226
   else
227
	   retcode = docmd(&mitem, ".status current\n", answer);
228

  
229

  
230
   if (mitem.D_sock) {
231
        mitem.D_sock->signal(BNET_TERMINATE); /* send EOF */
232
        mitem.D_sock->close();
233
        mitem.D_sock = NULL;
234
   }
235

  
236
   printf ("%s\n", answer);
237
   return retcode;
238
}
239

  
240

  
241
static int authenticate_daemon(monitoritem* item) {
242

  
243
   DIRRES *d;
244
   CLIENTRES *f;
245
   STORERES *s;
246

  
247
   switch (item->type) {
248
   case R_DIRECTOR:
249
      d = (DIRRES *)item->resource;
250
      return authenticate_director(item->D_sock, d->hdr.name, d->password);
251
      break;
252
   case R_CLIENT:
253
      f = (CLIENTRES *)item->resource;
254
      return authenticate_file_daemon(item->D_sock, f->hdr.name, f->password);
255
      break;
256
   case R_STORAGE:
257
      s = (STORERES *)item->resource;
258
      return authenticate_storage_daemon(item->D_sock, s->hdr.name, s->password);
259
      break;
260
   default:
261
      printf("Error, currentitem is not a Client or a Storage..\n");
262
      return FALSE;
263
   }
264
}
265

  
266

  
267

  
268
int docmd(monitoritem* item, const char* command, char *answer) {
269

  
270
   int stat;
271
   char num;
272
   const char *dname;
273

  
274
   dname = "";
275

  
276
   if (!item->D_sock) {
277

  
278
      DIRRES* dird;
279
      CLIENTRES* filed;
280
      STORERES* stored;
281
      item->D_sock = (BSOCK *)malloc(sizeof(BSOCK));
282
//      item->D_sock->init();
283

  
284
      switch (item->type) {
285
      case R_DIRECTOR:
286
		 dird = (DIRRES*)item->resource;
287
         item->D_sock->connect(NULL, 0, 0, 0, "Director daemon" , dird->address, NULL, dird->DIRport, 0);
288
		 dname = "Director";
289
		 break;
290
      case R_CLIENT:
291
		 filed = (CLIENTRES*)item->resource;
292
         item->D_sock->connect(NULL, 0, 0, 0, "File daemon" , filed->address, NULL, filed->FDport, 0);
293
		 dname = "FileDaemon";
294
		 break;
295
      case R_STORAGE:
296
		 stored = (STORERES*)item->resource;
297
         item->D_sock->connect(NULL, 0, 0, 0, "Storage daemon" , stored->address, NULL, stored->SDport, 0);
298
		 dname = "StorageDaemon";
299
		 break;
300
      default:
301
		 printf("Error, currentitem is not a Client, a Storage or a Director..\n");
302
		 return STATE_UNKNOWN;
303
      }
304

  
305
      if (item->D_sock == NULL) {
306
      		 sprintf (answer, "BACULA CRITICAL - Cannot connect to %s!", dname);
307
		 return STATE_CRITICAL;
308
      }
309

  
310
      if (!authenticate_daemon(item)) {
311
	 	sprintf (answer, "BACULA CRITICAL - Cannot authenticate to %s: %s", dname, item->D_sock->msg);
312
	 	item->D_sock = NULL;
313
	 	return STATE_CRITICAL;
314
      }
315

  
316
   }
317

  
318
   if (command[0] != 0)
319
      writecmd(item, command);
320

  
321
   while(1) {
322
      if ((stat = bnet_recv(item->D_sock)) >= 0) {
323

  
324
	/* welcome message of director */
325
	if ((item->type == R_DIRECTOR) && bstrncmp(item->D_sock->msg, "Using ", 6))
326
		continue;
327

  
328
	if (sscanf(item->D_sock->msg, OKqstatus, &num) != 1) {
329
		/* Error, couldn't find OK */
330
		sprintf (answer, "BACULA CRITICAL - %s Status: %s", dname, item->D_sock->msg);
331
		return STATE_CRITICAL;
332
	} else {
333
		sprintf (answer, "BACULA OK - %s Status OK", dname);
334
		return STATE_OK;
335
	}
336
      }
337
      else if (stat == BNET_SIGNAL) {
338
	 	if (item->D_sock->msglen == BNET_EOD) {
339
	    	strcpy(answer, "BACULA WARNING - << EOD >>");
340
	    	return STATE_WARNING;
341
	 }
342
	 else if (item->D_sock->msglen == BNET_SUB_PROMPT) {
343
	    strcpy(answer, "BACULA WARNING - BNET_SUB_PROMPT signal received.");
344
	    return STATE_WARNING;
345
	 }
346
	 else if (item->D_sock->msglen == BNET_HEARTBEAT) {
347
	    bnet_sig(item->D_sock, BNET_HB_RESPONSE);
348
	 }
349
	 else {
350
		sprintf(answer, "BACULA WARNING - Unexpected signal received : %s ", bnet_sig_to_ascii(item->D_sock));
351
	 }
352
      }
353
      else { /* BNET_HARDEOF || BNET_ERROR */
354
		 strcpy(answer, "BACULA CRITICAL - ERROR: BNET_HARDEOF or BNET_ERROR");
355
		 item->D_sock = NULL;
356
		 return STATE_CRITICAL;
357
      }
358

  
359
      if (is_bnet_stop(item->D_sock)) {
360
		 item->D_sock = NULL;
361
		 return STATE_WARNING;
362
      }
363
   }
364
}
365

  
366
void writecmd(monitoritem* item, const char* command) {
367
   if (item->D_sock) {
368
      item->D_sock->msglen = strlen(command);
369
      pm_strcpy(&item->D_sock->msg, command);
370
      bnet_send(item->D_sock);
371
   }
372
}
373

  
check_bareos/check_bareos.h
1
/*
2
   Copyright (C) 2004 Kern Sibbald and John Walker
3

  
4
   This program is free software; you can redistribute it and/or
5
   modify it under the terms of the GNU General Public License as
6
   published by the Free Software Foundation; either version 2 of
7
   the License, or (at your option) any later version.
8

  
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
   General Public License for more details.
13

  
14
   You should have received a copy of the GNU General Public
15
   License along with this program; if not, write to the Free
16
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17
   MA 02111-1307, USA.
18

  
19
 */
20
/*
21
 * Includes specific to the tray monitor
22
 *
23
 *     Nicolas Boichat, August MMIV
24
 */
25

  
26
/*
27
 * Resource codes -- they must be sequential for indexing
28
 */
29
enum rescode {
30
   R_MONITOR = 1001,
31
   R_DIRECTOR,
32
   R_CLIENT,
33
   R_STORAGE,
34
   R_FIRST = R_MONITOR,
35
   R_LAST  = R_STORAGE                /* keep this updated */
36
};
37

  
38
/*
39
 * Some resource attributes
40
 */
41
enum {
42
   R_NAME = 1020,
43
   R_ADDRESS,
44
   R_PASSWORD,
45
   R_TYPE,
46
   R_BACKUP
47
};
48

  
49
/* Director */
50
struct DIRRES {
51
   RES hdr;
52

  
53
   int DIRport;                       /* UA server port */
54
   char *address;                     /* UA server address */
55
   char *password;                    /* UA server password */
56
   int enable_ssl;                    /* Use SSL */
57
};
58

  
59
/*
60
 *   Tray Monitor Resource
61
 *
62
 */
63
//struct MONITORRES {
64
//   RES hdr;
65
//
66
//   int require_ssl;                   /* Require SSL for all connections */
67
//   MSGSRES *messages;                 /* Daemon message handler */
68
//   char *password;                    /* UA server password */
69
//   utime_t RefreshInterval;           /* Status refresh interval */
70
//   utime_t FDConnectTimeout;          /* timeout for connect in seconds */
71
//   utime_t SDConnectTimeout;          /* timeout in seconds */
72
//};
73

  
74
/*
75
 *   Client Resource
76
 *
77
 */
78
struct CLIENTRES {
79
   RES hdr;
80

  
81
   int FDport;                        /* Where File daemon listens */
82
   char *address;
83
   char *password;
84
   int enable_ssl;                    /* Use SSL */
85
};
86

  
87
/*
88
 *   Store Resource
89
 *
90
 */
91
struct STORERES {
92
   RES hdr;
93

  
94
   int SDport;                        /* port where Directors connect */
95
   char *address;
96
   char *password;
97
   int enable_ssl;                    /* Use SSL */
98
};
99

  
100
/* Define the Union of all the above
101
 * resource structure definitions.
102
 */
103
//union URES {
104
//   //MONITOR res_monitor;
105
//   DIRRES res_dir;
106
//   CLIENT res_client;
107
//   STORE res_store;
108
//   RES hdr;
109
//};
110

  
111
struct monitoritem {
112
   rescode type; /* R_DIRECTOR, R_CLIENT or R_STORAGE */
113
   void* resource; /* DIRRES*, CLIENT* or STORE* */
114
   BSOCK *D_sock;
115
};
check_bareos/check_bareos_pools.sh
1
#! /bin/sh
2
# Author : Ludovic Strappazon. l.strappazon@gmail.com
3
# Copyright 2004, Free Software Foundation Europe e.V.
4
# Any comment, advice or enhancement are welcome  :-)
5

  
6
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
7
MYSQL="/usr/bin/mysql -u bacula --password=mypassword"
8
TMP=/tmp
9
BACULA=/usr/local/bacula
10

  
11
PROGNAME=`basename $0`
12
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
13
STATUS=""
14

  
15
. $PROGPATH/utils.sh
16

  
17
print_usage() {
18
        echo "Usage: $PROGNAME -P <pool> -M <media-type> -w <warning threshold> -c <critical threshold> [-S]"
19
}
20

  
21
print_help() {
22
        echo ""
23
        print_usage
24
        echo ""
25
        echo "This plugin checks the space available in the pool against the space required for the next scheduled backups"
26
        echo "Example : $PROGNAME -P default -M LTO -w 20 -c 10 will check the default pool, return OK if (available space) > 1,20*(required space), WARNING if 1,20*(required space) > (available space) > 1,10*(required space), and CRITICAL else."
27
        echo ""
28
        echo "With the -S option, it will check the pool named Scratch and return WARNING instead of CRITICAL if the Scratch pool can save the situation."
29
        echo "Example : $PROGNAME -P default -M LTO -w 20 -c 10 -S will check the default pool, return OK if (available space) > 1,20*(required space), WARNING if 1,20*(required space) > (available space) > 1,10*(required space) or if (available space in default and Scratch) > 1,10*(required space) > (available space in default), and CRITICAL else."
30
        echo ""
31
        echo "The evaluation of the space required is done by adding the biggest backups of the same level than the scheduled jobs"
32
        echo "The available space is evaluated by the number of out of retention tapes and the average VolBytes of these Full tapes"
33
        echo ""
34
        echo "The Information Status are : \"Required, Available, Volume Errors\" and \"Will use Scratch pool\" if necessary."
35
        echo ""
36
        echo "I think this plugin should be used in passive mode, and ran by a RunAfterJob"
37
        exit 3
38
}
39

  
40
NB_ARGS=$#
41
SCRATCH=0
42
while getopts :P:M:w:c:hS OPTION
43
do
44
  case $OPTION in
45
    P) POOL="$OPTARG"
46
       ;;
47
    M) MEDIA_TYPE="$OPTARG"
48
       ;;
49
    S) SCRATCH=1
50
       ;;
51
    w) WARNING="$OPTARG"
52
       ;;
53
    c) CRITICAL="$OPTARG"
54
       ;;
55
    h) print_help
56
       exit 3
57
       ;;
58
    *) print_usage
59
       exit 3
60
       ;;
61
  esac
62
done
63
shift $(($OPTIND - 1))
64

  
65
if [ "$NB_ARGS" -ne 8 -a "$NB_ARGS" -ne 9 ]; then
66
        print_revision $PROGNAME 25/05/2005
67
        print_usage
68
        exit 3
69
fi
70

  
71
LAST_CHECK=`ps -ef | grep check_ba[Cc]ula_pools.sh | awk {'print $5'} | uniq | wc -l`
72
if [ "$LAST_CHECK" -gt 1 ]; then
73
        echo "The last check was not complete, you should increase the check_period."
74
        exit 3
75
fi
76

  
77
  NB_VOLUMES_OUT_OF_RETENTION=`$MYSQL << EOF
78
USE bacula
79
SELECT COUNT(MediaId) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND LastWritten <> "0000-00-00 00:00:00" AND UNIX_TIMESTAMP()-UNIX_TIMESTAMP(LastWritten)>Media.VolRetention AND Inchanger = "1";
80
EOF
81
`
82
  NB_VOLUMES_OUT_OF_RETENTION=`echo $NB_VOLUMES_OUT_OF_RETENTION | cut -f 2 -d ' '`
83

  
84
NB_VOLUMES_ERROR=`$MYSQL << EOF
85
USE bacula
86
SELECT COUNT(MediaId) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND VolStatus="Error" AND Inchanger = "1";
87
EOF
88
`
89
NB_VOLUMES_ERROR=`echo $NB_VOLUMES_ERROR | cut -f 2 -d ' '`
90

  
91
AVERAGE_CAPA_VOLUME=`$MYSQL << EOF
92
USE bacula
93
SELECT SUM(VolBytes)/COUNT(MediaId) FROM Media where VolStatus="Full" AND MediaType="$MEDIA_TYPE";
94
EOF
95
`
96
AVERAGE_CAPA_VOLUME=`echo $AVERAGE_CAPA_VOLUME | cut -f 2 -d ' ' | cut -f 1 -d '.'`
97

  
98
CAPA_VOLUMES_APPEND=`$MYSQL << EOF
99
USE bacula
100
SELECT SUM("$AVERAGE_CAPA_VOLUME"-VolBytes) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND (VolStatus = "Append" OR VolStatus = "Recycle" OR VolStatus = "Purge") AND Inchanger = "1" AND MediaType="$MEDIA_TYPE";
101
EOF
102
`
103
CAPA_VOLUMES_APPEND=`echo $CAPA_VOLUMES_APPEND | cut -f 2 -d ' '`
104

  
105
if [ $SCRATCH -eq 1 ]
106
then
107
CAPA_VOLUMES_SCRATCH=`$MYSQL << EOF
108
USE bacula
109
SELECT SUM("$AVERAGE_CAPA_VOLUME"-VolBytes) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="Scratch" AND VolStatus = "Append" AND Inchanger = "1" AND MediaType="$MEDIA_TYPE";
110
EOF
111
`
112
CAPA_VOLUMES_SCRATCH=`echo $CAPA_VOLUMES_SCRATCH | cut -f 2 -d ' '`
113
else
114
CAPA_VOLUMES_SCRATCH=0
115
fi
116

  
117
echo "st
118
1
119
q" | $BACULA/etc/bconsole | sed -n /Scheduled/,/Running/p | grep Backup | tr -s [:blank:] | tr '[:blank:]' '@' > ${TMP}/Scheduled.txt
120

  
121
CAPA_REQUIRED=0
122
for LINE in `cat ${TMP}/Scheduled.txt`
123
do
124
  SCHEDULED_JOB=`echo $LINE | awk -F@ '{print $6}'`
125
  LEVEL=`echo $LINE | awk -F@ '{print $1}' | cut -c 1`
126

  
127
MAX_VOLUME_JOB_FOR_LEVEL=`$MYSQL << EOF
128
USE bacula
129
SELECT MAX(JobBytes) from Job, Pool where Level="$LEVEL" AND Job.Name="$SCHEDULED_JOB" AND Job.PoolId=Pool.PoolId AND Pool.Name="$POOL";
130
EOF
131
`
132
MAX_VOLUME_JOB_FOR_LEVEL=`echo $MAX_VOLUME_JOB_FOR_LEVEL | cut -f 2 -d ' ' `
133

  
134
CAPA_REQUIRED=$[CAPA_REQUIRED+MAX_VOLUME_JOB_FOR_LEVEL]
135
done
136

  
137
rm ${TMP}/Scheduled.txt
138

  
139
CAPA_WARNING=`echo $[(WARNING+100)*CAPA_REQUIRED]/100 | bc | cut -f 1 -d '.'`
140
CAPA_CRITICAL=`echo $[(CRITICAL+100)*CAPA_REQUIRED]/100 | bc | cut -f 1 -d '.'`
141
CAPA_DISP=$[NB_VOLUMES_OUT_OF_RETENTION*AVERAGE_CAPA_VOLUME+CAPA_VOLUMES_APPEND]
142
CAPA_DISP_INCLUDING_SCRATCH=$[CAPA_DISP+CAPA_VOLUMES_SCRATCH]
143

  
144
MESSAGE="Required : $[CAPA_REQUIRED/1000000000] Go, available : $[CAPA_DISP/1000000000] Go, Volumes Error : $NB_VOLUMES_ERROR"
145

  
146
if [ "$CAPA_DISP" -gt $CAPA_WARNING ]; then
147
   echo $MESSAGE
148
   exit 0
149
elif [ "$CAPA_DISP" -gt $CAPA_CRITICAL ];then
150
   echo $MESSAGE
151
   exit 1
152
elif [ "$CAPA_DISP_INCLUDING_SCRATCH" -gt $CAPA_CRITICAL ];then
153
   MESSAGE="${MESSAGE}. Will use Scratch Pool !"
154
   echo $MESSAGE
155
   exit 1
156
else
157
   exit 2
158
fi
159
exit 3