]> git.sur5r.net Git - openldap/blob - servers/slapd/monitor.c
6a3403ea926f561092ce232791d6e5c5ee7e99af
[openldap] / servers / slapd / monitor.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 /* Revision history
14  *
15  * 5-Jun-96     jeff.hodges@stanford.edu
16  *      Added locking of new_conn_mutex when traversing the c[] array.
17  *      Added locking of currenttime_mutex to protect call(s) to localtime().
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include "slap.h"
26 #include "ldapconfig.h"
27
28 #if defined( SLAPD_MONITOR_DN )
29
30 extern int              nbackends;
31 extern Backend          *backends;
32 extern int              active_threads;
33 extern int              dtblsize;
34 extern Connection       *c;
35 extern long             ops_initiated;
36 extern long             ops_completed;
37 extern long             num_entries_sent;
38 extern long             num_bytes_sent;
39 extern time_t           currenttime;
40 extern time_t           starttime;
41 extern int              num_conns;
42
43 extern pthread_mutex_t  new_conn_mutex;
44 extern pthread_mutex_t  currenttime_mutex;
45
46 extern char Versionstr[];
47
48 void
49 monitor_info( Connection *conn, Operation *op )
50 {
51         Entry           *e;
52         char            buf[BUFSIZ], buf2[22];
53         struct berval   val;
54         struct berval   *vals[2];
55         int             i, nconns, nwritewaiters, nreadwaiters;
56         struct tm       *ltm;
57         char            *p, *tmpdn;
58
59         vals[0] = &val;
60         vals[1] = NULL;
61
62         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
63         /* initialize reader/writer lock */
64         entry_rdwr_init(e);
65         e->e_attrs = NULL;
66         e->e_dn = strdup( SLAPD_MONITOR_DN );
67
68         val.bv_val = Versionstr;
69         if (( p = strchr( Versionstr, '\n' )) == NULL ) {
70                 val.bv_len = strlen( Versionstr );
71         } else {
72                 val.bv_len = p - Versionstr;
73         }
74         attr_merge( e, "version", vals );
75
76         sprintf( buf, "%d", active_threads );
77         val.bv_val = buf;
78         val.bv_len = strlen( buf );
79         attr_merge( e, "threads", vals );
80
81         nconns = 0;
82         nwritewaiters = 0;
83         nreadwaiters = 0;
84
85         pthread_mutex_lock( &new_conn_mutex );
86         for ( i = 0; i < dtblsize; i++ ) {
87                 if ( c[i].c_sb.sb_sd != -1 ) {
88                         nconns++;
89                         if ( c[i].c_writewaiter ) {
90                                 nwritewaiters++;
91                         }
92                         if ( c[i].c_gettingber ) {
93                                 nreadwaiters++;
94                         }
95                         pthread_mutex_lock( &currenttime_mutex );
96 #ifdef LDAP_Y2K
97                         ltm = gmtime( &c[i].c_starttime );
98                         strftime( buf2, sizeof(buf2), "%Y%m%d%H%M%SZ", ltm );
99 #else
100                         ltm = localtime( &c[i].c_starttime );
101                         strftime( buf2, sizeof(buf2), "%y%m%d%H%M%SZ", ltm );
102 #endif
103                         pthread_mutex_unlock( &currenttime_mutex );
104
105                         pthread_mutex_lock( &c[i].c_dnmutex );
106                         sprintf( buf, "%d : %s : %ld : %ld : %s : %s%s", i,
107                             buf2, c[i].c_opsinitiated, c[i].c_opscompleted,
108                             c[i].c_dn ? c[i].c_dn : "NULLDN",
109                             c[i].c_gettingber ? "r" : "",
110                             c[i].c_writewaiter ? "w" : "" );
111                         pthread_mutex_unlock( &c[i].c_dnmutex );
112                         val.bv_val = buf;
113                         val.bv_len = strlen( buf );
114                         attr_merge( e, "connection", vals );
115                 }
116         }
117         pthread_mutex_unlock( &new_conn_mutex );
118
119         sprintf( buf, "%d", nconns );
120         val.bv_val = buf;
121         val.bv_len = strlen( buf );
122         attr_merge( e, "currentconnections", vals );
123
124         sprintf( buf, "%d", num_conns );
125         val.bv_val = buf;
126         val.bv_len = strlen( buf );
127         attr_merge( e, "totalconnections", vals );
128
129         sprintf( buf, "%d", dtblsize );
130         val.bv_val = buf;
131         val.bv_len = strlen( buf );
132         attr_merge( e, "dtablesize", vals );
133
134         sprintf( buf, "%d", nwritewaiters );
135         val.bv_val = buf;
136         val.bv_len = strlen( buf );
137         attr_merge( e, "writewaiters", vals );
138
139         sprintf( buf, "%d", nreadwaiters );
140         val.bv_val = buf;
141         val.bv_len = strlen( buf );
142         attr_merge( e, "readwaiters", vals );
143
144         sprintf( buf, "%ld", ops_initiated );
145         val.bv_val = buf;
146         val.bv_len = strlen( buf );
147         attr_merge( e, "opsinitiated", vals );
148
149         sprintf( buf, "%ld", ops_completed );
150         val.bv_val = buf;
151         val.bv_len = strlen( buf );
152         attr_merge( e, "opscompleted", vals );
153
154         sprintf( buf, "%ld", num_entries_sent );
155         val.bv_val = buf;
156         val.bv_len = strlen( buf );
157         attr_merge( e, "entriessent", vals );
158
159         sprintf( buf, "%ld", num_bytes_sent );
160         val.bv_val = buf;
161         val.bv_len = strlen( buf );
162         attr_merge( e, "bytessent", vals );
163
164         pthread_mutex_lock( &currenttime_mutex );
165         ltm = localtime( &currenttime );
166 #ifdef LDAP_Y2K
167         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
168 #else
169         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
170 #endif
171         pthread_mutex_unlock( &currenttime_mutex );
172         val.bv_val = buf;
173         val.bv_len = strlen( buf );
174         attr_merge( e, "currenttime", vals );
175
176         pthread_mutex_lock( &currenttime_mutex );
177         ltm = localtime( &starttime );
178 #ifdef LDAP_Y2K
179         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
180 #else
181         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
182 #endif
183         pthread_mutex_unlock( &currenttime_mutex );
184         val.bv_val = buf;
185         val.bv_len = strlen( buf );
186         attr_merge( e, "starttime", vals );
187
188         sprintf( buf, "%d", nbackends );
189         val.bv_val = buf;
190         val.bv_len = strlen( buf );
191         attr_merge( e, "nbackends", vals );
192
193 #ifdef THREAD_SUNOS5_LWP
194         sprintf( buf, "%d", thr_getconcurrency() );
195         val.bv_val = buf;
196         val.bv_len = strlen( buf );
197         attr_merge( e, "concurrency", vals );
198 #endif
199
200         send_search_entry( &backends[0], conn, op, e, NULL, 0 );
201         send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL, 1 );
202
203         entry_free( e );
204 }
205
206 #endif /* slapd_monitor_dn */