]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
64076993fe48b694aea87ddeffd5eae32d4729bd
[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 #define CONN_CN_PREFIX  "Connection"
32
33 int
34 monitor_subsys_conn_init(
35         BackendDB               *be
36 )
37 {
38         struct monitorinfo      *mi;
39         
40         Entry                   *e, *e_tmp, *e_conn;
41         struct monitorentrypriv *mp;
42         char                    buf[ BACKMONITOR_BUFSIZE ];
43         struct berval           bv;
44
45         assert( be != NULL );
46
47         mi = ( struct monitorinfo * )be->be_private;
48
49         if ( monitor_cache_get( mi,
50                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
51                 Debug( LDAP_DEBUG_ANY,
52                         "monitor_subsys_conn_init: "
53                         "unable to get entry '%s'\n%s%s",
54                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 
55                         "", "" );
56                 return( -1 );
57         }
58
59         e_tmp = NULL;
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%s%s",
86                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
87                         "", "" );
88                 return( -1 );
89         }
90         
91         bv.bv_val = "0";
92         bv.bv_len = 1;
93         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
94         
95         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
96         e->e_private = ( void * )mp;
97         mp->mp_next = e_tmp;
98         mp->mp_children = NULL;
99         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
100         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
101                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
102         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
103
104         if ( monitor_cache_add( mi, e ) ) {
105                 Debug( LDAP_DEBUG_ANY,
106                         "monitor_subsys_conn_init: "
107                         "unable to add entry 'cn=Total,%s'\n%s%s",
108                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
109                         "", "" );
110                 return( -1 );
111         }
112         
113         e_tmp = e;
114
115         /*
116          * Current conns
117          */
118         snprintf( buf, sizeof( buf ),
119                 "dn: cn=Current,%s\n"
120                 "objectClass: %s\n"
121                 "structuralObjectClass: %s\n"
122                 "cn: Current\n"
123                 "creatorsName: %s\n"
124                 "modifiersName: %s\n"
125                 "createTimestamp: %s\n"
126                 "modifyTimestamp: %s\n",
127                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
128                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
129                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
130                 mi->mi_creatorsName.bv_val,
131                 mi->mi_creatorsName.bv_val,
132                 mi->mi_startTime.bv_val,
133                 mi->mi_startTime.bv_val );
134         
135         e = str2entry( buf );
136         if ( e == NULL ) {
137                 Debug( LDAP_DEBUG_ANY,
138                         "monitor_subsys_conn_init: "
139                         "unable to create entry 'cn=Current,%s'\n%s%s",
140                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
141                         "", "" );
142                 return( -1 );
143         }
144         
145         bv.bv_val = "0";
146         bv.bv_len = 1;
147         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
148         
149         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
150         e->e_private = ( void * )mp;
151         mp->mp_next = e_tmp;
152         mp->mp_children = NULL;
153         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
154         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
155                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
156         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
157
158         if ( monitor_cache_add( mi, e ) ) {
159                 Debug( LDAP_DEBUG_ANY,
160                         "monitor_subsys_conn_init: "
161                         "unable to add entry 'cn=Current,%s'\n%s%s",
162                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
163                         "", "" );
164                 return( -1 );
165         }
166         
167         e_tmp = e;
168
169         mp = ( struct monitorentrypriv * )e_conn->e_private;
170         mp->mp_children = e_tmp;
171
172         monitor_cache_release( mi, e_conn );
173
174         return( 0 );
175 }
176
177 int
178 monitor_subsys_conn_update(
179         Operation               *op,
180         Entry                   *e
181 )
182 {
183         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
184         long            n = -1;
185
186         assert( mi );
187         assert( e );
188         
189         if ( strncasecmp( e->e_ndn, "cn=total", 
190                                 sizeof("cn=total")-1 ) == 0 ) {
191                 n = connections_nextid();
192
193         } else if ( strncasecmp( e->e_ndn, "cn=current", 
194                                 sizeof("cn=current")-1 ) == 0 ) {
195                 Connection      *c;
196                 int             connindex;
197
198                 for ( n = 0, c = connection_first( &connindex );
199                                 c != NULL;
200                                 n++, c = connection_next( c, &connindex ) ) {
201                         /* No Op */ ;
202                 }
203                 connection_done(c);
204         }
205
206         if ( n != -1 ) {
207                 Attribute       *a;
208                 char            buf[] = "+9223372036854775807L";
209
210                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
211                 if ( a == NULL ) {
212                         return( -1 );
213                 }
214
215                 snprintf( buf, sizeof( buf ), "%ld", n );
216                 free( a->a_vals[ 0 ].bv_val );
217                 ber_str2bv( buf, 0, 1, a->a_vals );
218         }
219
220         return( 0 );
221 }
222
223 static int
224 conn_create(
225         struct monitorinfo      *mi,
226         Connection              *c,
227         Entry                   **ep
228 )
229 {
230         struct monitorentrypriv *mp;
231         struct tm               *ltm;
232         char                    buf[ BACKMONITOR_BUFSIZE ];
233         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
234         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
235
236         struct berval           bv;
237
238         Entry                   *e;
239
240         struct tm       *ctm;
241         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
242         struct tm       *mtm;
243         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
244 #ifdef HAVE_GMTIME_R
245         struct tm       tm_buf;
246 #endif /* HAVE_GMTIME_R */
247
248         assert( c != NULL );
249         assert( ep != NULL );
250
251 #ifndef HAVE_GMTIME_R
252         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
253 #endif
254 #ifdef HACK_LOCAL_TIME
255 # ifdef HAVE_LOCALTIME_R
256         ctm = localtime_r( &c->c_starttime, &tm_buf );
257         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
258         mtm = localtime_r( &c->c_activitytime, &tm_buf );
259         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
260 # else
261         ctm = localtime( &c->c_starttime );
262         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
263         mtm = localtime( &c->c_activitytime );
264         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
265 # endif /* HAVE_LOCALTIME_R */
266 #else /* !HACK_LOCAL_TIME */
267 # ifdef HAVE_GMTIME_R
268         ctm = gmtime_r( &c->c_starttime, &tm_buf );
269         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
270         mtm = gmtime_r( &c->c_activitytime, &tm_buf );
271         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
272 # else
273         ctm = gmtime( &c->c_starttime );
274         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
275         mtm = gmtime( &c->c_activitytime );
276         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
277 # endif /* HAVE_GMTIME_R */
278 #endif /* !HACK_LOCAL_TIME */
279 #ifndef HAVE_GMTIME_R
280         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
281 #endif
282
283         snprintf( buf, sizeof( buf ),
284                 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
285                 "objectClass: %s\n"
286                 "structuralObjectClass: %s\n"
287                 "cn: " CONN_CN_PREFIX " %ld\n"
288                 "creatorsName: %s\n"
289                 "modifiersName: %s\n"
290                 "createTimestamp: %s\n"
291                 "modifyTimestamp: %s\n",
292                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
293                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
294                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
295                 c->c_connid,
296                 mi->mi_creatorsName.bv_val,
297                 mi->mi_creatorsName.bv_val,
298                 ctmbuf,
299                 mtmbuf );
300                 
301         e = str2entry( buf );
302
303         if ( e == NULL) {
304                 Debug( LDAP_DEBUG_ANY,
305                         "monitor_subsys_conn_create: "
306                         "unable to create entry "
307                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
308                         c->c_connid, 
309                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
310                 return( -1 );
311         }
312
313 #ifndef HAVE_GMTIME_R
314         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
315 #endif
316
317 #ifdef HAVE_GMTIME_R
318         ltm = gmtime_r( &c->c_starttime, &tm_buf );
319 #else
320         ltm = gmtime( &c->c_starttime );
321 #endif
322         lutil_gentime( buf2, sizeof( buf2 ), ltm );
323
324 #ifdef HAVE_GMTIME_R
325         ltm = gmtime_r( &c->c_activitytime, &tm_buf );
326 #else
327         ltm = gmtime( &c->c_activitytime );
328 #endif
329         lutil_gentime( buf3, sizeof( buf3 ), ltm );
330
331 #ifndef HAVE_GMTIME_R
332         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
333 #endif /* HAVE_GMTIME_R */
334
335         /* monitored info */
336         sprintf( buf,
337                 "%ld : %ld "
338                 ": %ld/%ld/%ld/%ld "
339                 ": %ld/%ld/%ld "
340                 ": %s%s%s%s%s%s "
341                 ": %s : %s : %s "
342                 ": %s : %s : %s : %s",
343                 c->c_connid,
344                 (long) c->c_protocol,
345                 c->c_n_ops_received, c->c_n_ops_executing,
346                 c->c_n_ops_pending, c->c_n_ops_completed,
347                 
348                 /* add low-level counters here */
349                 c->c_n_get, c->c_n_read, c->c_n_write,
350                 
351                 c->c_currentber ? "r" : "",
352                 c->c_writewaiter ? "w" : "",
353                 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
354                 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
355                 connection_state2str( c->c_conn_state ),
356                 c->c_sasl_bind_in_progress ? "S" : "",
357                 
358                 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
359                 
360                 c->c_listener_url.bv_val,
361                 c->c_peer_domain.bv_val,
362                 c->c_peer_name.bv_val,
363                 c->c_sock_name.bv_val,
364                 
365                 buf2,
366                 buf3
367                 );
368
369         bv.bv_val = buf;
370         bv.bv_len = strlen( buf );
371         attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
372
373         /* connection number */
374         snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
375         bv.bv_val = buf;
376         bv.bv_len = strlen( buf );
377         attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
378
379         /* authz DN */
380         attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
381                         &c->c_dn, &c->c_ndn );
382
383         /* local address */
384         attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
385                         &c->c_sock_name, NULL );
386
387         /* peer address */
388         attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
389                         &c->c_peer_name, NULL );
390
391         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
392         e->e_private = ( void * )mp;
393         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
394         mp->mp_children = NULL;
395         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
396
397         *ep = e;
398
399         return( 0 );
400 }
401
402 int 
403 monitor_subsys_conn_create( 
404         Operation               *op,
405         struct berval           *ndn,
406         Entry                   *e_parent,
407         Entry                   **ep
408 )
409 {
410         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
411         Connection              *c;
412         int                     connindex;
413         struct monitorentrypriv *mp;
414
415         assert( mi != NULL );
416         assert( e_parent != NULL );
417         assert( ep != NULL );
418
419         *ep = NULL;
420
421         if ( ndn == NULL ) {
422                 Entry *e, *e_tmp = NULL;
423
424                 /* create all the children of e_parent */
425                 for ( c = connection_first( &connindex );
426                                 c != NULL;
427                                 c = connection_next( c, &connindex )) {
428                         if ( conn_create( mi, c, &e ) || e == NULL ) {
429                                 connection_done(c);
430                                 for ( ; e_tmp != NULL; ) {
431                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
432                                         e = mp->mp_next;
433
434                                         ch_free( mp );
435                                         e_tmp->e_private = NULL;
436                                         entry_free( e_tmp );
437
438                                         e_tmp = e;
439                                 }
440                                 return( -1 );
441                         }
442                         mp = ( struct monitorentrypriv * )e->e_private;
443                         mp->mp_next = e_tmp;
444                         e_tmp = e;
445                 }
446                 connection_done(c);
447
448                 *ep = e;
449
450         } else {
451                 LDAPRDN         values = NULL;
452                 const char      *text = NULL;
453                 unsigned long   connid;
454                
455                 /* create exactly the required entry */
456
457                 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
458                         LDAP_DN_FORMAT_LDAP ) )
459                 {
460                         return( -1 );
461                 }
462                 
463                 assert( values );
464                 assert( values[ 0 ] );
465
466                 connid = atol( values[ 0 ]->la_value.bv_val
467                                 + sizeof( CONN_CN_PREFIX ) );
468
469                 ldap_rdnfree( values );
470
471                 for ( c = connection_first( &connindex );
472                                 c != NULL;
473                                 c = connection_next( c, &connindex )) {
474                         if ( c->c_connid == connid ) {
475                                 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
476                                         connection_done( c );
477                                         return( -1 );
478                                 }
479
480                                 break;
481                         }
482                 }
483                 
484                 connection_done(c);
485         
486         }
487
488         return( 0 );
489 }
490