slapd-watcher.c 20 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1999-2020 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Howard Chu for inclusion
 * in OpenLDAP Software.
 */

#include "portable.h"

#include <stdio.h>

#include "ac/stdlib.h"
#include "ac/time.h"

#include "ac/ctype.h"
#include "ac/param.h"
#include "ac/socket.h"
#include "ac/string.h"
#include "ac/unistd.h"
#include "ac/wait.h"
#include "ac/time.h"

#include "ldap.h"
#include "lutil.h"
#include "lutil_ldap.h"
#include "lber_pvt.h"
#include "ldap_pvt.h"

#include "slapd-common.h"

Howard Chu's avatar
Howard Chu committed
43
44
#define SLAP_SYNC_SID_MAX	4095

45
46
47
48
#define	HAS_MONITOR	1
#define	HAS_BASE	2
#define	HAS_ENTRIES	4
#define	HAS_SREPL	8
Howard Chu's avatar
Howard Chu committed
49
50
51
52
53
#define HAS_ALL (HAS_MONITOR|HAS_BASE|HAS_ENTRIES|HAS_SREPL)


#define WAS_LATE	0x100
#define WAS_DOWN	0x200
54
55
56

#define	MONFILTER	"(objectClass=monitorOperation)"

Howard Chu's avatar
Howard Chu committed
57
static const char *default_monfilter = MONFILTER;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

typedef enum {
    SLAP_OP_BIND = 0,
    SLAP_OP_UNBIND,
    SLAP_OP_SEARCH,
    SLAP_OP_COMPARE,
    SLAP_OP_MODIFY,
    SLAP_OP_MODRDN,
    SLAP_OP_ADD,
    SLAP_OP_DELETE,
    SLAP_OP_ABANDON,
    SLAP_OP_EXTENDED,
    SLAP_OP_LAST
} slap_op_t;

struct opname {
	struct berval rdn;
	char *display;
} opnames[] = {
	{ BER_BVC("cn=Bind"),		"Bind" },
	{ BER_BVC("cn=Unbind"),		"Unbind" },
	{ BER_BVC("cn=Search"),		"Search" },
	{ BER_BVC("cn=Compare"),	"Compare" },
	{ BER_BVC("cn=Modify"),		"Modify" },
	{ BER_BVC("cn=Modrdn"),		"ModDN" },
	{ BER_BVC("cn=Add"),		"Add" },
	{ BER_BVC("cn=Delete"),		"Delete" },
	{ BER_BVC("cn=Abandon"),	"Abandon" },
	{ BER_BVC("cn=Extended"),	"Extended" },
	{ BER_BVNULL, NULL }
};

typedef struct counters {
	struct timeval time;
	unsigned long entries;
	unsigned long ops[SLAP_OP_LAST];
} counters;

typedef struct csns {
	struct berval *vals;
	struct timeval *tvs;
} csns;

typedef struct activity {
	time_t active;
	time_t idle;
	time_t maxlag;
	time_t lag;
} activity;

typedef struct server {
	char *url;
	LDAP *ld;
	int flags;
	int sid;
	struct berval monitorbase;
	char *monitorfilter;
Howard Chu's avatar
Howard Chu committed
115
116
	time_t late;
	time_t down;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	counters c_prev;
	counters c_curr;
	csns csn_prev;
	csns csn_curr;
	activity *times;
} server;

static void
usage( char *name, char opt )
{
	if ( opt ) {
		fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
			name, opt );
	}

	fprintf( stderr, "usage: %s "
		"[-D <dn> [ -w <passwd> ]] "
		"[-d <level>] "
		"[-O <SASL secprops>] "
		"[-R <SASL realm>] "
		"[-U <SASL authcid> [-X <SASL authzid>]] "
		"[-x | -Y <SASL mech>] "
		"[-i <interval>] "
		"[-s <sids>] "
		"[-b <baseDN> ] URI[...]\n",
		name );
	exit( EXIT_FAILURE );
}

struct berval base;
int interval = 10;
int numservers;
server *servers;
char *monfilter;

struct berval at_namingContexts = BER_BVC("namingContexts");
struct berval at_monitorOpCompleted = BER_BVC("monitorOpCompleted");
struct berval at_olmMDBEntries = BER_BVC("olmMDBEntries");
struct berval at_contextCSN = BER_BVC("contextCSN");

