Back to index

openldap  2.4.31
sent.c
Go to the documentation of this file.
00001 /* sent.c - deal with data sent subsystem */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2001-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2001-2003 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <stdio.h>
00025 #include <ac/string.h>
00026 
00027 #include "slap.h"
00028 #include "back-monitor.h"
00029 
00030 static int
00031 monitor_subsys_sent_destroy(
00032        BackendDB            *be,
00033        monitor_subsys_t     *ms );
00034 
00035 static int
00036 monitor_subsys_sent_update(
00037        Operation            *op,
00038        SlapReply            *rs,
00039        Entry                   *e );
00040 
00041 enum {
00042        MONITOR_SENT_BYTES = 0,
00043        MONITOR_SENT_PDU,
00044        MONITOR_SENT_ENTRIES,
00045        MONITOR_SENT_REFERRALS,
00046 
00047        MONITOR_SENT_LAST
00048 };
00049 
00050 struct monitor_sent_t {
00051        struct berval rdn;
00052        struct berval nrdn;
00053 } monitor_sent[] = {
00054        { BER_BVC("cn=Bytes"),             BER_BVNULL },
00055        { BER_BVC("cn=PDU"),        BER_BVNULL },
00056        { BER_BVC("cn=Entries"),    BER_BVNULL },
00057        { BER_BVC("cn=Referrals"),  BER_BVNULL },
00058        { BER_BVNULL,               BER_BVNULL }
00059 };
00060 
00061 int
00062 monitor_subsys_sent_init(
00063        BackendDB            *be,
00064        monitor_subsys_t     *ms )
00065 {
00066        monitor_info_t       *mi;
00067        
00068        Entry         **ep, *e_sent;
00069        monitor_entry_t      *mp;
00070        int                  i;
00071 
00072        assert( be != NULL );
00073 
00074        ms->mss_destroy = monitor_subsys_sent_destroy;
00075        ms->mss_update = monitor_subsys_sent_update;
00076 
00077        mi = ( monitor_info_t * )be->be_private;
00078 
00079        if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
00080               Debug( LDAP_DEBUG_ANY,
00081                      "monitor_subsys_sent_init: "
00082                      "unable to get entry \"%s\"\n",
00083                      ms->mss_ndn.bv_val, 0, 0 );
00084               return( -1 );
00085        }
00086 
00087        mp = ( monitor_entry_t * )e_sent->e_private;
00088        mp->mp_children = NULL;
00089        ep = &mp->mp_children;
00090 
00091        for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
00092               struct berval        nrdn, bv;
00093               Entry                *e;
00094 
00095               e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
00096                      &monitor_sent[i].rdn, mi->mi_oc_monitorCounterObject,
00097                      mi, NULL, NULL );
00098                      
00099               if ( e == NULL ) {
00100                      Debug( LDAP_DEBUG_ANY,
00101                             "monitor_subsys_sent_init: "
00102                             "unable to create entry \"%s,%s\"\n",
00103                             monitor_sent[ i ].rdn.bv_val,
00104                             ms->mss_ndn.bv_val, 0 );
00105                      return( -1 );
00106               }
00107 
00108               /* steal normalized RDN */
00109               dnRdn( &e->e_nname, &nrdn );
00110               ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
00111        
00112               BER_BVSTR( &bv, "0" );
00113               attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
00114        
00115               mp = monitor_entrypriv_create();
00116               if ( mp == NULL ) {
00117                      return -1;
00118               }
00119               e->e_private = ( void * )mp;
00120               mp->mp_info = ms;
00121               mp->mp_flags = ms->mss_flags \
00122                      | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
00123 
00124               if ( monitor_cache_add( mi, e ) ) {
00125                      Debug( LDAP_DEBUG_ANY,
00126                             "monitor_subsys_sent_init: "
00127                             "unable to add entry \"%s,%s\"\n",
00128                             monitor_sent[ i ].rdn.bv_val,
00129                             ms->mss_ndn.bv_val, 0 );
00130                      return( -1 );
00131               }
00132        
00133               *ep = e;
00134               ep = &mp->mp_next;
00135        }
00136 
00137        monitor_cache_release( mi, e_sent );
00138 
00139        return( 0 );
00140 }
00141 
00142 static int
00143 monitor_subsys_sent_destroy(
00144        BackendDB            *be,
00145        monitor_subsys_t     *ms )
00146 {
00147        int           i;
00148 
00149        for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
00150               if ( !BER_BVISNULL( &monitor_sent[ i ].nrdn ) ) {
00151                      ch_free( monitor_sent[ i ].nrdn.bv_val );
00152               }
00153        }
00154 
00155        return 0;
00156 }
00157 
00158 static int
00159 monitor_subsys_sent_update(
00160        Operation            *op,
00161        SlapReply            *rs,
00162        Entry                   *e )
00163 {
00164        monitor_info_t       *mi = ( monitor_info_t *)op->o_bd->be_private;
00165        
00166        struct berval        nrdn;
00167        ldap_pvt_mp_t        n;
00168        Attribute            *a;
00169        slap_counters_t *sc;
00170        int                  i;
00171 
00172        assert( mi != NULL );
00173        assert( e != NULL );
00174 
00175        dnRdn( &e->e_nname, &nrdn );
00176 
00177        for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
00178               if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
00179                      break;
00180               }
00181        }
00182 
00183        if ( i == MONITOR_SENT_LAST ) {
00184               return SLAP_CB_CONTINUE;
00185        }
00186 
00187        ldap_pvt_thread_mutex_lock(&slap_counters.sc_mutex);
00188        switch ( i ) {
00189        case MONITOR_SENT_ENTRIES:
00190               ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
00191               for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00192                      ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00193                      ldap_pvt_mp_add( n, sc->sc_entries );
00194                      ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00195               }
00196               break;
00197 
00198        case MONITOR_SENT_REFERRALS:
00199               ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
00200               for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00201                      ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00202                      ldap_pvt_mp_add( n, sc->sc_refs );
00203                      ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00204               }
00205               break;
00206 
00207        case MONITOR_SENT_PDU:
00208               ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
00209               for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00210                      ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00211                      ldap_pvt_mp_add( n, sc->sc_pdu );
00212                      ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00213               }
00214               break;
00215 
00216        case MONITOR_SENT_BYTES:
00217               ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
00218               for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00219                      ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00220                      ldap_pvt_mp_add( n, sc->sc_bytes );
00221                      ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00222               }
00223               break;
00224 
00225        default:
00226               assert(0);
00227        }
00228        ldap_pvt_thread_mutex_unlock(&slap_counters.sc_mutex);
00229        
00230        a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
00231        assert( a != NULL );
00232 
00233        /* NOTE: no minus sign is allowed in the counters... */
00234        UI2BV( &a->a_vals[ 0 ], n );
00235        ldap_pvt_mp_clear( n );
00236 
00237        /* FIXME: touch modifyTimestamp? */
00238 
00239        return SLAP_CB_CONTINUE;
00240 }
00241