ldap-agent.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include "ldap-agent.h"
  43. static netsnmp_handler_registration *ops_handler = NULL;
  44. static netsnmp_handler_registration *entries_handler = NULL;
  45. static netsnmp_handler_registration *entity_handler = NULL;
  46. static netsnmp_table_array_callbacks ops_cb;
  47. static netsnmp_table_array_callbacks entries_cb;
  48. static netsnmp_table_array_callbacks entity_cb;
  49. extern server_instance *server_head;
  50. /* Set table oids */
  51. oid dsOpsTable_oid[] = { dsOpsTable_TABLE_OID };
  52. size_t dsOpsTable_oid_len = OID_LENGTH(dsOpsTable_oid);
  53. oid dsEntriesTable_oid[] = { dsEntriesTable_TABLE_OID };
  54. size_t dsEntriesTable_oid_len = OID_LENGTH(dsEntriesTable_oid);
  55. oid dsEntityTable_oid[] = {dsEntityTable_TABLE_OID };
  56. size_t dsEntityTable_oid_len = OID_LENGTH(dsEntityTable_oid);
  57. /* Set trap oids */
  58. oid snmptrap_oid[] = { snmptrap_OID };
  59. size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
  60. oid enterprise_oid[] = { enterprise_OID };
  61. size_t enterprise_oid_len = OID_LENGTH(enterprise_oid);
  62. /************************************************************
  63. * init_ldap_agent
  64. *
  65. * Initializes the agent and populates the stats table
  66. * with initial data.
  67. */
  68. void
  69. init_ldap_agent(void)
  70. {
  71. server_instance *serv_p = NULL;
  72. stats_table_context *new_row = NULL;
  73. /* Define and create the table */
  74. initialize_stats_table();
  75. /* Initialize data for each server in conf file */
  76. for (serv_p = server_head; serv_p != NULL; serv_p = serv_p->next) {
  77. /* Check if this row already exists. */
  78. if (stats_table_find_row(serv_p->port) == NULL) {
  79. /* Create a new row */
  80. if ((new_row = stats_table_create_row(serv_p->port)) != NULL) {
  81. /* Set pointer for entity table */
  82. new_row->entity_tbl = serv_p;
  83. /* Set previous state of server to unknown */
  84. serv_p->server_state = STATE_UNKNOWN;
  85. /* Insert new row into the table */
  86. snmp_log(LOG_DEBUG, "Inserting row for server: %d\n", serv_p->port);
  87. CONTAINER_INSERT(ops_cb.container, new_row);
  88. } else {
  89. /* error during malloc of row */
  90. snmp_log(LOG_ERR, "Error creating row for server: %d\n",
  91. serv_p->port);
  92. }
  93. }
  94. }
  95. /* Force load data into stats table */
  96. load_stats_table(NULL, NULL);
  97. }
  98. /************************************************************
  99. * initialize_stats_table
  100. *
  101. * Initializes the stats table by defining its contents,
  102. * how it's structured, and registering callbacks.
  103. */
  104. void
  105. initialize_stats_table(void)
  106. {
  107. netsnmp_table_registration_info *ops_table_info = NULL;
  108. netsnmp_table_registration_info *entries_table_info = NULL;
  109. netsnmp_table_registration_info *entity_table_info = NULL;
  110. /* This is a hacky way of figuring out if we are on Net-SNMP 5.2 or later */
  111. #ifdef NETSNMP_CACHE_AUTO_RELOAD
  112. netsnmp_cache *stats_table_cache = NULL;
  113. #endif
  114. if (ops_handler || entries_handler || entity_handler) {
  115. snmp_log(LOG_ERR, "initialize_stats_table called more than once.\n");
  116. return;
  117. }
  118. memset(&ops_cb, 0x00, sizeof(ops_cb));
  119. memset(&entries_cb, 0x00, sizeof(entries_cb));
  120. memset(&entity_cb, 0x00, sizeof(entity_cb));
  121. /* create table structures */
  122. ops_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  123. entries_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  124. entity_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  125. /* create handlers */
  126. ops_handler = netsnmp_create_handler_registration("dsOpsTable",
  127. netsnmp_table_array_helper_handler,
  128. dsOpsTable_oid,
  129. dsOpsTable_oid_len,
  130. HANDLER_CAN_RONLY);
  131. entries_handler = netsnmp_create_handler_registration("dsEntriesTable",
  132. netsnmp_table_array_helper_handler,
  133. dsEntriesTable_oid,
  134. dsEntriesTable_oid_len,
  135. HANDLER_CAN_RONLY);
  136. entity_handler = netsnmp_create_handler_registration("dsEntityTable",
  137. netsnmp_table_array_helper_handler,
  138. dsEntityTable_oid,
  139. dsEntityTable_oid_len,
  140. HANDLER_CAN_RONLY);
  141. if (!ops_handler || !entries_handler || !entity_handler ||
  142. !ops_table_info || !entries_table_info || !entity_table_info) {
  143. /* malloc failed */
  144. snmp_log(LOG_ERR, "malloc failed in initialize_stats_table\n");
  145. SNMP_FREE(ops_table_info);
  146. SNMP_FREE(entries_table_info);
  147. SNMP_FREE(entity_table_info);
  148. return;
  149. }
  150. /* define table structures */
  151. netsnmp_table_helper_add_index(ops_table_info, ASN_INTEGER);
  152. netsnmp_table_helper_add_index(entries_table_info, ASN_INTEGER);
  153. netsnmp_table_helper_add_index(entity_table_info, ASN_INTEGER);
  154. ops_table_info->min_column = dsOpsTable_COL_MIN;
  155. ops_table_info->max_column = dsOpsTable_COL_MAX;
  156. entries_table_info->min_column = dsEntriesTable_COL_MIN;
  157. entries_table_info->max_column = dsEntriesTable_COL_MAX;
  158. entity_table_info->min_column = dsEntityTable_COL_MIN;
  159. entity_table_info->max_column = dsEntityTable_COL_MAX;
  160. /*
  161. * Define callbacks and the container. We only use one container that
  162. * all of the tables use.
  163. */
  164. ops_cb.get_value = dsOpsTable_get_value;
  165. ops_cb.container = netsnmp_container_find("dsOpsTable_primary:"
  166. "dsOpsTable:" "table_container");
  167. entries_cb.get_value = dsEntriesTable_get_value;
  168. entries_cb.container = ops_cb.container;
  169. entity_cb.get_value = dsEntityTable_get_value;
  170. entity_cb.container = ops_cb.container;
  171. /* registering the tables with the master agent */
  172. netsnmp_table_container_register(ops_handler, ops_table_info, &ops_cb,
  173. ops_cb.container, 1);
  174. netsnmp_table_container_register(entries_handler, entries_table_info, &entries_cb,
  175. entries_cb.container, 1);
  176. netsnmp_table_container_register(entity_handler, entity_table_info, &entity_cb,
  177. entity_cb.container, 1);
  178. /* Setup cache for auto reloading of stats */
  179. #ifdef NETSNMP_CACHE_AUTO_RELOAD
  180. /* This is new api as of Net-SNMP 5.2 */
  181. stats_table_cache = netsnmp_cache_create(CACHE_REFRESH_INTERVAL, load_stats_table,
  182. NULL, dsOpsTable_oid, dsOpsTable_oid_len);
  183. stats_table_cache->flags |= NETSNMP_CACHE_DONT_FREE_EXPIRED;
  184. stats_table_cache->flags |= NETSNMP_CACHE_DONT_AUTO_RELEASE;
  185. stats_table_cache->flags |= NETSNMP_CACHE_AUTO_RELOAD;
  186. netsnmp_inject_handler(ops_handler, netsnmp_cache_handler_get(stats_table_cache));
  187. #else
  188. /* Do things the old way. This is only needed for Net-SNMP 5.1 and earlier. */
  189. netsnmp_inject_handler(ops_handler, netsnmp_get_cache_handler(CACHE_REFRESH_INTERVAL, load_stats_table,
  190. free_stats_table, dsOpsTable_oid, dsOpsTable_oid_len));
  191. #endif
  192. }
  193. /************************************************************
  194. * stats_table_create_row
  195. *
  196. * Creates a new table row using the supplied port number as
  197. * the index, then returns a pointer to the new row.
  198. */
  199. stats_table_context *
  200. stats_table_create_row(unsigned long portnum)
  201. {
  202. netsnmp_index index;
  203. stats_table_context *ctx = SNMP_MALLOC_TYPEDEF(stats_table_context);
  204. oid *index_oid = (oid *)malloc(sizeof(oid) * MAX_OID_LEN);
  205. /* Create index using port number */
  206. index_oid[0] = portnum;
  207. index.oids = index_oid;
  208. index.len = 1;
  209. /* Copy index into row structure */
  210. if (ctx && index_oid) {
  211. memcpy(&ctx->index, &index, sizeof(index));
  212. return ctx;
  213. } else {
  214. /* Error during malloc */
  215. snmp_log(LOG_ERR, "malloc failed in stats_table_create_row\n");
  216. return NULL;
  217. }
  218. }
  219. /************************************************************
  220. * stats_table_find_row
  221. *
  222. * Searches for a row by the port number. Returns NULL if
  223. * the row doesn't exist.
  224. */
  225. stats_table_context *
  226. stats_table_find_row(unsigned long portnum)
  227. {
  228. netsnmp_index index;
  229. oid index_oid[MAX_OID_LEN];
  230. index_oid[0] = portnum;
  231. index.oids = index_oid;
  232. index.len = 1;
  233. return (stats_table_context *)
  234. CONTAINER_FIND(ops_cb.container, &index);
  235. }
  236. /************************************************************
  237. * load_stats_table
  238. *
  239. * Reloads the stats into the table. This is called
  240. * automatically from the cache handler. This function
  241. * does not reload the entity table since it's static
  242. * information. We also check if any traps need to
  243. * be sent here.
  244. */
  245. int
  246. load_stats_table(netsnmp_cache *cache, void *foo)
  247. {
  248. server_instance *serv_p = NULL;
  249. stats_table_context *ctx = NULL;
  250. time_t previous_start;
  251. int previous_state;
  252. int stats_hdl = -1;
  253. sem_t *stats_sem = NULL;
  254. snmp_log(LOG_INFO, "Reloading stats.\n");
  255. /* Initialize data for each server in conf file */
  256. for (serv_p = server_head; serv_p != NULL; serv_p = serv_p->next) {
  257. if ((ctx = stats_table_find_row(serv_p->port)) != NULL) {
  258. /* Save previous state of the server to
  259. * see if a trap needs to be sent */
  260. previous_state = serv_p->server_state;
  261. previous_start = ctx->hdr_tbl.startTime;
  262. snmp_log(LOG_INFO, "Opening stats file (%s) for server: %d\n",
  263. serv_p->stats_file, serv_p->port);
  264. /* Open and acquire semaphore */
  265. if ((stats_sem = sem_open(serv_p->stats_sem_name, 0)) == SEM_FAILED) {
  266. stats_sem = NULL;
  267. snmp_log(LOG_INFO, "Unable to open semaphore for server: %d\n", serv_p->port);
  268. } else {
  269. int i = 0;
  270. int got_sem = 0;
  271. for (i=0; i < SNMP_NUM_SEM_WAITS; i++) {
  272. if (sem_trywait(stats_sem) == 0) {
  273. got_sem = 1;
  274. break;
  275. }
  276. PR_Sleep(PR_SecondsToInterval(1));
  277. }
  278. if (!got_sem) {
  279. /* We're unable to get the semaphore. Assume
  280. * that the server is down. */
  281. snmp_log(LOG_INFO, "Unable to acquire semaphore for server: %d\n", serv_p->port);
  282. sem_close(stats_sem);
  283. stats_sem = NULL;
  284. }
  285. }
  286. /* Open the stats file */
  287. if ((stats_sem == NULL) || (agt_mopen_stats(serv_p->stats_file, O_RDONLY, &stats_hdl) != 0)) {
  288. if (stats_sem) {
  289. /* Release and close semaphore */
  290. sem_post(stats_sem);
  291. sem_close(stats_sem);
  292. }
  293. /* Server must be down */
  294. serv_p->server_state = SERVER_DOWN;
  295. /* Zero out the ops and entries tables */
  296. memset(&ctx->ops_tbl, 0x00, sizeof(ctx->ops_tbl));
  297. memset(&ctx->entries_tbl, 0x00, sizeof(ctx->entries_tbl));
  298. if (previous_state != SERVER_DOWN)
  299. snmp_log(LOG_INFO, "Unable to open stats file (%s) for server: %d\n",
  300. serv_p->stats_file, serv_p->port);
  301. } else {
  302. /* Initialize ops table */
  303. if ( agt_mread_stats(stats_hdl, &ctx->hdr_tbl, &ctx->ops_tbl,
  304. &ctx->entries_tbl) != 0 )
  305. snmp_log(LOG_ERR, "Unable to read stats file: %s\n",
  306. serv_p->stats_file);
  307. /* Close stats file */
  308. if ( agt_mclose_stats(stats_hdl) != 0 )
  309. snmp_log(LOG_ERR, "Error closing stats file: %s\n",
  310. serv_p->stats_file);
  311. /* Release and close semaphore */
  312. sem_post(stats_sem);
  313. sem_close(stats_sem);
  314. /* Server must be down if the stats file hasn't been
  315. * updated in a while */
  316. if (difftime(time(NULL), ctx->hdr_tbl.updateTime) >= UPDATE_THRESHOLD) {
  317. serv_p->server_state = SERVER_DOWN;
  318. if (previous_state != SERVER_DOWN)
  319. snmp_log(LOG_INFO, "Stats file for server %d hasn't been updated"
  320. " in %d seconds.\n", serv_p->port, UPDATE_THRESHOLD);
  321. } else {
  322. serv_p->server_state = SERVER_UP;
  323. }
  324. }
  325. /* If the state of the server changed since the last
  326. * load of the stats, send a trap. */
  327. if (previous_state != STATE_UNKNOWN) {
  328. if (serv_p->server_state != previous_state) {
  329. if (serv_p->server_state == SERVER_UP) {
  330. snmp_log(LOG_INFO, "Detected start of server: %d\n",
  331. serv_p->port);
  332. send_DirectoryServerStart_trap(serv_p);
  333. } else {
  334. send_DirectoryServerDown_trap(serv_p);
  335. /* Zero out the ops and entries tables */
  336. memset(&ctx->ops_tbl, 0x00, sizeof(ctx->ops_tbl));
  337. memset(&ctx->entries_tbl, 0x00, sizeof(ctx->entries_tbl));
  338. }
  339. } else if (ctx->hdr_tbl.startTime != previous_start) {
  340. /* Send traps if the server has restarted since the last load */
  341. snmp_log(LOG_INFO, "Detected restart of server: %d\n", serv_p->port);
  342. send_DirectoryServerDown_trap(serv_p);
  343. send_DirectoryServerStart_trap(serv_p);
  344. }
  345. }
  346. } else {
  347. /* Can't find our row. This shouldn't ever happen. */
  348. snmp_log(LOG_ERR, "Row not found for server: %d\n",
  349. serv_p->port);
  350. }
  351. }
  352. return 0;
  353. }
  354. /************************************************************
  355. * free_stats_table
  356. *
  357. * This function doesn't need to free anything since the
  358. * load_stats_table function doesn't allocate any memory
  359. * itself. The cache handler requires us to have a callback
  360. * function for freeing the cache, so here it is.
  361. */
  362. void
  363. free_stats_table(netsnmp_cache *cache, void *foo)
  364. {
  365. return;
  366. }
  367. /************************************************************
  368. * dsOpsTable_get_value
  369. *
  370. * This routine is called for get requests to copy the data
  371. * from the context to the varbind for the request. If the
  372. * context has been properly maintained, you don't need to
  373. * change in code in this fuction.
  374. */
  375. int
  376. dsOpsTable_get_value(netsnmp_request_info *request,
  377. netsnmp_index * item,
  378. netsnmp_table_request_info *table_info)
  379. {
  380. PRUint64 *the_stat = NULL;
  381. integer64 new_val;
  382. netsnmp_variable_list *var = request->requestvb;
  383. stats_table_context *context = (stats_table_context *) item;
  384. switch (table_info->colnum) {
  385. case COLUMN_DSANONYMOUSBINDS:
  386. the_stat = &context->ops_tbl.dsAnonymousBinds;
  387. break;
  388. case COLUMN_DSUNAUTHBINDS:
  389. the_stat = &context->ops_tbl.dsUnAuthBinds;
  390. break;
  391. case COLUMN_DSSIMPLEAUTHBINDS:
  392. the_stat = &context->ops_tbl.dsSimpleAuthBinds;
  393. break;
  394. case COLUMN_DSSTRONGAUTHBINDS:
  395. the_stat = &context->ops_tbl.dsStrongAuthBinds;
  396. break;
  397. case COLUMN_DSBINDSECURITYERRORS:
  398. the_stat = &context->ops_tbl.dsBindSecurityErrors;
  399. break;
  400. case COLUMN_DSINOPS:
  401. the_stat = &context->ops_tbl.dsInOps;
  402. break;
  403. case COLUMN_DSREADOPS:
  404. the_stat = &context->ops_tbl.dsReadOps;
  405. break;
  406. case COLUMN_DSCOMPAREOPS:
  407. the_stat = &context->ops_tbl.dsCompareOps;
  408. break;
  409. case COLUMN_DSADDENTRYOPS:
  410. the_stat = &context->ops_tbl.dsAddEntryOps;
  411. break;
  412. case COLUMN_DSREMOVEENTRYOPS:
  413. the_stat = &context->ops_tbl.dsRemoveEntryOps;
  414. break;
  415. case COLUMN_DSMODIFYENTRYOPS:
  416. the_stat = &context->ops_tbl.dsModifyEntryOps;
  417. break;
  418. case COLUMN_DSMODIFYRDNOPS:
  419. the_stat = &context->ops_tbl.dsModifyRDNOps;
  420. break;
  421. case COLUMN_DSLISTOPS:
  422. the_stat = &context->ops_tbl.dsListOps;
  423. break;
  424. case COLUMN_DSSEARCHOPS:
  425. the_stat = &context->ops_tbl.dsSearchOps;
  426. break;
  427. case COLUMN_DSONELEVELSEARCHOPS:
  428. the_stat = &context->ops_tbl.dsOneLevelSearchOps;
  429. break;
  430. case COLUMN_DSWHOLESUBTREESEARCHOPS:
  431. the_stat = &context->ops_tbl.dsWholeSubtreeSearchOps;
  432. break;
  433. case COLUMN_DSREFERRALS:
  434. the_stat = &context->ops_tbl.dsReferrals;
  435. break;
  436. case COLUMN_DSCHAININGS:
  437. the_stat = &context->ops_tbl.dsChainings;
  438. break;
  439. case COLUMN_DSSECURITYERRORS:
  440. the_stat = &context->ops_tbl.dsSecurityErrors;
  441. break;
  442. case COLUMN_DSERRORS:
  443. the_stat = &context->ops_tbl.dsErrors;
  444. break;
  445. default:/* We shouldn't get here */
  446. snmp_log(LOG_ERR, "Unknown column in dsOpsTable_get_value\n");
  447. return SNMP_ERR_GENERR;
  448. }
  449. /* The Net-SNMP integer64 type isn't a true 64-bit value, but instead
  450. * a structure containing the high and low bits separately. We need
  451. * to split our value appropriately. */
  452. new_val.low = *the_stat & 0x00000000ffffffff;
  453. new_val.high = (*the_stat >> 32) & 0x00000000ffffffff;
  454. snmp_set_var_typed_value(var, ASN_COUNTER64,
  455. (u_char *) &new_val,
  456. sizeof(new_val));
  457. return SNMP_ERR_NOERROR;
  458. }
  459. /************************************************************
  460. * dsEntriesTable_get_value
  461. *
  462. * This routine is called for get requests to copy the data
  463. * from the context to the varbind for the request. If the
  464. * context has been properly maintained, you don't need to
  465. * change in code in this fuction.
  466. */
  467. int
  468. dsEntriesTable_get_value(netsnmp_request_info *request,
  469. netsnmp_index * item,
  470. netsnmp_table_request_info *table_info)
  471. {
  472. PRUint64 *the_stat = NULL;
  473. integer64 new_val;
  474. netsnmp_variable_list *var = request->requestvb;
  475. stats_table_context *context = (stats_table_context *) item;
  476. switch (table_info->colnum) {
  477. case COLUMN_DSMASTERENTRIES:
  478. the_stat = &context->entries_tbl.dsMasterEntries;
  479. break;
  480. case COLUMN_DSCOPYENTRIES:
  481. the_stat = &context->entries_tbl.dsCopyEntries;
  482. break;
  483. case COLUMN_DSCACHEENTRIES:
  484. the_stat = &context->entries_tbl.dsCacheEntries;
  485. break;
  486. case COLUMN_DSCACHEHITS:
  487. the_stat = &context->entries_tbl.dsCacheHits;
  488. break;
  489. case COLUMN_DSSLAVEHITS:
  490. the_stat = &context->entries_tbl.dsSlaveHits;
  491. break;
  492. default:/* We shouldn't get here */
  493. snmp_log(LOG_ERR, "Unknown column in dsEntriesTable_get_value\n");
  494. return SNMP_ERR_GENERR;
  495. }
  496. /* The Net-SNMP integer64 type isn't a true 64-bit value, but instead
  497. * a structure containing the high and low bits separately. We need
  498. * to split our value appropriately. */
  499. new_val.low = *the_stat & 0x00000000ffffffff;
  500. new_val.high = (*the_stat >> 32) & 0x00000000ffffffff;
  501. snmp_set_var_typed_value(var, ASN_COUNTER64,
  502. (u_char *) &new_val,
  503. sizeof(new_val));
  504. return SNMP_ERR_NOERROR;
  505. }
  506. /************************************************************
  507. * dsEntityTable_get_value
  508. *
  509. * This routine is called for get requests to copy the data
  510. * from the context to the varbind for the request. If the
  511. * context has been properly maintained, you don't need to
  512. * change in code in this fuction.
  513. */
  514. int
  515. dsEntityTable_get_value(netsnmp_request_info *request,
  516. netsnmp_index * item,
  517. netsnmp_table_request_info *table_info)
  518. {
  519. netsnmp_variable_list *var = request->requestvb;
  520. stats_table_context *context = (stats_table_context *) item;
  521. switch (table_info->colnum) {
  522. case COLUMN_DSENTITYDESCR:
  523. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  524. (u_char *) context->hdr_tbl.dsDescription,
  525. strlen(context->hdr_tbl.dsDescription));
  526. break;
  527. case COLUMN_DSENTITYVERS:
  528. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  529. (u_char *) context->hdr_tbl.dsVersion,
  530. strlen(context->hdr_tbl.dsVersion));
  531. break;
  532. case COLUMN_DSENTITYORG:
  533. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  534. (u_char *) context->hdr_tbl.dsOrganization,
  535. strlen(context->hdr_tbl.dsOrganization));
  536. break;
  537. case COLUMN_DSENTITYLOCATION:
  538. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  539. (u_char *) context->hdr_tbl.dsLocation,
  540. strlen(context->hdr_tbl.dsLocation));
  541. break;
  542. case COLUMN_DSENTITYCONTACT:
  543. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  544. (u_char *) context->hdr_tbl.dsContact,
  545. strlen(context->hdr_tbl.dsContact));
  546. break;
  547. case COLUMN_DSENTITYNAME:
  548. snmp_set_var_typed_value(var, ASN_OCTET_STR,
  549. (u_char *) context->hdr_tbl.dsName,
  550. strlen(context->hdr_tbl.dsName));
  551. break;
  552. default:/* We shouldn't get here */
  553. snmp_log(LOG_ERR, "Unknown column in dsEntityTable_get_value\n");
  554. return SNMP_ERR_GENERR;
  555. }
  556. return SNMP_ERR_NOERROR;
  557. }
  558. /************************************************************
  559. * send_DirectoryServerDown_trap
  560. *
  561. * Sends off the server down trap.
  562. */
  563. int
  564. send_DirectoryServerDown_trap(server_instance *serv_p)
  565. {
  566. netsnmp_variable_list *var_list = NULL;
  567. stats_table_context *ctx = NULL;
  568. /* Define the oids for the trap */
  569. oid DirectoryServerDown_oid[] = { DirectoryServerDown_OID };
  570. oid dsEntityDescr_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYDESCR, 0 };
  571. oid dsEntityVers_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYVERS, 0 };
  572. oid dsEntityLocation_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYLOCATION, 0 };
  573. oid dsEntityContact_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYCONTACT, 0 };
  574. dsEntityDescr_oid[3] = serv_p->port;
  575. dsEntityVers_oid[3] = serv_p->port;
  576. dsEntityLocation_oid[3] = serv_p->port;
  577. dsEntityContact_oid[3] = serv_p->port;
  578. snmp_log(LOG_INFO, "Sending down trap for server: %d\n", serv_p->port);
  579. /* Lookup row to get version string */
  580. if ((ctx = stats_table_find_row(serv_p->port)) == NULL) {
  581. snmp_log(LOG_ERR, "Malloc error finding row for server: %d\n", serv_p->port);
  582. return 1;
  583. }
  584. /* Setup the variable list to send with the trap */
  585. snmp_varlist_add_variable(&var_list,
  586. snmptrap_oid, OID_LENGTH(snmptrap_oid),
  587. ASN_OBJECT_ID,
  588. (u_char *) &DirectoryServerDown_oid,
  589. sizeof(DirectoryServerDown_oid));
  590. snmp_varlist_add_variable(&var_list,
  591. dsEntityDescr_oid,
  592. OID_LENGTH(dsEntityDescr_oid), ASN_OCTET_STR,
  593. (u_char *) ctx->hdr_tbl.dsDescription,
  594. strlen(ctx->hdr_tbl.dsDescription));
  595. snmp_varlist_add_variable(&var_list,
  596. dsEntityVers_oid,
  597. OID_LENGTH(dsEntityVers_oid), ASN_OCTET_STR,
  598. (u_char *) ctx->hdr_tbl.dsVersion,
  599. strlen(ctx->hdr_tbl.dsVersion));
  600. snmp_varlist_add_variable(&var_list,
  601. dsEntityLocation_oid,
  602. OID_LENGTH(dsEntityLocation_oid),
  603. ASN_OCTET_STR,
  604. (u_char *) ctx->hdr_tbl.dsLocation,
  605. strlen(ctx->hdr_tbl.dsLocation));
  606. snmp_varlist_add_variable(&var_list,
  607. dsEntityContact_oid,
  608. OID_LENGTH(dsEntityContact_oid),
  609. ASN_OCTET_STR,
  610. (u_char *) ctx->hdr_tbl.dsContact,
  611. strlen(ctx->hdr_tbl.dsContact));
  612. /* Send the trap */
  613. send_v2trap(var_list);
  614. snmp_free_varbind(var_list);
  615. return SNMP_ERR_NOERROR;
  616. }
  617. /************************************************************
  618. * send_DirectoryServerStart_trap
  619. *
  620. * Sends off the server start trap.
  621. */
  622. int
  623. send_DirectoryServerStart_trap(server_instance *serv_p)
  624. {
  625. netsnmp_variable_list *var_list = NULL;
  626. stats_table_context *ctx = NULL;
  627. /* Define the oids for the trap */
  628. oid DirectoryServerStart_oid[] = { DirectoryServerStart_OID };
  629. oid dsEntityDescr_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYDESCR, 0 };
  630. oid dsEntityVers_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYVERS, 0 };
  631. oid dsEntityLocation_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYLOCATION, 0 };
  632. dsEntityDescr_oid[3] = serv_p->port;
  633. dsEntityVers_oid[3] = serv_p->port;
  634. dsEntityLocation_oid[3] = serv_p->port;
  635. snmp_log(LOG_INFO, "Sending start trap for server: %d\n", serv_p->port);
  636. /* Lookup row to get version string */
  637. if ((ctx = stats_table_find_row(serv_p->port)) == NULL) {
  638. snmp_log(LOG_ERR, "Malloc error finding row for server: %d\n", serv_p->port);
  639. return 1;
  640. }
  641. /* Setup the variable list to send with the trap */
  642. snmp_varlist_add_variable(&var_list,
  643. snmptrap_oid, OID_LENGTH(snmptrap_oid),
  644. ASN_OBJECT_ID,
  645. (u_char *) &DirectoryServerStart_oid,
  646. sizeof(DirectoryServerStart_oid));
  647. snmp_varlist_add_variable(&var_list,
  648. dsEntityDescr_oid,
  649. OID_LENGTH(dsEntityDescr_oid), ASN_OCTET_STR,
  650. (u_char *) ctx->hdr_tbl.dsDescription,
  651. strlen(ctx->hdr_tbl.dsDescription));
  652. snmp_varlist_add_variable(&var_list,
  653. dsEntityVers_oid,
  654. OID_LENGTH(dsEntityVers_oid), ASN_OCTET_STR,
  655. (u_char *) ctx->hdr_tbl.dsVersion,
  656. strlen(ctx->hdr_tbl.dsVersion));
  657. snmp_varlist_add_variable(&var_list,
  658. dsEntityLocation_oid,
  659. OID_LENGTH(dsEntityLocation_oid),
  660. ASN_OCTET_STR,
  661. (u_char *) ctx->hdr_tbl.dsLocation,
  662. strlen(ctx->hdr_tbl.dsLocation));
  663. /* Send the trap */
  664. send_v2trap(var_list);
  665. snmp_free_varbind(var_list);
  666. return SNMP_ERR_NOERROR;
  667. }