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