]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
e41afb2aa243e36698260b797d11d7780094795a
[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-2005 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 #ifndef LDAP_DEVEL
32 #define MONITOR_LEGACY_CONN
33 #endif
34
35 int
36 monitor_subsys_conn_init(
37         BackendDB               *be,
38         monitor_subsys_t        *ms )
39 {
40         monitor_info_t  *mi;
41         Entry           *e, **ep, *e_conn;
42         monitor_entry_t *mp;
43         char            buf[ BACKMONITOR_BUFSIZE ];
44         struct berval   bv;
45
46         assert( be != NULL );
47
48         mi = ( monitor_info_t * )be->be_private;
49
50         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
51                 Debug( LDAP_DEBUG_ANY,
52                         "monitor_subsys_conn_init: "
53                         "unable to get entry \"%s\"\n",
54                         ms->mss_ndn.bv_val, 0, 0 );
55                 return( -1 );
56         }
57
58         mp = ( monitor_entry_t * )e_conn->e_private;
59         mp->mp_children = NULL;
60         ep = &mp->mp_children;
61
62         /*
63          * Total conns
64          */
65         snprintf( buf, sizeof( buf ),
66                 "dn: cn=Total,%s\n"
67                 "objectClass: %s\n"
68                 "structuralObjectClass: %s\n"
69                 "cn: Total\n"
70                 "creatorsName: %s\n"
71                 "modifiersName: %s\n"
72                 "createTimestamp: %s\n"
73                 "modifyTimestamp: %s\n",
74                 ms->mss_dn.bv_val,
75                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
76                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
77                 mi->mi_creatorsName.bv_val,
78                 mi->mi_creatorsName.bv_val,
79                 mi->mi_startTime.bv_val,
80                 mi->mi_startTime.bv_val );
81         
82         e = str2entry( buf );
83         if ( e == NULL ) {
84                 Debug( LDAP_DEBUG_ANY,
85                         "monitor_subsys_conn_init: "
86                         "unable to create entry \"cn=Total,%s\"\n",
87                         ms->mss_ndn.bv_val, 0, 0 );
88                 return( -1 );
89         }
90         
91         BER_BVSTR( &bv, "0" );
92         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
93         
94         mp = monitor_entrypriv_create();
95         if ( mp == NULL ) {
96                 return -1;
97         }
98         e->e_private = ( void * )mp;
99         mp->mp_info = ms;
100         mp->mp_flags = ms->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",
108                         ms->mss_ndn.bv_val, 0, 0 );
109                 return( -1 );
110         }
111
112         *ep = e;
113         ep = &mp->mp_next;
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                 ms->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",
140                         ms->mss_ndn.bv_val, 0, 0 );
141                 return( -1 );
142         }
143         
144         BER_BVSTR( &bv, "0" );
145         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
146         
147         mp = monitor_entrypriv_create();
148         if ( mp == NULL ) {
149                 return -1;
150         }
151         e->e_private = ( void * )mp;
152         mp->mp_info = ms;
153         mp->mp_flags = ms->mss_flags \
154                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
155         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
156
157         if ( monitor_cache_add( mi, e ) ) {
158                 Debug( LDAP_DEBUG_ANY,
159                         "monitor_subsys_conn_init: "
160                         "unable to add entry \"cn=Current,%s\"\n",
161                         ms->mss_ndn.bv_val, 0, 0 );
162                 return( -1 );
163         }
164         
165         *ep = e;
166         ep = &mp->mp_next;
167
168         monitor_cache_release( mi, e_conn );
169
170         return( 0 );
171 }
172
173 int
174 monitor_subsys_conn_update(
175         Operation               *op,
176         SlapReply               *rs,
177         Entry                   *e )
178 {
179         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
180
181         long                    n = -1;
182         static struct berval    total_bv = BER_BVC( "cn=total" ),
183                                 current_bv = BER_BVC( "cn=current" );
184         struct berval           rdn;
185
186         assert( mi );
187         assert( e );
188
189         dnRdn( &e->e_nname, &rdn );
190         
191         if ( dn_match( &rdn, &total_bv ) ) {
192                 n = connections_nextid();
193
194         } else if ( dn_match( &rdn, &current_bv ) ) {
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                 ber_len_t       len;
210
211                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
212                 if ( a == NULL ) {
213                         return( -1 );
214                 }
215
216                 snprintf( buf, sizeof( buf ), "%ld", n );
217                 len = strlen( buf );
218                 if ( len > a->a_vals[ 0 ].bv_len ) {
219                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
220                 }
221                 a->a_vals[ 0 ].bv_len = len;
222                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
223
224                 /* FIXME: touch modifyTimestamp? */
225         }
226
227         return SLAP_CB_CONTINUE;
228 }
229
230 static int
231 conn_create(
232         monitor_info_t          *mi,
233         Connection              *c,
234         Entry                   **ep,
235         monitor_subsys_t        *ms )
236 {
237         monitor_entry_t *mp;
238         struct tm       *ltm;
239         char            buf[ BACKMONITOR_BUFSIZE ];
240         char            buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
241         char            buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
242
243         Entry           *e;
244
245         struct tm       *ctm;
246         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
247         struct tm       *mtm;
248         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
249 #ifdef HAVE_GMTIME_R
250         struct tm       tm_buf;
251 #endif /* HAVE_GMTIME_R */
252
253         assert( c != NULL );
254         assert( ep != NULL );
255
256 #ifndef HAVE_GMTIME_R
257         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
258 #endif
259 #ifdef HACK_LOCAL_TIME
260 # ifdef HAVE_LOCALTIME_R
261         ctm = localtime_r( &c->c_starttime, &tm_buf );
262         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
263         mtm = localtime_r( &c->c_activitytime, &tm_buf );
264         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
265 # else
266         ctm = localtime( &c->c_starttime );
267         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
268         mtm = localtime( &c->c_activitytime );
269         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
270 # endif /* HAVE_LOCALTIME_R */
271 #else /* !HACK_LOCAL_TIME */
272 # ifdef HAVE_GMTIME_R
273         ctm = gmtime_r( &c->c_starttime, &tm_buf );
274         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
275         mtm = gmtime_r( &c->c_activitytime, &tm_buf );
276         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
277 # else
278         ctm = gmtime( &c->c_starttime );
279         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
280         mtm = gmtime( &c->c_activitytime );
281         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
282 # endif /* HAVE_GMTIME_R */
283 #endif /* !HACK_LOCAL_TIME */
284 #ifndef HAVE_GMTIME_R
285         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
286 #endif
287
288 #ifndef HAVE_GMTIME_R
289         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
290 #endif
291
292 #ifdef HAVE_GMTIME_R
293         ltm = gmtime_r( &c->c_starttime, &tm_buf );
294 #else
295         ltm = gmtime( &c->c_starttime );
296 #endif
297         lutil_gentime( buf2, sizeof( buf2 ), ltm );
298
299 #ifdef HAVE_GMTIME_R
300         ltm = gmtime_r( &c->c_activitytime, &tm_buf );
301 #else
302         ltm = gmtime( &c->c_activitytime );
303 #endif
304         lutil_gentime( buf3, sizeof( buf3 ), ltm );
305
306 #ifndef HAVE_GMTIME_R
307         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
308 #endif /* HAVE_GMTIME_R */
309
310         snprintf( buf, sizeof( buf ),
311                 "dn: cn=Connection %ld,%s\n"
312                 "objectClass: %s\n"
313                 "structuralObjectClass: %s\n"
314                 "cn: Connection %ld\n"
315
316 #ifdef MONITOR_LEGACY_CONN
317                 /* NOTE: this will disappear, as the exploded data
318                  * has been moved to dedicated attributes */
319                 "%s: "
320                         "%ld "
321                         ": %ld "
322                         ": %ld/%ld/%ld/%ld "
323                         ": %ld/%ld/%ld "
324                         ": %s%s%s%s%s%s "
325                         ": %s "
326                         ": %s "
327                         ": %s "
328                         ": %s "
329                         ": %s "
330                         ": %s "
331                         ": %s\n"
332 #endif /* MONITOR_LEGACY_CONN */
333
334                 "%s: %lu\n"
335                 "%s: %ld\n"
336
337                 "%s: %ld\n"
338                 "%s: %ld\n"
339                 "%s: %ld\n"
340                 "%s: %ld\n"
341
342                 "%s: %ld\n"
343                 "%s: %ld\n"
344                 "%s: %ld\n"
345
346                 "%s: %s%s%s%s%s%s\n"
347
348                 "%s: %s\n"
349
350                 "%s: %s\n"
351                 "%s: %s\n"
352                 "%s: %s\n"
353                 "%s: %s\n"
354
355                 "%s: %s\n"
356                 "%s: %s\n"
357
358                 "creatorsName: %s\n"
359                 "modifiersName: %s\n"
360                 "createTimestamp: %s\n"
361                 "modifyTimestamp: %s\n",
362                 c->c_connid, ms->mss_dn.bv_val,
363                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
364                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
365                 c->c_connid,
366
367 #ifdef MONITOR_LEGACY_CONN
368                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
369                         c->c_connid,
370                         (long) c->c_protocol,
371                         c->c_n_ops_received, c->c_n_ops_executing,
372                                 c->c_n_ops_pending, c->c_n_ops_completed,
373                         
374                         /* add low-level counters here */
375                         c->c_n_get, c->c_n_read, c->c_n_write,
376                         
377                         c->c_currentber ? "r" : "",
378                         c->c_writewaiter ? "w" : "",
379                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
380                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
381                         connection_state2str( c->c_conn_state ),
382                         c->c_sasl_bind_in_progress ? "S" : "",
383                         
384                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
385                         
386                         c->c_listener_url.bv_val,
387                         c->c_peer_domain.bv_val,
388                         c->c_peer_name.bv_val,
389                         c->c_sock_name.bv_val,
390                         
391                         buf2,
392                         buf3,
393 #endif /* MONITOR_LEGACY_CONN */
394
395                 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
396                         c->c_connid,
397                 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
398                         (long)c->c_protocol,
399
400                 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
401                         c->c_n_ops_received,
402                 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
403                         c->c_n_ops_executing,
404                 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
405                         c->c_n_ops_pending,
406                 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
407                         c->c_n_ops_completed,
408
409                 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
410                         c->c_n_get,
411                 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
412                         c->c_n_read,
413                 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
414                         c->c_n_write,
415
416                 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
417                         c->c_currentber ? "r" : "",
418                         c->c_writewaiter ? "w" : "",
419                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
420                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
421                         connection_state2str( c->c_conn_state ),
422                         c->c_sasl_bind_in_progress ? "S" : "",
423                 
424                 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
425                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
426
427                 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
428                         c->c_listener_url.bv_val,
429                 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
430                         c->c_peer_domain.bv_val,
431                 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
432                         c->c_peer_name.bv_val,
433                 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
434                         c->c_sock_name.bv_val,
435
436                 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
437                         buf2,
438                 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
439                         buf3,
440
441                 mi->mi_creatorsName.bv_val,
442                 mi->mi_creatorsName.bv_val,
443                 ctmbuf,
444                 mtmbuf );
445                 
446         e = str2entry( buf );
447
448         if ( e == NULL) {
449                 Debug( LDAP_DEBUG_ANY,
450                         "monitor_subsys_conn_create: "
451                         "unable to create entry "
452                         "\"cn=Connection %ld,%s\" entry\n",
453                         c->c_connid, 
454                         ms->mss_dn.bv_val, 0 );
455                 return( -1 );
456         }
457
458         mp = monitor_entrypriv_create();
459         if ( mp == NULL ) {
460                 return LDAP_OTHER;
461         }
462         e->e_private = ( void * )mp;
463         mp->mp_info = ms;
464         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
465
466         *ep = e;
467
468         return SLAP_CB_CONTINUE;
469 }
470
471 int 
472 monitor_subsys_conn_create( 
473         Operation               *op,
474         SlapReply               *rs,
475         struct berval           *ndn,
476         Entry                   *e_parent,
477         Entry                   **ep )
478 {
479         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
480
481         Connection              *c;
482         int                     connindex;
483         monitor_entry_t         *mp;
484         int                     rc = SLAP_CB_CONTINUE;
485         monitor_subsys_t        *ms;
486
487         assert( mi != NULL );
488         assert( e_parent != NULL );
489         assert( ep != NULL );
490
491         ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
492
493         *ep = NULL;
494
495         if ( ndn == NULL ) {
496                 Entry   *e = NULL,
497                         *e_tmp = NULL;
498
499                 /* create all the children of e_parent */
500                 for ( c = connection_first( &connindex );
501                                 c != NULL;
502                                 c = connection_next( c, &connindex ) )
503                 {
504                         if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
505                                         || e == NULL )
506                         {
507                                 for ( ; e_tmp != NULL; ) {
508                                         mp = ( monitor_entry_t * )e_tmp->e_private;
509                                         e = mp->mp_next;
510
511                                         ch_free( mp );
512                                         e_tmp->e_private = NULL;
513                                         entry_free( e_tmp );
514
515                                         e_tmp = e;
516                                 }
517                                 rc = rs->sr_err = LDAP_OTHER;
518                                 break;
519                         }
520                         mp = ( monitor_entry_t * )e->e_private;
521                         mp->mp_next = e_tmp;
522                         e_tmp = e;
523                 }
524                 connection_done( c );
525                 *ep = e;
526
527         } else {
528                 unsigned long           connid;
529                 char                    *next = NULL;
530                 static struct berval    nconn_bv = BER_BVC( "cn=connection " );
531
532                
533                 /* create exactly the required entry;
534                  * the normalized DN must start with "cn=connection ",
535                  * followed by the connection id, followed by
536                  * the RDN separator "," */
537                 if ( ndn->bv_len <= nconn_bv.bv_len
538                                 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
539                 {
540                         return -1;
541                 }
542                 
543                 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
544                 if ( next[ 0 ] != ',' ) {
545                         return ( rs->sr_err = LDAP_OTHER );
546                 }
547
548                 for ( c = connection_first( &connindex );
549                                 c != NULL;
550                                 c = connection_next( c, &connindex ) )
551                 {
552                         if ( c->c_connid == connid ) {
553                                 rc = conn_create( mi, c, ep, ms );
554                                 if ( rc != SLAP_CB_CONTINUE ) {
555                                         rs->sr_err = rc;
556
557                                 } else if ( *ep == NULL ) {
558                                         rc = rs->sr_err = LDAP_OTHER;
559                                 }
560
561                                 break;
562                         }
563                 }
564                 
565                 connection_done( c );
566         }
567
568         return rc;
569 }
570