summaryrefslogtreecommitdiffstats
path: root/ldap/servers/snmp/ldap-agent.c
blob: fac31c63ecbb03469e80cfe309e5f054a3d4b28b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/* --- BEGIN COPYRIGHT BLOCK ---
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK --- */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


#include <stdio.h>
#include <time.h>
#include "ldap-agent.h"

static netsnmp_handler_registration *ops_handler = NULL;
static netsnmp_handler_registration *entries_handler = NULL;
static netsnmp_handler_registration *entity_handler = NULL;
static netsnmp_table_array_callbacks ops_cb;
static netsnmp_table_array_callbacks entries_cb;
static netsnmp_table_array_callbacks entity_cb;
extern server_instance *server_head;

/* Set table oids */
oid    dsOpsTable_oid[] = { dsOpsTable_TABLE_OID };
size_t dsOpsTable_oid_len = OID_LENGTH(dsOpsTable_oid);
oid    dsEntriesTable_oid[] = { dsEntriesTable_TABLE_OID };
size_t dsEntriesTable_oid_len = OID_LENGTH(dsEntriesTable_oid);
oid    dsEntityTable_oid[] = {dsEntityTable_TABLE_OID };
size_t dsEntityTable_oid_len = OID_LENGTH(dsEntityTable_oid);

/* Set trap oids */
oid    snmptrap_oid[] = { snmptrap_OID };
size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
oid    enterprise_oid[] = { enterprise_OID };
size_t enterprise_oid_len = OID_LENGTH(enterprise_oid);

/************************************************************
 * init_ldap_agent
 *
 * Initializes the agent and populates the stats table
 * with initial data.
 */
void
init_ldap_agent(void)
{
    server_instance *serv_p = NULL;
    stats_table_context *new_row = NULL;

    /* Define and create the table */
    initialize_stats_table();

    /* Initialize data for each server in conf file */
    for (serv_p = server_head; serv_p != NULL; serv_p = serv_p->next) {
        /* Check if this row already exists. */
        if ((new_row = stats_table_find_row(serv_p->port)) == NULL) {
            /* Create a new row */
            if ((new_row = stats_table_create_row(serv_p->port)) != NULL) {
                /* Set pointer for entity table */
                new_row->entity_tbl = serv_p;

                /* Set previous state of server to unknown */
                serv_p->server_state = STATE_UNKNOWN;

                /* Insert new row into the table */
                snmp_log(LOG_DEBUG, "Inserting row for server: %d\n", serv_p->port);
                CONTAINER_INSERT(ops_cb.container, new_row);
            } else {
                /* error during malloc of row */
                snmp_log(LOG_ERR, "Error creating row for server: %d\n",
                                   serv_p->port);
            }
        }
    }

    /* Force load data into stats table */
    load_stats_table(NULL, NULL);
}

/************************************************************
 * initialize_stats_table
 *
 * Initializes the stats table by defining its contents,
 * how it's structured, and registering callbacks.
 */
