added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / examples / data_set.c
1 /**  @example data_set.c
2  *  This example creates a table full of information and stores all
3  *  that information within the agent's memory.  The "table_dataset"
4  *  helper routines take care of handling all aspects of SNMP requests
5  *  as they come in (yay!).
6  *
7  *  The exmaple we are instrumenting is an otherwise-useless table
8  *  containing the names of IETF working group chairs.  Obviously,
9  *  this data isn't all that useful from a network management point of
10  *  view but this example only demonstrates how to use and store data.
11  *  For more useful examples (but more complex), check out the
12  *  apps/notification_log.c file which implements parts of the
13  *  NOTIFICATION-LOG-MIB for logging incoming SNMP notifications.
14  *
15  *  Much of this code could be automatically generated by running
16  *  mib2c as follows:
17  *
18  *    - mib2c -c mib2c.create-dataset.conf netSnmpIETFWGTable
19  *
20  *  The table is defined roughly as follows:
21  *
22  *  <pre>
23  *    % snmptranslate -m NET-SNMP-EXAMPLES-MIB -Tp -IR netSnmpIETFWGTable
24  *    |+--netSnmpIETFWGTable(1)
25  *    |   |
26  *    |   +--netSnmpIETFWGEntry(1)
27  *    |      |  Index: nsIETFWGName
28  *    |      |
29  *    |      +-- ---- String    nsIETFWGName(1)
30  *    |      |        Size: 1..32
31  *    |      +-- CR-- String    nsIETFWGChair1(2)
32  *    |      +-- CR-- String    nsIETFWGChair2(3)
33  *  </pre>
34  *
35  *  If this module is compiled into an agent, you should be able to
36  *  issue snmp commands that look something like (valid authentication
37  *  information not shown in these commands):
38  *
39  *  <pre>
40  *      % snmpwalk localhost netSnmpIETFWGTable
41  *      nsIETFWGChair1."snmpv3" = "Russ Mundy"
42  *      nsIETFWGChair2."snmpv3" = "David Harrington"
43  *
44  *      % snmpset localhost nsIETFWGChair1.\"sming\" = "David Durham"
45  *      nsIETFWGChair1."sming" = "David Durham"
46  *  
47  *      % snmpwalk localhost netSnmpIETFWGTable
48  *      nsIETFWGChair1."sming" = "David Durham"
49  *      nsIETFWGChair1."snmpv3" = "Russ Mundy"
50  *      nsIETFWGChair2."snmpv3" = "David Harrington"
51  *
52  *      In your snmpd.conf file, put the following line:
53  *      add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
54  *
55  *      % snmpwalk localhost netSnmpIETFWGTable
56  *      nsIETFWGChair1.\"eos\" = "Glenn Waters"
57  *      nsIETFWGChair1.\"snmpv3\" = "Russ Mundy"
58  *      nsIETFWGChair2.\"eos\" = "Dale Francisco"
59  *      nsIETFWGChair2.\"snmpv3\" = "David Harrington"
60  *  </pre>
61  */
62
63 /*
64  * start be including the appropriate header files 
65  */
66 #include <net-snmp/net-snmp-config.h>
67 #include <net-snmp/net-snmp-includes.h>
68 #include <net-snmp/agent/net-snmp-agent-includes.h>
69
70 /*
71  * our initialization routine, automatically called by the agent 
72  */
73 /*
74  * (to get called, the function name must match init_FILENAME() 
75  */
76 void
77 init_data_set(void)
78 {
79     netsnmp_table_data_set *table_set;
80     netsnmp_table_row *row;
81
82     /*
83      * the OID we want to register our integer at.  This should be the
84      * * OID node for the entire table.  In our case this is the
85      * * netSnmpIETFWGTable oid definition 
86      */
87     oid             my_registration_oid[] =
88         { 1, 3, 6, 1, 4, 1, 8072, 2, 2, 1 };
89
90     /*
91      * a debugging statement.  Run the agent with -Dexample_data_set to see
92      * * the output of this debugging statement. 
93      */
94     DEBUGMSGTL(("example_data_set",
95                 "Initalizing example dataset table\n"));
96
97     /*
98      * It's going to be the "working group chairs" table, since I'm
99      * * sitting at an IETF convention while I'm writing this.
100      * *
101      * *  column 1 = index = string = WG name
102      * *  column 2 = string = chair #1
103      * *  column 3 = string = chair #2  (most WGs have 2 chairs now)
104      */
105
106     table_set = netsnmp_create_table_data_set("netSnmpIETFWGTable");
107
108     /*
109      * allow the creation of new rows via SNMP SETs 
110      */
111     table_set->allow_creation = 1;
112
113     /*
114      * set up what a row "should" look like, starting with the index 
115      */
116     netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR);
117
118     /*
119      * define what the columns should look like.  both are octet strings here 
120      */
121     netsnmp_table_set_multi_add_default_row(table_set,
122                                             /*
123                                              * column 2 = OCTET STRING,
124                                              * writable = 1,
125                                              * default value = NULL,
126                                              * default value len = 0 
127                                              */
128                                             2, ASN_OCTET_STR, 1, NULL, 0,
129                                             /*
130                                              * similar 
131                                              */
132                                             3, ASN_OCTET_STR, 1, NULL, 0,
133                                             0 /* done */ );
134
135     /*
136      * register the table 
137      */
138     /*
139      * if we wanted to handle specific data in a specific way, or note
140      * * when requests came in we could change the NULL below to a valid
141      * * handler method in which we could over ride the default
142      * * behaviour of the table_dataset helper 
143      */
144     netsnmp_register_table_data_set(netsnmp_create_handler_registration
145                                     ("netSnmpIETFWGTable", NULL,
146                                      my_registration_oid,
147                                      OID_LENGTH(my_registration_oid),
148                                      HANDLER_CAN_RWRITE), table_set, NULL);
149
150
151     /*
152      * create the a row for the table, and add the data 
153      */
154     row = netsnmp_create_table_data_row();
155     /*
156      * set the index to the IETF WG name "snmpv3" 
157      */
158     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpv3",
159                                 strlen("snmpv3"));
160
161
162     /*
163      * set column 2 to be the WG chair name "Russ Mundy" 
164      */
165     netsnmp_set_row_column(row, 2, ASN_OCTET_STR,
166                            "Russ Mundy", strlen("Russ Mundy"));
167     netsnmp_mark_row_column_writable(row, 2, 1);        /* make writable via SETs */
168
169     /*
170      * set column 3 to be the WG chair name "David Harrington" 
171      */
172     netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "David Harrington",
173                            strlen("David Harrington"));
174     netsnmp_mark_row_column_writable(row, 3, 1);        /* make writable via SETs */
175
176     /*
177      * add the row to the table 
178      */
179     netsnmp_table_dataset_add_row(table_set, row);
180
181 #ifdef ADD_MORE_DATA
182     /*
183      * add the data, for the second row 
184      */
185     row = netsnmp_create_table_data_row();
186     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpconf",
187                                 strlen("snmpconf"));
188     netsnmp_set_row_column(row, 2, ASN_OCTET_STR, "David Partain",
189                            strlen("David Partain"));
190     netsnmp_mark_row_column_writable(row, 2, 1);        /* make writable */
191     netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "Jon Saperia",
192                            strlen("Jon Saperia"));
193     netsnmp_mark_row_column_writable(row, 3, 1);        /* make writable */
194     netsnmp_table_dataset_add_row(table_set, row);
195 #endif
196
197     /*
198      * Finally, this actually allows the "add_row" token it the
199      * * snmpd.conf file to add rows to this table.
200      * * Example snmpd.conf line:
201      * *   add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
202      */
203     netsnmp_register_auto_data_table(table_set, NULL);
204
205     DEBUGMSGTL(("example_data_set", "Done initializing.\n"));
206 }