void timestamp(time_t *tt)
{
	struct tm *tm = gmtime(tt);
	printf("%d-%02d-%02d %02d:%02d:%02d",
		tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday,
		tm->tm_hour, tm->tm_min, tm->tm_sec);
}

void deltat(time_t *tt)
{
	struct tm *tm = gmtime(tt);
	if (tm->tm_mday-1)
		printf("%02d+", tm->tm_mday-1);
	printf("%02d:%02d:%02d",
		tm->tm_hour, tm->tm_min, tm->tm_sec);
}

static char *clearscreen = "\033[H\033[2J";

Howard Chu's avatar
Howard Chu committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
void rotate_stats( server *sv )
{
	if ( sv->flags & HAS_MONITOR )
		sv->c_prev = sv->c_curr;
	if ( sv->flags & HAS_BASE ) {
		int i;

		for (i=0; i<numservers; i++) {
			if ( sv->csn_curr.vals[i].bv_len ) {
				ber_bvreplace(&sv->csn_prev.vals[i],
					&sv->csn_curr.vals[i]);
				sv->csn_prev.tvs[i] = sv->csn_curr.tvs[i];
			} else {
				if ( sv->csn_prev.vals[i].bv_val )
					sv->csn_prev.vals[i].bv_val[0] = '\0';
			}
		}
	}
}

