monitor.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * Copyright (c) 1995 Regents of the University of Michigan.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms are permitted
  46. * provided that this notice is preserved and that due credit is given
  47. * to the University of Michigan at Ann Arbor. The name of the University
  48. * may not be used to endorse or promote products derived from this
  49. * software without specific prior written permission. This software
  50. * is provided ``as is'' without express or implied warranty.
  51. */
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <sys/types.h>
  55. #include <time.h>
  56. #ifndef _WIN32
  57. #include <sys/socket.h>
  58. #endif
  59. #include "slap.h"
  60. #include "fe.h"
  61. #if defined( SLAPD_MONITOR_DN )
  62. int
  63. monitor_info(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  64. {
  65. char buf[BUFSIZ];
  66. struct berval val;
  67. struct berval *vals[2];
  68. time_t curtime = current_time();
  69. struct tm utm;
  70. Slapi_Backend *be;
  71. char *cookie;
  72. PRUint32 len;
  73. vals[0] = &val;
  74. vals[1] = NULL;
  75. /* "version" value */
  76. val.bv_val = slapd_get_version_value();
  77. val.bv_len = strlen( val.bv_val );
  78. attrlist_replace( &e->e_attrs, "version", vals );
  79. slapi_ch_free( (void **) &val.bv_val );
  80. sprintf( buf, "%d", g_get_active_threadcnt() );
  81. val.bv_val = buf;
  82. val.bv_len = strlen( buf );
  83. attrlist_replace( &e->e_attrs, "threads", vals );
  84. connection_table_as_entry(the_connection_table, e);
  85. PR_Lock( ops_mutex );
  86. sprintf( buf, "%ld", (long) ops_initiated );
  87. PR_Unlock( ops_mutex );
  88. val.bv_val = buf;
  89. val.bv_len = strlen( buf );
  90. attrlist_replace( &e->e_attrs, "opsinitiated", vals );
  91. PR_Lock( ops_mutex );
  92. sprintf( buf, "%ld", (long) ops_completed );
  93. PR_Unlock( ops_mutex );
  94. val.bv_val = buf;
  95. val.bv_len = strlen( buf );
  96. attrlist_replace( &e->e_attrs, "opscompleted", vals );
  97. PR_Lock( g_get_num_sent_mutex() );
  98. len = PR_snprintf ( buf, BUFSIZ, "%llu", g_get_num_entries_sent() );
  99. PR_Unlock( g_get_num_sent_mutex() );
  100. val.bv_val = buf;
  101. val.bv_len = ( unsigned long ) len;
  102. attrlist_replace( &e->e_attrs, "entriessent", vals );
  103. PR_Lock( g_get_num_sent_mutex() );
  104. len = PR_snprintf ( buf, BUFSIZ, "%llu", g_get_num_bytes_sent() );
  105. PR_Unlock( g_get_num_sent_mutex() );
  106. val.bv_val = buf;
  107. val.bv_len = ( unsigned long ) len;
  108. attrlist_replace( &e->e_attrs, "bytessent", vals );
  109. #ifdef _WIN32
  110. {
  111. struct tm *pt;
  112. pt = gmtime( &curtime );
  113. memcpy(&utm, pt, sizeof(struct tm) );
  114. }
  115. #else
  116. gmtime_r( &curtime, &utm );
  117. #endif
  118. strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", &utm );
  119. val.bv_val = buf;
  120. val.bv_len = strlen( buf );
  121. attrlist_replace( &e->e_attrs, "currenttime", vals );
  122. #ifdef _WIN32
  123. {
  124. struct tm *pt;
  125. pt = gmtime( &starttime );
  126. memcpy(&utm, pt, sizeof(struct tm) );
  127. }
  128. #else
  129. gmtime_r( &starttime, &utm );
  130. #endif
  131. strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", &utm );
  132. val.bv_val = buf;
  133. val.bv_len = strlen( buf );
  134. attrlist_replace( &e->e_attrs, "starttime", vals );
  135. sprintf( buf, "%d", be_nbackends_public() );
  136. val.bv_val = buf;
  137. val.bv_len = strlen( buf );
  138. attrlist_replace( &e->e_attrs, "nbackends", vals );
  139. #ifdef THREAD_SUNOS5_LWP
  140. sprintf( buf, "%d", thr_getconcurrency() );
  141. val.bv_val = buf;
  142. val.bv_len = strlen( buf );
  143. attrlist_replace( &e->e_attrs, "concurrency", vals );
  144. #endif
  145. /*Loop through the backends, and stuff the monitordns
  146. into the entry we're sending back*/
  147. attrlist_delete( &e->e_attrs, "backendmonitordn");
  148. cookie = NULL;
  149. be = slapi_get_first_backend(&cookie);
  150. while (be)
  151. {
  152. if ( !be->be_private )
  153. {
  154. Slapi_DN dn;
  155. slapi_sdn_init(&dn);
  156. be_getmonitordn(be,&dn);
  157. val.bv_val = (char*)slapi_sdn_get_dn(&dn);
  158. val.bv_len = strlen( val.bv_val );
  159. attrlist_merge( &e->e_attrs, "backendmonitordn", vals );
  160. slapi_sdn_done(&dn);
  161. }
  162. be = slapi_get_next_backend(cookie);
  163. }
  164. slapi_ch_free((void **)&cookie);
  165. *returncode= LDAP_SUCCESS;
  166. return SLAPI_DSE_CALLBACK_OK;
  167. }
  168. #endif /* SLAPD_MONITOR_DN */
  169. /*
  170. * Return a malloc'd version value.
  171. * Used for the monitor entry's 'version' attribute.
  172. * Used for the root DSE's 'vendorVersion' attribute.
  173. */
  174. char *
  175. slapd_get_version_value( void )
  176. {
  177. char *versionstring, *buildnum, *vs;
  178. versionstring = config_get_versionstring();
  179. buildnum = config_get_buildnum();
  180. vs = slapi_ch_smprintf("%s B%s", versionstring, buildnum );
  181. slapi_ch_free( (void **) &buildnum);
  182. slapi_ch_free( (void **) &versionstring);
  183. return vs;
  184. }