]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
e9185b5a69c60cad4d2db2fd979ea346e4d0fb26
[openldap] / servers / slapd / back-monitor / thread.c
1 /* thread.c - deal with thread subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2004 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 /*
31 *  * initializes log subentry
32 *   */
33 int
34 monitor_subsys_thread_init(
35         BackendDB       *be
36 )
37 {
38         struct monitorinfo      *mi;
39         struct monitorentrypriv *mp;
40         Entry                   *e, **ep, *e_thread;
41         static char             buf[ BACKMONITOR_BUFSIZE ];
42
43         mi = ( struct monitorinfo * )be->be_private;
44
45         if ( monitor_cache_get( mi, 
46                 &monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn, &e_thread ) )
47         {
48                 Debug( LDAP_DEBUG_ANY,
49                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
50                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn.bv_val, 
51                         0, 0 );
52                 return( -1 );
53         }
54
55         mp = ( struct monitorentrypriv * )e_thread->e_private;
56         mp->mp_children = NULL;
57         ep = &mp->mp_children;
58
59         /*
60          * Max
61          */
62         snprintf( buf, sizeof( buf ),
63                         "dn: cn=Max,%s\n"
64                         "objectClass: %s\n"
65                         "structuralObjectClass: %s\n"
66                         "cn: Max\n"
67                         "%s: %d\n"
68                         "creatorsName: %s\n"
69                         "modifiersName: %s\n"
70                         "createTimestamp: %s\n"
71                         "modifyTimestamp: %s\n", 
72                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_dn.bv_val,
73                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
74                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
75                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
76                         SLAPD_GLOBAL(connection_pool_max),
77                         mi->mi_creatorsName.bv_val,
78                         mi->mi_creatorsName.bv_val,
79                         mi->mi_startTime.bv_val,
80                         mi->mi_startTime.bv_val );
81
82         e = str2entry( buf );
83         if ( e == NULL ) {
84                 Debug( LDAP_DEBUG_ANY,
85                         "monitor_subsys_thread_init: "
86                         "unable to create entry \"cn=Max,%s\"\n",
87                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn.bv_val, 0, 0 );
88                 return( -1 );
89         }
90         
91         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
92         e->e_private = ( void * )mp;
93         mp->mp_next = NULL;
94         mp->mp_children = NULL;
95         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_THREAD];
96         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_THREAD].mss_flags \
97                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
98
99         if ( monitor_cache_add( mi, e ) ) {
100                 Debug( LDAP_DEBUG_ANY,
101                         "monitor_subsys_thread_init: "
102                         "unable to add entry \"cn=Max,%s\"\n",
103                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn.bv_val, 0, 0 );
104                 return( -1 );
105         }
106         
107         *ep = e;
108         ep = &mp->mp_next;
109
110         /*
111          * Backload
112          */
113         snprintf( buf, sizeof( buf ),
114                         "dn: cn=Backload,%s\n"
115                         "objectClass: %s\n"
116                         "structuralObjectClass: %s\n"
117                         "cn: Backload\n"
118                         "%s: 0\n"
119                         "creatorsName: %s\n"
120                         "modifiersName: %s\n"
121                         "createTimestamp: %s\n"
122                         "modifyTimestamp: %s\n",
123                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_dn.bv_val,
124                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
125                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
126                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
127                         mi->mi_creatorsName.bv_val,
128                         mi->mi_creatorsName.bv_val,
129                         mi->mi_startTime.bv_val,
130                         mi->mi_startTime.bv_val );
131
132         e = str2entry( buf );
133         if ( e == NULL ) {
134                 Debug( LDAP_DEBUG_ANY,
135                         "monitor_subsys_thread_init: "
136                         "unable to create entry \"cn=Backload,%s\"\n",
137                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn.bv_val, 0, 0 );
138                 return( -1 );
139         }
140
141         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
142         e->e_private = ( void * )mp;
143         mp->mp_next = NULL;
144         mp->mp_children = NULL;
145         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_THREAD];
146         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_THREAD].mss_flags \
147                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
148
149         if ( monitor_cache_add( mi, e ) ) {
150                 Debug( LDAP_DEBUG_ANY,
151                         "monitor_subsys_thread_init: "
152                         "unable to add entry \"cn=Backload,%s\"\n",
153                         monitor_subsys[SLAPD_MONITOR_THREAD].mss_ndn.bv_val, 0, 0 );
154                 return( -1 );
155         }
156         
157         *ep = e;
158         ep = &mp->mp_next;
159
160         monitor_cache_release( mi, e_thread );
161
162         return( 0 );
163 }
164
165 int 
166 monitor_subsys_thread_update( 
167         Operation               *op,
168         Entry                   *e
169 )
170 {
171         struct monitorinfo      *mi =
172                 (struct monitorinfo *)op->o_bd->be_private;
173         Attribute               *a;
174         char                    buf[ BACKMONITOR_BUFSIZE ];
175         static struct berval    backload_bv = BER_BVC( "cn=backload" );
176         struct berval           rdn;
177         ber_len_t               len;
178
179         assert( mi != NULL );
180
181         dnRdn( &e->e_nname, &rdn );
182         if ( !dn_match( &rdn, &backload_bv ) ) {
183                 return 0;
184         }
185
186         a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
187         if ( a == NULL ) {
188                 return -1;
189         }
190
191         snprintf( buf, sizeof( buf ), "%d", 
192                         ldap_pvt_thread_pool_backload( &SLAPD_GLOBAL(connection_pool) ) );
193         len = strlen( buf );
194         if ( len > a->a_vals[ 0 ].bv_len ) {
195                 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
196         }
197         a->a_vals[ 0 ].bv_len = len;
198         AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
199
200         return( 0 );
201 }
202