]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/sent.c
f6c0a9f17fce87498d7ada9a35db5e7023a0da58
[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-2005 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 static int
31 monitor_subsys_sent_destroy(
32         BackendDB               *be,
33         monitor_subsys_t        *ms );
34
35 static int
36 monitor_subsys_sent_update(
37         Operation               *op,
38         SlapReply               *rs,
39         Entry                   *e );
40
41 enum {
42         MONITOR_SENT_BYTES = 0,
43         MONITOR_SENT_PDU,
44         MONITOR_SENT_ENTRIES,
45         MONITOR_SENT_REFERRALS,
46
47         MONITOR_SENT_LAST
48 };
49
50 struct monitor_sent_t {
51         struct berval   rdn;
52         struct berval   nrdn;
53 } monitor_sent[] = {
54         { BER_BVC("cn=Bytes"),          BER_BVNULL },
55         { BER_BVC("cn=PDU"),            BER_BVNULL },
56         { BER_BVC("cn=Entries"),        BER_BVNULL },
57         { BER_BVC("cn=Referrals"),      BER_BVNULL },
58         { BER_BVNULL,                   BER_BVNULL }
59 };
60
61 int
62 monitor_subsys_sent_init(
63         BackendDB               *be,
64         monitor_subsys_t        *ms )
65 {
66         monitor_info_t  *mi;
67         
68         Entry           **ep, *e_sent;
69         monitor_entry_t *mp;
70         int                     i;
71
72         assert( be != NULL );
73
74         ms->mss_destroy = monitor_subsys_sent_destroy;
75         ms->mss_update = monitor_subsys_sent_update;
76
77         mi = ( monitor_info_t * )be->be_private;
78
79         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
80                 Debug( LDAP_DEBUG_ANY,
81                         "monitor_subsys_sent_init: "
82                         "unable to get entry \"%s\"\n",
83                         ms->mss_ndn.bv_val, 0, 0 );
84                 return( -1 );
85         }
86
87         mp = ( monitor_entry_t * )e_sent->e_private;
88         mp->mp_children = NULL;
89         ep = &mp->mp_children;
90
91         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
92                 char                    buf[ BACKMONITOR_BUFSIZE ];
93                 struct berval           nrdn, bv;
94                 Entry                   *e;
95
96                 snprintf( buf, sizeof( buf ),
97                                 "dn: %s,%s\n"
98                                 "objectClass: %s\n"
99                                 "structuralObjectClass: %s\n"
100                                 "cn: %s\n"
101                                 "creatorsName: %s\n"
102                                 "modifiersName: %s\n"
103                                 "createTimestamp: %s\n"
104                                 "modifyTimestamp: %s\n",
105                                 monitor_sent[ i ].rdn.bv_val,
106                                 ms->mss_dn.bv_val,
107                                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
108                                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
109                                 &monitor_sent[ i ].rdn.bv_val[ STRLENOF( "cn=" ) ],
110                                 mi->mi_creatorsName.bv_val,
111                                 mi->mi_creatorsName.bv_val,
112                                 mi->mi_startTime.bv_val,
113                                 mi->mi_startTime.bv_val );
114
115                 e = str2entry( buf );
116                 if ( e == NULL ) {
117                         Debug( LDAP_DEBUG_ANY,
118                                 "monitor_subsys_sent_init: "
119                                 "unable to create entry \"%s,%s\"\n",
120                                 monitor_sent[ i ].rdn.bv_val,
121                                 ms->mss_ndn.bv_val, 0 );
122                         return( -1 );
123                 }
124
125                 /* steal normalized RDN */
126                 dnRdn( &e->e_nname, &nrdn );
127                 ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
128         
129                 BER_BVSTR( &bv, "0" );
130                 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
131         
132                 mp = monitor_entrypriv_create();
133                 if ( mp == NULL ) {
134                         return -1;
135                 }
136                 e->e_private = ( void * )mp;
137                 mp->mp_info = ms;
138                 mp->mp_flags = ms->mss_flags \
139                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
140
141                 if ( monitor_cache_add( mi, e ) ) {
142                         Debug( LDAP_DEBUG_ANY,
143                                 "monitor_subsys_sent_init: "
144                                 "unable to add entry \"%s,%s\"\n",
145                                 monitor_sent[ i ].rdn.bv_val,
146                                 ms->mss_ndn.bv_val, 0 );
147                         return( -1 );
148                 }
149         
150                 *ep = e;
151                 ep = &mp->mp_next;
152         }
153
154         monitor_cache_release( mi, e_sent );
155
156         return( 0 );
157 }
158
159 static int
160 monitor_subsys_sent_destroy(
161         BackendDB               *be,
162         monitor_subsys_t        *ms )
163 {
164         int             i;
165
166         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
167                 if ( !BER_BVISNULL( &monitor_sent[ i ].nrdn ) ) {
168                         ch_free( monitor_sent[ i ].nrdn.bv_val );
169                 }
170         }
171
172         return 0;
173 }
174
175 static int
176 monitor_subsys_sent_update(
177         Operation               *op,
178         SlapReply               *rs,
179         Entry                   *e )
180 {
181         monitor_info_t  *mi = ( monitor_info_t *)op->o_bd->be_private;
182         
183         struct berval           nrdn;
184         ldap_pvt_mp_t           n;
185         Attribute               *a;
186         int                     i;
187
188         assert( mi != NULL );
189         assert( e != NULL );
190
191         dnRdn( &e->e_nname, &nrdn );
192
193         for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
194                 if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
195                         break;
196                 }
197         }
198
199         if ( i == MONITOR_SENT_LAST ) {
200                 return SLAP_CB_CONTINUE;
201         }
202
203         ldap_pvt_thread_mutex_lock(&slap_counters.sc_sent_mutex);
204         switch ( i ) {
205         case MONITOR_SENT_ENTRIES:
206                 ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
207                 break;
208
209         case MONITOR_SENT_REFERRALS:
210                 ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
211                 break;
212
213         case MONITOR_SENT_PDU:
214                 ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
215                 break;
216
217         case MONITOR_SENT_BYTES:
218                 ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
219                 break;
220
221         default:
222                 assert(0);
223         }
224         ldap_pvt_thread_mutex_unlock(&slap_counters.sc_sent_mutex);
225         
226         a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
227         assert( a != NULL );
228
229         /* NOTE: no minus sign is allowed in the counters... */
230         UI2BV( &a->a_vals[ 0 ], n );
231         ldap_pvt_mp_clear( n );
232
233         /* FIXME: touch modifyTimestamp? */
234
235         return SLAP_CB_CONTINUE;
236 }
237