196
197
198
199
200
201
202
203
204
205
206
207
208
209
void display()
{
	int i, j;
	struct timeval now;
	struct tm *tm;
	time_t now_t;

	gettimeofday(&now, NULL);
	now_t = now.tv_sec;
	printf("%s", clearscreen);
	timestamp(&now_t);
	printf("\n");

	for (i=0; i<numservers; i++) {
Howard Chu's avatar
Howard Chu committed
210
211
212
213
214
215
216
217
218
219
		printf("\n%s", servers[i].url );
		if ( servers[i].flags & WAS_DOWN ) {
			printf(", down@");
			timestamp( &servers[i].down );
		}
		if ( servers[i].flags & WAS_LATE ) {
			printf(", late@");
			timestamp( &servers[i].late );
		}
		printf("\n");
220
		if ( servers[i].flags & HAS_MONITOR ) {
Howard Chu's avatar
Howard Chu committed
221
222
			struct timeval tv;
			double rate, duration;
223
			long delta;
224
225
226
227
228
229
230
231
232
233
234
235
236
			printf("      ");
			if ( servers[i].flags & HAS_ENTRIES )
				printf("  Entries  ");
			for ( j = 0; j<SLAP_OP_LAST; j++ )
				printf(" %9s ", opnames[j].display);
			printf("\n");
			printf("Num   ");
			if ( servers[i].flags & HAS_ENTRIES )
				printf("%10lu ", servers[i].c_curr.entries);
			for ( j = 0; j<SLAP_OP_LAST; j++ )
				printf("%10lu ", servers[i].c_curr.ops[j]);
			printf("\n");
			printf("Num/s ");
Howard Chu's avatar
Howard Chu committed
237
238
239
240
241
242
243
			tv.tv_usec = now.tv_usec - servers[i].c_prev.time.tv_usec;
			tv.tv_sec = now.tv_sec - servers[i].c_prev.time.tv_sec;
			if ( tv.tv_usec < 0 ) {
				tv.tv_usec += 1000000;
				tv.tv_sec--;
			}
			duration = tv.tv_sec + (tv.tv_usec / (double)1000000);
244
			if ( servers[i].flags & HAS_ENTRIES ) {
245
246
				delta = servers[i].c_curr.entries - servers[i].c_prev.entries;
				rate = delta / duration;
247
248
249
				printf("%10.2f ", rate);
			}
			for ( j = 0; j<SLAP_OP_LAST; j++ ) {
250
251
				delta = servers[i].c_curr.ops[j] - servers[i].c_prev.ops[j];
				rate = delta / duration;
252
253
254
255
256
				printf("%10.2f ", rate);
			}
			printf("\n");
		}
		if ( servers[i].flags & HAS_BASE ) {
Howard Chu's avatar
Howard Chu committed
257
258
259
260
261
			for (j=0; j<numservers; j++) {
				/* skip empty CSNs */
				if (!servers[i].csn_curr.vals[j].bv_len ||
					!servers[i].csn_curr.vals[j].bv_val[0])
					continue;
262
				printf("contextCSN: %s", servers[i].csn_curr.vals[j].bv_val );
Howard Chu's avatar
Howard Chu committed
263
264
				if (ber_bvcmp(&servers[i].csn_curr.vals[j],
							&servers[i].csn_prev.vals[j])) {
265
266
267
268
269
270
271
					/* a difference */
					if (servers[i].times[j].idle) {
						servers[i].times[j].idle = 0;
						servers[i].times[j].active = 0;
						servers[i].times[j].maxlag = 0;
						servers[i].times[j].lag = 0;
					}
Howard Chu's avatar
Howard Chu committed
272
active:
273
274
275
276
					if (!servers[i].times[j].active)
						servers[i].times[j].active = now_t;
					printf(" actv@");
					timestamp(&servers[i].times[j].active);
Howard Chu's avatar
Howard Chu committed
277
278
				} else if ( servers[i].times[j].lag || ( servers[i].flags & WAS_LATE )) {
					goto active;
279
280
281
282
283
284
285
286
287
288
289
290
				} else {
					if (servers[i].times[j].active && !servers[i].times[j].idle)
						servers[i].times[j].idle = now_t;
					if (servers[i].times[j].active) {
						printf(" actv@");
						timestamp(&servers[i].times[j].active);
						printf(", idle@");
						timestamp(&servers[i].times[j].idle);
					} else {
						printf(" idle");
					}
				}
Howard Chu's avatar
Howard Chu committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
				if (i != j) {
					if (ber_bvcmp(&servers[i].csn_curr.vals[j],
						&servers[j].csn_curr.vals[j])) {
						struct timeval delta;
						int ahead = 0;
						time_t deltatt;
						delta.tv_sec = servers[j].csn_curr.tvs[j].tv_sec -
							servers[i].csn_curr.tvs[j].tv_sec;
						delta.tv_usec = servers[j].csn_curr.tvs[j].tv_usec -
							servers[i].csn_curr.tvs[j].tv_usec;
						if (delta.tv_usec < 0) {
							delta.tv_usec += 1000000;
							delta.tv_sec--;
						}
						if (delta.tv_sec < 0) {
							delta.tv_sec = -delta.tv_sec;
							ahead = 1;
308
						}
Howard Chu's avatar
Howard Chu committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
						deltatt = delta.tv_sec;
						if (ahead)
							printf(", ahead ");
						else
							printf(", behind ");
						deltat( &deltatt );
						servers[i].times[j].lag = deltatt;
						if (deltatt > servers[i].times[j].maxlag)
							servers[i].times[j].maxlag = deltatt;
					} else {
						servers[i].times[j].lag = 0;
						printf(", sync'd");
					}
					if (servers[i].times[j].maxlag) {
						printf(", max delta ");
						deltat( &servers[i].times[j].maxlag );
325
326
327
328
329
					}
				}
				printf("\n");
			}
		}
Howard Chu's avatar
Howard Chu committed
330
331
		if ( !( servers[i].flags & WAS_LATE ))
			rotate_stats( &servers[i] );
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	}
}

void get_counters(
	LDAP *ld,
	LDAPMessage *e,
	BerElement *ber,
	counters *c )
{
	int i, rc;
	slap_op_t op = SLAP_OP_BIND;
	struct berval dn, bv, *bvals, **bvp = &bvals;

	do {
		int done = 0;
		for ( rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp );
			rc == LDAP_SUCCESS;
			rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp )) {

			if ( bv.bv_val == NULL ) break;
			if ( !ber_bvcmp( &bv, &at_monitorOpCompleted ) && bvals ) {
				c->ops[op] = strtoul( bvals[0].bv_val, NULL, 0 );
				done = 1;
			}
			if ( bvals ) {
				ber_memfree( bvals );
				bvals = NULL;
			}
			if ( done )
				break;
		}
		ber_free( ber, 0 );
		e = ldap_next_entry( ld, e );
		if ( !e )
			break;
		ldap_get_dn_ber( ld, e, &ber, &dn );
		op++;
	} while ( op < SLAP_OP_LAST );
}

