snmp_collator.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #ifndef _WIN32
  46. #include <sys/ipc.h>
  47. #include <sys/msg.h>
  48. #include <dirent.h>
  49. #include <semaphore.h>
  50. #endif
  51. #include <time.h>
  52. #include <signal.h>
  53. #include <string.h>
  54. #include <errno.h>
  55. #include "agtmmap.h"
  56. #include "slap.h"
  57. #include "prthread.h"
  58. #include "prlock.h"
  59. #include "prerror.h"
  60. #include "prcvar.h"
  61. #include "plstr.h"
  62. #ifdef HPUX
  63. /* HP-UX doesn't define SEM_FAILED like other platforms, so
  64. * we define it ourselves. */
  65. #define SEM_FAILED ((sem_t *)(-1))
  66. #endif
  67. #define SNMP_NUM_SEM_WAITS 10
  68. #include "snmp_collator.h"
  69. #include "../snmp/ntagt/nslagtcom_nt.h"
  70. /* stevross: safe to assume port should be at most 5 digits ? */
  71. #define PORT_LEN 5
  72. /* strlen of url portions ie "ldap://:/" */
  73. #define URL_CHARS_LEN 9
  74. static char *make_ds_url(char *host, int port);
  75. static void print_snmp_interaction_table();
  76. static int search_interaction_table(char *dsURL, int *isnew);
  77. static void loadConfigStats();
  78. static Slapi_Entry *getConfigEntry( Slapi_Entry **e );
  79. static void freeConfigEntry( Slapi_Entry **e );
  80. static void snmp_update_ops_table();
  81. static void snmp_update_entries_table();
  82. static void snmp_update_interactions_table();
  83. static void snmp_update_cache_stats();
  84. static void snmp_collator_create_semaphore();
  85. static void snmp_collator_sem_wait();
  86. /* snmp stats stuff */
  87. struct agt_stats_t *stats=NULL;
  88. /* mmap stuff */
  89. static int hdl;
  90. /* collator stuff */
  91. static char *tmpstatsfile = AGT_STATS_FILE;
  92. #ifdef _WIN32
  93. static TCHAR szStatsFile[_MAX_PATH];
  94. static TCHAR szTempDir[_MAX_PATH];
  95. static HANDLE hParentProcess = NULL;
  96. static HANDLE hStatSlot = NULL;
  97. static HANDLE hLogFile = INVALID_HANDLE_VALUE;
  98. static TCHAR szSpoolRootDir[_MAX_PATH];
  99. #else
  100. static char szStatsFile[_MAX_PATH];
  101. static char stats_sem_name[_MAX_PATH];
  102. #endif /* _WIN32*/
  103. static Slapi_Eq_Context snmp_eq_ctx;
  104. static int snmp_collator_stopped = 0;
  105. /* synchronization stuff */
  106. static PRLock *interaction_table_mutex;
  107. static sem_t *stats_sem;
  108. /***********************************************************************************
  109. *
  110. * int snmp_collator_init()
  111. *
  112. * initializes the global variables used by snmp
  113. *
  114. ************************************************************************************/
  115. static int snmp_collator_init(){
  116. int i;
  117. /*
  118. * Create the global SNMP counters
  119. */
  120. g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds = slapi_counter_new();
  121. g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds = slapi_counter_new();
  122. g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds = slapi_counter_new();
  123. g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds = slapi_counter_new();
  124. g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors = slapi_counter_new();
  125. g_get_global_snmp_vars()->ops_tbl.dsInOps = slapi_counter_new();
  126. g_get_global_snmp_vars()->ops_tbl.dsReadOps = slapi_counter_new();
  127. g_get_global_snmp_vars()->ops_tbl.dsCompareOps = slapi_counter_new();
  128. g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps = slapi_counter_new();
  129. g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps = slapi_counter_new();
  130. g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps = slapi_counter_new();
  131. g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps = slapi_counter_new();
  132. g_get_global_snmp_vars()->ops_tbl.dsListOps = slapi_counter_new();
  133. g_get_global_snmp_vars()->ops_tbl.dsSearchOps = slapi_counter_new();
  134. g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps = slapi_counter_new();
  135. g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps = slapi_counter_new();
  136. g_get_global_snmp_vars()->ops_tbl.dsReferrals = slapi_counter_new();
  137. g_get_global_snmp_vars()->ops_tbl.dsChainings = slapi_counter_new();
  138. g_get_global_snmp_vars()->ops_tbl.dsSecurityErrors = slapi_counter_new();
  139. g_get_global_snmp_vars()->ops_tbl.dsErrors = slapi_counter_new();
  140. g_get_global_snmp_vars()->ops_tbl.dsConnections = slapi_counter_new();
  141. g_get_global_snmp_vars()->ops_tbl.dsConnectionSeq = slapi_counter_new();
  142. g_get_global_snmp_vars()->ops_tbl.dsBytesRecv = slapi_counter_new();
  143. g_get_global_snmp_vars()->ops_tbl.dsBytesSent = slapi_counter_new();
  144. g_get_global_snmp_vars()->ops_tbl.dsEntriesReturned = slapi_counter_new();
  145. g_get_global_snmp_vars()->ops_tbl.dsReferralsReturned = slapi_counter_new();
  146. g_get_global_snmp_vars()->entries_tbl.dsMasterEntries = slapi_counter_new();
  147. g_get_global_snmp_vars()->entries_tbl.dsCopyEntries = slapi_counter_new();
  148. g_get_global_snmp_vars()->entries_tbl.dsCacheEntries = slapi_counter_new();
  149. g_get_global_snmp_vars()->entries_tbl.dsCacheHits = slapi_counter_new();
  150. g_get_global_snmp_vars()->entries_tbl.dsSlaveHits = slapi_counter_new();
  151. /* Initialize the global interaction table */
  152. for(i=0; i < NUM_SNMP_INT_TBL_ROWS; i++)
  153. {
  154. g_get_global_snmp_vars()->int_tbl[i].dsIntIndex = i + 1;
  155. strncpy(g_get_global_snmp_vars()->int_tbl[i].dsName, "Not Available",
  156. sizeof(g_get_global_snmp_vars()->int_tbl[i].dsName));
  157. g_get_global_snmp_vars()->int_tbl[i].dsTimeOfCreation = 0;
  158. g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastAttempt = 0;
  159. g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastSuccess = 0;
  160. g_get_global_snmp_vars()->int_tbl[i].dsFailuresSinceLastSuccess = 0;
  161. g_get_global_snmp_vars()->int_tbl[i].dsFailures = 0;
  162. g_get_global_snmp_vars()->int_tbl[i].dsSuccesses = 0;
  163. strncpy(g_get_global_snmp_vars()->int_tbl[i].dsURL, "Not Available",
  164. sizeof(g_get_global_snmp_vars()->int_tbl[i].dsURL));
  165. }
  166. /* Get the semaphore */
  167. snmp_collator_sem_wait();
  168. /* Initialize the mmap structure */
  169. memset((void *) stats, 0, sizeof(*stats));
  170. /* Load header stats table */
  171. strncpy(stats->hdr_stats.dsVersion, SLAPD_VERSION_STR,
  172. (sizeof(stats->hdr_stats.dsVersion)/sizeof(char)) - 1);
  173. stats->hdr_stats.restarted = 0;
  174. stats->hdr_stats.startTime = time(0); /* This is a bit off, hope it's ok */
  175. loadConfigStats();
  176. /* update the mmap'd tables */
  177. snmp_update_ops_table();
  178. snmp_update_entries_table();
  179. snmp_update_interactions_table();
  180. /* Release the semaphore */
  181. sem_post(stats_sem);
  182. /* create lock for interaction table */
  183. interaction_table_mutex = PR_NewLock();
  184. return 0;
  185. }
  186. /***********************************************************************************
  187. * given the name, whether or not it was successful and the URL updates snmp
  188. * interaction table appropriately
  189. *
  190. *
  191. ************************************************************************************/
  192. void set_snmp_interaction_row(char *host, int port, int error)
  193. {
  194. int index;
  195. int isnew;
  196. char *dsName;
  197. char *dsURL;
  198. /* stevross: our servers don't have a concept of dsName as a distinguished name
  199. as specified in the MIB. Make this "Not Available" for now waiting for
  200. sometime in the future when we do
  201. */
  202. dsName = "Not Available";
  203. dsURL= make_ds_url(host, port);
  204. /* lock around here to avoid race condition of two threads trying to update table at same time */
  205. PR_Lock(interaction_table_mutex);
  206. index = search_interaction_table(dsURL, &isnew);
  207. if(isnew){
  208. /* fillin the new row from scratch*/
  209. g_get_global_snmp_vars()->int_tbl[index].dsIntIndex = index;
  210. strncpy(g_get_global_snmp_vars()->int_tbl[index].dsName, dsName,
  211. sizeof(g_get_global_snmp_vars()->int_tbl[index].dsName));
  212. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfCreation = time(0);
  213. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfLastAttempt = time(0);
  214. if(error == 0){
  215. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfLastSuccess = time(0);
  216. g_get_global_snmp_vars()->int_tbl[index].dsFailuresSinceLastSuccess = 0;
  217. g_get_global_snmp_vars()->int_tbl[index].dsFailures = 0;
  218. g_get_global_snmp_vars()->int_tbl[index].dsSuccesses = 1;
  219. }else{
  220. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfLastSuccess = 0;
  221. g_get_global_snmp_vars()->int_tbl[index].dsFailuresSinceLastSuccess = 1;
  222. g_get_global_snmp_vars()->int_tbl[index].dsFailures = 1;
  223. g_get_global_snmp_vars()->int_tbl[index].dsSuccesses = 0;
  224. }
  225. strncpy(g_get_global_snmp_vars()->int_tbl[index].dsURL, dsURL,
  226. sizeof(g_get_global_snmp_vars()->int_tbl[index].dsURL));
  227. }else{
  228. /* just update the appropriate fields */
  229. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfLastAttempt = time(0);
  230. if(error == 0){
  231. g_get_global_snmp_vars()->int_tbl[index].dsTimeOfLastSuccess = time(0);
  232. g_get_global_snmp_vars()->int_tbl[index].dsFailuresSinceLastSuccess = 0;
  233. g_get_global_snmp_vars()->int_tbl[index].dsSuccesses += 1;
  234. }else{
  235. g_get_global_snmp_vars()->int_tbl[index].dsFailuresSinceLastSuccess +=1;
  236. g_get_global_snmp_vars()->int_tbl[index].dsFailures +=1;
  237. }
  238. }
  239. PR_Unlock(interaction_table_mutex);
  240. /* free the memory allocated for dsURL in call to ds_make_url */
  241. if(dsURL != NULL){
  242. slapi_ch_free( (void**)&dsURL );
  243. }
  244. }
  245. /***********************************************************************************
  246. * Given: host and port
  247. * Returns: ldapUrl in form of
  248. * ldap://host.mcom.com:port/
  249. *
  250. * this should point to root DSE
  251. ************************************************************************************/
  252. static char *make_ds_url(char *host, int port){
  253. char *url;
  254. url = slapi_ch_smprintf("ldap://%s:%d/",host, port);
  255. return url;
  256. }
  257. /***********************************************************************************
  258. * searches the table for the url specified
  259. * If there, returns index to update stats
  260. * if, not there returns index of oldest interaction, and isnew flag is set
  261. * so caller can rewrite this row
  262. ************************************************************************************/
  263. static int search_interaction_table(char *dsURL, int *isnew)
  264. {
  265. int i;
  266. int index = 0;
  267. time_t oldestattempt;
  268. time_t currentattempt;
  269. oldestattempt = g_get_global_snmp_vars()->int_tbl[0].dsTimeOfLastAttempt;
  270. *isnew = 1;
  271. for(i=0; i < NUM_SNMP_INT_TBL_ROWS; i++){
  272. if(!strcmp(g_get_global_snmp_vars()->int_tbl[i].dsURL, "Not Available"))
  273. {
  274. /* found it -- this is new, first time for this row */
  275. index = i;
  276. break;
  277. }else if(!strcmp(g_get_global_snmp_vars()->int_tbl[i].dsURL, dsURL)){
  278. /* found it -- it was already there*/
  279. *isnew = 0;
  280. index = i;
  281. break;
  282. }else{
  283. /* not found so figure out oldest row */
  284. currentattempt = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastAttempt;
  285. if(currentattempt <= oldestattempt){
  286. index=i;
  287. oldestattempt = currentattempt;
  288. }
  289. }
  290. }
  291. return index;
  292. }
  293. /* for debuging until subagent part working, print contents of interaction table */
  294. static void print_snmp_interaction_table()
  295. {
  296. int i;
  297. for(i=0; i < NUM_SNMP_INT_TBL_ROWS; i++)
  298. {
  299. fprintf(stderr, " dsIntIndex: %d \n", g_get_global_snmp_vars()->int_tbl[i].dsIntIndex);
  300. fprintf(stderr, " dsName: %s \n", g_get_global_snmp_vars()->int_tbl[i].dsName);
  301. fprintf(stderr, " dsTimeOfCreation: %ld \n", g_get_global_snmp_vars()->int_tbl[i].dsTimeOfCreation);
  302. fprintf(stderr, " dsTimeOfLastAttempt: %ld \n", g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastAttempt);
  303. fprintf(stderr, " dsTimeOfLastSuccess: %ld \n", g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastSuccess);
  304. fprintf(stderr, "dsFailuresSinceLastSuccess: %d \n", g_get_global_snmp_vars()->int_tbl[i].dsFailuresSinceLastSuccess);
  305. fprintf(stderr, " dsFailures: %d \n", g_get_global_snmp_vars()->int_tbl[i].dsFailures);
  306. fprintf(stderr, " dsSuccesses: %d \n", g_get_global_snmp_vars()->int_tbl[i].dsSuccesses);
  307. fprintf(stderr, " dsURL: %s \n", g_get_global_snmp_vars()->int_tbl[i].dsURL);
  308. fprintf(stderr, "\n");
  309. }
  310. }
  311. /*-------------------------------------------------------------------------
  312. *
  313. * sc_setevent: Sets the specified event (NT only). The input event has
  314. * to be created by the subagent during its initialization.
  315. *
  316. * Returns: None
  317. *
  318. *-----------------------------------------------------------------------*/
  319. #ifdef _WIN32
  320. void sc_setevent(char *ev)
  321. {
  322. HANDLE hTrapEvent;
  323. DWORD err = NO_ERROR;
  324. /*
  325. * Set the event handle to force NT SNMP service to call the subagent
  326. * DLL to generate a trap. Any error will be ignored as the subagent
  327. * may not have been loaded.
  328. */
  329. if ((hTrapEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE,
  330. (LPCTSTR) ev)) != NULL)
  331. {
  332. if (SetEvent(hTrapEvent) == FALSE)
  333. err = GetLastError();
  334. }
  335. else
  336. err = GetLastError();
  337. if (err != NO_ERROR)
  338. {
  339. fprintf(stderr, "Failed to set trap (error = %d).\n", err);
  340. }
  341. }
  342. #endif
  343. /***********************************************************************************
  344. *
  345. * int snmp_collator_start()
  346. *
  347. * open the memory map and initialize the variables
  348. * initializes the global variables used by snmp
  349. *
  350. * starts the collator thread
  351. ************************************************************************************/
  352. int snmp_collator_start()
  353. {
  354. int err;
  355. char *statspath = config_get_rundir();
  356. char *instdir = config_get_configdir();
  357. char *instname = NULL;
  358. /*
  359. * Get directory for our stats file
  360. */
  361. if (NULL == statspath) {
  362. statspath = slapi_ch_strdup("/tmp");
  363. }
  364. instname = PL_strrstr(instdir, "slapd-");
  365. if (!instname) {
  366. instname = PL_strrstr(instdir, "/");
  367. if (instname) {
  368. instname++;
  369. }
  370. }
  371. PR_snprintf(szStatsFile, sizeof(szStatsFile), "%s/%s%s",
  372. statspath, instname, AGT_STATS_EXTENSION);
  373. PR_snprintf(stats_sem_name, sizeof(stats_sem_name), "/%s%s",
  374. instname, AGT_STATS_EXTENSION);
  375. tmpstatsfile = szStatsFile;
  376. slapi_ch_free_string(&statspath);
  377. slapi_ch_free_string(&instdir);
  378. /* open the memory map */
  379. if ((err = agt_mopen_stats(tmpstatsfile, O_RDWR, &hdl) != 0))
  380. {
  381. if (err != EEXIST) /* Ignore if file already exists */
  382. {
  383. slapi_log_error(SLAPI_LOG_FATAL, "snmp collator", "Failed to open stats file (%s) "
  384. "(error %d): %s.\n", szStatsFile, err, slapd_system_strerror(err));
  385. exit(1);
  386. }
  387. }
  388. /* Create semaphore for stats file access */
  389. snmp_collator_create_semaphore();
  390. /* point stats struct at mmap data */
  391. stats = (struct agt_stats_t *) mmap_tbl [hdl].fp;
  392. /* initialize stats data */
  393. snmp_collator_init();
  394. /* Arrange to be called back periodically to update the mmap'd stats file. */
  395. snmp_eq_ctx = slapi_eq_repeat(snmp_collator_update, NULL, (time_t)0,
  396. SLAPD_SNMP_UPDATE_INTERVAL);
  397. return 0;
  398. }
  399. /***********************************************************************************
  400. *
  401. * int snmp_collator_stop()
  402. *
  403. * stops the collator thread
  404. * closes the memory map
  405. * cleans up any needed memory
  406. *
  407. ************************************************************************************/
  408. int snmp_collator_stop()
  409. {
  410. int err;
  411. /* Abort any pending events */
  412. slapi_eq_cancel(snmp_eq_ctx);
  413. snmp_collator_stopped = 1;
  414. /* acquire the semaphore */
  415. snmp_collator_sem_wait();
  416. /* close the memory map */
  417. if ((err = agt_mclose_stats(hdl)) != 0)
  418. {
  419. fprintf(stderr, "Failed to close stats file (%s) (error = %d).",
  420. AGT_STATS_FILE, err);
  421. }
  422. if (remove(tmpstatsfile) != 0)
  423. {
  424. fprintf(stderr, "Failed to remove (%s) (error = %d).\n",
  425. tmpstatsfile, errno);
  426. }
  427. /* close and delete semaphore */
  428. sem_close(stats_sem);
  429. sem_unlink(stats_sem_name);
  430. /* delete lock */
  431. PR_DestroyLock(interaction_table_mutex);
  432. #ifdef _WIN32
  433. /* send the event so server down trap gets set on NT */
  434. sc_setevent(MAGT_NSEV_SNMPTRAP);
  435. #endif
  436. /* stevross: I probably need to free stats too... make sure to add that later */
  437. return 0;
  438. }
  439. /*
  440. * snmp_collator_create_semaphore()
  441. *
  442. * Create a semaphore to synchronize access to the stats file with
  443. * the SNMP sub-agent. NSPR doesn't support a trywait function
  444. * for semaphores, so we just use POSIX semaphores directly.
  445. */
  446. static void
  447. snmp_collator_create_semaphore()
  448. {
  449. /* First just try to create the semaphore. This should usually just work. */
  450. if ((stats_sem = sem_open(stats_sem_name, O_CREAT | O_EXCL, SLAPD_DEFAULT_FILE_MODE, 1)) == SEM_FAILED) {
  451. if (errno == EEXIST) {
  452. /* It appears that we didn't exit cleanly last time and left the semaphore
  453. * around. Recreate it since we don't know what state it is in. */
  454. sem_unlink(stats_sem_name);
  455. if ((stats_sem = sem_open(stats_sem_name, O_CREAT | O_EXCL, SLAPD_DEFAULT_FILE_MODE, 1)) == SEM_FAILED) {
  456. /* No dice */
  457. LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d.\n",
  458. szStatsFile, errno, 0 );
  459. exit(1);
  460. }
  461. } else {
  462. /* Some other problem occurred creating the semaphore. */
  463. LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d.\n",
  464. szStatsFile, errno, 0 );
  465. exit(1);
  466. }
  467. }
  468. /* If we've reached this point, everything should be good. */
  469. return;
  470. }
  471. /*
  472. * snmp_collator_sem_wait()
  473. *
  474. * A wrapper used to get the semaphore. We don't want to block,
  475. * but we want to retry a specified number of times in case the
  476. * semaphore is help by the sub-agent.
  477. */
  478. static void
  479. snmp_collator_sem_wait()
  480. {
  481. int i = 0;
  482. int got_sem = 0;
  483. for (i=0; i < SNMP_NUM_SEM_WAITS; i++) {
  484. if (sem_trywait(stats_sem) == 0) {
  485. got_sem = 1;
  486. break;
  487. }
  488. PR_Sleep(PR_SecondsToInterval(1));
  489. }
  490. if (!got_sem) {
  491. /* If we've been unable to get the semaphore, there's
  492. * something wrong (likely the sub-agent went out to
  493. * lunch). We remove the old semaphore and recreate
  494. * a new one to avoid hanging up the server. */
  495. sem_close(stats_sem);
  496. sem_unlink(stats_sem_name);
  497. snmp_collator_create_semaphore();
  498. }
  499. }
  500. /***********************************************************************************
  501. *
  502. * int snmp_collator_update()
  503. *
  504. * Event callback function that updates the mmap'd stats file
  505. * for the SNMP sub-agent. This will use a semaphore while
  506. * updating the stats file to prevent the SNMP sub-agent from
  507. * reading it in the middle of an update.
  508. *
  509. ************************************************************************************/
  510. void
  511. snmp_collator_update(time_t start_time, void *arg)
  512. {
  513. if (snmp_collator_stopped) {
  514. return;
  515. }
  516. /* force an update of the backend cache stats. */
  517. snmp_update_cache_stats();
  518. /* get the semaphore */
  519. snmp_collator_sem_wait();
  520. /* just update the update time in the header */
  521. if( stats != NULL){
  522. stats->hdr_stats.updateTime = time(0);
  523. }
  524. /* update the mmap'd tables */
  525. snmp_update_ops_table();
  526. snmp_update_entries_table();
  527. snmp_update_interactions_table();
  528. /* release the semaphore */
  529. sem_post(stats_sem);
  530. }
  531. /*
  532. * snmp_update_ops_table()
  533. *
  534. * Updates the mmap'd operations table. The semaphore
  535. * should be acquired before you call this.
  536. */
  537. static void
  538. snmp_update_ops_table()
  539. {
  540. stats->ops_stats.dsAnonymousBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  541. stats->ops_stats.dsUnAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  542. stats->ops_stats.dsSimpleAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds);
  543. stats->ops_stats.dsStrongAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds);
  544. stats->ops_stats.dsBindSecurityErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  545. stats->ops_stats.dsInOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsInOps);
  546. stats->ops_stats.dsReadOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReadOps);
  547. stats->ops_stats.dsCompareOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsCompareOps);
  548. stats->ops_stats.dsAddEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps);
  549. stats->ops_stats.dsRemoveEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps);
  550. stats->ops_stats.dsModifyEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps);
  551. stats->ops_stats.dsModifyRDNOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps);
  552. stats->ops_stats.dsListOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsListOps);
  553. stats->ops_stats.dsSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSearchOps);
  554. stats->ops_stats.dsOneLevelSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps);
  555. stats->ops_stats.dsWholeSubtreeSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps);
  556. stats->ops_stats.dsReferrals = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferrals);
  557. stats->ops_stats.dsChainings = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsChainings);
  558. stats->ops_stats.dsSecurityErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSecurityErrors);
  559. stats->ops_stats.dsErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsErrors);
  560. stats->ops_stats.dsConnections = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnections);
  561. stats->ops_stats.dsConnectionSeq = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnectionSeq);
  562. stats->ops_stats.dsBytesRecv = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesRecv);
  563. stats->ops_stats.dsBytesSent = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesSent);
  564. stats->ops_stats.dsEntriesReturned = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsEntriesReturned);
  565. stats->ops_stats.dsReferralsReturned = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferralsReturned);
  566. }
  567. /*
  568. * snmp_update_entries_table()
  569. *
  570. * Updated the mmap'd entries table. The semaphore should
  571. * be acquired before you call this.
  572. */
  573. static void
  574. snmp_update_entries_table()
  575. {
  576. stats->entries_stats.dsMasterEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsMasterEntries);
  577. stats->entries_stats.dsCopyEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCopyEntries);
  578. stats->entries_stats.dsCacheEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries);
  579. stats->entries_stats.dsCacheHits = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits);
  580. stats->entries_stats.dsSlaveHits = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsSlaveHits);
  581. }
  582. /*
  583. * snmp_update_interactions_table()
  584. *
  585. * Updates the mmap'd interactions table. The semaphore should
  586. * be acquired before you call this.
  587. */
  588. static void
  589. snmp_update_interactions_table()
  590. {
  591. int i;
  592. for(i=0; i < NUM_SNMP_INT_TBL_ROWS; i++) {
  593. stats->int_stats[i].dsIntIndex = i;
  594. strncpy(stats->int_stats[i].dsName, g_get_global_snmp_vars()->int_tbl[i].dsName,
  595. sizeof(stats->int_stats[i].dsName));
  596. stats->int_stats[i].dsTimeOfCreation = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfCreation;
  597. stats->int_stats[i].dsTimeOfLastAttempt = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastAttempt;
  598. stats->int_stats[i].dsTimeOfLastSuccess = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastSuccess;
  599. stats->int_stats[i].dsFailuresSinceLastSuccess = g_get_global_snmp_vars()->int_tbl[i].dsFailuresSinceLastSuccess;
  600. stats->int_stats[i].dsFailures = g_get_global_snmp_vars()->int_tbl[i].dsFailures;
  601. stats->int_stats[i].dsSuccesses = g_get_global_snmp_vars()->int_tbl[i].dsSuccesses;
  602. strncpy(stats->int_stats[i].dsURL, g_get_global_snmp_vars()->int_tbl[i].dsURL,
  603. sizeof(stats->int_stats[i].dsURL));
  604. }
  605. }
  606. /*
  607. * snmp_update_cache_stats()
  608. *
  609. * Reads the backend cache stats from the backend monitor entry and
  610. * updates the global counter used by the SNMP sub-agent as well as
  611. * the SNMP monitor entry.
  612. */
  613. static void
  614. snmp_update_cache_stats()
  615. {
  616. Slapi_Backend *be, *be_next;
  617. char *cookie = NULL;
  618. Slapi_PBlock *search_result_pb = NULL;
  619. Slapi_Entry **search_entries;
  620. int search_result;
  621. /* set the cache hits/cache entries info */
  622. be = slapi_get_first_backend(&cookie);
  623. if (!be)
  624. return;
  625. be_next = slapi_get_next_backend(cookie);
  626. slapi_ch_free ((void **) &cookie);
  627. /* for now, only do it if there is only 1 backend, otherwise don't know
  628. * which backend to pick */
  629. if(be_next == NULL)
  630. {
  631. Slapi_DN monitordn;
  632. slapi_sdn_init(&monitordn);
  633. be_getmonitordn(be,&monitordn);
  634. /* do a search on the monitor dn to get info */
  635. search_result_pb = slapi_search_internal( slapi_sdn_get_dn(&monitordn),
  636. LDAP_SCOPE_BASE,
  637. "objectclass=*",
  638. NULL,
  639. NULL,
  640. 0);
  641. slapi_sdn_done(&monitordn);
  642. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  643. if(search_result == 0)
  644. {
  645. slapi_pblock_get( search_result_pb,SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  646. &search_entries);
  647. /* set the entrycachehits */
  648. slapi_counter_set_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits,
  649. slapi_entry_attr_get_ulonglong(search_entries[0], "entrycachehits"));
  650. /* set the currententrycachesize */
  651. slapi_counter_set_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries,
  652. slapi_entry_attr_get_ulonglong(search_entries[0], "currententrycachesize"));
  653. }
  654. slapi_free_search_results_internal(search_result_pb);
  655. slapi_pblock_destroy(search_result_pb);
  656. }
  657. }
  658. static void
  659. add_counter_to_value(Slapi_Entry *e, const char *type, PRUint64 countervalue)
  660. {
  661. char value[40];
  662. PR_snprintf(value,sizeof(value),"%" NSPRIu64, countervalue);
  663. slapi_entry_attr_set_charptr( e, type, value);
  664. }
  665. void
  666. snmp_as_entry(Slapi_Entry *e)
  667. {
  668. add_counter_to_value(e,"AnonymousBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds));
  669. add_counter_to_value(e,"UnAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds));
  670. add_counter_to_value(e,"SimpleAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds));
  671. add_counter_to_value(e,"StrongAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds));
  672. add_counter_to_value(e,"BindSecurityErrors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors));
  673. add_counter_to_value(e,"InOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsInOps));
  674. add_counter_to_value(e,"ReadOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReadOps));
  675. add_counter_to_value(e,"CompareOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsCompareOps));
  676. add_counter_to_value(e,"AddEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps));
  677. add_counter_to_value(e,"RemoveEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps));
  678. add_counter_to_value(e,"ModifyEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps));
  679. add_counter_to_value(e,"ModifyRDNOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps));
  680. add_counter_to_value(e,"ListOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsListOps));
  681. add_counter_to_value(e,"SearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSearchOps));
  682. add_counter_to_value(e,"OneLevelSearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps));
  683. add_counter_to_value(e,"WholeSubtreeSearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps));
  684. add_counter_to_value(e,"Referrals", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferrals));
  685. add_counter_to_value(e,"Chainings", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsChainings));
  686. add_counter_to_value(e,"SecurityErrors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSecurityErrors));
  687. add_counter_to_value(e,"Errors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsErrors));
  688. add_counter_to_value(e,"Connections", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnections));
  689. add_counter_to_value(e,"ConnectionSeq", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnectionSeq));
  690. add_counter_to_value(e,"BytesRecv", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesRecv));
  691. add_counter_to_value(e,"BytesSent", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesSent));
  692. add_counter_to_value(e,"EntriesReturned", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsEntriesReturned));
  693. add_counter_to_value(e,"ReferralsReturned", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferralsReturned));
  694. add_counter_to_value(e,"MasterEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsMasterEntries));
  695. add_counter_to_value(e,"CopyEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCopyEntries));
  696. add_counter_to_value(e,"CacheEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries));
  697. add_counter_to_value(e,"CacheHits", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits));
  698. add_counter_to_value(e,"SlaveHits", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsSlaveHits));
  699. }
  700. /*
  701. * loadConfigStats()
  702. *
  703. * Reads the header table SNMP settings and sets them in the mmap'd stats
  704. * file. This should be done only when the semaphore is held.
  705. */
  706. static void
  707. loadConfigStats() {
  708. Slapi_Entry *entry = NULL;
  709. char *name = NULL;
  710. char *desc = NULL;
  711. char *org = NULL;
  712. char *loc = NULL;
  713. char *contact = NULL;
  714. /* Read attributes from SNMP config entry */
  715. getConfigEntry( &entry );
  716. if ( entry != NULL ) {
  717. name = slapi_entry_attr_get_charptr( entry, SNMP_NAME_ATTR );
  718. desc = slapi_entry_attr_get_charptr( entry, SNMP_DESC_ATTR );
  719. org = slapi_entry_attr_get_charptr( entry, SNMP_ORG_ATTR );
  720. loc = slapi_entry_attr_get_charptr( entry, SNMP_LOC_ATTR );
  721. contact = slapi_entry_attr_get_charptr( entry, SNMP_CONTACT_ATTR );
  722. freeConfigEntry( &entry );
  723. }
  724. /* Load stats into table */
  725. if ( name != NULL) {
  726. PL_strncpyz(stats->hdr_stats.dsName, name, SNMP_FIELD_LENGTH);
  727. }
  728. if ( desc != NULL) {
  729. PL_strncpyz(stats->hdr_stats.dsDescription, desc, SNMP_FIELD_LENGTH);
  730. }
  731. if ( org != NULL) {
  732. PL_strncpyz(stats->hdr_stats.dsOrganization, org, SNMP_FIELD_LENGTH);
  733. }
  734. if ( loc != NULL) {
  735. PL_strncpyz(stats->hdr_stats.dsLocation, loc, SNMP_FIELD_LENGTH);
  736. }
  737. if ( contact != NULL) {
  738. PL_strncpyz(stats->hdr_stats.dsContact, contact, SNMP_FIELD_LENGTH);
  739. }
  740. /* Free strings */
  741. slapi_ch_free((void **) &name);
  742. slapi_ch_free((void **) &desc);
  743. slapi_ch_free((void **) &org);
  744. slapi_ch_free((void **) &loc);
  745. slapi_ch_free((void **) &contact);
  746. }
  747. static Slapi_Entry *
  748. getConfigEntry( Slapi_Entry **e ) {
  749. Slapi_DN sdn;
  750. slapi_sdn_init_dn_byref( &sdn, SNMP_CONFIG_DN );
  751. slapi_search_internal_get_entry( &sdn, NULL, e,
  752. plugin_get_default_component_id());
  753. slapi_sdn_done( &sdn );
  754. return *e;
  755. }
  756. static void
  757. freeConfigEntry( Slapi_Entry **e ) {
  758. if ( (e != NULL) && (*e != NULL) ) {
  759. slapi_entry_free( *e );
  760. *e = NULL;
  761. }
  762. }