void
initialize_stats_table(void)
{
    netsnmp_table_registration_info *ops_table_info = NULL;
    netsnmp_table_registration_info *entries_table_info = NULL;
    netsnmp_table_registration_info *entity_table_info = NULL;
    /* This is a hacky way of figuring out if we are on Net-SNMP 5.2 or later */
#ifdef NETSNMP_CACHE_AUTO_RELOAD
    netsnmp_cache *stats_table_cache = NULL;
#endif

    if (ops_handler || entries_handler || entity_handler) {
        snmp_log(LOG_ERR, "initialize_stats_table called more than once.\n");
        return;
    }

    memset(&ops_cb, 0x00, sizeof(ops_cb));
    memset(&entries_cb, 0x00, sizeof(entries_cb));
    memset(&entity_cb, 0x00, sizeof(entity_cb));

    /* create table structures */
    ops_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    entries_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    entity_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);

    /* create handlers */
    ops_handler = netsnmp_create_handler_registration("dsOpsTable",
                                                     netsnmp_table_array_helper_handler,
                                                     dsOpsTable_oid,
                                                     dsOpsTable_oid_len,
                                                     HANDLER_CAN_RONLY);
    entries_handler = netsnmp_create_handler_registration("dsEntriesTable",
                                                     netsnmp_table_array_helper_handler,
                                                     dsEntriesTable_oid,
                                                     dsEntriesTable_oid_len,
                                                     HANDLER_CAN_RONLY);
    entity_handler = netsnmp_create_handler_registration("dsEntityTable",
                                                     netsnmp_table_array_helper_handler,
                                                     dsEntityTable_oid,
                                                     dsEntityTable_oid_len,
                                                     HANDLER_CAN_RONLY);

    if (!ops_handler || !entries_handler || !entity_handler ||
        !ops_table_info || !entries_table_info || !entity_table_info) {
        /* malloc failed */
        snmp_log(LOG_ERR, "malloc failed in initialize_stats_table\n");
        return;
    }

    /* define table structures */
    netsnmp_table_helper_add_index(ops_table_info, ASN_INTEGER);
    netsnmp_table_helper_add_index(entries_table_info, ASN_INTEGER);
    netsnmp_table_helper_add_index(entity_table_info, ASN_INTEGER);

    ops_table_info->min_column = dsOpsTable_COL_MIN;
    ops_table_info->max_column = dsOpsTable_COL_MAX;
    entries_table_info->min_column = dsEntriesTable_COL_MIN;
    entries_table_info->max_column = dsEntriesTable_COL_MAX;
    entity_table_info->min_column = dsEntityTable_COL_MIN;
    entity_table_info->max_column = dsEntityTable_COL_MAX;

    /* 
     * Define callbacks and the container.  We only use one container that
     * all of the tables use.
     */
    ops_cb.get_value = dsOpsTable_get_value;
    ops_cb.container = netsnmp_container_find("dsOpsTable_primary:"
                                          "dsOpsTable:" "table_container");
    entries_cb.get_value = dsEntriesTable_get_value;
    entries_cb.container = ops_cb.container;
    entity_cb.get_value = dsEntityTable_get_value;
    entity_cb.container = ops_cb.container;

    /* registering the tables with the master agent */
    netsnmp_table_container_register(ops_handler, ops_table_info, &ops_cb,
                                     ops_cb.container, 1);
    netsnmp_table_container_register(entries_handler, entries_table_info, &entries_cb,
                                     entries_cb.container, 1);
    netsnmp_table_container_register(entity_handler, entity_table_info, &entity_cb,
                                     entity_cb.container, 1);

    /* Setup cache for auto reloading of stats */
#ifdef NETSNMP_CACHE_AUTO_RELOAD
    /* This is new api as of Net-SNMP 5.2 */
    stats_table_cache = netsnmp_cache_create(CACHE_REFRESH_INTERVAL, load_stats_table,
                                            NULL, dsOpsTable_oid, dsOpsTable_oid_len);
    stats_table_cache->flags |= NETSNMP_CACHE_DONT_FREE_EXPIRED;
    stats_table_cache->flags |= NETSNMP_CACHE_DONT_AUTO_RELEASE;
    stats_table_cache->flags |= NETSNMP_CACHE_AUTO_RELOAD;
    netsnmp_inject_handler(ops_handler, netsnmp_cache_handler_get(stats_table_cache));
#else
    /* Do things the old way.  This is only needed for Net-SNMP 5.1 and earlier. */
    netsnmp_inject_handler(ops_handler, netsnmp_get_cache_handler(CACHE_REFRESH_INTERVAL, load_stats_table,
                                            free_stats_table, dsOpsTable_oid, dsOpsTable_oid_len));
#endif
}

/************************************************************
 * stats_table_create_row
 *
 * Creates a new table row using the supplied port number as
 * the index, then returns a pointer to the new row.
 */
stats_table_context *
stats_table_create_row(unsigned long portnum)
{
    netsnmp_index index;
    stats_table_context *ctx = SNMP_MALLOC_TYPEDEF(stats_table_context);
    oid *index_oid = (oid *)malloc(sizeof(oid) * MAX_OID_LEN);

    /* Create index using port number */
    index_oid[0] = portnum;
    index.oids = index_oid;
    index.len = 1;

    /* Copy index into row structure */
    if (ctx && index_oid) {
        memcpy(&ctx->index, &index, sizeof(index));
        return ctx;
    } else {
        /* Error during malloc */
        snmp_log(LOG_ERR, "malloc failed in stats_table_create_row\n");
        return NULL;
    }
}

/************************************************************
 * stats_table_find_row
 *
 * Searches for a row by the port number.  Returns NULL if
 * the row doesn't exist.
 */
