]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
Update copyright statements
[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                 "objectClass: top\n"
82                 "objectClass: LDAPsubEntry\n"
83 #ifdef SLAPD_MONITORSUBENTRY
84                 "objectClass: monitorSubEntry\n"
85 #else /* !SLAPD_MONITORSUBENTRY */
86                 "objectClass: extensibleObject\n"
87 #endif /* !SLAPD_MONITORSUBENTRY */
88                 "cn: Total\n",
89                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
90         
91         e = str2entry( buf );
92         if ( e == NULL ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG(( "operation", LDAP_LEVEL_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 ));
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[1].bv_val = NULL;
109         bv[0].bv_val = "0";
110         bv[0].bv_len = 1;
111         attr_merge( e, monitor_ad_desc, bv );
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", LDAP_LEVEL_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 ));
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: top\n"
146                 "objectClass: LDAPsubEntry\n"
147 #ifdef SLAPD_MONITORSUBENTRY
148                 "objectClass: monitorSubEntry\n"
149 #else /* !SLAPD_MONITORSUBENTRY */
150                 "objectClass: extensibleObject\n"
151 #endif /* !SLAPD_MONITORSUBENTRY */
152                 "cn: Current\n",
153                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
154         
155         e = str2entry( buf );
156         if ( e == NULL ) {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
159                         "monitor_subsys_conn_init: "
160                         "unable to create entry 'cn=Current,%s'\n",
161                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val ));
162 #else
163                 Debug( LDAP_DEBUG_ANY,
164                         "monitor_subsys_conn_init: "
165                         "unable to create entry 'cn=Current,%s'\n%s%s",
166                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
167                         "", "" );
168 #endif
169                 return( -1 );
170         }
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", LDAP_LEVEL_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 ));
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         struct monitorinfo      *mi,
210         Entry                   *e
211 )
212 {
213         long            n = -1;
214
215         assert( mi );
216         assert( e );
217         
218         if ( strncasecmp( e->e_ndn, "CN=TOTAL", 
219                                 sizeof("CN=TOTAL")-1 ) == 0 ) {
220                 n = connections_nextid();
221
222         } else if ( strncasecmp( e->e_ndn, "CN=CURRENT", 
223                                 sizeof("CN=CURRENT")-1 ) == 0 ) {
224                 Connection      *c;
225                 int             connindex;
226
227                 for ( n = 0, c = connection_first( &connindex );
228                                 c != NULL;
229                                 n++, c = connection_next( c, &connindex ) ) {
230                         /* No Op */ ;
231                 }
232                 connection_done(c);
233         }
234
235         if ( n != -1 ) {
236                 Attribute       *a;
237                 char            buf[16];
238
239                 a = attr_find( e->e_attrs, monitor_ad_desc );
240                 if ( a == NULL ) {
241                         return( -1 );
242                 }
243
244                 snprintf( buf, sizeof( buf ), "%ld", n );
245                 free( a->a_vals[ 0 ].bv_val );
246                 ber_str2bv( buf, 0, 1, a->a_vals );
247         }
248
249         return( 0 );
250 }
251
252 static int
253 conn_create(
254         Connection              *c,
255         Entry                   **ep
256 )
257 {
258         struct monitorentrypriv *mp;
259         struct tm               *ltm;
260         char                    buf[1024];
261         char                    buf2[22];
262         char                    buf3[22];
263
264         struct berval           bv[2];
265
266         Entry                   *e;
267
268         assert( c != NULL );
269         assert( ep != NULL );
270
271         snprintf( buf, sizeof( buf ),
272                 "dn: cn=%ld,%s\n"
273                 "objectClass: top\n"
274                 "objectClass: LDAPsubEntry\n"
275 #ifdef SLAPD_MONITORSUBENTRY
276                 "objectClass: monitorSubEntry\n"
277 #else /* !SLAPD_MONITORSUBENTRY */
278                 "objectClass: extensibleObject\n"
279 #endif /* !SLAPD_MONITORSUBENTRY */
280                 "cn: %ld\n",
281                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
282                 c->c_connid );
283         e = str2entry( buf );
284
285         if ( e == NULL) {
286 #ifdef NEW_LOGGING
287                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
288                         "monitor_subsys_conn_create: "
289                         "unable to create entry "
290                         "'cn=%ld,%s' entry\n",
291                         c->c_connid, 
292                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val ));
293 #else
294                 Debug( LDAP_DEBUG_ANY,
295                         "monitor_subsys_conn_create: "
296                         "unable to create entry "
297                         "'cn=%ld,%s' entry\n",
298                         c->c_connid, 
299                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
300 #endif
301                 return( -1 );
302         }
303
304         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
305         
306         ltm = gmtime( &c->c_starttime );
307         strftime( buf2, sizeof(buf2), "%Y%m%d%H%M%SZ", ltm );
308                         
309         ltm = gmtime( &c->c_activitytime );
310         strftime( buf3, sizeof(buf2), "%Y%m%d%H%M%SZ", ltm );
311                         
312         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
313
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_cdn ? c->c_cdn : SLAPD_ANONYMOUS,
337                 
338                 c->c_listener_url,
339                 c->c_peer_domain,
340                 c->c_peer_name,
341                 c->c_sock_name,
342                 
343                 buf2,
344                 buf3
345                 );
346
347         bv[1].bv_val = NULL;
348         bv[0].bv_val = buf;
349         bv[0].bv_len = strlen( buf );
350         attr_merge( e, monitor_ad_desc, bv );
351
352         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
353         e->e_private = ( void * )mp;
354         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
355         mp->mp_children = NULL;
356         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
357
358         *ep = e;
359
360         return( 0 );
361 }
362
363 int 
364 monitor_subsys_conn_create( 
365         struct monitorinfo      *mi,
366         struct berval           *ndn,
367         Entry                   *e_parent,
368         Entry                   **ep
369 )
370 {
371         Connection              *c;
372         int                     connindex;
373         struct monitorentrypriv *mp;
374
375         assert( mi != NULL );
376         assert( e_parent != NULL );
377         assert( ep != NULL );
378
379         *ep = NULL;
380
381         if ( ndn == NULL ) {
382                 Entry *e, *e_tmp = NULL;
383
384                 /* create all the children of e_parent */
385                 for ( c = connection_first( &connindex );
386                                 c != NULL;
387                                 c = connection_next( c, &connindex )) {
388                         if ( conn_create( c, &e ) || e == NULL ) {
389                                 connection_done(c);
390                                 return( -1 );
391                         }
392                         mp = ( struct monitorentrypriv * )e->e_private;
393                         mp->mp_next = e_tmp;
394                         e_tmp = e;
395                 }
396                 connection_done(c);
397
398                 *ep = e;
399
400         } else {
401                 LDAPRDN         *values = NULL;
402                 const char      *text = NULL;
403                 unsigned long   connid;
404                
405                 /* create exactly the required entry */
406
407                 if ( ldap_str2rdn( ndn->bv_val, &values, &text, LDAP_DN_FORMAT_LDAP ) ) {
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
416                 ldap_rdnfree( values );
417
418                 for ( c = connection_first( &connindex );
419                                 c != NULL;
420                                 c = connection_next( c, &connindex )) {
421                         if ( c->c_connid == connid ) {
422                                 if ( conn_create( c, ep ) || *ep == NULL ) {
423                                         connection_done(c);
424                                         return( -1 );
425                                 }
426                         }
427                 }
428                 
429                 connection_done(c);
430         
431         }
432
433         return( 0 );
434 }
435