int
slap_parse_csn_sid( struct berval *csnp )
{
	char *p, *q;
	struct berval csn = *csnp;
	int i;

	p = ber_bvchr( &csn, '#' );
	if ( !p )
		return -1;
	p++;
	csn.bv_len -= p - csn.bv_val;
	csn.bv_val = p;

	p = ber_bvchr( &csn, '#' );
	if ( !p )
		return -1;
	p++;
	csn.bv_len -= p - csn.bv_val;
	csn.bv_val = p;

	q = ber_bvchr( &csn, '#' );
	if ( !q )
		return -1;

	csn.bv_len = q - p;

	i = strtol( p, &q, 16 );
	if ( p == q || q != p + csn.bv_len || i < 0 || i > SLAP_SYNC_SID_MAX ) {
		i = -1;
	}

	return i;
}

void get_csns(
	csns *c,
	struct berval *bvs
)
{
Howard Chu's avatar
Howard Chu committed
412
	int i, j;
413

Howard Chu's avatar
Howard Chu committed
414
415
416
417
418
419
	/* clear old values if any */
	for (i=0; i<numservers; i++)
		if ( c->vals[i].bv_val )
			c->vals[i].bv_val[0] = '\0';

	for (i=0; bvs[i].bv_val; i++) {
420
421
		struct lutil_tm tm;
		struct lutil_timet tt;
Howard Chu's avatar
Howard Chu committed
422
423
424
425
426
427
428
429
430
431
		int sid = slap_parse_csn_sid( &bvs[i] );
		for (j=0; j<numservers; j++)
			if (sid == servers[j].sid) break;
		if (j < numservers) {
			ber_bvreplace( &c->vals[j], &bvs[i] );
			lutil_parsetime(bvs[i].bv_val, &tm);
			c->tvs[j].tv_usec = tm.tm_usec;
			lutil_tm2time( &tm, &tt );
			c->tvs[j].tv_sec = tt.tt_sec;
		}
432
433
434
	}
}