stats_table_context *
stats_table_find_row(unsigned long portnum)
{
    netsnmp_index index;
    oid index_oid[MAX_OID_LEN];
                                                                                                      
    index_oid[0] = portnum;
    index.oids = index_oid;
    index.len = 1;
                                                                                                      
    return (stats_table_context *)
        CONTAINER_FIND(ops_cb.container, &index);
}

/************************************************************
 * load_stats_table
 *
 * Reloads the stats into the table. This is called
 * automatically from the cache handler. This function
 * does not reload the entity table since it's static
 * information.  We also check if any traps need to
 * be sent here.
 */
int
load_stats_table(netsnmp_cache *cache, void *foo)
{
    server_instance *serv_p = NULL;
    stats_table_context *ctx = NULL;
    time_t previous_start;
    int previous_state;
    int stats_hdl = -1;
    int err;

    snmp_log(LOG_INFO, "Reloading stats.\n");

    /* Initialize data for each server in conf file */
    for (serv_p = server_head; serv_p != NULL; serv_p = serv_p->next) {
        if ((ctx = stats_table_find_row(serv_p->port)) != NULL) {
            /* Save previous state of the server to
             * see if a trap needs to be sent */
            previous_state = serv_p->server_state;
            previous_start = ctx->hdr_tbl.startTime;

            snmp_log(LOG_INFO, "Opening stats file (%s) for server: %d\n",
                     serv_p->stats_file, serv_p->port);

            /* Open the stats file */
            if ((err = agt_mopen_stats(serv_p->stats_file, O_RDONLY, &stats_hdl)) != 0) {
                /* Server must be down */
                serv_p->server_state = SERVER_DOWN;
                /* Zero out the ops and entries tables */
                memset(&ctx->ops_tbl, 0x00, sizeof(ctx->ops_tbl));
                memset(&ctx->entries_tbl, 0x00, sizeof(ctx->entries_tbl));
                if (previous_state != SERVER_DOWN)
                    snmp_log(LOG_INFO, "Unable to open stats file (%s) for server: %d\n",
                                       serv_p->stats_file, serv_p->port);
            } else {
                /* Initialize ops table */
                if ((err = agt_mread_stats(stats_hdl, &ctx->hdr_tbl, &ctx->ops_tbl,
                                           &ctx->entries_tbl)) != 0)
                    snmp_log(LOG_ERR, "Unable to read stats file: %s\n",
                                       serv_p->stats_file);
                                                                                                                
                /* Close stats file */
                if ((err = agt_mclose_stats(stats_hdl)) != 0)
                    snmp_log(LOG_ERR, "Error closing stats file: %s\n",
                                       serv_p->stats_file);

                /* Server must be down if the stats file hasn't been
                 * updated in a while */
                if (difftime(time(NULL), ctx->hdr_tbl.updateTime) >= UPDATE_THRESHOLD) {
                    serv_p->server_state = SERVER_DOWN;
                    if (previous_state != SERVER_DOWN)
                        snmp_log(LOG_INFO, "Stats file for server %d hasn't been updated"
                                    " in %d seconds.\n", serv_p->port, UPDATE_THRESHOLD);
                } else {
                    serv_p->server_state = SERVER_UP;
                }
            }

            /* If the state of the server changed since the last
             * load of the stats, send a trap. */
            if (previous_state != STATE_UNKNOWN) {
                if (serv_p->server_state != previous_state) {
                    if (serv_p->server_state == SERVER_UP) {
                        snmp_log(LOG_INFO, "Detected start of server: %d\n",
                                            serv_p->port);
                        send_DirectoryServerStart_trap(serv_p);
                    } else {
                        send_DirectoryServerDown_trap(serv_p);
                        /* Zero out the ops and entries tables */
                        memset(&ctx->ops_tbl, 0x00, sizeof(ctx->ops_tbl));
                        memset(&ctx->entries_tbl, 0x00, sizeof(ctx->entries_tbl));
                    } 
                } else if (ctx->hdr_tbl.startTime != previous_start) {
                    /* Send traps if the server has restarted since the last load */
                    snmp_log(LOG_INFO, "Detected restart of server: %d\n", serv_p->port);
                    send_DirectoryServerDown_trap(serv_p);
                    send_DirectoryServerStart_trap(serv_p);
                }
            }
        } else {
            /* Can't find our row.  This shouldn't ever happen. */
            snmp_log(LOG_ERR, "Row not found for server: %d\n",
                               serv_p->port);
        }
    }
    return 0;
}

