]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
explode connection monitored info into dedicated attributes
[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 int
32 monitor_subsys_conn_init(
33         BackendDB               *be,
34         monitor_subsys_t        *ms
35 )
36 {
37         monitor_info_t  *mi;
38         Entry           *e, **ep, *e_conn;
39         monitor_entry_t *mp;
40         char            buf[ BACKMONITOR_BUFSIZE ];
41         struct berval   bv;
42
43         assert( be != NULL );
44
45         mi = ( monitor_info_t * )be->be_private;
46
47         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
48                 Debug( LDAP_DEBUG_ANY,
49                         "monitor_subsys_conn_init: "
50                         "unable to get entry \"%s\"\n",
51                         ms->mss_ndn.bv_val, 0, 0 );
52                 return( -1 );
53         }
54
55         mp = ( monitor_entry_t * )e_conn->e_private;
56         mp->mp_children = NULL;
57         ep = &mp->mp_children;
58
59         /*
60          * Total conns
61          */
62         snprintf( buf, sizeof( buf ),
63                 "dn: cn=Total,%s\n"
64                 "objectClass: %s\n"
65                 "structuralObjectClass: %s\n"
66                 "cn: Total\n"
67                 "creatorsName: %s\n"
68                 "modifiersName: %s\n"
69                 "createTimestamp: %s\n"
70                 "modifyTimestamp: %s\n",
71                 ms->mss_dn.bv_val,
72                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
73                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
74                 mi->mi_creatorsName.bv_val,
75                 mi->mi_creatorsName.bv_val,
76                 mi->mi_startTime.bv_val,
77                 mi->mi_startTime.bv_val );
78         
79         e = str2entry( buf );
80         if ( e == NULL ) {
81                 Debug( LDAP_DEBUG_ANY,
82                         "monitor_subsys_conn_init: "
83                         "unable to create entry \"cn=Total,%s\"\n",
84                         ms->mss_ndn.bv_val, 0, 0 );
85                 return( -1 );
86         }
87         
88         BER_BVSTR( &bv, "0" );
89         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
90         
91         mp = monitor_entrypriv_create();
92         if ( mp == NULL ) {
93                 return -1;
94         }
95         e->e_private = ( void * )mp;
96         mp->mp_info = ms;
97         mp->mp_flags = ms->mss_flags \
98                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
99         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
100
101         if ( monitor_cache_add( mi, e ) ) {
102                 Debug( LDAP_DEBUG_ANY,
103                         "monitor_subsys_conn_init: "
104                         "unable to add entry \"cn=Total,%s\"\n",
105                         ms->mss_ndn.bv_val, 0, 0 );
106                 return( -1 );
107         }
108
109         *ep = e;
110         ep = &mp->mp_next;
111         
112         /*
113          * Current conns
114          */
115         snprintf( buf, sizeof( buf ),
116                 "dn: cn=Current,%s\n"
117                 "objectClass: %s\n"
118                 "structuralObjectClass: %s\n"
119                 "cn: Current\n"
120                 "creatorsName: %s\n"
121                 "modifiersName: %s\n"
122                 "createTimestamp: %s\n"
123                 "modifyTimestamp: %s\n",
124                 ms->mss_dn.bv_val,
125                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
126                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
127                 mi->mi_creatorsName.bv_val,
128                 mi->mi_creatorsName.bv_val,
129                 mi->mi_startTime.bv_val,
130                 mi->mi_startTime.bv_val );
131         
132         e = str2entry( buf );
133         if ( e == NULL ) {
134                 Debug( LDAP_DEBUG_ANY,
135                         "monitor_subsys_conn_init: "
136                         "unable to create entry \"cn=Current,%s\"\n",
137                         ms->mss_ndn.bv_val, 0, 0 );
138                 return( -1 );
139         }
140         
141         BER_BVSTR( &bv, "0" );
142         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
143         
144         mp = monitor_entrypriv_create();
145         if ( mp == NULL ) {
146                 return -1;
147         }
148         e->e_private = ( void * )mp;
149         mp->mp_info = ms;
150         mp->mp_flags = ms->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                         ms->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         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
177
178         long                    n = -1;
179         static struct berval    total_bv = BER_BVC( "cn=total" ),
180                                 current_bv = BER_BVC( "cn=current" );
181         struct berval           rdn;
182
183         assert( mi );
184         assert( e );
185
186         dnRdn( &e->e_nname, &rdn );
187         
188         if ( dn_match( &rdn, &total_bv ) ) {
189                 n = connections_nextid();
190
191         } else if ( dn_match( &rdn, &current_bv ) ) {
192                 Connection      *c;
193                 int             connindex;
194
195                 for ( n = 0, c = connection_first( &connindex );
196                                 c != NULL;
197                                 n++, c = connection_next( c, &connindex ) ) {
198                         /* No Op */ ;
199                 }
200                 connection_done( c );
201         }
202
203         if ( n != -1 ) {
204                 Attribute       *a;
205                 char            buf[] = "+9223372036854775807L";
206                 ber_len_t       len;
207
208                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
209                 if ( a == NULL ) {
210                         return( -1 );
211                 }
212
213                 snprintf( buf, sizeof( buf ), "%ld", n );
214                 len = strlen( buf );
215                 if ( len > a->a_vals[ 0 ].bv_len ) {
216                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
217                 }
218                 a->a_vals[ 0 ].bv_len = len;
219                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
220
221                 /* FIXME: touch modifyTimestamp? */
222         }
223
224         return( 0 );
225 }
226
227 static int
228 conn_create(
229         monitor_info_t          *mi,
230         Connection              *c,
231         Entry                   **ep,
232         monitor_subsys_t        *ms
233 )
234 {
235         monitor_entry_t *mp;
236         struct tm               *ltm;
237         char                    buf[ BACKMONITOR_BUFSIZE ];
238         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
240
241         struct berval           bv;
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                 /* NOTE: this will disappear, as the exploded data
317                  * has been moved to dedicated attributes */
318                 "%s: "
319                         "%ld "
320                         ": %ld "
321                         ": %ld/%ld/%ld/%ld "
322                         ": %ld/%ld/%ld "
323                         ": %s%s%s%s%s%s "
324                         ": %s "
325                         ": %s "
326                         ": %s "
327                         ": %s "
328                         ": %s "
329                         ": %s "
330                         ": %s\n"
331
332                 "%s: %lu\n"
333                 "%s: %ld\n"
334
335                 "%s: %ld\n"
336                 "%s: %ld\n"
337                 "%s: %ld\n"
338                 "%s: %ld\n"
339
340                 "%s: %ld\n"
341                 "%s: %ld\n"
342                 "%s: %ld\n"
343
344                 "%s: %s%s%s%s%s%s\n"
345
346                 "%s: %s\n"
347
348                 "%s: %s\n"
349                 "%s: %s\n"
350                 "%s: %s\n"
351                 "%s: %s\n"
352
353                 "%s: %s\n"
354                 "%s: %s\n"
355
356                 "creatorsName: %s\n"
357                 "modifiersName: %s\n"
358                 "createTimestamp: %s\n"
359                 "modifyTimestamp: %s\n",
360                 c->c_connid, ms->mss_dn.bv_val,
361                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
362                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
363                 c->c_connid,
364
365                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
366                         c->c_connid,
367                         (long) c->c_protocol,
368                         c->c_n_ops_received, c->c_n_ops_executing,
369                                 c->c_n_ops_pending, c->c_n_ops_completed,
370                         
371                         /* add low-level counters here */
372                         c->c_n_get, c->c_n_read, c->c_n_write,
373                         
374                         c->c_currentber ? "r" : "",
375                         c->c_writewaiter ? "w" : "",
376                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
377                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
378                         connection_state2str( c->c_conn_state ),
379                         c->c_sasl_bind_in_progress ? "S" : "",
380                         
381                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
382                         
383                         c->c_listener_url.bv_val,
384                         c->c_peer_domain.bv_val,
385                         c->c_peer_name.bv_val,
386                         c->c_sock_name.bv_val,
387                         
388                         buf2,
389                         buf3,
390
391                 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
392                         c->c_connid,
393                 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
394                         (long)c->c_protocol,
395
396                 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
397                         c->c_n_ops_received,
398                 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
399                         c->c_n_ops_executing,
400                 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
401                         c->c_n_ops_pending,
402                 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
403                         c->c_n_ops_completed,
404
405                 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
406                         c->c_n_get,
407                 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
408                         c->c_n_read,
409                 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
410                         c->c_n_write,
411
412                 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
413                         c->c_currentber ? "r" : "",
414                         c->c_writewaiter ? "w" : "",
415                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
416                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
417                         connection_state2str( c->c_conn_state ),
418                         c->c_sasl_bind_in_progress ? "S" : "",
419                 
420                 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
421                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
422
423                 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
424                         c->c_listener_url.bv_val,
425                 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
426                         c->c_peer_domain.bv_val,
427                 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
428                         c->c_peer_name.bv_val,
429                 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
430                         c->c_sock_name.bv_val,
431
432                 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
433                         buf2,
434                 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
435                         buf3,
436
437                 mi->mi_creatorsName.bv_val,
438                 mi->mi_creatorsName.bv_val,
439                 ctmbuf,
440                 mtmbuf );
441                 
442         e = str2entry( buf );
443
444         if ( e == NULL) {
445                 Debug( LDAP_DEBUG_ANY,
446                         "monitor_subsys_conn_create: "
447                         "unable to create entry "
448                         "\"cn=Connection %ld,%s\" entry\n",
449                         c->c_connid, 
450                         ms->mss_dn.bv_val, 0 );
451                 return( -1 );
452         }
453
454         mp = monitor_entrypriv_create();
455         if ( mp == NULL ) {
456                 return -1;
457         }
458         e->e_private = ( void * )mp;
459         mp->mp_info = ms;
460         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
461
462         *ep = e;
463
464         return( 0 );
465 }
466
467 int 
468 monitor_subsys_conn_create( 
469         Operation               *op,
470         struct berval           *ndn,
471         Entry                   *e_parent,
472         Entry                   **ep
473 )
474 {
475         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
476
477         Connection              *c;
478         int                     connindex;
479         monitor_entry_t         *mp;
480         int                     rc = 0;
481         monitor_subsys_t        *ms;
482
483         assert( mi != NULL );
484         assert( e_parent != NULL );
485         assert( ep != NULL );
486
487         ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
488
489         *ep = NULL;
490
491         if ( ndn == NULL ) {
492                 Entry   *e = NULL,
493                         *e_tmp = NULL;
494
495                 /* create all the children of e_parent */
496                 for ( c = connection_first( &connindex );
497                                 c != NULL;
498                                 c = connection_next( c, &connindex ))
499                 {
500                         if ( conn_create( mi, c, &e, ms ) || e == NULL ) {
501                                 for ( ; e_tmp != NULL; ) {
502                                         mp = ( monitor_entry_t * )e_tmp->e_private;
503                                         e = mp->mp_next;
504
505                                         ch_free( mp );
506                                         e_tmp->e_private = NULL;
507                                         entry_free( e_tmp );
508
509                                         e_tmp = e;
510                                 }
511                                 rc = -1;
512                                 break;
513                         }
514                         mp = ( monitor_entry_t * )e->e_private;
515                         mp->mp_next = e_tmp;
516                         e_tmp = e;
517                 }
518                 connection_done(c);
519                 *ep = e;
520
521         } else {
522                 unsigned long           connid;
523                 char                    *next = NULL;
524                 static struct berval    nconn_bv = BER_BVC( "cn=connection " );
525
526                
527                 /* create exactly the required entry;
528                  * the normalized DN must start with "cn=connection ",
529                  * followed by the connection id, followed by
530                  * the RDN separator "," */
531                 if ( ndn->bv_len <= nconn_bv.bv_len
532                                 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
533                 {
534                         return -1;
535                 }
536                 
537                 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
538                 if ( next[ 0 ] != ',' ) {
539                         return -1;
540                 }
541
542                 for ( c = connection_first( &connindex );
543                                 c != NULL;
544                                 c = connection_next( c, &connindex )) {
545                         if ( c->c_connid == connid ) {
546                                 if ( conn_create( mi, c, ep, ms ) || *ep == NULL ) {
547                                         rc = -1;
548                                 }
549
550                                 break;
551                         }
552                 }
553                 
554                 connection_done(c);
555         }
556
557         return rc;
558 }
559