]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
4e6fb54d722b0a93262bce89dfaf6ce2cf3cc8b0
[openldap] / servers / slapd / back-monitor / sent.c
1 /* sent.c - deal with data sent subsystem */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
8  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9  * 
10  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
11  * 
12  * This work has beed deveolped for the OpenLDAP Foundation 
13  * in the hope that it may be useful to the Open Source community, 
14  * but WITHOUT ANY WARRANTY.
15  * 
16  * Permission is granted to anyone to use this software for any purpose
17  * on any computer system, and to alter it and redistribute it, subject
18  * to the following restrictions:
19  * 
20  * 1. The author and SysNet s.n.c. are not responsible for the consequences
21  *    of use of this software, no matter how awful, even if they arise from
22  *    flaws in it.
23  * 
24  * 2. The origin of this software must not be misrepresented, either by
25  *    explicit claim or by omission.  Since few users ever read sources,
26  *    credits should appear in the documentation.
27  * 
28  * 3. Altered versions must be plainly marked as such, and must not be
29  *    misrepresented as being the original software.  Since few users
30  *    ever read sources, credits should appear in the documentation.
31  *    SysNet s.n.c. cannot be responsible for the consequences of the
32  *    alterations.
33  * 
34  * 4. This notice may not be removed or altered.
35  */
36
37 #include "portable.h"
38
39 #include <stdio.h>
40
41 #include "slap.h"
42 #include "back-monitor.h"
43
44 int
45 monitor_subsys_sent_init(
46         BackendDB               *be
47 )
48 {
49         struct monitorinfo      *mi;
50         
51         Entry                   *e, *e_tmp, *e_sent;
52         struct monitorentrypriv *mp;
53         char                    buf[1024];
54         struct berval           val, *bv[2] = { &val, NULL };
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_SENT].mss_ndn, &e_sent ) ) {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
64                         "monitor_subsys_sent_init: "
65                         "unable to get entry '%s'\n",
66                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
67 #else
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_sent_init: "
70                         "unable to get entry '%s'\n%s%s",
71                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val, 
72                         "", "" );
73 #endif
74                 return( -1 );
75         }
76
77         e_tmp = NULL;
78
79         /*
80          * Entries
81          */
82         snprintf( buf, sizeof( buf ),
83                         "dn: cn=Entries,%s\n"
84                         "objectClass: top\n"
85                         "objectClass: LDAPsubEntry\n"
86 #ifdef SLAPD_MONITORSUBENTRY
87                         "objectClass: monitorSubEntry\n"
88 #else /* !SLAPD_MONITORSUBENTRY */
89                         "objectClass: extensibleObject\n"
90 #endif /* !SLAPD_MONITORSUBENTRY */
91                         "cn: Entries\n",
92                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn->bv_val );
93
94         e = str2entry( buf );
95         if ( e == NULL ) {
96 #ifdef NEW_LOGGING
97                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
98                         "monitor_subsys_sent_init: "
99                         "unable to create entry 'cn=Entries,%s'\n",
100                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
101 #else
102                 Debug( LDAP_DEBUG_ANY,
103                         "monitor_subsys_sent_init: "
104                         "unable to create entry 'cn=Entries,%s'\n%s%s",
105                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
106                         "", "" );
107 #endif
108                 return( -1 );
109         }
110         
111         val.bv_val = "0";
112         val.bv_len = 1;
113         attr_merge( e, monitor_ad_desc, bv );
114         
115         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
116         e->e_private = ( void * )mp;
117         mp->mp_next = e_tmp;
118         mp->mp_children = NULL;
119         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
120         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
121                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
122
123         if ( monitor_cache_add( mi, e ) ) {
124 #ifdef NEW_LOGGING
125                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
126                         "monitor_subsys_sent_init: "
127                         "unable to add entry 'cn=Entries,%s'\n",
128                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
129 #else
130                 Debug( LDAP_DEBUG_ANY,
131                         "monitor_subsys_sent_init: "
132                         "unable to add entry 'cn=Entries,%s'\n%s%s",
133                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
134                         "", "" );
135 #endif
136                 return( -1 );
137         }
138         
139         e_tmp = e;
140
141         /*
142          * Referrals
143          */
144         snprintf( buf, sizeof( buf ),
145                         "dn: cn=Referrals,%s\n"
146                         "objectClass: top\n"
147                         "objectClass: LDAPsubEntry\n"
148 #ifdef SLAPD_MONITORSUBENTRY
149                         "objectClass: monitorSubEntry\n"
150 #else /* !SLAPD_MONITORSUBENTRY */
151                         "objectClass: extensibleObject\n"
152 #endif /* !SLAPD_MONITORSUBENTRY */
153                         "cn: Referrals\n",
154                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn->bv_val );
155
156         e = str2entry( buf );
157         if ( e == NULL ) {
158 #ifdef NEW_LOGGING
159                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
160                         "monitor_subsys_sent_init: "
161                         "unable to create entry 'cn=Referrals,%s'\n",
162                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
163 #else
164                 Debug( LDAP_DEBUG_ANY,
165                         "monitor_subsys_sent_init: "
166                         "unable to create entry 'cn=Referrals,%s'\n%s%s",
167                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
168                         "", "" );
169 #endif
170                 return( -1 );
171         }
172
173         val.bv_val = "0";
174         val.bv_len = 1;
175         attr_merge( e, monitor_ad_desc, bv );
176         
177         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
178         e->e_private = ( void * )mp;
179         mp->mp_next = e_tmp;
180         mp->mp_children = NULL;
181         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
182         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
183                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
184
185         if ( monitor_cache_add( mi, e ) ) {
186 #ifdef NEW_LOGGING
187                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
188                         "monitor_subsys_sent_init: "
189                         "unable to add entry 'cn=Referrals,%s'\n",
190                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
191 #else
192                 Debug( LDAP_DEBUG_ANY,
193                         "monitor_subsys_sent_init: "
194                         "unable to add entry 'cn=Referrals,%s'\n%s%s",
195                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
196                         "", "" );
197 #endif
198                 return( -1 );
199         }
200         
201         e_tmp = e;
202
203         /*
204          * PDU
205          */
206         snprintf( buf, sizeof( buf ),
207                         "dn: cn=PDU,%s\n"
208                         "objectClass: top\n"
209                         "objectClass: LDAPsubEntry\n"
210 #ifdef SLAPD_MONITORSUBENTRY
211                         "objectClass: monitorSubEntry\n"
212 #else /* !SLAPD_MONITORSUBENTRY */
213                         "objectClass: extensibleObject\n"
214 #endif /* !SLAPD_MONITORSUBENTRY */
215                         "cn: PDU\n",
216                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn->bv_val );
217
218         e = str2entry( buf );
219         if ( e == NULL ) {
220 #ifdef NEW_LOGGING
221                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
222                         "monitor_subsys_sent_init: "
223                         "unable to create entry 'cn=PDU,%s'\n",
224                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
225 #else
226                 Debug( LDAP_DEBUG_ANY,
227                         "monitor_subsys_sent_init: "
228                         "unable to create entry 'cn=PDU,%s'\n%s%s",
229                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
230                         "", "" );
231 #endif
232                 return( -1 );
233         }
234
235         val.bv_val = "0";
236         val.bv_len = 1;
237         attr_merge( e, monitor_ad_desc, bv );
238         
239         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
240         e->e_private = ( void * )mp;
241         mp->mp_next = e_tmp;
242         mp->mp_children = NULL;
243         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
244         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
245                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
246
247         if ( monitor_cache_add( mi, e ) ) {
248 #ifdef NEW_LOGGING
249                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
250                         "monitor_subsys_sent_init: "
251                         "unable to add entry 'cn=PDU,%s'\n",
252                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
253 #else
254                 Debug( LDAP_DEBUG_ANY,
255                         "monitor_subsys_sent_init: "
256                         "unable to add entry 'cn=PDU,%s'\n%s%s",
257                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
258                         "", "" );
259 #endif
260                 return( -1 );
261         }
262         
263         e_tmp = e;
264
265         /*
266          * Bytes
267          */
268         snprintf( buf, sizeof( buf ),
269                         "dn: cn=Bytes,%s\n"
270                         "objectClass: top\n"
271                         "objectClass: LDAPsubEntry\n"
272 #ifdef SLAPD_MONITORSUBENTRY
273                         "objectClass: monitorSubEntry\n"
274 #else /* !SLAPD_MONITORSUBENTRY */
275                         "objectClass: extensibleObject\n"
276 #endif /* !SLAPD_MONITORSUBENTRY */
277                         "cn: Bytes\n",
278                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn->bv_val );
279
280         e = str2entry( buf );
281         if ( e == NULL ) {
282 #ifdef NEW_LOGGING
283                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
284                         "monitor_subsys_sent_init: "
285                         "unable to create entry 'cn=Bytes,%s'\n",
286                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
287 #else
288                 Debug( LDAP_DEBUG_ANY,
289                         "monitor_subsys_sent_init: "
290                         "unable to create entry 'cn=Bytes,%s'\n%s%s",
291                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
292                         "", "" );
293 #endif
294                 return( -1 );
295         }
296
297         val.bv_val = "0";
298         val.bv_len = 1;
299         attr_merge( e, monitor_ad_desc, bv );
300         
301         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
302         e->e_private = ( void * )mp;
303         mp->mp_next = e_tmp;
304         mp->mp_children = NULL;
305         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
306         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
307                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
308
309         if ( monitor_cache_add( mi, e ) ) {
310 #ifdef NEW_LOGGING
311                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
312                         "monitor_subsys_sent_init: "
313                         "unable to add entry 'cn=Bytes,%s'\n",
314                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val ));
315 #else
316                 Debug( LDAP_DEBUG_ANY,
317                         "monitor_subsys_sent_init: "
318                         "unable to add entry 'cn=Bytes,%s'\n%s%s",
319                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn->bv_val,
320                         "", "" );
321 #endif
322                 return( -1 );
323         }
324         
325         e_tmp = e;
326
327         mp = ( struct monitorentrypriv * )e_sent->e_private;
328         mp->mp_children = e_tmp;
329
330         monitor_cache_release( mi, e_sent );
331
332         return( 0 );
333 }
334
335 int
336 monitor_subsys_sent_update(
337         struct monitorinfo      *mi,
338         Entry                   *e
339 )
340 {
341         long            n = -1;
342
343         assert( mi );
344         assert( e );
345         
346         if ( strncasecmp( e->e_ndn, "CN=ENTRIES", 
347                                 sizeof("CN=ENTRIES")-1 ) == 0 ) {
348                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
349                 n = num_entries_sent;
350                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
351
352         } else if ( strncasecmp( e->e_ndn, "CN=REFERRALS", 
353                                 sizeof("CN=REFERRALS")-1 ) == 0 ) {
354                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
355                 n = num_refs_sent;
356                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
357
358         } else if ( strncasecmp( e->e_ndn, "CN=PDU", 
359                                 sizeof("CN=PDU")-1 ) == 0 ) {
360                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
361                 n = num_pdu_sent;
362                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
363
364         } else if ( strncasecmp( e->e_ndn, "CN=BYTES", 
365                                 sizeof("CN=BYTES")-1 ) == 0 ) {
366                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
367                 n = num_bytes_sent;
368                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
369         }
370
371         if ( n != -1 ) {
372                 Attribute       *a;
373                 char            buf[16];
374
375                 a = attr_find( e->e_attrs, monitor_ad_desc );
376                 if ( a == NULL ) {
377                         return( -1 );
378                 }
379
380                 snprintf( buf, sizeof( buf ), "%ld", n );
381                 ber_bvfree( a->a_vals[ 0 ] );
382                 a->a_vals[ 0 ] = ber_bvstrdup( buf );
383         }
384
385         return( 0 );
386 }
387