snmp_collator.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. if (sem_unlink(stats_sem_name) != 0) {
  455. LDAPDebug( LDAP_DEBUG_ANY, "Failed to delete old semaphore for stats file (%s). "
  456. "Error %d (%s).\n", szStatsFile, errno, slapd_system_strerror(errno) );
  457. exit(1);
  458. }
  459. if ((stats_sem = sem_open(stats_sem_name, O_CREAT | O_EXCL, SLAPD_DEFAULT_FILE_MODE, 1)) == SEM_FAILED) {
  460. /* No dice */
  461. LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d (%s).\n",
  462. szStatsFile, errno, slapd_system_strerror(errno) );
  463. exit(1);
  464. }
  465. } else {
  466. /* Some other problem occurred creating the semaphore. */
  467. LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d.(%s)\n",
  468. szStatsFile, errno, slapd_system_strerror(errno) );
  469. exit(1);
  470. }
  471. }
  472. /* If we've reached this point, everything should be good. */
  473. return;
  474. }
  475. /*
  476. * snmp_collator_sem_wait()
  477. *
  478. * A wrapper used to get the semaphore. We don't want to block,
  479. * but we want to retry a specified number of times in case the
  480. * semaphore is help by the sub-agent.
  481. */
  482. static void
  483. snmp_collator_sem_wait()
  484. {
  485. int i = 0;
  486. int got_sem = 0;
  487. for (i=0; i < SNMP_NUM_SEM_WAITS; i++) {
  488. if (sem_trywait(stats_sem) == 0) {
  489. got_sem = 1;
  490. break;
  491. }
  492. PR_Sleep(PR_SecondsToInterval(1));
  493. }
  494. if (!got_sem) {
  495. /* If we've been unable to get the semaphore, there's
  496. * something wrong (likely the sub-agent went out to
  497. * lunch). We remove the old semaphore and recreate
  498. * a new one to avoid hanging up the server. */
  499. sem_close(stats_sem);
  500. sem_unlink(stats_sem_name);
  501. snmp_collator_create_semaphore();
  502. }
  503. }
  504. /***********************************************************************************
  505. *
  506. * int snmp_collator_update()
  507. *
  508. * Event callback function that updates the mmap'd stats file
  509. * for the SNMP sub-agent. This will use a semaphore while
  510. * updating the stats file to prevent the SNMP sub-agent from
  511. * reading it in the middle of an update.
  512. *
  513. ************************************************************************************/
  514. void
  515. snmp_collator_update(time_t start_time, void *arg)
  516. {
  517. if (snmp_collator_stopped) {
  518. return;
  519. }
  520. /* force an update of the backend cache stats. */
  521. snmp_update_cache_stats();
  522. /* get the semaphore */
  523. snmp_collator_sem_wait();
  524. /* just update the update time in the header */
  525. if( stats != NULL){
  526. stats->hdr_stats.updateTime = time(0);
  527. }
  528. /* update the mmap'd tables */
  529. snmp_update_ops_table();
  530. snmp_update_entries_table();
  531. snmp_update_interactions_table();
  532. /* release the semaphore */
  533. sem_post(stats_sem);
  534. }
  535. /*
  536. * snmp_update_ops_table()
  537. *
  538. * Updates the mmap'd operations table. The semaphore
  539. * should be acquired before you call this.
  540. */
  541. static void
  542. snmp_update_ops_table()
  543. {
  544. stats->ops_stats.dsAnonymousBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  545. stats->ops_stats.dsUnAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  546. stats->ops_stats.dsSimpleAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds);
  547. stats->ops_stats.dsStrongAuthBinds = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds);
  548. stats->ops_stats.dsBindSecurityErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  549. stats->ops_stats.dsInOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsInOps);
  550. stats->ops_stats.dsReadOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReadOps);
  551. stats->ops_stats.dsCompareOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsCompareOps);
  552. stats->ops_stats.dsAddEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps);
  553. stats->ops_stats.dsRemoveEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps);
  554. stats->ops_stats.dsModifyEntryOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps);
  555. stats->ops_stats.dsModifyRDNOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps);
  556. stats->ops_stats.dsListOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsListOps);
  557. stats->ops_stats.dsSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSearchOps);
  558. stats->ops_stats.dsOneLevelSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps);
  559. stats->ops_stats.dsWholeSubtreeSearchOps = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps);
  560. stats->ops_stats.dsReferrals = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferrals);
  561. stats->ops_stats.dsChainings = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsChainings);
  562. stats->ops_stats.dsSecurityErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSecurityErrors);
  563. stats->ops_stats.dsErrors = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsErrors);
  564. stats->ops_stats.dsConnections = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnections);
  565. stats->ops_stats.dsConnectionSeq = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnectionSeq);
  566. stats->ops_stats.dsBytesRecv = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesRecv);
  567. stats->ops_stats.dsBytesSent = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesSent);
  568. stats->ops_stats.dsEntriesReturned = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsEntriesReturned);
  569. stats->ops_stats.dsReferralsReturned = slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferralsReturned);
  570. }
  571. /*
  572. * snmp_update_entries_table()
  573. *
  574. * Updated the mmap'd entries table. The semaphore should
  575. * be acquired before you call this.
  576. */
  577. static void
  578. snmp_update_entries_table()
  579. {
  580. stats->entries_stats.dsMasterEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsMasterEntries);
  581. stats->entries_stats.dsCopyEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCopyEntries);
  582. stats->entries_stats.dsCacheEntries = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries);
  583. stats->entries_stats.dsCacheHits = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits);
  584. stats->entries_stats.dsSlaveHits = slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsSlaveHits);
  585. }
  586. /*
  587. * snmp_update_interactions_table()
  588. *
  589. * Updates the mmap'd interactions table. The semaphore should
  590. * be acquired before you call this.
  591. */
  592. static void
  593. snmp_update_interactions_table()
  594. {
  595. int i;
  596. for(i=0; i < NUM_SNMP_INT_TBL_ROWS; i++) {
  597. stats->int_stats[i].dsIntIndex = i;
  598. strncpy(stats->int_stats[i].dsName, g_get_global_snmp_vars()->int_tbl[i].dsName,
  599. sizeof(stats->int_stats[i].dsName));
  600. stats->int_stats[i].dsTimeOfCreation = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfCreation;
  601. stats->int_stats[i].dsTimeOfLastAttempt = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastAttempt;
  602. stats->int_stats[i].dsTimeOfLastSuccess = g_get_global_snmp_vars()->int_tbl[i].dsTimeOfLastSuccess;
  603. stats->int_stats[i].dsFailuresSinceLastSuccess = g_get_global_snmp_vars()->int_tbl[i].dsFailuresSinceLastSuccess;
  604. stats->int_stats[i].dsFailures = g_get_global_snmp_vars()->int_tbl[i].dsFailures;
  605. stats->int_stats[i].dsSuccesses = g_get_global_snmp_vars()->int_tbl[i].dsSuccesses;
  606. strncpy(stats->int_stats[i].dsURL, g_get_global_snmp_vars()->int_tbl[i].dsURL,
  607. sizeof(stats->int_stats[i].dsURL));
  608. }
  609. }
  610. /*
  611. * snmp_update_cache_stats()
  612. *
  613. * Reads the backend cache stats from the backend monitor entry and
  614. * updates the global counter used by the SNMP sub-agent as well as
  615. * the SNMP monitor entry.
  616. */
  617. static void
  618. snmp_update_cache_stats()
  619. {
  620. Slapi_Backend *be, *be_next;
  621. char *cookie = NULL;
  622. Slapi_PBlock *search_result_pb = NULL;
  623. Slapi_Entry **search_entries;
  624. int search_result;
  625. /* set the cache hits/cache entries info */
  626. be = slapi_get_first_backend(&cookie);
  627. if (!be)
  628. return;
  629. be_next = slapi_get_next_backend(cookie);
  630. slapi_ch_free ((void **) &cookie);
  631. /* for now, only do it if there is only 1 backend, otherwise don't know
  632. * which backend to pick */
  633. if(be_next == NULL)
  634. {
  635. Slapi_DN monitordn;
  636. slapi_sdn_init(&monitordn);
  637. be_getmonitordn(be,&monitordn);
  638. /* do a search on the monitor dn to get info */
  639. search_result_pb = slapi_search_internal( slapi_sdn_get_dn(&monitordn),
  640. LDAP_SCOPE_BASE,
  641. "objectclass=*",
  642. NULL,
  643. NULL,
  644. 0);
  645. slapi_sdn_done(&monitordn);
  646. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  647. if(search_result == 0)
  648. {
  649. slapi_pblock_get( search_result_pb,SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  650. &search_entries);
  651. /* set the entrycachehits */
  652. slapi_counter_set_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits,
  653. slapi_entry_attr_get_ulonglong(search_entries[0], "entrycachehits"));
  654. /* set the currententrycachesize */
  655. slapi_counter_set_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries,
  656. slapi_entry_attr_get_ulonglong(search_entries[0], "currententrycachesize"));
  657. }
  658. slapi_free_search_results_internal(search_result_pb);
  659. slapi_pblock_destroy(search_result_pb);
  660. }
  661. }
  662. static void
  663. add_counter_to_value(Slapi_Entry *e, const char *type, PRUint64 countervalue)
  664. {
  665. char value[40];
  666. PR_snprintf(value,sizeof(value),"%" NSPRIu64, countervalue);
  667. slapi_entry_attr_set_charptr( e, type, value);
  668. }
  669. void
  670. snmp_as_entry(Slapi_Entry *e)
  671. {
  672. add_counter_to_value(e,"AnonymousBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds));
  673. add_counter_to_value(e,"UnAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds));
  674. add_counter_to_value(e,"SimpleAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds));
  675. add_counter_to_value(e,"StrongAuthBinds", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds));
  676. add_counter_to_value(e,"BindSecurityErrors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors));
  677. add_counter_to_value(e,"InOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsInOps));
  678. add_counter_to_value(e,"ReadOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReadOps));
  679. add_counter_to_value(e,"CompareOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsCompareOps));
  680. add_counter_to_value(e,"AddEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsAddEntryOps));
  681. add_counter_to_value(e,"RemoveEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps));
  682. add_counter_to_value(e,"ModifyEntryOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyEntryOps));
  683. add_counter_to_value(e,"ModifyRDNOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsModifyRDNOps));
  684. add_counter_to_value(e,"ListOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsListOps));
  685. add_counter_to_value(e,"SearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSearchOps));
  686. add_counter_to_value(e,"OneLevelSearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps));
  687. add_counter_to_value(e,"WholeSubtreeSearchOps", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps));
  688. add_counter_to_value(e,"Referrals", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferrals));
  689. add_counter_to_value(e,"Chainings", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsChainings));
  690. add_counter_to_value(e,"SecurityErrors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsSecurityErrors));
  691. add_counter_to_value(e,"Errors", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsErrors));
  692. add_counter_to_value(e,"Connections", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnections));
  693. add_counter_to_value(e,"ConnectionSeq", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsConnectionSeq));
  694. add_counter_to_value(e,"BytesRecv", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesRecv));
  695. add_counter_to_value(e,"BytesSent", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsBytesSent));
  696. add_counter_to_value(e,"EntriesReturned", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsEntriesReturned));
  697. add_counter_to_value(e,"ReferralsReturned", slapi_counter_get_value(g_get_global_snmp_vars()->ops_tbl.dsReferralsReturned));
  698. add_counter_to_value(e,"MasterEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsMasterEntries));
  699. add_counter_to_value(e,"CopyEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCopyEntries));
  700. add_counter_to_value(e,"CacheEntries", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheEntries));
  701. add_counter_to_value(e,"CacheHits", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsCacheHits));
  702. add_counter_to_value(e,"SlaveHits", slapi_counter_get_value(g_get_global_snmp_vars()->entries_tbl.dsSlaveHits));
  703. }
  704. /*
  705. * loadConfigStats()
  706. *
  707. * Reads the header table SNMP settings and sets them in the mmap'd stats
  708. * file. This should be done only when the semaphore is held.
  709. */
  710. static void
  711. loadConfigStats() {
  712. Slapi_Entry *entry = NULL;
  713. char *name = NULL;
  714. char *desc = NULL;
  715. char *org = NULL;
  716. char *loc = NULL;
  717. char *contact = NULL;
  718. /* Read attributes from SNMP config entry */
  719. getConfigEntry( &entry );
  720. if ( entry != NULL ) {
  721. name = slapi_entry_attr_get_charptr( entry, SNMP_NAME_ATTR );
  722. desc = slapi_entry_attr_get_charptr( entry, SNMP_DESC_ATTR );
  723. org = slapi_entry_attr_get_charptr( entry, SNMP_ORG_ATTR );
  724. loc = slapi_entry_attr_get_charptr( entry, SNMP_LOC_ATTR );
  725. contact = slapi_entry_attr_get_charptr( entry, SNMP_CONTACT_ATTR );
  726. freeConfigEntry( &entry );
  727. }
  728. /* Load stats into table */
  729. if ( name != NULL) {
  730. PL_strncpyz(stats->hdr_stats.dsName, name, SNMP_FIELD_LENGTH);
  731. }
  732. if ( desc != NULL) {
  733. PL_strncpyz(stats->hdr_stats.dsDescription, desc, SNMP_FIELD_LENGTH);
  734. }
  735. if ( org != NULL) {
  736. PL_strncpyz(stats->hdr_stats.dsOrganization, org, SNMP_FIELD_LENGTH);
  737. }
  738. if ( loc != NULL) {
  739. PL_strncpyz(stats->hdr_stats.dsLocation, loc, SNMP_FIELD_LENGTH);
  740. }
  741. if ( contact != NULL) {
  742. PL_strncpyz(stats->hdr_stats.dsContact, contact, SNMP_FIELD_LENGTH);
  743. }
  744. /* Free strings */
  745. slapi_ch_free((void **) &name);
  746. slapi_ch_free((void **) &desc);
  747. slapi_ch_free((void **) &org);
  748. slapi_ch_free((void **) &loc);
  749. slapi_ch_free((void **) &contact);
  750. }
  751. static Slapi_Entry *
  752. getConfigEntry( Slapi_Entry **e ) {
  753. Slapi_DN sdn;
  754. slapi_sdn_init_dn_byref( &sdn, SNMP_CONFIG_DN );
  755. slapi_search_internal_get_entry( &sdn, NULL, e,
  756. plugin_get_default_component_id());
  757. slapi_sdn_done( &sdn );
  758. return *e;
  759. }
  760. static void
  761. freeConfigEntry( Slapi_Entry **e ) {
  762. if ( (e != NULL) && (*e != NULL) ) {
  763. slapi_entry_free( *e );
  764. *e = NULL;
  765. }
  766. }