monitor.c 4.38 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
115
116
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
/*
 * Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "slap.h"
#include "ldapconfig.h"

#if defined( SLAPD_MONITOR_DN )

extern int		nbackends;
extern Backend		*backends;
extern int		active_threads;
extern int		dtblsize;
extern Connection	*c;
extern long		ops_initiated;
extern long		ops_completed;
extern long		num_entries_sent;
extern long		num_bytes_sent;
extern time_t		currenttime;
extern time_t		starttime;
extern int		num_conns;


extern char Versionstr[];

/*
 * no mutex protection in here - take our chances!
 */

void
monitor_info( Connection *conn, Operation *op )
{
	Entry		*e;
	char		buf[BUFSIZ], buf2[20];
	struct berval	val;
	struct berval	*vals[2];
	int		i, nconns, nwritewaiters, nreadwaiters;
	struct tm	*ltm;
	char		*p, *tmpdn;

	vals[0] = &val;
	vals[1] = NULL;

	e = (Entry *) ch_calloc( 1, sizeof(Entry) );
	e->e_attrs = NULL;
	e->e_dn = strdup( SLAPD_MONITOR_DN );

	val.bv_val = Versionstr;
	if (( p = strchr( Versionstr, '\n' )) == NULL ) {
		val.bv_len = strlen( Versionstr );
	} else {
		val.bv_len = p - Versionstr;
	}
	attr_merge( e, "version", vals );

	sprintf( buf, "%d", active_threads );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "threads", vals );

	nconns = 0;
	nwritewaiters = 0;
	nreadwaiters = 0;
	for ( i = 0; i < dtblsize; i++ ) {
		if ( c[i].c_sb.sb_sd != -1 ) {
			nconns++;
			if ( c[i].c_writewaiter ) {
				nwritewaiters++;
			}
			if ( c[i].c_gettingber ) {
				nreadwaiters++;
			}
			ltm = localtime( &c[i].c_starttime );
			strftime( buf2, sizeof(buf2), "%y%m%d%H%M%SZ", ltm );
			pthread_mutex_lock( &c[i].c_dnmutex );
			sprintf( buf, "%d : %s : %ld : %ld : %s : %s%s", i,
			    buf2, c[i].c_opsinitiated, c[i].c_opscompleted,
			    c[i].c_dn ? c[i].c_dn : "NULLDN",
			    c[i].c_gettingber ? "r" : "",
			    c[i].c_writewaiter ? "w" : "" );
			pthread_mutex_unlock( &c[i].c_dnmutex );
			val.bv_val = buf;
			val.bv_len = strlen( buf );
			attr_merge( e, "connection", vals );
		}
	}
	sprintf( buf, "%d", nconns );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "currentconnections", vals );

	sprintf( buf, "%d", num_conns );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "totalconnections", vals );

	sprintf( buf, "%d", dtblsize );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "dtablesize", vals );

	sprintf( buf, "%d", nwritewaiters );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "writewaiters", vals );

	sprintf( buf, "%d", nreadwaiters );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "readwaiters", vals );

	sprintf( buf, "%ld", ops_initiated );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "opsinitiated", vals );

	sprintf( buf, "%ld", ops_completed );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "opscompleted", vals );

	sprintf( buf, "%ld", num_entries_sent );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "entriessent", vals );

	sprintf( buf, "%ld", num_bytes_sent );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "bytessent", vals );

        ltm = localtime( &currenttime );
        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "currenttime", vals );

        ltm = localtime( &starttime );
        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "starttime", vals );

	sprintf( buf, "%d", nbackends );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "nbackends", vals );

#ifdef THREAD_SUNOS5_LWP
	sprintf( buf, "%d", thr_getconcurrency() );
	val.bv_val = buf;
	val.bv_len = strlen( buf );
	attr_merge( e, "concurrency", vals );
#endif

	send_search_entry( &backends[0], conn, op, e, NULL, 0 );
	send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL, 1 );

	entry_free( e );
}

#endif /* slapd_monitor_dn */