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