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