Howard Chu's avatar
Howard Chu committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
int
setup_server( struct tester_conn_args *config, server *sv, int first )
{
	config->uri = sv->url;
	tester_init_ld( &sv->ld, config, first ? 0 : TESTER_INIT_NOEXIT );
	if ( !sv->ld )
		return -1;

	sv->flags &= ~HAS_ALL;
	{
		char *attrs[] = { at_namingContexts.bv_val, at_monitorOpCompleted.bv_val,
			at_olmMDBEntries.bv_val, NULL };
		LDAPMessage *res = NULL, *e = NULL;
		BerElement *ber = NULL;
		LDAP *ld = sv->ld;
		struct berval dn, bv, *bvals, **bvp = &bvals;
		int j, rc;

		rc = ldap_search_ext_s( ld, "cn=monitor", LDAP_SCOPE_SUBTREE, monfilter,
			attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
		switch(rc) {
		case LDAP_SIZELIMIT_EXCEEDED:
		case LDAP_TIMELIMIT_EXCEEDED:
		case LDAP_SUCCESS:
			gettimeofday( &sv->c_curr.time, 0 );
			sv->flags |= HAS_MONITOR;
			for ( e = ldap_first_entry( ld, res ); e; e = ldap_next_entry( ld, e )) {
				ldap_get_dn_ber( ld, e, &ber, &dn );
				if ( !strncasecmp( dn.bv_val, "cn=Database", sizeof("cn=Database")-1 ) ||
					!strncasecmp( dn.bv_val, "cn=Frontend", sizeof("cn=Frontend")-1 )) {
					int matched = 0;
					for ( rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp );
						rc == LDAP_SUCCESS;
						rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp )) {
						if ( bv.bv_val == NULL ) break;
						if (!ber_bvcmp( &bv, &at_namingContexts ) && bvals ) {
							for (j=0; bvals[j].bv_val; j++) {
								if ( !ber_bvstrcasecmp( &base, &bvals[j] )) {
									matched = 1;
									break;
								}
							}
							if (!matched) {
								ber_memfree( bvals );
								bvals = NULL;
								break;
							}
						}
						if (!ber_bvcmp( &bv, &at_olmMDBEntries )) {
							ber_bvreplace( &sv->monitorbase, &dn );
							sv->flags |= HAS_ENTRIES;
							sv->c_curr.entries = strtoul( bvals[0].bv_val, NULL, 0 );
						}
						ber_memfree( bvals );
						bvals = NULL;
					}
				} else if (!strncasecmp( dn.bv_val, opnames[0].rdn.bv_val,
					opnames[0].rdn.bv_len )) {
					get_counters( ld, e, ber, &sv->c_curr );
					break;
				}
				if ( ber )
					ber_free( ber, 0 );
			}
			break;

		case LDAP_NO_SUCH_OBJECT:
			/* no cn=monitor */
			break;

		default:
			tester_ldap_error( ld, "ldap_search_ext_s(cn=Monitor)", sv->url );
			if ( first )
				exit( EXIT_FAILURE );
		}
		ldap_msgfree( res );

		if ( base.bv_val ) {
			char *attr2[] = { at_contextCSN.bv_val, NULL };
			rc = ldap_search_ext_s( ld, base.bv_val, LDAP_SCOPE_BASE, "(objectClass=*)",
				attr2, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
			switch(rc) {
			case LDAP_SUCCESS:
				e = ldap_first_entry( ld, res );
				if ( e ) {
					sv->flags |= HAS_BASE;
					ldap_get_dn_ber( ld, e, &ber, &dn );
					for ( rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp );
						rc == LDAP_SUCCESS;
						rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp )) {
						int done = 0;
						if ( bv.bv_val == NULL ) break;
						if ( bvals ) {
							if ( !ber_bvcmp( &bv, &at_contextCSN )) {
								get_csns( &sv->csn_curr, bvals );
								done = 1;
							}
							ber_memfree( bvals );
							bvals = NULL;
							if ( done )
								break;
						}
					}
				}
				ldap_msgfree( res );
				break;

			default:
				tester_ldap_error( ld, "ldap_search_ext_s(baseDN)", sv->url );
				if ( first )
					exit( EXIT_FAILURE );
			}
		}
	}

	if ( sv->monitorfilter != default_monfilter )
		free( sv->monitorfilter );
	if ( sv->flags & HAS_ENTRIES ) {
		int len = sv->monitorbase.bv_len + sizeof("(|(entryDN=)" MONFILTER ")");
		char *ptr = malloc(len);
		sprintf(ptr, "(|(entryDN=%s)" MONFILTER ")", sv->monitorbase.bv_val );
		sv->monitorfilter = ptr;
	} else if ( sv->flags & HAS_MONITOR ) {
		sv->monitorfilter = (char *)default_monfilter;
	}
	if ( first )
		rotate_stats( sv );
}

