/
snmp_agent.c
executable file
·1277 lines (1120 loc) · 29 KB
/
snmp_agent.c
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
/*
* snmp_agent.c
* this file is deprecated in favor of ssds_agent.c
* small system data server snmp dynamic interface
*
* Copyright 2009 System Design & Consulting Services, LLC
*
* All Rights Reserved
* Licensed under GPLv2, see file LICENSE in this tarball for details.
*
* May 12, 2009
* if you change this file name look in
*
* agent/Makefile
* agent/mibgroup/Makefile
*
*
* testing
* in one term run
* agent/snmpd -M mibs -DALL -f -V > out.log 2>&1
* in the other term run
* * snmpwalk -v1 -M mibs -c public localhost .1.3.6.1.4.1.9334.100.104.1
iso.3.6.1.4.1.9334.100.104.1.1.1.0 = INTEGER: 1
iso.3.6.1.4.1.9334.100.104.1.1.2.0 = Opaque: Float: 1000.400024
iso.3.6.1.4.1.9334.100.104.1.1.3.0 = Opaque: Float: 1100.400024
iso.3.6.1.4.1.9334.100.104.1.1.4.0 = Opaque: Float: 1200.400024
iso.3.6.1.4.1.9334.100.104.1.1.5.0 = Opaque: Float: 1300.400024
iso.3.6.1.4.1.9334.100.104.1.1.6.0 = INTEGER: 56
iso.3.6.1.4.1.9334.100.104.1.1.7.0 = INTEGER: 21
this one gets the whole database
* snmpwalk -v1 -M mibs -c public localhost .1.3.6.1.4.1.9334.100.104
* snmpget -v1 -M /home/student-10/work/canam/net-snmp-5.2.5.1/mibs -c public
-Cf localhost .1.3.6.1.4.1.9334.100.104.1.1.1.7.0
SNMPv2-SMI::enterprises.9334.100.104.1.1.7.0 = INTEGER: 21
* snmpget -v1 -M /home/student-10/work/canam/net-snmp-5.2.5.1/mibs -c public
-Cf localhost .1.3.6.1.4.1.9334.100.104.1.1.5.0
SNMPv2-SMI::enterprises.9334.100.104.1.1.1.5.0 = Opaque: Float: 1300.400024
* snmpset -v1 -M /home/student-10/work/canam/net-snmp-5.2.5.1/mibs -c public
localhost .1.3.6.1.4.1.9334.100.104.1.1.6.0 i 2
*/
#include <net-snmp/net-snmp-config.h>
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <ctype.h>
#include <signal.h>
#if HAVE_MACHINE_PARAM_H
#include <machine/param.h>
#endif
#if HAVE_SYS_VMMETER_H
#if !defined(bsdi2) && !defined(netbsd1)
#include <sys/vmmeter.h>
#endif
#endif
#if HAVE_SYS_CONF_H
#include <sys/conf.h>
#endif
#if HAVE_SYS_FS_H
#include <sys/fs.h>
#else
#if HAVE_UFS_FS_H
#include <ufs/fs.h>
#else
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_VNODE_H
#include <sys/vnode.h>
#endif
#ifdef HAVE_UFS_UFS_QUOTA_H
#include <ufs/ufs/quota.h>
#endif
#ifdef HAVE_UFS_UFS_INODE_H
#include <ufs/ufs/inode.h>
#endif
#if HAVE_UFS_FFS_FS_H
#include <ufs/ffs/fs.h>
#endif
#endif
#endif
#if HAVE_MTAB_H
#include <mtab.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#if HAVE_FSTAB_H
#include <fstab.h>
#endif
#if HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#if HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#define statvfs statfs
#endif
#if HAVE_VM_SWAP_PAGER_H
#include <vm/swap_pager.h>
#endif
#if HAVE_SYS_FIXPOINT_H
#include <sys/fixpoint.h>
#endif
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <sys/utsname.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/auto_nlist.h>
#include "mibdefs.h"
#include "struct.h"
#include "util_funcs.h"
#include "agent.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define CANAM_MIB 1,3,6,1,4,1,9334
#define CANAM_ECM 100,104
#define SSDS_NODE_ADDR "127.0.0.1"
#define SSDS_PORT 4321
#define SSDS_DEF_RBUF_LEN 256
#define SSDS_DEF_SBUF_LEN 256
#define SSDS_MAX_DATA (1024 * 8)
#define SSDS_INT 1
#define SSDS_FLOAT 2
#define SSDS_STR 3
struct oid_store {
struct oid_store *next;
int id;
int num_oids;
int depth;
//struct oid_store *oids;
struct oid_store *first_oid;
struct oid_store *last_oid;
//application data
int type;
char name[32];
char *new_data;
char *old_data;
int len;
};
char *def_node_addr = SSDS_NODE_ADDR;
int port_num = SSDS_PORT;
char ssds_data[SSDS_MAX_DATA];
int cli_debug = 0;
int ssds_num_data = 0;
int ssds_max_data = SSDS_MAX_DATA-1;
int ssds_send_debug = 0;
int ssds_fd = 0;
int cl_db;
char *gen_argvb[32];
int gen_argc;
char **gen_argv;
char gen_rbuf[SSDS_DEF_RBUF_LEN];
int gen_rlen = SSDS_DEF_RBUF_LEN;
char gen_sbuf[SSDS_DEF_SBUF_LEN];
int gen_slen = SSDS_DEF_SBUF_LEN;
struct variable7 *ssds_vars;
struct oid_store **ssds_oids;
int ssds_num_vars;
// this is for canam_ecm
oid ssds_variables_oid[] = { CANAM_MIB, CANAM_ECM};
struct oid_store i_oids; // one for each class containing
// one for each item containing
// one for each field containing
// new and backup data stores
struct oid_store c_oids; // one for each class containing
// one for each field containing
// data type and data size
// used to cache class field information
int ssds_get_oid_len(char *name, int num);
int ssds_get_oid_type(char *name, int num);
char *ssds_get_oid_new_data(char *name, int num);
char *ssds_get_oid_old_data(char *name, int num);
size_t ssds_snmp_get_name_len(int ssfd, oid *name, int len, int num);
int ssds_snmp_get_name_val(int ssfd, char *name, int num,
char *res, int rlen);
int ssds_snmp_set_name_val(int ssfd, char *name, int num,
char *res, int rlen);
int ssds_snmp_get_name_type(int ssfd, oid *name,int len, int num);
int ssds_get_num_classes(int pcfd);
int ssds_get_class_id(int pcfd, int num);
int ssds_get_num_class_fields(int pcfd, int num);
int ssds_get_num_class_items(int pcfd, int num);
int ssds_get_field_id(int pcfd, int cnum, int fnum);
int ssds_get_item_id(int pcfd, int cnum, int inum);
int ssds_get_field_len(int pcfd, int cnum, int fnum);
int ssds_get_field_tid(int pcfd, int cnum, int fnum);
int ssds_reset_rbuf(void);
char * ssds_malloc(int len)
{
char *data;
data = malloc(len);
return data;
}
struct oid_store *new_oid(struct oid_store *old_oid)
{
struct oid_store *oid;
struct oid_store *last_oid;
oid = (struct oid_store *)ssds_malloc(sizeof(struct oid_store));
oid->depth=old_oid->depth+1;
oid->num_oids=0;
oid->first_oid=NULL;
oid->last_oid=NULL;
oid->new_data=NULL;
oid->old_data=NULL;
oid->id=0;
oid->next=NULL;
if(old_oid->num_oids==0) {
old_oid->first_oid=oid;
} else {
last_oid=old_oid->last_oid;
last_oid->next = oid;
}
old_oid->last_oid=oid;
old_oid->num_oids++;
return oid;
}
struct oid_store *init_oid(struct oid_store *oid, int num)
{
int i;
struct oid_store *this_oid;
for(i=0;i<num;i++) {
this_oid = new_oid(oid);
}
return oid;
}
struct oid_store *find_oid(struct oid_store *oid, int id)
{
int i;
struct oid_store *this_oid;
struct oid_store *ret;
if (oid == NULL) oid = &i_oids;
ret = NULL;
this_oid = oid->first_oid;
for (i=0;i< oid->num_oids;i++) {
if(this_oid->id == id) {
ret = this_oid;
break;
} else {
this_oid = this_oid->next;
}
}
return ret;
}
struct oid_store *find_oid_name(struct oid_store *xoid, char *name, int num)
{
int i;
struct oid_store *ret;
ret = xoid;
for (i=0; i<num; i++) {
ret = find_oid(ret, name[i]);
if(ret == NULL) {
break;
}
}
return ret;
}
int ssds_find_oid_magic(char *name, int num)
{
struct oid_store *oid;
int ret=-1;
int ix;
oid = find_oid_name(NULL, name, num);
if(oid) {
for (ix=0; ix<ssds_num_vars; ix++) {
if(ssds_oids[ix] == oid) {
ret = ix;
break;
}
}
} else {
}
printf("SDCSDB oid found %p magic %d\n", oid, ret);
return ret;
}
int ssds_get_oid_len(char *name, int num)
{
struct oid_store *oid;
int ret=-1;
oid = find_oid_name(NULL, name, num);
if(oid) {
ret = oid->len;
}
return ret;
}
int ssds_get_oid_type(char *name, int num)
{
struct oid_store *oid;
int ret=-1;
oid = find_oid_name(NULL, name, num);
if(oid) {
ret = oid->type;
}
return ret;
}
char *ssds_get_oid_new_data(char *name, int num)
{
struct oid_store *oid;
char *ret=NULL;
oid = find_oid_name(NULL, name, num);
if(oid) {
ret = oid->new_data;
}
return ret;
}
char *ssds_get_oid_old_data(char *name, int num)
{
struct oid_store *oid;
char *ret=NULL;
oid = find_oid_name(NULL, name, num);
if(oid) {
ret = oid->old_data;
}
return ret;
}
int ssds_set_up_oids(int ssfd)
{
int num_defs;
int i,j,k;
int num_fields;
int num_items;
int num_classes;
int item_id;
struct oid_store *this_ioid;
struct oid_store *this_coid;
struct oid_store *tmp_oid;
struct oid_store *tmp_oid1;
struct oid_store *tmp_oid2;
ssds_reset_rbuf();
num_classes = ssds_get_num_classes(ssfd);
printf("SSDS - num_classes %d\n", num_classes);
num_defs = 0;
/*
* first set up the base stores for items and fields
* the c_oid store is used to keep class information
* the i_oid store is the system structure base
*/
i_oids.depth=0;
c_oids.depth=0;
init_oid(&i_oids, num_classes); /* item oid */
init_oid(&c_oids, num_classes); /* class oid */
this_ioid = i_oids.first_oid;
this_coid = c_oids.first_oid;
for (i=0; i<num_classes; i++) {
ssds_reset_rbuf();
this_ioid->id = ssds_get_class_id(ssfd, i);
this_coid->id = ssds_get_class_id(ssfd, i);
// Set up master fields for each class
num_fields = ssds_get_num_class_fields(ssfd, i);
printf("SSDS - class %d num_fields %d\n", i, num_fields);
init_oid(this_coid, num_fields); // field oid
tmp_oid = this_coid->first_oid;
for (j = 0; j<num_fields; j++) {
ssds_reset_rbuf();
cl_db = 1;
tmp_oid->id = ssds_get_field_id(ssfd,i,j);
tmp_oid->len = ssds_get_field_len(ssfd,i,j);
tmp_oid->type = ssds_get_field_tid(ssfd,i,j);
tmp_oid = tmp_oid->next;
}
ssds_reset_rbuf();
num_items = ssds_get_num_class_items(ssfd,i);
printf("SSDS - class %d num_items %d\n", i, num_items);
init_oid(this_ioid, num_items); // item oid
num_defs += (num_items * num_fields);
tmp_oid = this_ioid->first_oid;
for (j=0; j<num_items; j++) {
ssds_reset_rbuf();
item_id = ssds_get_item_id(ssfd, i,j);
tmp_oid->id=item_id;
init_oid(tmp_oid, num_fields); // item oid
// Copy field data for each item
tmp_oid1 = tmp_oid->first_oid;
tmp_oid2 = this_coid->first_oid;
for (k=0; k<num_fields; k++) {
//item = field
tmp_oid1->id = tmp_oid2->id;
tmp_oid1->len = tmp_oid2->len;
tmp_oid1->type = tmp_oid2->type;
tmp_oid1->old_data = ssds_malloc(tmp_oid1->len);
tmp_oid1->new_data = ssds_malloc(tmp_oid1->len);
tmp_oid1->name[0]= this_coid->id;
tmp_oid1->name[1]= item_id;
tmp_oid1->name[2]= tmp_oid2->id;
tmp_oid1 = tmp_oid1->next;
tmp_oid2 = tmp_oid2->next;
} // end num fields
tmp_oid = tmp_oid->next;
} // end num items
this_ioid = this_ioid->next;
this_coid = this_coid->next;
} // end num classes
return num_defs;
}
// arg splitters
/////////////////////////////////////////////////////////////////
static char *get_del(char *sp, char *argv[], int ix)
{
while(*sp && (*sp == ':')) sp++;
if (*sp) argv[ix] = sp;
while((*sp) && (*sp != ':')) sp++;
if (*sp == ':') {
*sp=0;
sp++;
}
return sp;
}
///////////////////////////////////////////////////////////////
static int get_argc(char *buf, char *argv[], int max)
{
char * sp;
int ix;
sp = buf;
ix = 0;
while (*sp && (ix < max)) {
sp = get_del(sp, argv, ix);
ix++;
}
return ix;
}
/*
* char **argv;
* char *myargv[16];
* int argc;
* argv = myargv;
* argc = get_argc(lbuf, argv, 16);
* argv[0]:etc
*/
int get_arg_int(int argc, char **argv, char *key, int *num)
{
int ret;
int i;
ret = -1;
for (i = 0 ; i < argc; i++) {
if (strcmp(argv[i],key) == 0) {
ret = sscanf(argv[i+1],"%d", num);
ret = 0;
break;
}
}
return ret;
}
int get_arg_char(int argc, char **argv, char *key, char *sp, int len)
{
int ret;
int i;
char *xsp;
ret = 0;
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], key) == 0) {
xsp = argv[i+1];
while(*xsp && len--) {
ret ++;
*sp++=*xsp++;
}
*sp = 0;
break;
}
}
return ret;
}
int get_arg_bin(int argc, char **argv, char *key, char *sp, int len)
{
int ret;
int i;
char *xsp;
ret = 0;
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], key) == 0) {
xsp = argv[i+1];
while(len--) {
ret ++;
*sp++=*xsp++;
}
*sp = 0;
break;
}
}
return ret;
}
char *ssds_alloc_data(int size)
{
char *ret;
ret = NULL;
if ((size + ssds_num_data) < ssds_max_data) {
ret = &ssds_data[ssds_num_data];
ssds_num_data += size;
}
return ret;
}
int ssds_reset_rbuf(void)
{
gen_rbuf[0] = 0;
return 0;
}
int ssds_send_len(int fd, char *sbuf, int slen, char *rbuf, int rlen)
{
int len;
int mlen;
int res;
int ret;
char snum[8];
sprintf(snum, "%04d", slen);
memcpy((void*)&sbuf[4], (void *)&snum[0], 4);
if(ssds_send_debug)
printf("sending len %04d \n", slen);
res = write(fd, sbuf, slen);
if (res == slen) {
res = read(fd, rbuf, 8);
if (res == 8) {
memcpy((void *)&snum[0], (void *)&rbuf[4], 4);
snum[4]=0;
mlen=atoi(snum);
if(ssds_send_debug){
printf(" Client len (%d) \n", len);
printf(" Client rbuf %c %c %c %c %x %x %x %x\n"
, rbuf[0], rbuf[1], rbuf[2], rbuf[3]
, rbuf[4], rbuf[5], rbuf[6], rbuf[7]);
}
len=mlen;
if(len>rlen)len=rlen;
res = read(fd, &rbuf[8], len-8);
res += 8;
rbuf[res]=0;
if(ssds_send_debug){
printf(" Client rbuf len %d (%s) \n", len, rbuf);
}
/* we have to throw the rest away */
while(mlen > rlen) {
ret = read(fd, snum, 8);
if(ret >0) {
mlen -= ret;
}
}
}
} else {
res = -1;
}
return res;
}
int ssds_send(int fd, char *sbuf, char *rbuf)
{
int len;
len = strlen(sbuf);
return ssds_send_len(fd, sbuf, len, rbuf , SSDS_DEF_RBUF_LEN);
}
int ssds_snmp_open(void /*struct ssds_connect *pccon*/)
{
int cli_fd, cli_len;
struct sockaddr_in cli_addr;
char *node_addr = def_node_addr;
int res;
cli_fd = socket(AF_INET,SOCK_STREAM,0);
cli_addr.sin_family = AF_INET;
if(cli_debug)
printf("<!--SSDS Client attempting to connect to %s:%d-->\n",
node_addr, port_num);
res = inet_aton(node_addr, &cli_addr.sin_addr);
if (res == 0) {
perror("inet_aton error");
exit(1);
}
if(cli_debug)
printf("<!-- SSDS Client socket set up -->\n");
cli_addr.sin_port = htons(port_num);
cli_len = sizeof(cli_addr);
res=connect(cli_fd,
(struct sockaddr *)&cli_addr,cli_len);
if (res == -1) {
perror(" Client cannot connect\n");
exit(1);
}
if(cli_debug)
printf("<!--SSDS Client connected -->\n");
sprintf(gen_sbuf, "msg 5678:action:hello:");
res = ssds_send(cli_fd, gen_sbuf, gen_rbuf);
ssds_fd = cli_fd;
return cli_fd;
}
int ssds_get_gen_buf(int ssfd, char *sdef, char *skey)
{
int num;
int res;
int ret;
int argc;
//char sbuf[SSDS_DEF_SBUF_LEN];
int i;
gen_argv = gen_argvb;
if(sdef) {
sprintf(gen_sbuf,"msg 5678:action:%s.%s:", skey, sdef);
} else {
sprintf(gen_sbuf,"msg 5678:action:%s:", skey);
}
if(cl_db) {
printf("sending (%s) \n", gen_sbuf);
}
res = ssds_send(ssfd, gen_sbuf, gen_rbuf);
if(cl_db) {
printf("got (%s) \n", gen_rbuf);
}
gen_argc = get_argc(gen_rbuf, gen_argv, 32);
if(cl_db) {
for (i = 0; i<gen_argc; i++) {
printf(" argv[%d] (%s) \n", i, gen_argv[i]);
}
}
return gen_argc;
}
int ssds_get_gen_num(int ssfd, char *sdef, char *skey, char *rkey)
{
int num;
int ret;
if(strlen(gen_rbuf)== 0) {
ssds_get_gen_buf(ssfd, sdef, skey);
}
ret = get_arg_int(gen_argc, gen_argv, rkey, &num);
if(cl_db) {
printf("ret %d key (%s) num %d\n", ret, rkey, num);
}
if(ret >= 0) {
ret = num;
}
if(cl_db)cl_db--;
return ret;
}
int ssds_get_gen_name(int ssfd, char *sdef, char *skey, char *rkey
, char *res, int rlen)
{
int ret;
if(strlen(gen_rbuf)== 0) {
ssds_get_gen_buf(ssfd, sdef, skey);
}
res[0]=0;
ret = get_arg_char(gen_argc, gen_argv, rkey, res, rlen);
if(cl_db) {
printf("ret %d for key (%s) res (%s)\n", ret, rkey, res);
}
if(cl_db) cl_db--;
return ret;
}
int ssds_get_num_classes(int ssfd)
{
return ssds_get_gen_num(ssfd, NULL, "num_classes", "reply");
}
int ssds_get_class_id(int ssfd, int num)
{
char tbuf[32];
sprintf(tbuf,"0%d",num);
return ssds_get_gen_num(ssfd, tbuf, "gc", "id");
}
int ssds_get_item_id(int ssfd, int cnum, int inum)
{
char tbuf[32];
sprintf(tbuf,"0%d.0%d.0",cnum,inum);
return ssds_get_gen_num(ssfd, tbuf, "gvf", "i_id");
}
size_t ssds_snmp_get_name_len(int ssfd, oid *name, int len, int num)
{
size_t ret;
char tbuf[32];
sprintf(tbuf,"%d.%d.%d",name[len],name[len+1],name[len+2]);
ret = ssds_get_gen_num(ssfd, tbuf, "gvf", "f_len");
return ret;
}
int ssds_snmp_get_name_val(int ssfd, char *name,int num, char *res
, int rlen)
{
char tbuf[32];
int ret;
sprintf(tbuf,"%d.%d.%d",name[0], name[1], name[2]);
ret = ssds_get_gen_name(ssfd, tbuf, "gv", "value", res, rlen);
return ret;
}
int ssds_snmp_set_name_val(int ssfd, char *name, int num, char *res
, int rlen)
{
char tbuf[128];
int ret;
sprintf(tbuf,"%d.%d.%d=%s",name[0], name[1], name[2], res);
ret = ssds_get_gen_name(ssfd, tbuf, "sv", "value", res, rlen);
return ret;
}
int ssds_snmp_get_name_type(int ssfd, oid *name,int len, int num)
{
char tbuf[32];
int ret;
sprintf(tbuf,"%d.%d.%d",name[len],name[len+1],name[len+2]);
ret = ssds_get_gen_num(ssfd, tbuf, "gvf", "f_tid");
return ret;
}
int ssds_get_field_id(int ssfd, int cnum, int fnum)
{
char tbuf[32];
sprintf(tbuf,"0%d.0%d", cnum, fnum);
return ssds_get_gen_num(ssfd, tbuf, "gf", "f_id");
}
int ssds_get_field_tid(int ssfd, int cnum, int fnum)
{
int ret;
char tbuf[32];
sprintf(tbuf,"0%d.0%d", cnum, fnum);
ret = ssds_get_gen_num(ssfd, tbuf, "gf", "f_tid");
return ret;
}
int ssds_get_field_len(int ssfd, int cnum, int fnum)
{
char tbuf[32];
sprintf(tbuf,"0%d.0%d",cnum,fnum);
return ssds_get_gen_num(ssfd, tbuf, "gf", "f_len");
}
int ssds_get_num_class_fields(int ssfd, int num)
{
char tbuf[32];
sprintf(tbuf,"0%d",num);
return ssds_get_gen_num(ssfd, tbuf, "gc", "num_fields");
}
int ssds_get_num_class_items(int ssfd, int num)
{
char tbuf[32];
sprintf(tbuf,"0%d",num);
return ssds_get_gen_num(ssfd, tbuf, "gc", "num_items");
}
FindVarMethod var_extensible_ssds;
// look in
// /agent/mibgroup/mib_module_inits.h
// include/net-snmp/agent/var_struct.h
// use variable7 but only populate 3
//
//
/*
* TODO turn this into a multi depth scanner
*/
int init_ssds_vars(int num_vars)
{
int i,j,k;
int var_num;
int num_classes;
int num_items;
int num_fields;
struct oid_store *this_oid;
struct oid_store *this_coid;
struct oid_store *this_ioid;
struct oid_store *this_foid;
struct variable7 *this_var;
ssds_vars = (struct variable7 *)
ssds_malloc(sizeof(struct variable7) * num_vars);
ssds_oids = (struct oid_store **)
ssds_malloc(sizeof(struct oid_store*) * num_vars);
var_num = 0;
this_oid = &i_oids;
this_coid = this_oid->first_oid;
num_classes = this_oid->num_oids;
for (i = 0; i < num_classes ; i++) {
num_items = this_coid->num_oids;
this_ioid = this_coid->first_oid;
for (j = 0; j < num_items ; j++) {
num_fields = this_ioid->num_oids;
this_foid = this_ioid->first_oid;
for (k = 0; k < num_fields ; k++) {
this_var = &ssds_vars[var_num];
this_var->magic = var_num;
if(this_foid->type == SSDS_FLOAT) {
this_var->type = ASN_OPAQUE_FLOAT;
} else {
this_var->type = ASN_INTEGER;
}
this_var->acl = RWRITE;
this_var->findVar = var_extensible_ssds;
this_var->namelen = 3;
this_var->name[0] = this_coid->id;
this_var->name[1] = this_ioid->id;
this_var->name[2] = this_foid->id;
ssds_oids[var_num] = this_foid;
var_num++;
this_foid = this_foid->next;
}
this_ioid = this_ioid->next;
}
this_coid = this_coid->next;
}
return var_num;
}
/*
* TODO turned this into a multi depth scanner
*/
#define MAX_DEPTH 6
/* test code not for use yet */
int do_depth(int depth, int *idx, int *num_items,
int var_num, struct oid_store **this_oid)
{
int id;
if(depth>=MAX_DEPTH) {
printf(" max depth reached %d \n", depth);
return var_num;
}
if(0)
printf(" doing depth %d idx[depth] %d this_oid id %d num %d\n",
depth, idx[depth], this_oid[depth]->id,
this_oid[depth]->num_oids);
this_oid[depth+1] = this_oid[depth]->first_oid;
if (this_oid[depth+1] == NULL) {
if(0) {
/* process items, we have a leaf node */
printf("Leaf found at depth %d var %d idx[", depth, var_num);
for(id=0; id<depth+1; id++) {
//printf(".%d",this_oid[idx[id]]->id);
printf(".%d",idx[id]);
}
printf("]\n");
printf(" Leaf id [");
for(id=0; id<depth; id++) {
printf(".%d",this_oid[id+1]->id);
//printf(".%d",idx[id]);
}
printf("]\n");
}
return var_num + 1;
} else {
idx[depth+1]=0;
}
/* not a leaf go deeper */
num_items[depth] = this_oid[depth]->num_oids;
for (idx[depth]=0; idx[depth]<num_items[depth]; idx[depth]++) {
var_num = do_depth(depth+1, idx, num_items, var_num, this_oid);
this_oid[depth+1] = this_oid[depth+1]->next;
}
return var_num;
}
int init_ssds_var_depth(int num_vars)
{
int i,j,k;
int depth;
int num_items[MAX_DEPTH];
int idx[MAX_DEPTH];
struct oid_store *this_oid[MAX_DEPTH];
depth=0;
int var_num;
this_oid[depth] = &i_oids;
var_num = 0;
idx[0] = 0;