-
Notifications
You must be signed in to change notification settings - Fork 2
/
EcAlarmSinkSNMP.m
1871 lines (1621 loc) · 46.4 KB
/
EcAlarmSinkSNMP.m
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import "config.h"
#import "EcAlarm.h"
#import "EcAlarmDestination.h"
#import "EcAlarmSinkSNMP.h"
#import "EcProcess.h"
#import "EcLogger.h"
static EcAlarmSinkSNMP *alarmSink = nil; // The singleton
static NSLock *classLock = nil;
#if defined(WITH_NET_SNMP)
@interface EcAlarmSinkSNMP (Private)
/* Archives the SNMP data to persistent storage so we can re-load it on
* startup. Also stores a description of the active alarms for readability.
*/
- (void) _store;
/* Sends the alarm out as an SNMP trap.
* If forceClear is YES then sends the trap with a cleared severity
* irrespective of the actual severity stored in the alarm object.
*/
- (void) _trap: (EcAlarm*)alarm forceClear: (BOOL)forceClear;
@end
/* The ObjC __block define might conflict with system headers ...
* */
#if defined(__block)
#undef __block
#endif
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/library/snmp_assert.h>
#include <time.h>
static NSString *persistentStore = nil;
static int32_t notificationID = 0;
static NSUInteger managedObjectsCount = 0;
static NSMutableArray *managedObjects = nil;
static NSString *alarmsOID = nil;
static NSString *objectsOID = nil;
static NSString *trapOID = nil;
static netsnmp_tdata *alarmsTable = 0;
static netsnmp_tdata *objectsTable = 0;
/* The following scalar variables are made available via SNMP OIDs.
* The agent will handle all GET and (if applicable) SET requests
* to these variables, changing the values as needed.
*/
static int32_t resyncFlag = 0; /* normally not re-syncing */
static int resyncTimer = 0; /* seconds since started */
static int32_t trapSequenceNumber = 0; /* XXX: set default value */
static int32_t pollHeartBeat = 5; /* heartbeat every 5 minutes */
/* SNMP data structure for an alarm table row entry
*/
struct alarmsTable_entry
{
/* Index
*/
int32_t notificationID;
/* Column values
*/
int32_t perceivedSeverity;
char firstEventDate[32];
size_t firstEventDate_len;
char eventDate[32];
size_t eventDate_len;
char managedObject[128];
size_t managedObject_len;
int32_t eventTypeID;
int32_t probableCauseID;
char specificProblem[256];
size_t specificProblem_len;
char proposedRepairAction[256];
size_t proposedRepairAction_len;
char additionalText[256];
size_t additionalText_len;
char trendIndicator[2];
size_t trendIndicator_len;
int valid;
};
/* SNMP data structure for a managed objects table row entry
*/
struct objectsTable_entry
{
/* Index
*/
char objectID[128];
size_t objectID_len;
int valid;
};
/*
* function declarations
*/
static BOOL
heartbeat(time_t now);
static void
init_EcAlarmSink(void);
static Netsnmp_Node_Handler alarmsTable_handler;
static Netsnmp_Node_Handler objectsTable_handler;
static netsnmp_tdata_row *
alarmsTable_createEntry(int32_t notificationID);
static int
pollHeartBeat_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests);
static netsnmp_tdata_row *
objectsTable_createEntry(NSString *objectID);
/*
* column number definitions for table alarmsTable
*/
#define COLUMN_NOTIFICATIONID 1
#define COLUMN_PERCEIVEDSEVERITY 2
#define COLUMN_FIRSTEVENTDATE 3
#define COLUMN_EVENTDATE 4
#define COLUMN_MANAGEDOBJECT 5
#define COLUMN_IDEVENTTYPE 6
#define COLUMN_IDPROBABLECAUSE 7
#define COLUMN_SPECIFICPROBLEM 8
#define COLUMN_PROPOSEDREPAIRACTION 9
#define COLUMN_ADDITIONALTEXT 10
#define COLUMN_TRENDINDICATOR 11
/*
* column number definitions for table objectsTable
*/
#define COLUMN_OBJECTID 1
@interface EcAlarmSinkSNMP (SNMP)
- (BOOL) snmpClearAlarms: (NSString*)managed;
- (void) snmpHousekeeping;
@end
/* alarmTrap stuff
*/
static oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
static oid *additionalText_oid = 0;
static size_t additionalText_len = 0;
static oid *alarmsTable_oid = 0;
static size_t alarmsTable_len = 0;
static oid *alarmTrap_oid = 0;
static size_t alarmTrap_len = 0;
static oid *eventDate_oid = 0;
static size_t eventDate_len = 0;
static oid *eventType_oid = 0;
static size_t eventType_len = 0;
static oid *firstEventDate_oid = 0;
static size_t firstEventDate_len = 0;
static oid *eventTypeID_oid = 0;
static size_t eventTypeID_len = 0;
static oid *probableCauseID_oid = 0;
static size_t probableCauseID_len = 0;
static oid *notificationID_oid = 0;
static size_t notificationID_len = 0;
static oid *objectID_oid = 0;
static size_t objectID_len = 0;
static oid *objectsTable_oid = 0;
static size_t objectsTable_len = 0;
static oid *perceivedSeverity_oid = 0;
static size_t perceivedSeverity_len = 0;
static oid *pollHeartBeat_oid = 0;
static size_t pollHeartBeat_len = 0;
static oid *proposedRepairAction_oid = 0;
static size_t proposedRepairAction_len = 0;
static oid *resyncFlag_oid = 0;
static size_t resyncFlag_len = 0;
static oid *specificProblem_oid = 0;
static size_t specificProblem_len = 0;
static oid *trapSequenceNumber_oid = 0;
static size_t trapSequenceNumber_len = 0;
static oid *trendIndicator_oid = 0;
static size_t trendIndicator_len = 0;
static int
logSNMP(int major, int minor, void* server, void* client)
{
struct snmp_log_message *slm = (struct snmp_log_message *)server;
if (nil == EcProc)
{
NSLog(@"%s", slm->msg);
}
else
{
switch (slm->priority)
{
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
[[EcLogger loggerForType: LT_ALERT] log: @"%s", slm->msg];
break;
case LOG_ERR:
[[EcLogger loggerForType: LT_ERROR] log: @"%s", slm->msg];
break;
case LOG_WARNING:
case LOG_NOTICE:
case LOG_INFO:
case LOG_DEBUG:
default:
NSLog(@"%s", slm->msg); break;
}
}
return 0;
}
static const char *
stringFromDate(NSDate *d)
{
NSCalendarDate *c;
c = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
[d timeIntervalSinceReferenceDate]];
[c setCalendarFormat: @"%Y%m%d %H:%M:%S"];
return [[c description] UTF8String];
}
/* Function to send heartbeat alarm iff the pollHeartBeat interval has passed.
*/
static BOOL
heartbeat(time_t now)
{
static time_t last = 0;
struct tm *t;
char timestamp[18];
netsnmp_variable_list *var_list = NULL;
const char *trapName = "HEARTBEAT TRAP";
const int32_t cause = 0;
const int32_t notification = 0;
const int32_t severity = 4;
const int32_t eventType = 2;
/* Build current timestamp and send a heartbeat
*/
now = time(0);
if (((now - last) / 60) < pollHeartBeat)
{
return NO; /* Not yet time for a heartbeat */
}
last = now;
t = gmtime(&now);
sprintf(timestamp, "%04d%02d%02d %02d:%02d:%02d",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
/*
* Set the snmpTrapOid.0 value
*/
snmp_varlist_add_variable(&var_list,
snmptrap_oid, OID_LENGTH(snmptrap_oid),
ASN_OBJECT_ID,
(u_char*)alarmTrap_oid,
alarmTrap_len * sizeof(oid));
if (++trapSequenceNumber <= 0) trapSequenceNumber = 1;
snmp_varlist_add_variable(&var_list,
trapSequenceNumber_oid,
trapSequenceNumber_len,
ASN_INTEGER,
(u_char*)&trapSequenceNumber,
sizeof(trapSequenceNumber));
snmp_varlist_add_variable(&var_list,
notificationID_oid,
notificationID_len, ASN_INTEGER,
(u_char*)¬ification, /* Special for heartbeat */
sizeof(notification));
snmp_varlist_add_variable(&var_list,
perceivedSeverity_oid,
perceivedSeverity_len,
ASN_INTEGER,
(u_char*)&severity, /* warning */
sizeof(severity));
snmp_varlist_add_variable(&var_list,
firstEventDate_oid,
firstEventDate_len,
ASN_OCTET_STR,
0, /* not required */
0);
snmp_varlist_add_variable(&var_list,
eventDate_oid, eventDate_len,
ASN_OCTET_STR,
(u_char*)timestamp,
strlen(timestamp));
snmp_varlist_add_variable(&var_list,
objectID_oid, objectID_len,
ASN_OCTET_STR,
0, /* not required */
0);
snmp_varlist_add_variable(&var_list,
eventTypeID_oid, eventTypeID_len,
ASN_INTEGER,
(u_char*)&eventType, /* heartbeat */
sizeof(eventType));
snmp_varlist_add_variable(&var_list,
probableCauseID_oid,
probableCauseID_len, ASN_INTEGER,
(u_char*)&cause,
sizeof(cause));
snmp_varlist_add_variable(&var_list,
specificProblem_oid,
specificProblem_len,
ASN_OCTET_STR,
0, /* not required */
0);
snmp_varlist_add_variable(&var_list,
proposedRepairAction_oid,
proposedRepairAction_len,
ASN_OCTET_STR,
0, /* not required */
0);
snmp_varlist_add_variable(&var_list,
additionalText_oid,
additionalText_len,
ASN_OCTET_STR,
(u_char*)trapName,
strlen(trapName));
snmp_varlist_add_variable(&var_list,
trendIndicator_oid,
trendIndicator_len,
ASN_OCTET_STR,
0, /* not required */
0);
/*
* Send the trap to the list of configured destinations
* and clean up
*/
DEBUGMSGTL(("EcAlarmSinkHeartbeat", "Sending trap.\n"));
send_v2trap(var_list);
snmp_free_varbind(var_list);
return YES;
}
static void
setAlarmTableEntry(netsnmp_tdata_row *row, EcAlarm *alarm)
{
struct alarmsTable_entry *e;
const char *s;
[alarm setExtra: (void*)row];
[alarm freeze]; // Once it's in the snmp table, no more change
e = (struct alarmsTable_entry *)row->data;
e->notificationID = [alarm notificationID];
e->perceivedSeverity = [alarm perceivedSeverity];
s = stringFromDate([alarm firstEventDate]);
strcpy(e->firstEventDate, s);
e->firstEventDate_len = strlen(s);
s = stringFromDate([alarm eventDate]);
strcpy(e->eventDate, s);
e->eventDate_len = strlen(s);
s = [[alarm managedObject] UTF8String];
strcpy(e->managedObject, s);
e->managedObject_len = strlen(s);
e->eventTypeID = [alarm eventType];
e->probableCauseID = [alarm probableCause];
s = [[alarm specificProblem] UTF8String];
strcpy(e->specificProblem, s);
e->specificProblem_len = strlen(s);
s = [[alarm proposedRepairAction] UTF8String];
strcpy(e->proposedRepairAction, s);
e->proposedRepairAction_len = strlen(s);
s = [[alarm additionalText] UTF8String];
strcpy(e->additionalText, s);
e->additionalText_len = strlen(s);
e->trendIndicator[0] = [alarm trendIndicator];
if (0 == e->trendIndicator[0])
{
e->trendIndicator_len = 0;
}
else
{
e->trendIndicator_len = 1;
e->trendIndicator[1] = 0;
}
}
/* Regular timer called at one second intervals to check for updates from
* alarm sources, update SNMP tables, generate alerts, and send heartbeats
*/
static void
housekeeping(unsigned int clientreg, void *clientarg)
{
if (0 == resyncFlag)
{
resyncTimer = 0;
}
else
{
/* We automatically leave resync mode after five minutes (300 seconds)
* if resync mode has not been turned off via SNMP.
*/
if (300 <= ++resyncTimer)
{
resyncTimer = 0;
resyncFlag = 0;
}
}
[alarmSink snmpHousekeeping];
}
static void
init_EcAlarmSink(void)
{
netsnmp_handler_registration *reg;
netsnmp_table_registration_info *tinfo;
netsnmp_watcher_info *winfo;
NSString *oidString;
NSUserDefaults *defaults;
NSArray *array;
oid *oids;
int len;
int i;
defaults = [EcProc cmdDefaults];
/* First convert the trap OID from dotted integer format to an array
* of net-snmp oid values.
*/
oidString = trapOID = [[defaults stringForKey: @"TrapOID"] copy];
if (nil == oidString) oidString = trapOID = @"1.3.6.1.4.1.39543.3.0.1";
array = [oidString componentsSeparatedByString: @"."];
alarmTrap_len = [array count];
alarmTrap_oid = (oid*)malloc(sizeof(oid) * alarmTrap_len);
for (i = 0; i < alarmTrap_len; i++)
{
alarmTrap_oid[i] = [[array objectAtIndex: i] intValue];
}
/* Now use the dotted integer format 'alarms' OID as the basis to set up
* all the alarm data OIDs.
* The OID buffer needs to allow for two more OIDs after the 'alarms'
* OID because the alarmsTable (in the alarms OID) has entries in it.
*/
oidString = alarmsOID = [[defaults stringForKey: @"AlarmsOID"] copy];
if (nil == oidString) oidString = alarmsOID = @"1.3.6.1.4.1.39543.1";
array = [oidString componentsSeparatedByString: @"."];
len = [array count];
oids = (oid*)malloc(sizeof(oid) * (len + 2));
for (i = 0; i < len; i++)
{
oids[i] = [[array objectAtIndex: i] intValue];
}
oids[len] = 0; // alarmsTable
oids[len + 1] = 0; // alarmsEntry
alarmsTable_len = len + 1;
alarmsTable_oid = (oid*)malloc(sizeof(oid) * alarmsTable_len);
memcpy(alarmsTable_oid, oids, sizeof(oid) * len + 1);
alarmsTable_oid[len] = 1;
resyncFlag_len = len + 1;
resyncFlag_oid = (oid*)malloc(sizeof(oid) * resyncFlag_len);
memcpy(resyncFlag_oid, oids, sizeof(oid) * len);
resyncFlag_oid[len] = 2;
trapSequenceNumber_len = len + 1;
trapSequenceNumber_oid = (oid*)malloc(sizeof(oid) * trapSequenceNumber_len);
memcpy(trapSequenceNumber_oid, oids, sizeof(oid) * len);
trapSequenceNumber_oid[len] = 3;
pollHeartBeat_len = len + 1;
pollHeartBeat_oid = (oid*)malloc(sizeof(oid) * pollHeartBeat_len);
memcpy(pollHeartBeat_oid, oids, sizeof(oid) * len);
pollHeartBeat_oid[len] = 4;
notificationID_len = len + 3;
notificationID_oid = (oid*)malloc(sizeof(oid) * notificationID_len);
memcpy(notificationID_oid, oids, sizeof(oid) * (len + 2));
notificationID_oid[len+2] = 1;
perceivedSeverity_len = len + 3;
perceivedSeverity_oid = (oid*)malloc(sizeof(oid) * perceivedSeverity_len);
memcpy(perceivedSeverity_oid, oids, sizeof(oid) * (len + 2));
perceivedSeverity_oid[len+2] = 2;
firstEventDate_len = len + 3;
firstEventDate_oid = (oid*)malloc(sizeof(oid) * firstEventDate_len);
memcpy(firstEventDate_oid, oids, sizeof(oid) * (len + 2));
firstEventDate_oid[len+2] = 3;
eventDate_len = len + 3;
eventDate_oid = (oid*)malloc(sizeof(oid) * eventDate_len);
memcpy(eventDate_oid, oids, sizeof(oid) * (len + 2));
eventDate_oid[len+2] = 4;
eventType_len = len + 3;
eventType_oid = (oid*)malloc(sizeof(oid) * eventType_len);
memcpy(eventType_oid, oids, sizeof(oid) * (len + 2));
eventType_oid[len+2] = 6;
probableCauseID_len = len + 3;
probableCauseID_oid = (oid*)malloc(sizeof(oid) * probableCauseID_len);
memcpy(probableCauseID_oid, oids, sizeof(oid) * (len + 2));
probableCauseID_oid[len+2] = 7;
specificProblem_len = len + 3;
specificProblem_oid = (oid*)malloc(sizeof(oid) * specificProblem_len);
memcpy(specificProblem_oid, oids, sizeof(oid) * (len + 2));
specificProblem_oid[len+2] = 8;
proposedRepairAction_len = len + 3;
proposedRepairAction_oid
= (oid*)malloc(sizeof(oid) * proposedRepairAction_len);
memcpy(proposedRepairAction_oid, oids, sizeof(oid) * (len + 2));
proposedRepairAction_oid[len+2] = 9;
additionalText_len = len + 3;
additionalText_oid = (oid*)malloc(sizeof(oid) * additionalText_len);
memcpy(additionalText_oid, oids, sizeof(oid) * (len + 2));
additionalText_oid[len+2] = 10;
trendIndicator_len = len + 3;
trendIndicator_oid = (oid*)malloc(sizeof(oid) * trendIndicator_len);
memcpy(trendIndicator_oid, oids, sizeof(oid) * (len + 2));
trendIndicator_oid[len+2] = 11;
free(oids);
oidString = objectsOID = [[defaults stringForKey: @"ObjectsOID"] copy];
if (nil == oidString) oidString = objectsOID = @"1.3.6.1.4.1.39543.2";
array = [oidString componentsSeparatedByString: @"."];
len = [array count];
objectID_len = len + 3;
objectID_oid = (oid*)malloc(sizeof(oid) * objectID_len);
for (i = 0; i < len; i++)
{
objectID_oid[i] = [[array objectAtIndex: i] intValue];
}
objectID_oid[len] = 1; // objectsTable
objectID_oid[len+1] = 1; // objectsEntry
objectID_oid[len+2] = 1; // objectID
objectsTable_len = len + 1;
objectsTable_oid = (oid*)malloc(sizeof(oid) * objectsTable_len);
memcpy(objectsTable_oid, objectID_oid, sizeof(oid) * objectsTable_len);
/* Create the managed objects table as a read-only item for SNMP.
*/
reg = netsnmp_create_handler_registration(
"objectsTable",
objectsTable_handler,
objectsTable_oid,
objectsTable_len,
HANDLER_CAN_RONLY);
if (NULL == reg)
snmp_perror("register objectsTable");
objectsTable = netsnmp_tdata_create_table("objectsTable", 0);
if (NULL == objectsTable)
snmp_perror("create objectsTable");
tinfo = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
netsnmp_table_helper_add_indexes(tinfo,
ASN_OCTET_STR, /* index: objectID */
0);
tinfo->min_column = COLUMN_OBJECTID;
tinfo->max_column = COLUMN_OBJECTID;
if (netsnmp_tdata_register(reg, objectsTable, tinfo) != SNMPERR_SUCCESS)
snmp_perror("register objectsTable tdata");
/* Create the alarms table as a read-only item for SNMP.
*/
reg = netsnmp_create_handler_registration(
"alarmsTable",
alarmsTable_handler,
alarmsTable_oid,
alarmsTable_len,
HANDLER_CAN_RONLY);
if (NULL == reg)
snmp_perror("register alarmsTable");
alarmsTable = netsnmp_tdata_create_table("alarmsTable", 0);
if (NULL == alarmsTable)
snmp_perror("create alarmsTable");
tinfo = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
netsnmp_table_helper_add_indexes(tinfo,
ASN_INTEGER, /* index: notificationID */
0);
tinfo->min_column = COLUMN_NOTIFICATIONID;
tinfo->max_column = COLUMN_TRENDINDICATOR;
if (netsnmp_tdata_register(reg, alarmsTable, tinfo) != SNMPERR_SUCCESS)
snmp_perror("register alarmsTable tdata");
/* Register scalar watchers for each of the MIB objects.
*/
reg = netsnmp_create_handler_registration(
"resyncFlag",
NULL,
resyncFlag_oid,
resyncFlag_len,
HANDLER_CAN_RWRITE);
if (NULL == reg)
snmp_perror("register resyncFlag");
winfo = netsnmp_create_watcher_info(&resyncFlag,
sizeof(int32_t), ASN_INTEGER, WATCHER_FIXED_SIZE);
if (netsnmp_register_watched_scalar(reg, winfo) != MIB_REGISTERED_OK)
snmp_perror("register watched resyncFlag");
reg = netsnmp_create_handler_registration(
"trapSequenceNumber",
NULL,
trapSequenceNumber_oid,
trapSequenceNumber_len,
HANDLER_CAN_RONLY);
if (NULL == reg)
snmp_perror("register trapSequenceNumber");
winfo = netsnmp_create_watcher_info(&trapSequenceNumber,
sizeof(int32_t), ASN_INTEGER, WATCHER_FIXED_SIZE);
if (netsnmp_register_watched_scalar(reg, winfo) != MIB_REGISTERED_OK)
snmp_perror("register watched trapSequenceNumber");
reg = netsnmp_create_handler_registration(
"pollHeartBeat",
pollHeartBeat_handler,
pollHeartBeat_oid,
pollHeartBeat_len,
HANDLER_CAN_RWRITE);
if (NULL == reg)
snmp_perror("register pollHeartBeat");
winfo = netsnmp_create_watcher_info(&pollHeartBeat,
sizeof(int32_t), ASN_INTEGER, WATCHER_FIXED_SIZE);
if (netsnmp_register_watched_scalar(reg, winfo) != MIB_REGISTERED_OK)
snmp_perror("register watched pollHeartBeat");
/* get alarms at one second intervals to do housekeeping.
*/
if (snmp_alarm_register(1, SA_REPEAT, housekeeping, NULL) == 0)
snmp_perror("register housekeeping alarm");
}
/*
* create a new row in the table
*/
static netsnmp_tdata_row *
alarmsTable_createEntry(int32_t notificationID)
{
struct alarmsTable_entry *entry;
netsnmp_tdata_row *row;
entry = SNMP_MALLOC_TYPEDEF(struct alarmsTable_entry);
if (!entry)
{
return NULL;
}
row = netsnmp_tdata_create_row();
if (!row)
{
SNMP_FREE(entry);
return NULL;
}
row->data = entry;
entry->notificationID = notificationID;
netsnmp_tdata_row_add_index(row,
ASN_INTEGER, &(entry->notificationID), sizeof(entry->notificationID));
if (netsnmp_tdata_add_row(alarmsTable, row) != SNMPERR_SUCCESS)
snmp_perror("add alarmsTable entry");
return row;
}
static int
pollHeartBeat_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
int32_t *pollHeartBeat_cache = NULL;
int32_t tmp;
DEBUGMSGTL(("EcAlarmSink", "Got pollHeartBeat_handler request:\n"));
switch (reqinfo->mode)
{
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
(u_char*) &pollHeartBeat, sizeof(pollHeartBeat));
break;
case MODE_SET_RESERVE1:
if (requests->requestvb->type != ASN_INTEGER)
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_WRONGTYPE);
break;
case MODE_SET_RESERVE2:
/*
* store old info for undo later
*/
pollHeartBeat_cache = malloc(sizeof(pollHeartBeat));
if (pollHeartBeat_cache == NULL)
{
netsnmp_set_request_error(reqinfo, requests,
SNMP_ERR_RESOURCEUNAVAILABLE);
return SNMP_ERR_NOERROR;
}
memcpy(&pollHeartBeat_cache, &pollHeartBeat, sizeof(pollHeartBeat));
netsnmp_request_add_list_data(requests,
netsnmp_create_data_list
("EcAlarmSink",
pollHeartBeat_cache, free));
break;
case MODE_SET_ACTION:
/*
* update current
*/
tmp = *(requests->requestvb->val.integer);
if (tmp > 0)
{
pollHeartBeat = tmp;
[alarmSink _store];
DEBUGMSGTL(("EcAlarmSink", "updated pollHeartBeat -> %d\n", tmp));
}
else
{
DEBUGMSGTL(("EcAlarmSink", "ignored pollHeartBeat -> %d\n", tmp));
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_WRONGVALUE);
}
break;
case MODE_SET_UNDO:
pollHeartBeat =
*((int32_t*)netsnmp_request_get_list_data(requests, "EcAlarmSink"));
break;
case MODE_SET_COMMIT:
case MODE_SET_FREE:
/*
* nothing to do
*/
break;
}
return SNMP_ERR_NOERROR;
}
/** handles requests for the alarmsTable table */
static int
alarmsTable_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
netsnmp_request_info *request;
netsnmp_table_request_info *table_info;
struct alarmsTable_entry *table_entry;
DEBUGMSGTL(("EcAlarmSink", "Got alarmsTable_handler request:\n"));
switch (reqinfo->mode)
{
/*
* Read-support (also covers GetNext requests)
*/
case MODE_GET:
for (request = requests; request; request = request->next)
{
table_entry = (struct alarmsTable_entry *)
netsnmp_tdata_extract_entry(request);
table_info = netsnmp_extract_table_info(request);
switch (table_info->colnum)
{
case COLUMN_NOTIFICATIONID:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
table_entry->notificationID);
break;
case COLUMN_PERCEIVEDSEVERITY:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
table_entry->perceivedSeverity);
break;
case COLUMN_FIRSTEVENTDATE:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
firstEventDate,
table_entry->firstEventDate_len);
break;
case COLUMN_EVENTDATE:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->eventDate,
table_entry->eventDate_len);
break;
case COLUMN_MANAGEDOBJECT:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
managedObject,
table_entry->managedObject_len);
break;
case COLUMN_IDEVENTTYPE:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
table_entry->eventTypeID);
break;
case COLUMN_IDPROBABLECAUSE:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
table_entry->probableCauseID);
break;
case COLUMN_SPECIFICPROBLEM:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
specificProblem,
table_entry->specificProblem_len);
break;
case COLUMN_PROPOSEDREPAIRACTION:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
proposedRepairAction,
table_entry->
proposedRepairAction_len);
break;
case COLUMN_ADDITIONALTEXT:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
additionalText,
table_entry->additionalText_len);
break;
case COLUMN_TRENDINDICATOR:
if (!table_entry)
{
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
(u_char *) table_entry->
trendIndicator,
table_entry->trendIndicator_len);
break;
default:
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHOBJECT);
break;
}
}
break;
}
return SNMP_ERR_NOERROR;
}
/*
* create a new row in the table