564
565
566
567
568
569
int
main( int argc, char **argv )
{
	int		i, rc, *msg1, *msg2;
	char **sids = NULL;
	struct tester_conn_args *config;
Howard Chu's avatar
Howard Chu committed
570
	int first = 1;
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

	config = tester_init( "slapd-watcher", TESTER_TESTER );
	config->authmethod = LDAP_AUTH_SIMPLE;

	while ( ( i = getopt( argc, argv, "D:O:R:U:X:Y:b:d:i:s:w:x" ) ) != EOF )
	{
		switch ( i ) {
		case 'b':		/* base DN for contextCSN lookups */
			ber_str2bv( optarg, 0, 0, &base );
			break;

		case 'i':
			interval = atoi(optarg);
			break;

		case 's':
			sids = ldap_str2charray( optarg, "," );
			break;

		default:
			if ( tester_config_opt( config, i, optarg ) == LDAP_SUCCESS )
				break;

			usage( argv[0], i );
			break;
		}
	}

	tester_config_finish( config );
600
	signal(SIGPIPE, SIG_IGN);
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

	/* don't clear the screen if debug is enabled */
	if (debug)
		clearscreen = "\n\n";

	numservers = argc - optind;
	if ( !numservers )
		usage( argv[0], 0 );

	if ( sids ) {
		for (i=0; sids[i]; i++ );
		if ( i != numservers ) {
			fprintf(stderr, "Number of sids doesn't equal number of server URLs\n");
			exit( EXIT_FAILURE );
		}
	}

	argv += optind;
	argc -= optind;
	servers = calloc( numservers, sizeof(server));

	if ( base.bv_val ) {
		monfilter = "(|(entryDN:dnOneLevelMatch:=cn=Databases,cn=Monitor)" MONFILTER ")";
	} else {
		monfilter = MONFILTER;
	}

	if ( numservers > 1 ) {
		for ( i=0; i<numservers; i++ )
			if ( sids )
				servers[i].sid = atoi(sids[i]);
			else
				servers[i].sid = i+1;
	}

	for ( i = 0; i < numservers; i++ ) {
		servers[i].url = argv[i];
		servers[i].times = calloc( numservers, sizeof(activity));
Howard Chu's avatar
Howard Chu committed
639
640
641
642
		servers[i].csn_curr.vals = calloc( numservers, sizeof(struct berval));
		servers[i].csn_prev.vals = calloc( numservers, sizeof(struct berval));
		servers[i].csn_curr.tvs = calloc( numservers, sizeof(struct timeval));
		servers[i].csn_prev.tvs = calloc( numservers, sizeof(struct timeval));
643
644
645
646
647
648
649
650
651
	}

	msg1 = malloc( numservers * 2 * sizeof(int));
	msg2 = msg1 + numservers;

	for (;;) {
		LDAPMessage *res = NULL, *e = NULL;
		BerElement *ber = NULL;
		struct berval dn, bv, *bvals, **bvp = &bvals;
Howard Chu's avatar
Howard Chu committed
652
		struct timeval tv;
653
654
655
		LDAP *ld;

		for (i=0; i<numservers; i++) {
Howard Chu's avatar
Howard Chu committed
656
657
658
			if ( !servers[i].ld || !(servers[i].flags & WAS_LATE )) {
				msg1[i] = 0;
				msg2[i] = 0;
659
			}
Howard Chu's avatar
Howard Chu committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
			if ( !servers[i].ld ) {
				setup_server( config, &servers[i], first );
			} else {
				ld = servers[i].ld;
				rc = -1;
				if ( servers[i].flags & WAS_DOWN )
					servers[i].flags ^= WAS_DOWN;
				if (( servers[i].flags & HAS_MONITOR ) && !msg1[i] ) {
					char *attrs[3] = { at_monitorOpCompleted.bv_val };
					if ( servers[i].flags & HAS_ENTRIES )
						attrs[1] = at_olmMDBEntries.bv_val;
					rc = ldap_search_ext( ld, "cn=monitor",
						LDAP_SCOPE_SUBTREE, servers[i].monitorfilter,
						attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &msg1[i] );
					if ( rc != LDAP_SUCCESS ) {
						tester_ldap_error( ld, "ldap_search_ext(cn=Monitor)", servers[i].url );
						if ( first )
							exit( EXIT_FAILURE );
						else {
server_down1:
							ldap_unbind_ext( ld, NULL, NULL );
							servers[i].flags |= WAS_DOWN;
							servers[i].ld = NULL;
							gettimeofday( &tv, NULL );
							servers[i].down = tv.tv_sec;
							msg1[i] = 0;
							msg2[i] = 0;
							continue;
						}
					}
				}
				if (( servers[i].flags & HAS_BASE ) && !msg2[i] ) {
					char *attrs[2] = { at_contextCSN.bv_val };
					rc = ldap_search_ext( ld, base.bv_val,
						LDAP_SCOPE_BASE, "(objectClass=*)",
						attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &msg2[i] );
					if ( rc != LDAP_SUCCESS ) {
						tester_ldap_error( ld, "ldap_search_ext(baseDN)", servers[i].url );
						if ( first )
							exit( EXIT_FAILURE );
						else
							goto server_down1;
					}
703
				}
Howard Chu's avatar
Howard Chu committed
704
705
				if ( rc != -1 );
					gettimeofday( &servers[i].c_curr.time, 0 );
706
707
			}
		}
Howard Chu's avatar
Howard Chu committed
708

709
710
		for (i=0; i<numservers; i++) {
			ld = servers[i].ld;
Howard Chu's avatar
Howard Chu committed
711
712
713
714
			if ( msg1[i] ) {
				tv.tv_sec = 0;
				tv.tv_usec = 250000;
				rc = ldap_result( ld, msg1[i], LDAP_MSG_ALL, &tv, &res );
715
				if ( rc < 0 ) {
Howard Chu's avatar
Howard Chu committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
					tester_ldap_error( ld, "ldap_result(cn=Monitor)", servers[i].url );
					if ( first )
						exit( EXIT_FAILURE );
					else {
server_down2:
						ldap_unbind_ext( ld, NULL, NULL );
						servers[i].flags |= WAS_DOWN;
						servers[i].ld = NULL;
						servers[i].down = servers[i].c_curr.time.tv_sec;
						msg1[i] = 0;
						msg2[i] = 0;
						continue;
					}
				}
				if ( rc == 0 ) {
					if ( !( servers[i].flags & WAS_LATE ))
						servers[i].late = servers[i].c_curr.time.tv_sec;
					servers[i].flags |= WAS_LATE;
					continue;
735
				}
Howard Chu's avatar
Howard Chu committed
736
737
				if ( servers[i].flags & WAS_LATE )
					servers[i].flags ^= WAS_LATE;
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
				for ( e = ldap_first_entry( ld, res ); e; e = ldap_next_entry( ld, e )) {
					ldap_get_dn_ber( ld, e, &ber, &dn );
					if ( !strncasecmp( dn.bv_val, "cn=Database", sizeof("cn=Database")-1 ) ||
						!strncasecmp( dn.bv_val, "cn=Frontend", sizeof("cn=Frontend")-1 )) {
						for ( rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp );
							rc == LDAP_SUCCESS;
							rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp )) {
							if ( bv.bv_val == NULL ) break;
							if ( !ber_bvcmp( &bv, &at_olmMDBEntries )) {
								if ( !BER_BVISNULL( &servers[i].monitorbase )) {
									servers[i].c_curr.entries = strtoul( bvals[0].bv_val, NULL, 0 );
								}
							}
							ber_memfree( bvals );
							bvals = NULL;
						}
					} else if (!strncasecmp( dn.bv_val, opnames[0].rdn.bv_val,
						opnames[0].rdn.bv_len )) {
						get_counters( ld, e, ber, &servers[i].c_curr );
						break;
					}
					if ( ber )
						ber_free( ber, 0 );
				}
				ldap_msgfree( res );
			}
Howard Chu's avatar
Howard Chu committed
764
765
766
767
			if ( msg2[i] ) {
				tv.tv_sec = 0;
				tv.tv_usec = 250000;
				rc = ldap_result( ld, msg2[i], LDAP_MSG_ALL, &tv, &res );
768
				if ( rc < 0 ) {
Howard Chu's avatar
Howard Chu committed
769
770
771
772
773
					tester_ldap_error( ld, "ldap_result(baseDN)", servers[i].url );
					if ( first )
						exit( EXIT_FAILURE );
					else
						goto server_down2;
774
				}
Howard Chu's avatar
Howard Chu committed
775
776
777
778
779
780
781
782
				if ( rc == 0 ) {
					if ( !( servers[i].flags & WAS_LATE ))
						servers[i].late = servers[i].c_curr.time.tv_sec;
					servers[i].flags |= WAS_LATE;
					continue;
				}
				if ( servers[i].flags & WAS_LATE )
					servers[i].flags ^= WAS_LATE;
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
				e = ldap_first_entry( ld, res );
				if ( e ) {
					ldap_get_dn_ber( ld, e, &ber, &dn );
					for ( rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp );
						rc == LDAP_SUCCESS;
						rc = ldap_get_attribute_ber( ld, e, ber, &bv, bvp )) {
						int done = 0;
						if ( bv.bv_val == NULL ) break;
						if ( bvals ) {
							if ( !ber_bvcmp( &bv, &at_contextCSN )) {
								get_csns( &servers[i].csn_curr, bvals );
								done = 1;
							}
							ber_memfree( bvals );
							bvals = NULL;
							if ( done )
								break;
						}
					}
				}
				ldap_msgfree( res );
			}
		}
		display();
Howard Chu's avatar
Howard Chu committed
807
808
		sleep(interval);
		first = 0;
809
810
811
812
813
	}

	exit( EXIT_SUCCESS );
}