/************************************************************
 * free_stats_table
 *
 * This function doesn't need to free anything since the
 * load_stats_table function doesn't allocate any memory
 * itself.  The cache handler requires us to have a callback
 * function for freeing the cache, so here it is.
 */
void
free_stats_table(netsnmp_cache *cache, void *foo)
{
    return;
}

/************************************************************
 * dsOpsTable_get_value
 *
 * This routine is called for get requests to copy the data
 * from the context to the varbind for the request. If the
 * context has been properly maintained, you don't need to
 * change in code in this fuction.
 */
int
dsOpsTable_get_value(netsnmp_request_info *request,
                     netsnmp_index * item,
                     netsnmp_table_request_info *table_info)
{
    netsnmp_variable_list *var = request->requestvb;
    stats_table_context *context = (stats_table_context *) item;

    switch (table_info->colnum) {

    case COLUMN_DSANONYMOUSBINDS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsAnonymousBinds,
                                 sizeof(context->ops_tbl.dsAnonymousBinds));
        break;

    case COLUMN_DSUNAUTHBINDS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsUnAuthBinds,
                                 sizeof(context->ops_tbl.dsUnAuthBinds));
        break;

    case COLUMN_DSSIMPLEAUTHBINDS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsSimpleAuthBinds,
                                 sizeof(context->ops_tbl.dsSimpleAuthBinds));
        break;

    case COLUMN_DSSTRONGAUTHBINDS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsStrongAuthBinds,
                                 sizeof(context->ops_tbl.dsStrongAuthBinds));
        break;

    case COLUMN_DSBINDSECURITYERRORS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsBindSecurityErrors,
                                 sizeof(context->ops_tbl.dsBindSecurityErrors));
        break;

    case COLUMN_DSINOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsInOps,
                                 sizeof(context->ops_tbl.dsInOps));
        break;

    case COLUMN_DSREADOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsReadOps,
                                 sizeof(context->ops_tbl.dsReadOps));
        break;

    case COLUMN_DSCOMPAREOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsCompareOps,
                                 sizeof(context->ops_tbl.dsCompareOps));
        break;

    case COLUMN_DSADDENTRYOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsAddEntryOps,
                                 sizeof(context->ops_tbl.dsAddEntryOps));
        break;

    case COLUMN_DSREMOVEENTRYOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsRemoveEntryOps,
                                 sizeof(context->ops_tbl.dsRemoveEntryOps));
        break;

    case COLUMN_DSMODIFYENTRYOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsModifyEntryOps,
                                 sizeof(context->ops_tbl.dsModifyEntryOps));
        break;

    case COLUMN_DSMODIFYRDNOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsModifyRDNOps,
                                 sizeof(context->ops_tbl.dsModifyRDNOps));
        break;

    case COLUMN_DSLISTOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsListOps,
                                 sizeof(context->ops_tbl.dsListOps));
        break;

    case COLUMN_DSSEARCHOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsSearchOps,
                                 sizeof(context->ops_tbl.dsSearchOps));
        break;

    case COLUMN_DSONELEVELSEARCHOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsOneLevelSearchOps,
                                 sizeof(context->ops_tbl.dsOneLevelSearchOps));
        break;

    case COLUMN_DSWHOLESUBTREESEARCHOPS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsWholeSubtreeSearchOps,
                                 sizeof(context->ops_tbl.dsWholeSubtreeSearchOps));
        break;

    case COLUMN_DSREFERRALS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsReferrals,
                                 sizeof(context->ops_tbl.dsReferrals));
        break;

    case COLUMN_DSCHAININGS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsChainings,
                                 sizeof(context->ops_tbl.dsChainings));
        break;

    case COLUMN_DSSECURITYERRORS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsSecurityErrors,
                                 sizeof(context->ops_tbl.dsSecurityErrors));
        break;

    case COLUMN_DSERRORS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->ops_tbl.dsErrors,
                                 sizeof(context->ops_tbl.dsErrors));
        break;

    default:/* We shouldn't get here */
        snmp_log(LOG_ERR, "Unknown column in dsOpsTable_get_value\n");
        return SNMP_ERR_GENERR;
    }
    return SNMP_ERR_NOERROR;
}

/************************************************************
 * dsEntriesTable_get_value
 *
 * This routine is called for get requests to copy the data
 * from the context to the varbind for the request. If the
 * context has been properly maintained, you don't need to
 * change in code in this fuction.
 */
