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