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