]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
291b2efe145b5cc7e6604a53c87e38b6374f6aaa
[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           bv[2];
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         bv[1].bv_val = NULL;
112         bv[0].bv_val = "0";
113         bv[0].bv_len = 1;
114         attr_merge( e, monitor_ad_desc, bv );
115         
116         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
117         e->e_private = ( void * )mp;
118         mp->mp_next = e_tmp;
119         mp->mp_children = NULL;
120         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
121         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
122                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
123
124         if ( monitor_cache_add( mi, e ) ) {
125 #ifdef NEW_LOGGING
126                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
127                         "monitor_subsys_sent_init: "
128                         "unable to add entry 'cn=Entries,%s'\n",
129                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
130 #else
131                 Debug( LDAP_DEBUG_ANY,
132                         "monitor_subsys_sent_init: "
133                         "unable to add entry 'cn=Entries,%s'\n%s%s",
134                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
135                         "", "" );
136 #endif
137                 return( -1 );
138         }
139         
140         e_tmp = e;
141
142         /*
143          * Referrals
144          */
145         snprintf( buf, sizeof( buf ),
146                         "dn: cn=Referrals,%s\n"
147                         "objectClass: top\n"
148                         "objectClass: LDAPsubEntry\n"
149 #ifdef SLAPD_MONITORSUBENTRY
150                         "objectClass: monitorSubEntry\n"
151 #else /* !SLAPD_MONITORSUBENTRY */
152                         "objectClass: extensibleObject\n"
153 #endif /* !SLAPD_MONITORSUBENTRY */
154                         "cn: Referrals\n",
155                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
156
157         e = str2entry( buf );
158         if ( e == NULL ) {
159 #ifdef NEW_LOGGING
160                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
161                         "monitor_subsys_sent_init: "
162                         "unable to create entry 'cn=Referrals,%s'\n",
163                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
164 #else
165                 Debug( LDAP_DEBUG_ANY,
166                         "monitor_subsys_sent_init: "
167                         "unable to create entry 'cn=Referrals,%s'\n%s%s",
168                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
169                         "", "" );
170 #endif
171                 return( -1 );
172         }
173
174         bv[0].bv_val = "0";
175         bv[0].bv_len = 1;
176         attr_merge( e, monitor_ad_desc, bv );
177         
178         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
179         e->e_private = ( void * )mp;
180         mp->mp_next = e_tmp;
181         mp->mp_children = NULL;
182         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
183         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
184                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
185
186         if ( monitor_cache_add( mi, e ) ) {
187 #ifdef NEW_LOGGING
188                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
189                         "monitor_subsys_sent_init: "
190                         "unable to add entry 'cn=Referrals,%s'\n",
191                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
192 #else
193                 Debug( LDAP_DEBUG_ANY,
194                         "monitor_subsys_sent_init: "
195                         "unable to add entry 'cn=Referrals,%s'\n%s%s",
196                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
197                         "", "" );
198 #endif
199                 return( -1 );
200         }
201         
202         e_tmp = e;
203
204         /*
205          * PDU
206          */
207         snprintf( buf, sizeof( buf ),
208                         "dn: cn=PDU,%s\n"
209                         "objectClass: top\n"
210                         "objectClass: LDAPsubEntry\n"
211 #ifdef SLAPD_MONITORSUBENTRY
212                         "objectClass: monitorSubEntry\n"
213 #else /* !SLAPD_MONITORSUBENTRY */
214                         "objectClass: extensibleObject\n"
215 #endif /* !SLAPD_MONITORSUBENTRY */
216                         "cn: PDU\n",
217                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
218
219         e = str2entry( buf );
220         if ( e == NULL ) {
221 #ifdef NEW_LOGGING
222                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
223                         "monitor_subsys_sent_init: "
224                         "unable to create entry 'cn=PDU,%s'\n",
225                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
226 #else
227                 Debug( LDAP_DEBUG_ANY,
228                         "monitor_subsys_sent_init: "
229                         "unable to create entry 'cn=PDU,%s'\n%s%s",
230                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
231                         "", "" );
232 #endif
233                 return( -1 );
234         }
235
236         bv[0].bv_val = "0";
237         bv[0].bv_len = 1;
238         attr_merge( e, monitor_ad_desc, bv );
239         
240         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
241         e->e_private = ( void * )mp;
242         mp->mp_next = e_tmp;
243         mp->mp_children = NULL;
244         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
245         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
246                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
247
248         if ( monitor_cache_add( mi, e ) ) {
249 #ifdef NEW_LOGGING
250                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
251                         "monitor_subsys_sent_init: "
252                         "unable to add entry 'cn=PDU,%s'\n",
253                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
254 #else
255                 Debug( LDAP_DEBUG_ANY,
256                         "monitor_subsys_sent_init: "
257                         "unable to add entry 'cn=PDU,%s'\n%s%s",
258                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
259                         "", "" );
260 #endif
261                 return( -1 );
262         }
263         
264         e_tmp = e;
265
266         /*
267          * Bytes
268          */
269         snprintf( buf, sizeof( buf ),
270                         "dn: cn=Bytes,%s\n"
271                         "objectClass: top\n"
272                         "objectClass: LDAPsubEntry\n"
273 #ifdef SLAPD_MONITORSUBENTRY
274                         "objectClass: monitorSubEntry\n"
275 #else /* !SLAPD_MONITORSUBENTRY */
276                         "objectClass: extensibleObject\n"
277 #endif /* !SLAPD_MONITORSUBENTRY */
278                         "cn: Bytes\n",
279                         monitor_subsys[SLAPD_MONITOR_SENT].mss_dn.bv_val );
280
281         e = str2entry( buf );
282         if ( e == NULL ) {
283 #ifdef NEW_LOGGING
284                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
285                         "monitor_subsys_sent_init: "
286                         "unable to create entry 'cn=Bytes,%s'\n",
287                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
288 #else
289                 Debug( LDAP_DEBUG_ANY,
290                         "monitor_subsys_sent_init: "
291                         "unable to create entry 'cn=Bytes,%s'\n%s%s",
292                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
293                         "", "" );
294 #endif
295                 return( -1 );
296         }
297
298         bv[0].bv_val = "0";
299         bv[0].bv_len = 1;
300         attr_merge( e, monitor_ad_desc, bv );
301         
302         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
303         e->e_private = ( void * )mp;
304         mp->mp_next = e_tmp;
305         mp->mp_children = NULL;
306         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_SENT];
307         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_SENT].mss_flags \
308                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
309
310         if ( monitor_cache_add( mi, e ) ) {
311 #ifdef NEW_LOGGING
312                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
313                         "monitor_subsys_sent_init: "
314                         "unable to add entry 'cn=Bytes,%s'\n",
315                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val ));
316 #else
317                 Debug( LDAP_DEBUG_ANY,
318                         "monitor_subsys_sent_init: "
319                         "unable to add entry 'cn=Bytes,%s'\n%s%s",
320                         monitor_subsys[SLAPD_MONITOR_SENT].mss_ndn.bv_val,
321                         "", "" );
322 #endif
323                 return( -1 );
324         }
325         
326         e_tmp = e;
327
328         mp = ( struct monitorentrypriv * )e_sent->e_private;
329         mp->mp_children = e_tmp;
330
331         monitor_cache_release( mi, e_sent );
332
333         return( 0 );
334 }
335
336 int
337 monitor_subsys_sent_update(
338         struct monitorinfo      *mi,
339         Entry                   *e
340 )
341 {
342         long            n = -1;
343
344         assert( mi );
345         assert( e );
346         
347         if ( strncasecmp( e->e_ndn, "CN=ENTRIES", 
348                                 sizeof("CN=ENTRIES")-1 ) == 0 ) {
349                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
350                 n = num_entries_sent;
351                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
352
353         } else if ( strncasecmp( e->e_ndn, "CN=REFERRALS", 
354                                 sizeof("CN=REFERRALS")-1 ) == 0 ) {
355                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
356                 n = num_refs_sent;
357                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
358
359         } else if ( strncasecmp( e->e_ndn, "CN=PDU", 
360                                 sizeof("CN=PDU")-1 ) == 0 ) {
361                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
362                 n = num_pdu_sent;
363                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
364
365         } else if ( strncasecmp( e->e_ndn, "CN=BYTES", 
366                                 sizeof("CN=BYTES")-1 ) == 0 ) {
367                 ldap_pvt_thread_mutex_lock(&num_sent_mutex);
368                 n = num_bytes_sent;
369                 ldap_pvt_thread_mutex_unlock(&num_sent_mutex);
370         }
371
372         if ( n != -1 ) {
373                 Attribute       *a;
374                 char            buf[16];
375
376                 a = attr_find( e->e_attrs, monitor_ad_desc );
377                 if ( a == NULL ) {
378                         return( -1 );
379                 }
380
381                 snprintf( buf, sizeof( buf ), "%ld", n );
382                 free( a->a_vals[ 0 ].bv_val );
383                 ber_str2bv( buf, 0, 1, a->a_vals );
384         }
385
386         return( 0 );
387 }
388