]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
1c84cfef6c6eaf5b9ed10950f8231e5521c6bbe5
[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 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software.
19  */
20 /* This is an altered version */
21 /*
22  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
23  * 
24  * This work has beed deveolped for the OpenLDAP Foundation 
25  * in the hope that it may be useful to the Open Source community, 
26  * but WITHOUT ANY WARRANTY.
27  * 
28  * Permission is granted to anyone to use this software for any purpose
29  * on any computer system, and to alter it and redistribute it, subject
30  * to the following restrictions:
31  * 
32  * 1. The author and SysNet s.n.c. are not responsible for the consequences
33  *    of use of this software, no matter how awful, even if they arise from
34  *    flaws in it.
35  * 
36  * 2. The origin of this software must not be misrepresented, either by
37  *    explicit claim or by omission.  Since few users ever read sources,
38  *    credits should appear in the documentation.
39  * 
40  * 3. Altered versions must be plainly marked as such, and must not be
41  *    misrepresented as being the original software.  Since few users
42  *    ever read sources, credits should appear in the documentation.
43  *    SysNet s.n.c. cannot be responsible for the consequences of the
44  *    alterations.
45  * 
46  * 4. This notice may not be removed or altered.
47  */
48
49 #ifndef _BACK_MONITOR_H_
50 #define _BACK_MONITOR_H_
51
52 #include <ldap_pvt.h>
53 #include <ldap_pvt_thread.h>
54 #include <avl.h>
55 #include <slap.h>
56
57 LDAP_BEGIN_DECL
58
59 /*
60  * The cache maps DNs to Entries.
61 /*
62  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
63  * 
64  * This work has beed deveolped for the OpenLDAP Foundation 
65  * in the hope that it may be useful to the Open Source community, 
66  * but WITHOUT ANY WARRANTY.
67  * 
68  * Permission is granted to anyone to use this software for any purpose
69  * on any computer system, and to alter it and redistribute it, subject
70  * to the following restrictions:
71  * 
72  * 1. The author and SysNet s.n.c. are not responsible for the consequences
73  *    of use of this software, no matter how awful, even if they arise from
74  *    flaws in it.
75  * 
76  * 2. The origin of this software must not be misrepresented, either by
77  *    explicit claim or by omission.  Since few users ever read sources,
78  *    credits should appear in the documentation.
79  * 
80  * 3. Altered versions must be plainly marked as such, and must not be
81  *    misrepresented as being the original software.  Since few users
82  *    ever read sources, credits should appear in the documentation.
83  *    SysNet s.n.c. cannot be responsible for the consequences of the
84  *    alterations.
85  * 
86  * 4. This notice may not be removed or altered.
87  */
88
89 #include "portable.h"
90
91 #include <stdio.h>
92 #include <ac/string.h>
93
94 #include "slap.h"
95 #include "lutil.h"
96 #include "back-monitor.h"
97
98 #define CONN_CN_PREFIX  "Connection"
99
100 int
101 monitor_subsys_conn_init(
102         BackendDB               *be
103 )
104 {
105         struct monitorinfo      *mi;
106         
107         Entry                   *e, *e_tmp, *e_conn;
108         struct monitorentrypriv *mp;
109         char                    buf[ BACKMONITOR_BUFSIZE ];
110         struct berval           bv;
111
112         assert( be != NULL );
113
114         mi = ( struct monitorinfo * )be->be_private;
115
116         if ( monitor_cache_get( mi,
117                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG( OPERATION, CRIT,
120                         "monitor_subsys_conn_init: "
121                         "unable to get entry '%s'\n",
122                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
123 #else
124                 Debug( LDAP_DEBUG_ANY,
125                         "monitor_subsys_conn_init: "
126                         "unable to get entry '%s'\n%s%s",
127                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 
128                         "", "" );
129 #endif
130                 return( -1 );
131         }
132
133         e_tmp = NULL;
134
135         /*
136          * Total conns
137          */
138         snprintf( buf, sizeof( buf ),
139                 "dn: cn=Total,%s\n"
140                 "objectClass: %s\n"
141                 "structuralObjectClass: %s\n"
142                 "cn: Total\n"
143                 "createTimestamp: %s\n"
144                 "modifyTimestamp: %s\n",
145                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
146                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
147                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
148                 mi->mi_startTime.bv_val,
149                 mi->mi_startTime.bv_val );
150         
151         e = str2entry( buf );
152         if ( e == NULL ) {
153 #ifdef NEW_LOGGING
154                 LDAP_LOG( OPERATION, CRIT,
155                         "monitor_subsys_conn_init: "
156                         "unable to create entry 'cn=Total,%s'\n",
157                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
158 #else
159                 Debug( LDAP_DEBUG_ANY,
160                         "monitor_subsys_conn_init: "
161                         "unable to create entry 'cn=Total,%s'\n%s%s",
162                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
163                         "", "" );
164 #endif
165                 return( -1 );
166         }
167         
168         bv.bv_val = "0";
169         bv.bv_len = 1;
170         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
171         
172         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
173         e->e_private = ( void * )mp;
174         mp->mp_next = e_tmp;
175         mp->mp_children = NULL;
176         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
177         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
178                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
179         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
180
181         if ( monitor_cache_add( mi, e ) ) {
182 #ifdef NEW_LOGGING
183                 LDAP_LOG( OPERATION, CRIT,
184                         "monitor_subsys_conn_init: "
185                         "unable to add entry 'cn=Total,%s'\n",
186                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
187 #else
188                 Debug( LDAP_DEBUG_ANY,
189                         "monitor_subsys_conn_init: "
190                         "unable to add entry 'cn=Total,%s'\n%s%s",
191                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
192                         "", "" );
193 #endif
194                 return( -1 );
195         }
196         
197         e_tmp = e;
198
199         /*
200          * Current conns
201          */
202         snprintf( buf, sizeof( buf ),
203                 "dn: cn=Current,%s\n"
204                 "objectClass: %s\n"
205                 "structuralObjectClass: %s\n"
206                 "cn: Current\n"
207                 "createTimestamp: %s\n"
208                 "modifyTimestamp: %s\n",
209                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
210                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
211                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
212                 mi->mi_startTime.bv_val,
213                 mi->mi_startTime.bv_val );
214         
215         e = str2entry( buf );
216         if ( e == NULL ) {
217 #ifdef NEW_LOGGING
218                 LDAP_LOG( OPERATION, CRIT,
219                         "monitor_subsys_conn_init: "
220                         "unable to create entry 'cn=Current,%s'\n",
221                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
222 #else
223                 Debug( LDAP_DEBUG_ANY,
224                         "monitor_subsys_conn_init: "
225                         "unable to create entry 'cn=Current,%s'\n%s%s",
226                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
227                         "", "" );
228 #endif
229                 return( -1 );
230         }
231         
232         bv.bv_val = "0";
233         bv.bv_len = 1;
234         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
235         
236         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
237         e->e_private = ( void * )mp;
238         mp->mp_next = e_tmp;
239         mp->mp_children = NULL;
240         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
241         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
242                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
243         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
244
245         if ( monitor_cache_add( mi, e ) ) {
246 #ifdef NEW_LOGGING
247                 LDAP_LOG( OPERATION, CRIT,
248                         "monitor_subsys_conn_init: "
249                         "unable to add entry 'cn=Current,%s'\n",
250                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
251 #else
252                 Debug( LDAP_DEBUG_ANY,
253                         "monitor_subsys_conn_init: "
254                         "unable to add entry 'cn=Current,%s'\n%s%s",
255                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
256                         "", "" );
257 #endif
258                 return( -1 );
259         }
260         
261         e_tmp = e;
262
263         mp = ( struct monitorentrypriv * )e_conn->e_private;
264         mp->mp_children = e_tmp;
265
266         monitor_cache_release( mi, e_conn );
267
268         return( 0 );
269 }
270
271 int
272 monitor_subsys_conn_update(
273         Operation               *op,
274         Entry                   *e
275 )
276 {
277         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
278         long            n = -1;
279
280         assert( mi );
281         assert( e );
282         
283         if ( strncasecmp( e->e_ndn, "cn=total", 
284                                 sizeof("cn=total")-1 ) == 0 ) {
285                 n = connections_nextid();
286
287         } else if ( strncasecmp( e->e_ndn, "cn=current", 
288                                 sizeof("cn=current")-1 ) == 0 ) {
289                 Connection      *c;
290                 int             connindex;
291
292                 for ( n = 0, c = connection_first( &connindex );
293                                 c != NULL;
294                                 n++, c = connection_next( c, &connindex ) ) {
295                         /* No Op */ ;
296                 }
297                 connection_done(c);
298         }
299
300         if ( n != -1 ) {
301                 Attribute       *a;
302                 char            buf[] = "+9223372036854775807L";
303
304                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
305                 if ( a == NULL ) {
306                         return( -1 );
307                 }
308
309                 snprintf( buf, sizeof( buf ), "%ld", n );
310                 free( a->a_vals[ 0 ].bv_val );
311                 ber_str2bv( buf, 0, 1, a->a_vals );
312         }
313
314         return( 0 );
315 }
316
317 static int
318 conn_create(
319         struct monitorinfo      *mi,
320         Connection              *c,
321         Entry                   **ep
322 )
323 {
324         struct monitorentrypriv *mp;
325         struct tm               *ltm;
326         char                    buf[ BACKMONITOR_BUFSIZE ];
327         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
328         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
329
330         struct berval           bv;
331
332         Entry                   *e;
333
334         struct tm       *ctm;
335         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
336         struct tm       *mtm;
337         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
338
339         assert( c != NULL );
340         assert( ep != NULL );
341
342         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
343 #ifdef HACK_LOCAL_TIME
344         ctm = localtime( &c->c_starttime );
345         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
346         mtm = localtime( &c->c_activitytime );
347         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
348 #else /* !HACK_LOCAL_TIME */
349         ctm = gmtime( &c->c_starttime );
350         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
351         mtm = gmtime( &c->c_activitytime );
352         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
353 #endif /* !HACK_LOCAL_TIME */
354         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
355
356         snprintf( buf, sizeof( buf ),
357                 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
358                 "objectClass: %s\n"
359                 "structuralObjectClass: %s\n"
360                 "cn: " CONN_CN_PREFIX " %ld\n"
361                 "createTimestamp: %s\n"
362                 "modifyTimestamp: %s\n",
363                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
364                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
365                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
366                 c->c_connid,
367                 ctmbuf, mtmbuf );
368                 
369         e = str2entry( buf );
370
371         if ( e == NULL) {
372 #ifdef NEW_LOGGING
373                 LDAP_LOG( OPERATION, CRIT,
374                         "monitor_subsys_conn_create: "
375                         "unable to create entry "
376                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
377                         c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
378 #else
379                 Debug( LDAP_DEBUG_ANY,
380                         "monitor_subsys_conn_create: "
381                         "unable to create entry "
382                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
383                         c->c_connid, 
384                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
385 #endif
386                 return( -1 );
387         }
388
389         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
390         
391         ltm = gmtime( &c->c_starttime );
392         lutil_gentime( buf2, sizeof( buf2 ), ltm );
393                         
394         ltm = gmtime( &c->c_activitytime );
395         lutil_gentime( buf3, sizeof( buf3 ), ltm );
396                         
397         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
398
399         /* monitored info */
400         sprintf( buf,
401                 "%ld : %ld "
402                 ": %ld/%ld/%ld/%ld "
403                 ": %ld/%ld/%ld "
404                 ": %s%s%s%s%s%s "
405                 ": %s : %s : %s "
406                 ": %s : %s : %s : %s",
407                 c->c_connid,
408                 (long) c->c_protocol,
409                 c->c_n_ops_received, c->c_n_ops_executing,
410                 c->c_n_ops_pending, c->c_n_ops_completed,
411                 
412                 /* add low-level counters here */
413                 c->c_n_get, c->c_n_read, c->c_n_write,
414                 
415                 c->c_currentber ? "r" : "",
416                 c->c_writewaiter ? "w" : "",
417                 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
418                 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
419                 connection_state2str( c->c_conn_state ),
420                 c->c_sasl_bind_in_progress ? "S" : "",
421                 
422                 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
423                 
424                 c->c_listener_url.bv_val,
425                 c->c_peer_domain.bv_val,
426                 c->c_peer_name.bv_val,
427                 c->c_sock_name.bv_val,
428                 
429                 buf2,
430                 buf3
431                 );
432
433         bv.bv_val = buf;
434         bv.bv_len = strlen( buf );
435         attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
436
437         /* connection number */
438         snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
439         bv.bv_val = buf;
440         bv.bv_len = strlen( buf );
441         attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
442
443         /* authz DN */
444         attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
445                         &c->c_dn, &c->c_ndn );
446
447         /* local address */
448         attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
449                         &c->c_sock_name, NULL );
450
451         /* peer address */
452         attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
453                         &c->c_peer_name, NULL );
454
455         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
456         e->e_private = ( void * )mp;
457         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
458         mp->mp_children = NULL;
459         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
460
461         *ep = e;
462
463         return( 0 );
464 }
465
466 int 
467 monitor_subsys_conn_create( 
468         Operation               *op,
469         struct berval           *ndn,
470         Entry                   *e_parent,
471         Entry                   **ep
472 )
473 {
474         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
475         Connection              *c;
476         int                     connindex;
477         struct monitorentrypriv *mp;
478
479         assert( mi != NULL );
480         assert( e_parent != NULL );
481         assert( ep != NULL );
482
483         *ep = NULL;
484
485         if ( ndn == NULL ) {
486                 Entry *e, *e_tmp = NULL;
487
488                 /* create all the children of e_parent */
489                 for ( c = connection_first( &connindex );
490                                 c != NULL;
491                                 c = connection_next( c, &connindex )) {
492                         if ( conn_create( mi, c, &e ) || e == NULL ) {
493                                 connection_done(c);
494                                 for ( ; e_tmp != NULL; ) {
495                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
496                                         e = mp->mp_next;
497
498                                         ch_free( mp );
499                                         e_tmp->e_private = NULL;
500                                         entry_free( e_tmp );
501
502                                         e_tmp = e;
503                                 }
504                                 return( -1 );
505                         }
506                         mp = ( struct monitorentrypriv * )e->e_private;
507                         mp->mp_next = e_tmp;
508                         e_tmp = e;
509                 }
510                 connection_done(c);
511
512                 *ep = e;
513
514         } else {
515                 LDAPRDN         values = NULL;
516                 const char      *text = NULL;
517                 unsigned long   connid;
518                
519                 /* create exactly the required entry */
520
521                 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
522                         LDAP_DN_FORMAT_LDAP ) )
523                 {
524                         return( -1 );
525                 }
526                 
527                 assert( values );
528                 assert( values[ 0 ] );
529
530                 connid = atol( values[ 0 ]->la_value.bv_val
531                                 + sizeof( CONN_CN_PREFIX ) );
532
533                 ldap_rdnfree( values );
534
535                 for ( c = connection_first( &connindex );
536                                 c != NULL;
537                                 c = connection_next( c, &connindex )) {
538                         if ( c->c_connid == connid ) {
539                                 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
540                                         connection_done( c );
541                                         return( -1 );
542                                 }
543
544                                 break;
545                         }
546                 }
547                 
548                 connection_done(c);
549         
550         }
551
552         return( 0 );
553 }
554