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