]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
#include <ac/string.h>, to get strlen(), strncmp() and strncasecmp().
[openldap] / servers / slapd / back-monitor / conn.c
1 /* conn.c - deal with connection subsystem */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include <ac/string.h>
38
39 #include "slap.h"
40 #include "lutil.h"
41 #include "back-monitor.h"
42
43 #define CONN_CN_PREFIX  "Connection"
44
45 int
46 monitor_subsys_conn_init(
47         BackendDB               *be
48 )
49 {
50         struct monitorinfo      *mi;
51         
52         Entry                   *e, *e_tmp, *e_conn;
53         struct monitorentrypriv *mp;
54         char                    buf[ BACKMONITOR_BUFSIZE ];
55         struct berval           bv;
56
57         assert( be != NULL );
58
59         mi = ( struct monitorinfo * )be->be_private;
60
61         if ( monitor_cache_get( mi,
62                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
63 #ifdef NEW_LOGGING
64                 LDAP_LOG( OPERATION, CRIT,
65                         "monitor_subsys_conn_init: "
66                         "unable to get entry '%s'\n",
67                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
68 #else
69                 Debug( LDAP_DEBUG_ANY,
70                         "monitor_subsys_conn_init: "
71                         "unable to get entry '%s'\n%s%s",
72                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 
73                         "", "" );
74 #endif
75                 return( -1 );
76         }
77
78         e_tmp = NULL;
79
80         /*
81          * Total conns
82          */
83         snprintf( buf, sizeof( buf ),
84                 "dn: cn=Total,%s\n"
85                 "objectClass: %s\n"
86                 "structuralObjectClass: %s\n"
87                 "cn: Total\n",
88                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
89                 mi->oc_monitorCounterObject->soc_cname.bv_val,
90                 mi->oc_monitorCounterObject->soc_cname.bv_val );
91         
92         e = str2entry( buf );
93         if ( e == NULL ) {
94 #ifdef NEW_LOGGING
95                 LDAP_LOG( OPERATION, CRIT,
96                         "monitor_subsys_conn_init: "
97                         "unable to create entry 'cn=Total,%s'\n",
98                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
99 #else
100                 Debug( LDAP_DEBUG_ANY,
101                         "monitor_subsys_conn_init: "
102                         "unable to create entry 'cn=Total,%s'\n%s%s",
103                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
104                         "", "" );
105 #endif
106                 return( -1 );
107         }
108         
109         bv.bv_val = "0";
110         bv.bv_len = 1;
111         attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
112         
113         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
114         e->e_private = ( void * )mp;
115         mp->mp_next = e_tmp;
116         mp->mp_children = NULL;
117         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
118         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
119                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
120         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
121
122         if ( monitor_cache_add( mi, e ) ) {
123 #ifdef NEW_LOGGING
124                 LDAP_LOG( OPERATION, CRIT,
125                         "monitor_subsys_conn_init: "
126                         "unable to add entry 'cn=Total,%s'\n",
127                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
128 #else
129                 Debug( LDAP_DEBUG_ANY,
130                         "monitor_subsys_conn_init: "
131                         "unable to add entry 'cn=Total,%s'\n%s%s",
132                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
133                         "", "" );
134 #endif
135                 return( -1 );
136         }
137         
138         e_tmp = e;
139
140         /*
141          * Current conns
142          */
143         snprintf( buf, sizeof( buf ),
144                 "dn: cn=Current,%s\n"
145                 "objectClass: %s\n"
146                 "structuralObjectClass: %s\n"
147                 "cn: Current\n",
148                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
149                 mi->oc_monitorCounterObject->soc_cname.bv_val,
150                 mi->oc_monitorCounterObject->soc_cname.bv_val );
151         
152         e = str2entry( buf );
153         if ( e == NULL ) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( OPERATION, CRIT,
156                         "monitor_subsys_conn_init: "
157                         "unable to create entry 'cn=Current,%s'\n",
158                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
159 #else
160                 Debug( LDAP_DEBUG_ANY,
161                         "monitor_subsys_conn_init: "
162                         "unable to create entry 'cn=Current,%s'\n%s%s",
163                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
164                         "", "" );
165 #endif
166                 return( -1 );
167         }
168         
169         bv.bv_val = "0";
170         bv.bv_len = 1;
171         attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
172         
173         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
174         e->e_private = ( void * )mp;
175         mp->mp_next = e_tmp;
176         mp->mp_children = NULL;
177         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
178         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
179                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
180         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
181
182         if ( monitor_cache_add( mi, e ) ) {
183 #ifdef NEW_LOGGING
184                 LDAP_LOG( OPERATION, CRIT,
185                         "monitor_subsys_conn_init: "
186                         "unable to add entry 'cn=Current,%s'\n",
187                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
188 #else
189                 Debug( LDAP_DEBUG_ANY,
190                         "monitor_subsys_conn_init: "
191                         "unable to add entry 'cn=Current,%s'\n%s%s",
192                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
193                         "", "" );
194 #endif
195                 return( -1 );
196         }
197         
198         e_tmp = e;
199
200         mp = ( struct monitorentrypriv * )e_conn->e_private;
201         mp->mp_children = e_tmp;
202
203         monitor_cache_release( mi, e_conn );
204
205         return( 0 );
206 }
207
208 int
209 monitor_subsys_conn_update(
210         Operation               *op,
211         Entry                   *e
212 )
213 {
214         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
215         long            n = -1;
216
217         assert( mi );
218         assert( e );
219         
220         if ( strncasecmp( e->e_ndn, "cn=total", 
221                                 sizeof("cn=total")-1 ) == 0 ) {
222                 n = connections_nextid();
223
224         } else if ( strncasecmp( e->e_ndn, "cn=current", 
225                                 sizeof("cn=current")-1 ) == 0 ) {
226                 Connection      *c;
227                 int             connindex;
228
229                 for ( n = 0, c = connection_first( &connindex );
230                                 c != NULL;
231                                 n++, c = connection_next( c, &connindex ) ) {
232                         /* No Op */ ;
233                 }
234                 connection_done(c);
235         }
236
237         if ( n != -1 ) {
238                 Attribute       *a;
239                 char            buf[] = "+9223372036854775807L";
240
241                 a = attr_find( e->e_attrs, mi->ad_monitorCounter );
242                 if ( a == NULL ) {
243                         return( -1 );
244                 }
245
246                 snprintf( buf, sizeof( buf ), "%ld", n );
247                 free( a->a_vals[ 0 ].bv_val );
248                 ber_str2bv( buf, 0, 1, a->a_vals );
249         }
250
251         return( 0 );
252 }
253
254 static int
255 conn_create(
256         struct monitorinfo      *mi,
257         Connection              *c,
258         Entry                   **ep
259 )
260 {
261         struct monitorentrypriv *mp;
262         struct tm               *ltm;
263         char                    buf[ BACKMONITOR_BUFSIZE ];
264         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
265         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
266
267         struct berval           bv;
268
269         Entry                   *e;
270
271         assert( c != NULL );
272         assert( ep != NULL );
273
274         snprintf( buf, sizeof( buf ),
275                 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
276                 "objectClass: %s\n"
277                 "structuralObjectClass: %s\n"
278                 "cn: " CONN_CN_PREFIX " %ld\n",
279                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
280                 mi->oc_monitorConnection->soc_cname.bv_val,
281                 mi->oc_monitorConnection->soc_cname.bv_val,
282                 c->c_connid );
283         e = str2entry( buf );
284
285         if ( e == NULL) {
286 #ifdef NEW_LOGGING
287                 LDAP_LOG( OPERATION, CRIT,
288                         "monitor_subsys_conn_create: "
289                         "unable to create entry "
290                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
291                         c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
292 #else
293                 Debug( LDAP_DEBUG_ANY,
294                         "monitor_subsys_conn_create: "
295                         "unable to create entry "
296                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
297                         c->c_connid, 
298                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
299 #endif
300                 return( -1 );
301         }
302
303         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
304         
305         ltm = gmtime( &c->c_starttime );
306         lutil_gentime( buf2, sizeof( buf2 ), ltm );
307                         
308         ltm = gmtime( &c->c_activitytime );
309         lutil_gentime( buf3, sizeof( buf3 ), ltm );
310                         
311         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
312
313         /* monitored info */
314         sprintf( buf,
315                 "%ld : %ld "
316                 ": %ld/%ld/%ld/%ld "
317                 ": %ld/%ld/%ld "
318                 ": %s%s%s%s%s%s "
319                 ": %s : %s : %s "
320                 ": %s : %s : %s : %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                 );
346
347         bv.bv_val = buf;
348         bv.bv_len = strlen( buf );
349         attr_merge_one( e, mi->ad_monitoredInfo, &bv, NULL );
350
351         /* connection number */
352         snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
353         bv.bv_val = buf;
354         bv.bv_len = strlen( buf );
355         attr_merge_one( e, mi->ad_monitorConnectionNumber, &bv, NULL );
356
357         /* authz DN */
358         attr_merge_one( e, mi->ad_monitorConnectionAuthzDN,
359                         &c->c_dn, &c->c_ndn );
360
361         /* local address */
362         attr_merge_one( e, mi->ad_monitorConnectionLocalAddress,
363                         &c->c_sock_name, NULL );
364
365         /* peer address */
366         attr_merge_one( e, mi->ad_monitorConnectionPeerAddress,
367                         &c->c_peer_name, NULL );
368
369         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
370         e->e_private = ( void * )mp;
371         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
372         mp->mp_children = NULL;
373         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
374
375         *ep = e;
376
377         return( 0 );
378 }
379
380 int 
381 monitor_subsys_conn_create( 
382         Operation               *op,
383         struct berval           *ndn,
384         Entry                   *e_parent,
385         Entry                   **ep
386 )
387 {
388         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
389         Connection              *c;
390         int                     connindex;
391         struct monitorentrypriv *mp;
392
393         assert( mi != NULL );
394         assert( e_parent != NULL );
395         assert( ep != NULL );
396
397         *ep = NULL;
398
399         if ( ndn == NULL ) {
400                 Entry *e, *e_tmp = NULL;
401
402                 /* create all the children of e_parent */
403                 for ( c = connection_first( &connindex );
404                                 c != NULL;
405                                 c = connection_next( c, &connindex )) {
406                         if ( conn_create( mi, c, &e ) || e == NULL ) {
407                                 connection_done(c);
408                                 for ( ; e_tmp != NULL; ) {
409                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
410                                         e = mp->mp_next;
411
412                                         ch_free( mp );
413                                         e_tmp->e_private = NULL;
414                                         entry_free( e_tmp );
415
416                                         e_tmp = e;
417                                 }
418                                 return( -1 );
419                         }
420                         mp = ( struct monitorentrypriv * )e->e_private;
421                         mp->mp_next = e_tmp;
422                         e_tmp = e;
423                 }
424                 connection_done(c);
425
426                 *ep = e;
427
428         } else {
429                 LDAPRDN         values = NULL;
430                 const char      *text = NULL;
431                 unsigned long   connid;
432                
433                 /* create exactly the required entry */
434
435                 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
436                         LDAP_DN_FORMAT_LDAP ) )
437                 {
438                         return( -1 );
439                 }
440                 
441                 assert( values );
442                 assert( values[ 0 ] );
443
444                 connid = atol( values[ 0 ]->la_value.bv_val
445                                 + sizeof( CONN_CN_PREFIX ) );
446
447                 ldap_rdnfree( values );
448
449                 for ( c = connection_first( &connindex );
450                                 c != NULL;
451                                 c = connection_next( c, &connindex )) {
452                         if ( c->c_connid == connid ) {
453                                 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
454                                         connection_done( c );
455                                         return( -1 );
456                                 }
457
458                                 break;
459                         }
460                 }
461                 
462                 connection_done(c);
463         
464         }
465
466         return( 0 );
467 }
468