]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
fix attr nvals & more improvements
[openldap] / servers / slapd / back-monitor / conn.c
1 /* conn.c - deal with connection subsystem */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "lutil.h"
40 #include "back-monitor.h"
41
42 #define CONN_CN_PREFIX  "Connection"
43
44 int
45 monitor_subsys_conn_init(
46         BackendDB               *be
47 )
48 {
49         struct monitorinfo      *mi;
50         
51         Entry                   *e, *e_tmp, *e_conn;
52         struct monitorentrypriv *mp;
53         char                    buf[1024];
54         struct berval           bv;
55
56         assert( be != NULL );
57
58         mi = ( struct monitorinfo * )be->be_private;
59
60         if ( monitor_cache_get( mi,
61                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG( OPERATION, CRIT,
64                         "monitor_subsys_conn_init: "
65                         "unable to get entry '%s'\n",
66                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
67 #else
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_conn_init: "
70                         "unable to get entry '%s'\n%s%s",
71                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 
72                         "", "" );
73 #endif
74                 return( -1 );
75         }
76
77         e_tmp = NULL;
78
79         /*
80          * Total conns
81          */
82         snprintf( buf, sizeof( buf ),
83                 "dn: cn=Total,%s\n"
84                 SLAPD_MONITOR_OBJECTCLASSES
85                 "cn: Total\n",
86                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
87         
88         e = str2entry( buf );
89         if ( e == NULL ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( OPERATION, CRIT,
92                         "monitor_subsys_conn_init: "
93                         "unable to create entry 'cn=Total,%s'\n",
94                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
95 #else
96                 Debug( LDAP_DEBUG_ANY,
97                         "monitor_subsys_conn_init: "
98                         "unable to create entry 'cn=Total,%s'\n%s%s",
99                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
100                         "", "" );
101 #endif
102                 return( -1 );
103         }
104         
105         bv.bv_val = "0";
106         bv.bv_len = 1;
107         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
108         
109         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
110         e->e_private = ( void * )mp;
111         mp->mp_next = e_tmp;
112         mp->mp_children = NULL;
113         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
114         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
115                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
116         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
117
118         if ( monitor_cache_add( mi, e ) ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( OPERATION, CRIT,
121                         "monitor_subsys_conn_init: "
122                         "unable to add entry 'cn=Total,%s'\n",
123                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
124 #else
125                 Debug( LDAP_DEBUG_ANY,
126                         "monitor_subsys_conn_init: "
127                         "unable to add entry 'cn=Total,%s'\n%s%s",
128                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
129                         "", "" );
130 #endif
131                 return( -1 );
132         }
133         
134         e_tmp = e;
135
136         /*
137          * Current conns
138          */
139         snprintf( buf, sizeof( buf ),
140                 "dn: cn=Current,%s\n"
141                 SLAPD_MONITOR_OBJECTCLASSES
142                 "cn: Current\n",
143                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
144         
145         e = str2entry( buf );
146         if ( e == NULL ) {
147 #ifdef NEW_LOGGING
148                 LDAP_LOG( OPERATION, CRIT,
149                         "monitor_subsys_conn_init: "
150                         "unable to create entry 'cn=Current,%s'\n",
151                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
152 #else
153                 Debug( LDAP_DEBUG_ANY,
154                         "monitor_subsys_conn_init: "
155                         "unable to create entry 'cn=Current,%s'\n%s%s",
156                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
157                         "", "" );
158 #endif
159                 return( -1 );
160         }
161         
162         bv.bv_val = "0";
163         bv.bv_len = 1;
164         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
165         
166         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
167         e->e_private = ( void * )mp;
168         mp->mp_next = e_tmp;
169         mp->mp_children = NULL;
170         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
171         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
172                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
173         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
174
175         if ( monitor_cache_add( mi, e ) ) {
176 #ifdef NEW_LOGGING
177                 LDAP_LOG( OPERATION, CRIT,
178                         "monitor_subsys_conn_init: "
179                         "unable to add entry 'cn=Current,%s'\n",
180                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
181 #else
182                 Debug( LDAP_DEBUG_ANY,
183                         "monitor_subsys_conn_init: "
184                         "unable to add entry 'cn=Current,%s'\n%s%s",
185                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
186                         "", "" );
187 #endif
188                 return( -1 );
189         }
190         
191         e_tmp = e;
192
193         mp = ( struct monitorentrypriv * )e_conn->e_private;
194         mp->mp_children = e_tmp;
195
196         monitor_cache_release( mi, e_conn );
197
198         return( 0 );
199 }
200
201 int
202 monitor_subsys_conn_update(
203         struct monitorinfo      *mi,
204         Entry                   *e
205 )
206 {
207         long            n = -1;
208
209         assert( mi );
210         assert( e );
211         
212         if ( strncasecmp( e->e_ndn, "cn=total", 
213                                 sizeof("cn=total")-1 ) == 0 ) {
214                 n = connections_nextid();
215
216         } else if ( strncasecmp( e->e_ndn, "cn=current", 
217                                 sizeof("cn=current")-1 ) == 0 ) {
218                 Connection      *c;
219                 int             connindex;
220
221                 for ( n = 0, c = connection_first( &connindex );
222                                 c != NULL;
223                                 n++, c = connection_next( c, &connindex ) ) {
224                         /* No Op */ ;
225                 }
226                 connection_done(c);
227         }
228
229         if ( n != -1 ) {
230                 Attribute       *a;
231                 char            buf[16];
232
233                 a = attr_find( e->e_attrs, monitor_ad_desc );
234                 if ( a == NULL ) {
235                         return( -1 );
236                 }
237
238                 snprintf( buf, sizeof( buf ), "%ld", n );
239                 free( a->a_vals[ 0 ].bv_val );
240                 ber_str2bv( buf, 0, 1, a->a_vals );
241         }
242
243         return( 0 );
244 }
245
246 static int
247 conn_create(
248         Connection              *c,
249         Entry                   **ep
250 )
251 {
252         struct monitorentrypriv *mp;
253         struct tm               *ltm;
254         char                    buf[ 1024 ];
255         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
256         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
257
258         struct berval           bv;
259
260         Entry                   *e;
261
262         assert( c != NULL );
263         assert( ep != NULL );
264
265         snprintf( buf, sizeof( buf ),
266                 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
267                 SLAPD_MONITOR_OBJECTCLASSES
268                 "cn: " CONN_CN_PREFIX " %ld\n",
269                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
270                 c->c_connid );
271         e = str2entry( buf );
272
273         if ( e == NULL) {
274 #ifdef NEW_LOGGING
275                 LDAP_LOG( OPERATION, CRIT,
276                         "monitor_subsys_conn_create: "
277                         "unable to create entry "
278                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
279                         c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
280 #else
281                 Debug( LDAP_DEBUG_ANY,
282                         "monitor_subsys_conn_create: "
283                         "unable to create entry "
284                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
285                         c->c_connid, 
286                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
287 #endif
288                 return( -1 );
289         }
290
291         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
292         
293         ltm = gmtime( &c->c_starttime );
294         lutil_gentime( buf2, sizeof( buf2 ), ltm );
295                         
296         ltm = gmtime( &c->c_activitytime );
297         lutil_gentime( buf3, sizeof( buf3 ), ltm );
298                         
299         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
300
301         sprintf( buf,
302                 "%ld : %ld "
303                 ": %ld/%ld/%ld/%ld "
304                 ": %ld/%ld/%ld "
305                 ": %s%s%s%s%s%s "
306                 ": %s : %s : %s "
307                 ": %s : %s : %s : %s",
308                 c->c_connid,
309                 (long) c->c_protocol,
310                 c->c_n_ops_received, c->c_n_ops_executing,
311                 c->c_n_ops_pending, c->c_n_ops_completed,
312                 
313                 /* add low-level counters here */
314                 c->c_n_get, c->c_n_read, c->c_n_write,
315                 
316                 c->c_currentber ? "r" : "",
317                 c->c_writewaiter ? "w" : "",
318                 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
319                 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
320                 connection_state2str( c->c_conn_state ),
321                 c->c_sasl_bind_in_progress ? "S" : "",
322                 
323                 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
324                 
325                 c->c_listener_url.bv_val,
326                 c->c_peer_domain.bv_val,
327                 c->c_peer_name.bv_val,
328                 c->c_sock_name.bv_val,
329                 
330                 buf2,
331                 buf3
332                 );
333
334         bv.bv_val = buf;
335         bv.bv_len = strlen( buf );
336         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
337
338         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
339         e->e_private = ( void * )mp;
340         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
341         mp->mp_children = NULL;
342         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
343
344         *ep = e;
345
346         return( 0 );
347 }
348
349 int 
350 monitor_subsys_conn_create( 
351         struct monitorinfo      *mi,
352         struct berval           *ndn,
353         Entry                   *e_parent,
354         Entry                   **ep
355 )
356 {
357         Connection              *c;
358         int                     connindex;
359         struct monitorentrypriv *mp;
360
361         assert( mi != NULL );
362         assert( e_parent != NULL );
363         assert( ep != NULL );
364
365         *ep = NULL;
366
367         if ( ndn == NULL ) {
368                 Entry *e, *e_tmp = NULL;
369
370                 /* create all the children of e_parent */
371                 for ( c = connection_first( &connindex );
372                                 c != NULL;
373                                 c = connection_next( c, &connindex )) {
374                         if ( conn_create( c, &e ) || e == NULL ) {
375                                 connection_done(c);
376                                 for ( ; e_tmp != NULL; ) {
377                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
378                                         e = mp->mp_next;
379
380                                         ch_free( mp );
381                                         e_tmp->e_private = NULL;
382                                         entry_free( e_tmp );
383
384                                         e_tmp = e;
385                                 }
386                                 return( -1 );
387                         }
388                         mp = ( struct monitorentrypriv * )e->e_private;
389                         mp->mp_next = e_tmp;
390                         e_tmp = e;
391                 }
392                 connection_done(c);
393
394                 *ep = e;
395
396         } else {
397                 LDAPRDN         *values = NULL;
398                 const char      *text = NULL;
399                 unsigned long   connid;
400                
401                 /* create exactly the required entry */
402
403                 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
404                         LDAP_DN_FORMAT_LDAP ) )
405                 {
406                         return( -1 );
407                 }
408                 
409                 assert( values );
410                 assert( values[ 0 ][ 0 ] );
411
412                 connid = atol( values[ 0 ][ 0 ]->la_value.bv_val
413                                 + sizeof( CONN_CN_PREFIX ) );
414
415                 ldap_rdnfree( values );
416
417                 for ( c = connection_first( &connindex );
418                                 c != NULL;
419                                 c = connection_next( c, &connindex )) {
420                         if ( c->c_connid == connid ) {
421                                 if ( conn_create( c, ep ) || *ep == NULL ) {
422                                         connection_done(c);
423                                         return( -1 );
424                                 }
425
426                                 break;
427                         }
428                 }
429                 
430                 connection_done(c);
431         
432         }
433
434         return( 0 );
435 }
436