]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
ce61417eb480e0046aa1c9981130670b45fa6f40
[openldap] / servers / slapd / back-monitor / conn.c
1 /* conn.c - deal with connection subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "lutil.h"
29 #include "back-monitor.h"
30
31 int
32 monitor_subsys_conn_init(
33         BackendDB               *be
34 )
35 {
36         struct monitorinfo      *mi;
37         
38         Entry                   *e, **ep, *e_conn;
39         struct monitorentrypriv *mp;
40         char                    buf[ BACKMONITOR_BUFSIZE ];
41         struct berval           bv;
42
43         assert( be != NULL );
44
45         mi = ( struct monitorinfo * )be->be_private;
46
47         if ( monitor_cache_get( mi,
48                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) )
49         {
50                 Debug( LDAP_DEBUG_ANY,
51                         "monitor_subsys_conn_init: "
52                         "unable to get entry \"%s\"\n",
53                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
54                 return( -1 );
55         }
56
57         mp = ( struct monitorentrypriv * )e_conn->e_private;
58         mp->mp_children = NULL;
59         ep = &mp->mp_children;
60
61         /*
62          * Total conns
63          */
64         snprintf( buf, sizeof( buf ),
65                 "dn: cn=Total,%s\n"
66                 "objectClass: %s\n"
67                 "structuralObjectClass: %s\n"
68                 "cn: Total\n"
69                 "creatorsName: %s\n"
70                 "modifiersName: %s\n"
71                 "createTimestamp: %s\n"
72                 "modifyTimestamp: %s\n",
73                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
74                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
75                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
76                 mi->mi_creatorsName.bv_val,
77                 mi->mi_creatorsName.bv_val,
78                 mi->mi_startTime.bv_val,
79                 mi->mi_startTime.bv_val );
80         
81         e = str2entry( buf );
82         if ( e == NULL ) {
83                 Debug( LDAP_DEBUG_ANY,
84                         "monitor_subsys_conn_init: "
85                         "unable to create entry \"cn=Total,%s\"\n",
86                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
87                 return( -1 );
88         }
89         
90         BER_BVSTR( &bv, "0" );
91         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
92         
93         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
94         e->e_private = ( void * )mp;
95         mp->mp_next = NULL;
96         mp->mp_children = NULL;
97         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
98         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
99                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
100         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
101
102         if ( monitor_cache_add( mi, e ) ) {
103                 Debug( LDAP_DEBUG_ANY,
104                         "monitor_subsys_conn_init: "
105                         "unable to add entry \"cn=Total,%s\"\n",
106                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
107                 return( -1 );
108         }
109
110         *ep = e;
111         ep = &mp->mp_next;
112         
113         /*
114          * Current conns
115          */
116         snprintf( buf, sizeof( buf ),
117                 "dn: cn=Current,%s\n"
118                 "objectClass: %s\n"
119                 "structuralObjectClass: %s\n"
120                 "cn: Current\n"
121                 "creatorsName: %s\n"
122                 "modifiersName: %s\n"
123                 "createTimestamp: %s\n"
124                 "modifyTimestamp: %s\n",
125                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
126                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
127                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
128                 mi->mi_creatorsName.bv_val,
129                 mi->mi_creatorsName.bv_val,
130                 mi->mi_startTime.bv_val,
131                 mi->mi_startTime.bv_val );
132         
133         e = str2entry( buf );
134         if ( e == NULL ) {
135                 Debug( LDAP_DEBUG_ANY,
136                         "monitor_subsys_conn_init: "
137                         "unable to create entry \"cn=Current,%s\"\n",
138                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
139                 return( -1 );
140         }
141         
142         BER_BVSTR( &bv, "0" );
143         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
144         
145         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
146         e->e_private = ( void * )mp;
147         mp->mp_next = NULL;
148         mp->mp_children = NULL;
149         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
150         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
151                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
152         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
153
154         if ( monitor_cache_add( mi, e ) ) {
155                 Debug( LDAP_DEBUG_ANY,
156                         "monitor_subsys_conn_init: "
157                         "unable to add entry \"cn=Current,%s\"\n",
158                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
159                 return( -1 );
160         }
161         
162         *ep = e;
163         ep = &mp->mp_next;
164
165         monitor_cache_release( mi, e_conn );
166
167         return( 0 );
168 }
169
170 int
171 monitor_subsys_conn_update(
172         Operation               *op,
173         Entry                   *e
174 )
175 {
176         struct monitorinfo      *mi =
177                 (struct monitorinfo *)op->o_bd->be_private;
178
179         long                    n = -1;
180         static struct berval    total_bv = BER_BVC( "cn=total" ),
181                                 current_bv = BER_BVC( "cn=current" );
182         struct berval           rdn;
183
184         assert( mi );
185         assert( e );
186
187         dnRdn( &e->e_nname, &rdn );
188         
189         if ( dn_match( &rdn, &total_bv ) ) {
190                 n = connections_nextid();
191
192         } else if ( dn_match( &rdn, &current_bv ) ) {
193                 Connection      *c;
194                 int             connindex;
195
196                 for ( n = 0, c = connection_first( &connindex );
197                                 c != NULL;
198                                 n++, c = connection_next( c, &connindex ) ) {
199                         /* No Op */ ;
200                 }
201                 connection_done( c );
202         }
203
204         if ( n != -1 ) {
205                 Attribute       *a;
206                 char            buf[] = "+9223372036854775807L";
207                 ber_len_t       len;
208
209                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
210                 if ( a == NULL ) {
211                         return( -1 );
212                 }
213
214                 snprintf( buf, sizeof( buf ), "%ld", n );
215                 len = strlen( buf );
216                 if ( len > a->a_vals[ 0 ].bv_len ) {
217                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
218                 }
219                 a->a_vals[ 0 ].bv_len = len;
220                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
221         }
222
223         return( 0 );
224 }
225
226 static int
227 conn_create(
228         struct monitorinfo      *mi,
229         Connection              *c,
230         Entry                   **ep
231 )
232 {
233         struct monitorentrypriv *mp;
234         struct tm               *ltm;
235         char                    buf[ BACKMONITOR_BUFSIZE ];
236         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
237         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
238
239         struct berval           bv;
240
241         Entry                   *e;
242
243         struct tm       *ctm;
244         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
245         struct tm       *mtm;
246         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
247 #ifdef HAVE_GMTIME_R
248         struct tm       tm_buf;
249 #endif /* HAVE_GMTIME_R */
250
251         assert( c != NULL );
252         assert( ep != NULL );
253
254 #ifndef HAVE_GMTIME_R
255         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
256 #endif
257 #ifdef HACK_LOCAL_TIME
258 # ifdef HAVE_LOCALTIME_R
259         ctm = localtime_r( &c->c_starttime, &tm_buf );
260         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
261         mtm = localtime_r( &c->c_activitytime, &tm_buf );
262         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
263 # else
264         ctm = localtime( &c->c_starttime );
265         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
266         mtm = localtime( &c->c_activitytime );
267         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
268 # endif /* HAVE_LOCALTIME_R */
269 #else /* !HACK_LOCAL_TIME */
270 # ifdef HAVE_GMTIME_R
271         ctm = gmtime_r( &c->c_starttime, &tm_buf );
272         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
273         mtm = gmtime_r( &c->c_activitytime, &tm_buf );
274         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
275 # else
276         ctm = gmtime( &c->c_starttime );
277         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
278         mtm = gmtime( &c->c_activitytime );
279         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
280 # endif /* HAVE_GMTIME_R */
281 #endif /* !HACK_LOCAL_TIME */
282 #ifndef HAVE_GMTIME_R
283         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
284 #endif
285
286         snprintf( buf, sizeof( buf ),
287                 "dn: cn=Connection %ld,%s\n"
288                 "objectClass: %s\n"
289                 "structuralObjectClass: %s\n"
290                 "cn: Connection %ld\n"
291                 "creatorsName: %s\n"
292                 "modifiersName: %s\n"
293                 "createTimestamp: %s\n"
294                 "modifyTimestamp: %s\n",
295                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
296                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
297                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
298                 c->c_connid,
299                 mi->mi_creatorsName.bv_val,
300                 mi->mi_creatorsName.bv_val,
301                 ctmbuf,
302                 mtmbuf );
303                 
304         e = str2entry( buf );
305
306         if ( e == NULL) {
307                 Debug( LDAP_DEBUG_ANY,
308                         "monitor_subsys_conn_create: "
309                         "unable to create entry "
310                         "\"cn=Connection %ld,%s\" entry\n",
311                         c->c_connid, 
312                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
313                 return( -1 );
314         }
315
316 #ifndef HAVE_GMTIME_R
317         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
318 #endif
319
320 #ifdef HAVE_GMTIME_R
321         ltm = gmtime_r( &c->c_starttime, &tm_buf );
322 #else
323         ltm = gmtime( &c->c_starttime );
324 #endif
325         lutil_gentime( buf2, sizeof( buf2 ), ltm );
326
327 #ifdef HAVE_GMTIME_R
328         ltm = gmtime_r( &c->c_activitytime, &tm_buf );
329 #else
330         ltm = gmtime( &c->c_activitytime );
331 #endif
332         lutil_gentime( buf3, sizeof( buf3 ), ltm );
333
334 #ifndef HAVE_GMTIME_R
335         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
336 #endif /* HAVE_GMTIME_R */
337
338         /* monitored info */
339         sprintf( buf,
340                 "%ld "
341                 ": %ld "
342                 ": %ld/%ld/%ld/%ld "
343                 ": %ld/%ld/%ld "
344                 ": %s%s%s%s%s%s "
345                 ": %s "
346                 ": %s "
347                 ": %s "
348                 ": %s "
349                 ": %s "
350                 ": %s "
351                 ": %s",
352                 c->c_connid,
353                 (long) c->c_protocol,
354                 c->c_n_ops_received, c->c_n_ops_executing,
355                         c->c_n_ops_pending, c->c_n_ops_completed,
356                 
357                 /* add low-level counters here */
358                 c->c_n_get, c->c_n_read, c->c_n_write,
359                 
360                 c->c_currentber ? "r" : "",
361                 c->c_writewaiter ? "w" : "",
362                 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
363                 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
364                 connection_state2str( c->c_conn_state ),
365                 c->c_sasl_bind_in_progress ? "S" : "",
366                 
367                 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
368                 
369                 c->c_listener_url.bv_val,
370                 c->c_peer_domain.bv_val,
371                 c->c_peer_name.bv_val,
372                 c->c_sock_name.bv_val,
373                 
374                 buf2,
375                 buf3
376                 );
377
378         bv.bv_val = buf;
379         bv.bv_len = strlen( buf );
380         attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
381
382         /* connection number */
383         snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
384         bv.bv_val = buf;
385         bv.bv_len = strlen( buf );
386         attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
387
388         /* authz DN */
389         attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
390                         &c->c_dn, &c->c_ndn );
391
392         /* local address */
393         attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
394                         &c->c_sock_name, NULL );
395
396         /* peer address */
397         attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
398                         &c->c_peer_name, NULL );
399
400         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
401         e->e_private = ( void * )mp;
402         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
403         mp->mp_children = NULL;
404         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
405
406         *ep = e;
407
408         return( 0 );
409 }
410
411 int 
412 monitor_subsys_conn_create( 
413         Operation               *op,
414         struct berval           *ndn,
415         Entry                   *e_parent,
416         Entry                   **ep
417 )
418 {
419         struct monitorinfo      *mi =
420                 (struct monitorinfo *)op->o_bd->be_private;
421
422         Connection              *c;
423         int                     connindex;
424         struct monitorentrypriv *mp;
425         int                     rc = 0;
426
427         assert( mi != NULL );
428         assert( e_parent != NULL );
429         assert( ep != NULL );
430
431         *ep = NULL;
432
433         if ( ndn == NULL ) {
434                 Entry   *e = NULL,
435                         *e_tmp = NULL;
436
437                 /* create all the children of e_parent */
438                 for ( c = connection_first( &connindex );
439                                 c != NULL;
440                                 c = connection_next( c, &connindex )) {
441                         if ( conn_create( mi, c, &e ) || e == NULL ) {
442                                 for ( ; e_tmp != NULL; ) {
443                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
444                                         e = mp->mp_next;
445
446                                         ch_free( mp );
447                                         e_tmp->e_private = NULL;
448                                         entry_free( e_tmp );
449
450                                         e_tmp = e;
451                                 }
452                                 rc = -1;
453                                 break;
454                         }
455                         mp = ( struct monitorentrypriv * )e->e_private;
456                         mp->mp_next = e_tmp;
457                         e_tmp = e;
458                 }
459                 connection_done(c);
460                 *ep = e;
461
462         } else {
463                 unsigned long           connid;
464                 char                    *next = NULL;
465                 static struct berval    nconn_bv = BER_BVC( "cn=connection " );
466
467                
468                 /* create exactly the required entry;
469                  * the normalized DN must start with "cn=connection ",
470                  * followed by the connection id, followed by
471                  * the RDN separator "," */
472                 if ( ndn->bv_len <= nconn_bv.bv_len
473                                 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
474                 {
475                         return -1;
476                 }
477                 
478                 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
479                 if ( next[ 0 ] != ',' ) {
480                         return -1;
481                 }
482
483                 for ( c = connection_first( &connindex );
484                                 c != NULL;
485                                 c = connection_next( c, &connindex )) {
486                         if ( c->c_connid == connid ) {
487                                 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
488                                         rc = -1;
489                                 }
490
491                                 break;
492                         }
493                 }
494                 
495                 connection_done(c);
496         }
497
498         return rc;
499 }
500