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