int
dsEntriesTable_get_value(netsnmp_request_info *request,
                     netsnmp_index * item,
                     netsnmp_table_request_info *table_info)
{
    netsnmp_variable_list *var = request->requestvb;
    stats_table_context *context = (stats_table_context *) item;
                                                                                                                
    switch (table_info->colnum) {
                                                                                                                
    case COLUMN_DSMASTERENTRIES:
        snmp_set_var_typed_value(var, ASN_GAUGE,
                                 (u_char *) &context->entries_tbl.dsMasterEntries,
                                 sizeof(context->entries_tbl.dsMasterEntries));
        break;
                                                                                                                
    case COLUMN_DSCOPYENTRIES:
        snmp_set_var_typed_value(var, ASN_GAUGE,
                                 (u_char *) &context->entries_tbl.dsCopyEntries,
                                 sizeof(context->entries_tbl.dsCopyEntries));
        break;
                                                                                                                
    case COLUMN_DSCACHEENTRIES:
        snmp_set_var_typed_value(var, ASN_GAUGE,
                                 (u_char *) &context->entries_tbl.dsCacheEntries,
                                 sizeof(context->entries_tbl.dsCacheEntries));
        break;
                                                                                                                
    case COLUMN_DSCACHEHITS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->entries_tbl.dsCacheHits,
                                 sizeof(context->entries_tbl.dsCacheHits));
        break;
                                                                                                                
    case COLUMN_DSSLAVEHITS:
        snmp_set_var_typed_value(var, ASN_COUNTER,
                                 (u_char *) &context->entries_tbl.dsSlaveHits,
                                 sizeof(context->entries_tbl.dsSlaveHits));
        break;
                                                                                                                
    default:/* We shouldn't get here */
        snmp_log(LOG_ERR, "Unknown column in dsEntriesTable_get_value\n");
        return SNMP_ERR_GENERR;
    }
    return SNMP_ERR_NOERROR;
}

/************************************************************
 * dsEntityTable_get_value
 *
 * This routine is called for get requests to copy the data
 * from the context to the varbind for the request. If the
 * context has been properly maintained, you don't need to
 * change in code in this fuction.
 */
int
dsEntityTable_get_value(netsnmp_request_info *request,
                     netsnmp_index * item,
                     netsnmp_table_request_info *table_info)
{
    netsnmp_variable_list *var = request->requestvb;
    stats_table_context *context = (stats_table_context *) item;
                                                                                                                
    switch (table_info->colnum) {
                                                                                                                
    case COLUMN_DSENTITYDESCR:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsDescription,
                                 strlen(context->hdr_tbl.dsDescription));
        break;
                                                                                                                
    case COLUMN_DSENTITYVERS:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsVersion,
                                 strlen(context->hdr_tbl.dsVersion));
        break;
                                                                                                                
    case COLUMN_DSENTITYORG:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsOrganization,
                                 strlen(context->hdr_tbl.dsOrganization));
        break;
                                                                                                                
    case COLUMN_DSENTITYLOCATION:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsLocation,
                                 strlen(context->hdr_tbl.dsLocation));
        break;
                                                                                                                
    case COLUMN_DSENTITYCONTACT:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsContact,
                                 strlen(context->hdr_tbl.dsContact));
        break;
                                                                                                                
    case COLUMN_DSENTITYNAME:
        snmp_set_var_typed_value(var, ASN_OCTET_STR,
                                 (u_char *) context->hdr_tbl.dsName,
                                 strlen(context->hdr_tbl.dsName));
        break;

    default:/* We shouldn't get here */
        snmp_log(LOG_ERR, "Unknown column in dsEntityTable_get_value\n");
        return SNMP_ERR_GENERR;
    }
    return SNMP_ERR_NOERROR;
}

/************************************************************
 * send_DirectoryServerDown_trap
 *
 * Sends off the server down trap.
 */
