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