int
send_DirectoryServerDown_trap(server_instance *serv_p)
{
    netsnmp_variable_list *var_list = NULL;
    stats_table_context *ctx = NULL;

    /* Define the oids for the trap */
    oid DirectoryServerDown_oid[] = { DirectoryServerDown_OID };
    oid dsEntityDescr_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYDESCR, 0 };
    oid dsEntityVers_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYVERS, 0 };
    oid dsEntityLocation_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYLOCATION, 0 };
    oid dsEntityContact_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYCONTACT, 0 };

    dsEntityDescr_oid[3] = serv_p->port;
    dsEntityVers_oid[3] = serv_p->port;
    dsEntityLocation_oid[3] = serv_p->port;
    dsEntityContact_oid[3] = serv_p->port;

    snmp_log(LOG_INFO, "Sending down trap for server: %d\n", serv_p->port);

    /* Lookup row to get version string */
    if ((ctx = stats_table_find_row(serv_p->port)) == NULL) {
        snmp_log(LOG_ERR, "Malloc error finding row for server: %d\n", serv_p->port); 
        return 1;
    }

    /* Setup the variable list to send with the trap */
    snmp_varlist_add_variable(&var_list,
                              snmptrap_oid, OID_LENGTH(snmptrap_oid),
                              ASN_OBJECT_ID,
                              (u_char *) &DirectoryServerDown_oid,
                              sizeof(DirectoryServerDown_oid));
    snmp_varlist_add_variable(&var_list,
                              dsEntityDescr_oid,
                              OID_LENGTH(dsEntityDescr_oid), ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsDescription,
                              strlen(ctx->hdr_tbl.dsDescription));
    snmp_varlist_add_variable(&var_list,
                              dsEntityVers_oid,
                              OID_LENGTH(dsEntityVers_oid), ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsVersion,
                              strlen(ctx->hdr_tbl.dsVersion));
    snmp_varlist_add_variable(&var_list,
                              dsEntityLocation_oid,
                              OID_LENGTH(dsEntityLocation_oid),
                              ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsLocation,
                              strlen(ctx->hdr_tbl.dsLocation));
    snmp_varlist_add_variable(&var_list,
                              dsEntityContact_oid,
                              OID_LENGTH(dsEntityContact_oid),
                              ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsContact,
                              strlen(ctx->hdr_tbl.dsContact));

    /* Send the trap */
    send_v2trap(var_list);
    snmp_free_varbind(var_list);
                                                                                                                
    return SNMP_ERR_NOERROR;
}

/************************************************************
 * send_DirectoryServerStart_trap
 *
 * Sends off the server start trap.
 */
int
send_DirectoryServerStart_trap(server_instance *serv_p)
{
    netsnmp_variable_list *var_list = NULL;
    stats_table_context *ctx = NULL;

    /* Define the oids for the trap */
    oid DirectoryServerStart_oid[] = { DirectoryServerStart_OID };
    oid dsEntityDescr_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYDESCR, 0 };
    oid dsEntityVers_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYVERS, 0 };
    oid dsEntityLocation_oid[] = { dsEntityTable_TABLE_OID, 1, COLUMN_DSENTITYLOCATION, 0 };

    dsEntityDescr_oid[3] = serv_p->port;
    dsEntityVers_oid[3] = serv_p->port;
    dsEntityLocation_oid[3] = serv_p->port;

    snmp_log(LOG_INFO, "Sending start trap for server: %d\n", serv_p->port);

    /* Lookup row to get version string */
    if ((ctx = stats_table_find_row(serv_p->port)) == NULL) {
        snmp_log(LOG_ERR, "Malloc error finding row for server: %d\n", serv_p->port);
        return 1;
    }

    /* Setup the variable list to send with the trap */
    snmp_varlist_add_variable(&var_list,
                              snmptrap_oid, OID_LENGTH(snmptrap_oid),
                              ASN_OBJECT_ID,
                              (u_char *) &DirectoryServerStart_oid,
                              sizeof(DirectoryServerStart_oid));
    snmp_varlist_add_variable(&var_list,
                              dsEntityDescr_oid,
                              OID_LENGTH(dsEntityDescr_oid), ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsDescription,
                              strlen(ctx->hdr_tbl.dsDescription));
    snmp_varlist_add_variable(&var_list,
                              dsEntityVers_oid,
                              OID_LENGTH(dsEntityVers_oid), ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsVersion,
                              strlen(ctx->hdr_tbl.dsVersion));
    snmp_varlist_add_variable(&var_list,
                              dsEntityLocation_oid,
                              OID_LENGTH(dsEntityLocation_oid),
                              ASN_OCTET_STR,
                              (u_char *) ctx->hdr_tbl.dsLocation,
                              strlen(ctx->hdr_tbl.dsLocation));
                                                                                                                
    /* Send the trap */
    send_v2trap(var_list);
    snmp_free_varbind(var_list);
                                                                                                                
    return SNMP_ERR_NOERROR;
}