Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1 | Note, this is based on the text from a web page, which can be found in |
Wes Hardaker | 6241ebf | 2001-04-06 19:12:28 +0000 | [diff] [blame] | 2 | the documentation section of the http://www.net-snmp.org web page. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 3 | |
| 4 | Extending the UCD-SNMP agent |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 5 | ============================ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 6 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 7 | This document describes the procedure for writing code to extend |
Dave Shield | 1d65285 | 2003-05-27 15:56:46 +0000 | [diff] [blame] | 8 | the functionality of the v4 UCD-SNMP network management agent. |
Dave Shield | 658d54c | 2004-11-17 14:49:38 +0000 | [diff] [blame] | 9 | Modules written using this procedure should also work with the v5 |
| 10 | Net-SNMP agent, though such modules would not take advantage of the |
| 11 | new handler-based helper mechanism. See the on-line documentation |
| 12 | for more information and examples of the newer approach. |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 13 | We would be very interested in comment and feedback about how useful |
Dave Shield | 658d54c | 2004-11-17 14:49:38 +0000 | [diff] [blame] | 14 | (or otherwise) you find this description, and ways in which it could |
| 15 | be improved. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 16 | |
| 17 | The information is designed to be read in order - the structure being: |
| 18 | |
| 19 | 1. Overview & Introduction |
| 20 | 2. MIB files, and how they relate to the agent implementation |
| 21 | 3. Header files |
| 22 | 4. The basic structure of module implementation code |
| 23 | 5. The details of non-table based implementations |
| 24 | 6. The details of simple table based implementations |
| 25 | 7. The details of more general table based implementations |
| 26 | 8. How to implement SET-able variables |
| 27 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 28 | While the document is intended to be generally self-contained, |
| 29 | it does occasionally refer to code files shipped with the main UCD |
| 30 | distribution (in particular the example module), and it may prove |
| 31 | useful to have these files available for reference. |
| 32 | |
| 33 | 1. How to write a Mib module |
| 34 | ============================ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 35 | |
| 36 | Introduction |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 37 | ------------ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 38 | |
| 39 | The design of the UCD SNMP agent has always been shaped by the desire to be |
| 40 | able to extend its functionality by adding new modules. One of the earliest |
| 41 | developments from the underlying CMU code base was the ability to call |
| 42 | external scripts, and this is probably the simplest method of extending the |
| 43 | agent. |
| 44 | However, there are circumstances where such an approach is felt to be |
| 45 | inappropriate - perhaps from considerations of speed, access to the |
| 46 | necessary data, reliability or elegance. In such cases, the obvious solution |
| 47 | is to provide C code that can be compiled into the agent itself to implement |
| 48 | the desired module. Many of the more recent developments in the code |
| 49 | structure have been intended to ease this process. In particular, one of the |
| 50 | more recent additions to the suite is the tool mib2c. This is designed to |
| 51 | take a portion of the MIB tree (as defined by a MIB file) and generate the |
| 52 | code skeleton necessary to implement this. This document will cover the use |
| 53 | mib2c, as well as describing the requirements and functionality of the code |
| 54 | in more detail. |
| 55 | |
| 56 | In order to implement a new MIB module, three files are necessary, and these |
| 57 | will be considered in turn. Note that, by the very nature of the task, this |
| 58 | document cannot cover the details of precisely how to obtain the necessary |
| 59 | information from the operating system or application. Instead, it describes |
| 60 | the code framework that is needed, freeing the implementer from needing to |
| 61 | understand the detailed internals of the agent, and allowing them to |
| 62 | concentrate on the particular problem in hand. |
| 63 | |
| 64 | It may prove useful to examine some of the existing module implementations |
| 65 | and examples in the light of this description, and suitable examples will be |
| 66 | referred to at the appropriate points. However, it should be remembered that |
| 67 | the UCD agent seeks to support a wide variety of systems, often with |
| 68 | dramatically differing implementations and interfaces, and this is reflected |
| 69 | in the complexity of the code. Also, the agent has developed gradually over |
| 70 | the years, and there is often some measure of duplication or redundancy as a |
| 71 | result. |
| 72 | As the FAQ states, the official slogan of the UCD-SNMP developers is |
| 73 | |
| 74 | The current implementation is non-obvious and may need to be |
| 75 | improved. |
| 76 | |
| 77 | This document describes the ideal, straightforward cases - real life is |
| 78 | rarely so simple, and the example modules may prove easier to follow at a |
| 79 | first reading. |
| 80 | It is also advisable to have a compiled and installed implementation |
| 81 | available before starting to extend the agent. This will make debugging and |
| 82 | testing the agent much easier. |
| 83 | |
| 84 | A note regarding terminology - the word "module" is widely used throughout |
| 85 | this document, with a number of different meanings. |
| 86 | |
| 87 | * support for a new MIB, |
| 88 | i.e. the whole of the functionality that is required. This is usually |
| 89 | termed a MIB module; |
| 90 | * a self-contained subset of this, implemented as a single unit. |
| 91 | This is usually termed an implementation module (or simply "a module"); |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 92 | * the combination of such subsets, usually termed a module group. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 93 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 94 | Note that the first and third of these are often synonymous - the |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 95 | difference being that a MIB module refers to the view from outside the |
| 96 | agent, regarding this as a seamless whole and hiding the internal |
| 97 | implementation. A "module group" is used where the internal structure is of |
| 98 | more relevance, and recognises the fact that the functionality may be |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 99 | provided by a number of co-operating implementation modules. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 100 | |
| 101 | Anyway, enough waffle - on with the details: The three files needed are |
| 102 | |
| 103 | * a MIB definition file; |
| 104 | * a C header file; |
| 105 | * a C implementation file. |
| 106 | |
| 107 | The next part looks at the MIB definition file, and how this impacts on the |
| 108 | agent implementation. |
| 109 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 110 | 2. The MIB File |
| 111 | =============== |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 112 | |
| 113 | The first file needed is the MIB file that defines the MIB module to be |
| 114 | implemented. |
| 115 | Strictly speaking, this is not absolutely necessary, as the agent itself |
| 116 | does not make any direct use of the MIB definitions. However, it is |
| 117 | advisable to start with this for three reasons: |
| 118 | |
| 119 | * It provides an initial specification for what is to be implemented. |
| 120 | Code development is always easier if you know what you are meant to be |
| 121 | writing! |
| 122 | * If the new MIB file is read in with the other MIB files, |
| 123 | this lets the applications provided with the suite be used to test the |
| 124 | new agent, and report (hopefully meaningful) symbolic OIDs and values, |
| 125 | rather than the bare numeric forms. |
| 126 | (N.B: Remember to tell the application to load the new MIB. See the |
| 127 | relevant question in the FAQ) |
| 128 | * The tool mib2c uses this description to produce the two code files. |
| 129 | This is by far the easiest way to develop a new module. |
Dave Shield | 658d54c | 2004-11-17 14:49:38 +0000 | [diff] [blame] | 130 | (Note that the v5 version of mib2c is generally similar, but does |
| 131 | not correspond exactly to the v4 version described here) |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 132 | |
| 133 | If the intention is to implement a 'standard' MIB module, or a |
| 134 | vendor-specific one, then the construction of this file will have already |
| 135 | been done for you. If the intention is to provide a totally new, private |
| 136 | module, then you will need to write this yourself, in addition to the agent |
| 137 | code files. |
| 138 | A description of MIB file format and syntax is beyond the scope of this |
| 139 | document, and most books on SNMP management should provide some information |
| 140 | on this subject. One book which concentrates on this is |
| 141 | |
| 142 | Understanding SNMP MIBS |
| 143 | (Perkins & McGinnis, Prentice Hall, ISBN 0-13-437708-7). |
| 144 | |
| 145 | This blatant plug is wholly unrelated to the fact that David Perkins is an |
| 146 | active member of the development group, and is regarded as our resident |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 147 | "protocol guru and policeman". (In fact, this book concentrates on MIB |
| 148 | files in rather more detail than is appropriate in more general SNMP works). |
| 149 | Information on other books covering SNMP and Network Management more generally |
| 150 | is available on the SimpleWeb site (among other places). |
| 151 | See the FAQ for more details. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 152 | |
| 153 | Assigned OID numbers |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 154 | -------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 155 | |
| 156 | One word of advice - even if you are developing a totally private MIB |
| 157 | module, you will still need to position this somewhere within the overall |
| 158 | MIB tree. Please do NOT simply choose a location "at random". Any such is |
| 159 | likely to have either been assigned to some other organisation, or may be so |
| 160 | assigned some time in the future. However much you may regard your project |
| 161 | as a totally internal affair, such projects have a tendency to exceed their |
| 162 | expected scope, both in terms of lifetime and distribution (not to mention |
| 163 | the potential OID clash if you subsequently need to use elements from the |
| 164 | legitimate owner's tree). |
| 165 | It is simple and cheap (i.e. free!) to obtain your own official segment of |
| 166 | the MIB tree (see http://www.iana.org for an application form), and having |
| 167 | done so, you then have complete global authority over it. If you have |
| 168 | problems with this, it's worth contacting the development team (email: |
Wes Hardaker | 36091e5 | 2000-12-02 00:35:27 +0000 | [diff] [blame] | 169 | net-snmp-coders@lists.sourceforge.net) for advice. Please do think to the |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 170 | future, and be a good Net citizen by using a legitimately assigned OID as |
| 171 | the root of your new MIB. |
| 172 | |
| 173 | MIB division |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 174 | ------------ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 175 | |
| 176 | The next point to consider, whether writing by hand or using mib2c, |
| 177 | implementing an existing MIB, or writing a new one, is whether and how to |
| 178 | divide up the MIB tree. This is a purely internal implementation decision, |
| 179 | and will not be visible to management applications querying the agent. A |
| 180 | sensible choice of partitioning will result in a simpler, clearer |
| 181 | implementation, which should ease both the initial development and |
| 182 | subsequent maintenance of the module. |
| 183 | Unfortunately, this choice is one of the module-specific decisions, so must |
| 184 | be made on a case-by-case basis. For a simple, self-contained module, it may |
| 185 | well be reasonable to implement the module as a single block (examples |
| 186 | include the SNMP statistics subtree RFC 1907 or the TCP subtree RFC 2011). |
| 187 | More complex and diverse modules (such as the Host Resources MIB - RFC 1514) |
| 188 | are more naturally considered as a number of individual sub-modules. |
| 189 | Some guidelines to bear in mind when deciding on this division: |
| 190 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 191 | * A MIB sub-tree consisting purely of scalar objects with a common |
| 192 | OID prefix would normally be handled in a single implementation module; |
| 193 | * Separate scalar subtrees would normally be in different implementation |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 194 | modules; |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 195 | * A table can either be handled within the same implementation module |
| 196 | as related scalar objects in the same subtree, or in a separate |
| 197 | implementation module; |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 198 | * Variables that rely on the same underlying data structure to retrieve |
| 199 | their values, should probably be in the same implementation module (and |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 200 | conversely, (though less so) those that don't, shouldn't). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 201 | |
| 202 | As an initial rule of thumb, a good initial division is likely to be |
| 203 | obtained by treating each table and each scalar sub-tree separately. This |
| 204 | can be seen in the current agent, where most of the MIB-II modules (RFC |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 205 | 1213) are implemented in separate files (see the files under mibgroup/mibII). |
| 206 | Note that many of these combine scalar and table handling in the same file, |
| 207 | though they are implemented using separate routines. |
| 208 | This is also the approach used by mib2c, which constructs a single pair of |
| 209 | code files, but uses a separate routine for each table (and another for all |
| 210 | the scalar variables). |
| 211 | Ultimately, the final consideration (concerning the underlying data) is |
| 212 | the most important, and should guide the basic division. For example, the |
| 213 | Host Resources Running Software and Running Software Performance modules, |
| 214 | while separate in the MIB tree, use the same underlying kernel data and so |
| 215 | are implemented together. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 216 | |
| 217 | MIB name |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 218 | -------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 219 | |
| 220 | The final requirement at this stage is to choose a name for each |
| 221 | implementation module. This should be reasonably short, meaningful, unique |
| 222 | and unlikely to clash with other (existing or future) modules. Mib2c uses |
| 223 | the label of the root node of the MIB sub-tree as this name, and this is a |
| 224 | reasonable choice in most cases. |
| 225 | Recent changes to the agent code organisation have introduced the idea of |
| 226 | module groups of related implementation modules. This is used, for example, |
| 227 | to identify the constituent modules of a 'split' MIB (such as the Host |
| 228 | Resources MIB), or those relating to a particular organisation (such as |
| 229 | UCD). |
| 230 | As with the division, this naming and grouping is a purely internal matter, |
| 231 | and is really only visible when configuring and compiling the agent. |
| 232 | |
| 233 | So much for the MIB file. The next part considers the C header file. |
| 234 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 235 | 3. The C code header file |
| 236 | ========================= |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 237 | |
| 238 | If the MIB file is the definition of the module for external network |
| 239 | management applications (where applications includes network management |
| 240 | personnel!), then the header file has traditionally served effectively the |
| 241 | same purpose for the agent itself. |
| 242 | Recent changes to the recommended code structure has resulted in the header |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 243 | file becoming increasingly simpler. It now simply contains definitions of the |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 244 | publically visible routines, and can be generated completely by mib2c. |
| 245 | |
| 246 | Function prototypes |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 247 | ------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 248 | |
| 249 | For those interested in the details of this file (for example, if coding a |
| 250 | module by hand), then the details of these definitions are as follows. Every |
| 251 | header file will have the following two function prototype definitions |
| 252 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 253 | extern void init_example (void); |
| 254 | extern FindVarMethod var_example; |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 255 | |
| 256 | If the module includes any tables, or other collections of variables that |
| 257 | are implemented in separate routines, then this second definition will be |
| 258 | repeated for each of these. |
| 259 | In addition, if any of the variables can be SET (and it is intended to |
| 260 | implement them as such), there will be a function prototype definitions for |
| 261 | each of these, of the form: |
| 262 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 263 | extern WriteMethod write_varName; |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 264 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 265 | These prototypes are in fact typedef'ed in <agent/snmp_vars.h>. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 266 | |
Dave Shield | 658d54c | 2004-11-17 14:49:38 +0000 | [diff] [blame] | 267 | Module dependencies |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 268 | ------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 269 | |
| 270 | This header file is also used to inform the compilation system of any |
| 271 | dependancies between this module and any others. There is one utility module |
| 272 | which is required by almost every module, and this is included using the |
| 273 | directive |
| 274 | |
Dave Shield | 658d54c | 2004-11-17 14:49:38 +0000 | [diff] [blame] | 275 | config_require( util_funcs ) |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 276 | |
| 277 | (which is produced automatically by mib2c). This same syntax can be used to |
| 278 | trigger the inclusion of other related modules. An example of this can be |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 279 | seen in mibII/route_write.h which relies on the mibII/ip module, thus: |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 280 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 281 | config_require( mibII/ip ) |
| 282 | |
| 283 | One use of this directive is to define a module group, by supplying a header |
| 284 | file consisting exclusively of such config_require directives. It can then |
| 285 | be included or excluded from the agent very simply. Examples of this can be |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 286 | seen in mibgroup/mibII.h or mibgroup/host.h, which list the consituent |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 287 | sub-modules of the MIB-II and Host Resources MIBs respectively. |
| 288 | |
| 289 | MIB file information |
| 290 | -------------------- |
| 291 | |
| 292 | Most of the information in this file is (understandably) aimed at the network |
| 293 | management agent itself. However, there is one common header file directive |
| 294 | that is actually intended to affect the utility commands that are included |
| 295 | within the full distribution: |
| 296 | |
| 297 | config_add_mib( HOST-RESOURCES-MIB ) |
| 298 | |
| 299 | This is used to add the MIB file being implemented to the default list of |
| 300 | MIBs loaded by such commands. This means that querying the agent will return |
| 301 | informative names and values, rather than the raw numeric forms that SNMP |
| 302 | actually works with. Of course, it is always possible for the utilities |
| 303 | to specify that this MIB should be loaded anyway. But specifying this file |
| 304 | within the module header file is a useful hint that a particular MIB should |
| 305 | be loaded, without needing to ask for it explicitly. |
| 306 | Note that this will only affect the binaries compiled as part of the same |
| 307 | configuration run. It will have no effect on pre-installed binaries, or |
| 308 | those compiled following a different configuration specification. |
| 309 | |
| 310 | Magic Numbers |
| 311 | ------------- |
| 312 | |
| 313 | The other common element within the header file defines a set of "magic |
| 314 | numbers" - one for each object within the implementation module. In fact, |
| 315 | this can equally well appear within the main code file, as part of the |
| 316 | variable structure (which will be described in the next part). |
| 317 | This is the technique used by mib2c, but most handcrafted modules have |
| 318 | tended to define these as part of the header file, probably for clarity. |
| 319 | |
| 320 | The only necessity is that the names and values are distinct (or more |
| 321 | precisely, the values are distinct within a single variable handling routine). |
| 322 | In practise, they tend to be defined using integers incrementing from 1, |
| 323 | or as the same as the final sub-identifier of the corresponding MIB object |
| 324 | (or indeed both, as these are frequently themselves successive integers). |
| 325 | This is not mandatory, and a counter-example can be seen in the |
| 326 | example module, where two of the object form a sub-tree, and the corresponding |
| 327 | magic numbers are based on the final *two* sub-identifiers (to ensure that |
| 328 | the values are unique). But this construction is definitely unusual, and |
| 329 | the majority of modules simply use successive integers. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 330 | |
| 331 | Header file protection |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 332 | ---------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 333 | |
| 334 | Normally, the only other contents of the header file will be the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 335 | #ifndef/#define/#endif statements surrounding the whole file. This is used |
| 336 | to ensure that the header file is only included once by any source code file |
| 337 | (or more accurately, that there is no effect if it is inadvertantly included |
| 338 | a second time). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 339 | Again, as with the rest of the header file, this is generated automatically |
| 340 | by mib2c. |
| 341 | |
| 342 | Having finished all the preparatory work (or let mib2c deal with it), the |
| 343 | next part starts to look at the code file that actually implements the |
| 344 | module. |
| 345 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 346 | 4. Core structure of the implementation code |
| 347 | ============================================ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 348 | |
| 349 | The core work of implementing the module is done in the C code file. As |
| 350 | indicated earlier, much of the detail of this will be dependent on the |
| 351 | particular module being implemented, and this can only be described by the |
| 352 | individual programmer concerned. |
| 353 | However, there is a fairly clearly defined framework that the implementation |
| 354 | will need to follow, though this varies slightly depending on the style of |
| 355 | the module being implemented (in particular whether it forms a table or a |
| 356 | series of individual values). The differences will be covered in the |
| 357 | following pages, but we first need to consider the overall shape of the |
| 358 | framework, and the elements that are common to all styles. These are |
| 359 | essentially the compulsory routines, the common header definitions, and |
| 360 | assorted initialisation code. |
| 361 | As with the header file, most of this will be generated automatically by |
| 362 | mib2c. |
| 363 | |
| 364 | Standard includes |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 365 | ----------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 366 | |
| 367 | Certain header files are either compulsory, or required so frequently that |
| 368 | they should be included as a matter of course. These are as follows: |
| 369 | |
| 370 | #include <config.h> // local SNMP configuration details |
| 371 | #include "mib_module_config.h" // list of which modules are supported |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 372 | #if HAVE_STDLIB_H |
| 373 | #include <stdlib.h> |
| 374 | #endif |
Michael Slifcak | 41b8c8f | 1999-11-04 12:33:32 +0000 | [diff] [blame] | 375 | #if HAVE_STRING_H |
| 376 | #include <string.h> |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 377 | #else |
| 378 | #include <strings.h> |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 379 | #endif |
| 380 | |
| 381 | #include <sys/types.h> |
| 382 | |
| 383 | All of these will usually be the first files to be included. |
| 384 | |
| 385 | #include "mibincl.h" // Standard set of SNMP includes |
| 386 | #include "util_funcs.h" // utility function declarations |
| 387 | #include "read_config.h" // if the module uses run-time |
| 388 | // configuration controls |
| 389 | #include "auto_nlist.h" // structures for a BSD-based |
| 390 | // kernel using nlist |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 391 | #include "system.h" |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 392 | |
| 393 | #include "name.h" // the module-specific header |
| 394 | |
| 395 | These conventionally come at the end of the list of includes. In between |
| 396 | will come all the standard system-provided header files required for the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 397 | library functions used in the file. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 398 | |
| 399 | Module definition |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 400 | ----------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 401 | |
| 402 | Much of the code defining the contents of the MIB has traditionally been |
| 403 | held in the header file. However, much of this has slowly migrated to the |
| 404 | code file, and this is now the recommended location for it (as typified by |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 405 | the output of mib2c). |
| 406 | The main element of this is a variable structure specifying the details of |
| 407 | the objects implemented. This takes the form of an unconstrained array of |
| 408 | type struct variableN (where N is the length of the longest suffix in the |
| 409 | table). Thus |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 410 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 411 | struct variable2 example_variables[] = { |
| 412 | <individual entries go here> |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 415 | Each entry corresponds to one object in the MIB tree (or one column in the |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 416 | case of table entries), and these should be listed in increasing OID order. |
| 417 | A single entry consists of six fields: |
| 418 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 419 | * a magic number (the #defined integer constant described above) |
| 420 | * a type indicator (from the values listed in <snmplib/snmp_impl.h>) |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 421 | * an access indicator (essentially RWRITE or RONLY) |
| 422 | * the name of the routine used to handle this entry |
| 423 | * the length of the OID suffix used, and |
| 424 | * an array of integers specifying this suffix (more on this in a moment) |
| 425 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 426 | Thus a typical variable entry would look like: |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 427 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 428 | { EXAMPLESTRING, ASN_OCTET_STR, RONLY, var_example, 1, {1}} |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 429 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 430 | If the magic numbers have not been defined in the header file, then they |
| 431 | should be defined here, usually comming immediately before the corresponding |
| 432 | variable entry. This is the technique used by mib2c. |
| 433 | |
| 434 | Note that in practise, only certain sizes of the structure variableN |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 435 | are defined (listed in <agent/var_struct.h>), being sufficient to meet the |
| 436 | common requirements. If your particular module needs a non-supported value, |
| 437 | the easiest thing is simply to use the next largest value that is supported. |
| 438 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 439 | The module also needs to declare the location within the MIB tree where |
| 440 | it should be registered. This is done using a declaration of the form |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 441 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 442 | oid example_variables_oid[] = { 1,3,6,1,4,1,2021,254 } |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 443 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 444 | where the contents of the array give the object identifier of the root of |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 445 | the module. |
| 446 | |
| 447 | Module initialisation |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 448 | --------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 449 | |
| 450 | Many modules require some form of initialisation before they can start |
| 451 | providing the necessary information. This is done by providing a routine |
| 452 | called init_{name} (where {name} is the name of the module). |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 453 | This routine is theoretically optional, but in practise is required to |
| 454 | register this module with the main agent at the very least. This specifies |
| 455 | the list of variables being implemented (from the variableN structure) |
| 456 | and declare where these fit into the overall MIB tree. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 457 | |
| 458 | This is done by using the REGISTER_MIB macro, as follows: |
| 459 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 460 | REGISTER_MIB( "example", example_variables, variable2, |
| 461 | example_variables_oid ); |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 462 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 463 | where "example" is used for identification purposed (and is usually the name |
| 464 | being used for the module), example_variables is the structure defining the |
| 465 | variables being implemented, variable2 is the type used for this structure, |
| 466 | and example_variables_oid is the location of the root. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 467 | |
| 468 | In fact, this macro is simply a wrapper round the routine register_mib(), |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 469 | but the details of this can safely be ignored, unless more control over the |
| 470 | registration is required. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 471 | |
| 472 | One common requirement, particularly on older operating systems or for the |
| 473 | more obscure areas of the system, is to be able to read data directly from |
| 474 | kernel memory. The preparation for this is typically done here by one or |
| 475 | more statements of the form |
| 476 | |
| 477 | #ifdef {NAME}_SYMBOL |
| 478 | auto_nlist( {NAME}_SYMBOL, 0, 0); |
| 479 | #endif |
| 480 | |
| 481 | where {NAME}_SYMBOL is defined as part of the system-specific configuration, |
| 482 | to be the name of the appropriate kernel variable or data structure. (The |
| 483 | two 0 values are because the kernel information is simply being primed at |
| 484 | this point - this call will be reused later when the actual values are |
| 485 | required). Note that this is probably the first thing described so far which |
| 486 | isn't provided by mib2c! |
| 487 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 488 | Other possibilities for initialisation may include registering config file |
| 489 | directive handlers (which are documented in the read_config(5) man page), and |
| 490 | registering the MIB module (either in whole or in part) in the sysOR table. |
| 491 | The first of these is covered in the example module, and the second in many |
| 492 | of the other modules within the main UCD distribution. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 493 | |
| 494 | Variable handling |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 495 | ----------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 496 | |
| 497 | The other obligatory routine is that which actually handles a request for a |
| 498 | particular variable instance. This is the routine that appeared in the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 499 | variableN structure, so while the name is not fixed, it should be the same |
| 500 | as was used there. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 501 | This routine has six parameters, which will be described in turn. |
| 502 | |
| 503 | Four of these parameters are used for passing in information about the |
| 504 | request, these being: |
| 505 | |
| 506 | struct variable *vp; |
| 507 | // The entry in the variableN array from the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 508 | // header file, for the object under consideration. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 509 | // Note that the name field of this structure has been |
| 510 | // completed into a fully qualified OID, by prepending |
| 511 | // the prefix common to the whole array. |
| 512 | oid *name; // The OID from the request |
| 513 | int *length; // The length of this OID |
| 514 | int exact; // A flag to indicate whether this is an exact |
| 515 | // request (GET/SET) or an 'inexact' one (GETNEXT) |
| 516 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 517 | Four of the parameters are used to return information about the answer. |
| 518 | The function also returns a pointer to the actual data for the variable |
| 519 | requested (or NULL if this data is not available for any reason). |
| 520 | The other result parameters are: |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 521 | |
| 522 | oid *name; // The OID being returned |
| 523 | int *length; // The length of this OID |
| 524 | int *var_len; // The length of the answer being returned |
| 525 | WriteMethod **write_method; |
| 526 | // A pointer to the SET function for this variable |
| 527 | |
| 528 | Note that two of the parameters (name and length) serve a dual purpose, |
| 529 | being used for both input and output. |
| 530 | |
| 531 | The first thing that this routine needs to do is to validate the request, to |
| 532 | ensure that it does indeed lie in the range implemented by this particular |
| 533 | module. This is done in slightly different ways, depending on the style of |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 534 | the module, so this will be discussed in more detail later. |
| 535 | At the same time, it is common to retrieve some of the information needed |
| 536 | for answering the query. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 537 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 538 | Then the routine uses the Magic Number field from the vp parameter to determine |
| 539 | which of the possible variables being implemented is being requested. This is |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 540 | done using a switch statement, which should have as many cases as there are |
| 541 | entries in the variableN array (or more precisely, as many as specify this |
| 542 | routine as their handler), plus an additional default case to handle an |
| 543 | erroneous call. |
| 544 | Each branch of the switch statement needs to ensure that the return |
| 545 | parameters are filled in correctly, set up a (static) return variable with |
| 546 | the correct data, and then return a pointer to this value. These can be done |
| 547 | separately for each branch, or once at the start, being overridden in |
| 548 | particular branches if necessary. |
| 549 | |
| 550 | In fact, the default validation routines make the assumption that the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 551 | variable is both read-only, and of integer type (which includes the COUNTER |
| 552 | and GAUGE types among others), and set the return paramaters write_method and |
| 553 | var_len appropriately. These settings can then be corrected for those cases |
| 554 | when either or both of these assumptions are wrong. Examples of this can be |
| 555 | seen in the example module. |
| 556 | EXAMPLEINTEGER is writeable, so this branch sets the write_method parameter, |
| 557 | and EXAMPLEOBJECTID is not an integer, so this branch sets the var_len |
| 558 | parameter. In the case of EXAMPLESTRING, both assumptions are wrong, so this |
| 559 | branch needs to set both these parameters explicitly. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 560 | |
| 561 | Note that because the routine returns a pointer to a static result, a |
| 562 | suitable variable must be declared somewhere for this. Two global variables |
| 563 | are provided for this purpose - long_return (for integer results) and |
| 564 | return_buf (for other types). This latter is a generic array (of type |
| 565 | u_char) that can contain up to 256 bytes of data. Alternatively, static |
| 566 | variables can be declared, either within the code file, or local to this |
| 567 | particular variable routine. This last is the approach adopted by mib2c, |
| 568 | which defines four such local variables, (long_ret, string, objid and c64). |
| 569 | |
| 570 | Mib2c requirements |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 571 | ------------------ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 572 | |
| 573 | Most of the code described here is generated by mib2c. The main exceptions |
| 574 | (which therefore need to be provided by the programmer) are |
| 575 | |
| 576 | * Any initialisation, other than the basic registration |
| 577 | (including kernel data initialisation, config file handling, or sysOR |
| 578 | registration). |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 579 | * Retrieving the necessary data, and setting the appropriate return |
| 580 | value correctly. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 581 | * The var_len (and possibly write_method) return parameters for variable |
| 582 | types that are not recognised by mib2c |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 583 | * The contents of any write routines (see later). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 584 | |
| 585 | Everything else should be useable as generated. |
| 586 | |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 587 | This concludes the preliminary walk-through of the general structure of the |
| 588 | C implementation. To fill in the details, we will need to consider the |
| 589 | various styles of module separately. The next part will look at scalar (i.e. |
| 590 | non-table based) modules. |
| 591 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 592 | 5. Non-table-based modules |
| 593 | ========================== |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 594 | |
| 595 | Having looked at the general structure of a module implementation, it's now |
| 596 | time to look at this in more detail. We'll start with the simplest style of |
| 597 | module - a collection of independent variables. This could easily be |
| 598 | implemented as a series of completely separate modules - the main reason for |
| 599 | combining them is to avoid the proliferation of multiple versions of very |
| 600 | similar code. |
| 601 | |
| 602 | Recall that the variable handling routine needs to cover two distinct |
| 603 | purposes - validation of the request, and provision of the answer. In this |
| 604 | style of module, these are handled separately. Once again, mib2c does much |
| 605 | of the donkey work, generating the whole of the request validation code (so |
| 606 | the description of this section can be skipped if desired), and even |
| 607 | providing a skeleton for returning the data. This latter still requires some |
| 608 | input from the programmer, to actually return the correct results (rather |
| 609 | than dummy values). |
| 610 | |
| 611 | Request Validation |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 612 | ------------------ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 613 | |
| 614 | This is done using a standard utility function header_generic. The |
| 615 | parameters for this are exactly the same as for the main routine, and are |
| 616 | simply passed through directly. It returns an integer result, as a flag to |
| 617 | indicate whether the validation succeeded or not. |
| 618 | If the validation fails, then the main routine should return immediately, |
| 619 | leaving the parameters untouched, and indicate the failure by returning a |
| 620 | NULL value. Thus the initial code fragment of a scalar-variable style |
| 621 | implementation will typically look like: |
| 622 | |
| 623 | u_char * |
| 624 | var_system(vp, name, length, exact, var_len, write_method) |
| 625 | { |
| 626 | if (header_generic(vp, name, length, exact, var_len, write_method) |
| 627 | == MATCH_FAILED ) |
| 628 | return NULL; |
| 629 | |
| 630 | [ etc, etc, etc ] |
| 631 | } |
| 632 | |
| 633 | Although the utility function can be used as a "black box", it's worth |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 634 | looking more closely at exactly what it does (since the table-handling |
| 635 | modules will need to do something fairly similar). It has two (or possibly |
| 636 | three) separate functions: |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 637 | |
| 638 | * checking that the request is valid, |
| 639 | * setting up the OID for the result, |
| 640 | * and (optionally) setting up default values for the other return |
| 641 | parameters. |
| 642 | |
| 643 | In order to actually validate the request, the header routine first needs to |
| 644 | construct the OID under consideration, in order to compare it with that |
| 645 | originally asked for. The driving code has already combined the OID prefix |
| 646 | (constant throughout the module) with the entry-specific suffix, before |
| 647 | calling the main variable handler. This is available via the name field of |
| 648 | the parameter vp. For a scalar variable, completing the OID is therefore |
| 649 | simply a matter of appending the instance identifier 0 to this. The full OID |
| 650 | is built up in a local oid array newname defined for this purpose. |
| 651 | This gives the following code fragment: |
| 652 | |
| 653 | int |
| 654 | header_generic(vp, name, length, exact, var_len, write_method) |
| 655 | { |
Wes Hardaker | 84f4a45 | 1999-03-10 23:14:17 +0000 | [diff] [blame] | 656 | oid newname[MAX_OID_LEN]; |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 657 | |
| 658 | memcpy((char *)newname, (char *)vp->name, |
| 659 | (int)vp->namelen * sizeof(oid)); |
| 660 | newname[ vp->namelen ] = 0; |
| 661 | |
| 662 | : |
| 663 | } |
| 664 | |
| 665 | Having formed the OID, this can then be compared against the variable |
| 666 | specified in the original request, which is available as the name parameter. |
| 667 | This comparison is done using the snmp_oid_compare function, which takes the |
| 668 | two OIDs (together with their respective lengths), and returns -1, 0 or 1 |
| 669 | depending on whether the first OID precedes, matches or follows the second. |
| 670 | |
| 671 | In the case of an 'exact' match (i.e. a GET/SET/etc), then the request is |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 672 | only valid if the two OIDs are identical (snmp_oid_compare returns 0). In |
| 673 | the case of a GETNEXT (or GETBULK) request, it's valid if the OID being |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 674 | considered comes after that of the original request (snmp_oid_compare |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 675 | returns -1). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 676 | |
| 677 | This gives the code fragment |
| 678 | |
| 679 | result = snmp_oid_compare(name, *length, newname, (int)vp->namelen + 1); |
| 680 | // +1 because of the extra instance sub-identifier |
| 681 | if ((exact && (result != 0)) // GET match fails |
| 682 | || (!exact && (result >= 0))) // GETNEXT match fails |
| 683 | return(MATCH_FAILED); |
| 684 | |
| 685 | Note that in this case, we're only interested in the single variable |
| 686 | indicated by the vp parameter. The fact that this module may well implement |
| 687 | other variables as well is ignored. The 'lexically next' requirement of the |
| 688 | GETNEXT request is handled by working through the variable entries in order |
| 689 | until one matches. And yes, this is not the most efficient implementation |
| 690 | possible! |
| 691 | Note that in releases prior to 3.6, the snmp_oid_compare function was called |
| 692 | simply compare. |
| 693 | |
| 694 | Finally, having determined that the request is valid, this routine must |
| 695 | update the name and length parameters to return the OID being processed. It |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 696 | also sets default values for the other two return parameters. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 697 | |
| 698 | memcpy( (char *)name,(char *)newname, |
| 699 | ((int)vp->namelen + 1) * sizeof(oid)); |
| 700 | *length = vp->namelen + 1; |
| 701 | *write_method = 0; // Non-writeable |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 702 | *var_len = sizeof(long); // default to integer results |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 703 | return(MATCH_SUCCEEDED); |
| 704 | |
| 705 | These three code fragments combine to form the full header_generic code |
| 706 | which can be seen in the file util_funcs.c |
| 707 | |
| 708 | Note: This validation used to be done using a separate function for each |
| 709 | module (conventionally called header_{name}), and many modules may still be |
| 710 | coded in this style. The code for these are to all intents and purposes |
| 711 | identical to the header_generic routine described above. |
| 712 | |
| 713 | Data Retrieval |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 714 | -------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 715 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 716 | The other main job of the request handling routine is to retrieve any |
| 717 | necessary data, and return the appropriate answer to the original request. |
| 718 | This must be done even if mib2c is being used to generate the framework of |
| 719 | the implementation. As has been indicated earlier, the different cases are |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 720 | handled using a switch statement, with the Magic Number field of the vp |
| 721 | parameter being used to distinguish between them. |
| 722 | The data necessary for answering the request can be retrieved for each |
| 723 | variable individually in the relevant case statement (as is the case with |
| 724 | the system group), or using a common block of data before processing the |
| 725 | switch (as is done for the ICMP group, among others). |
| 726 | |
| 727 | With many of the modules implemented so far, this data is read from a kernel |
| 728 | structure. This can be done using the auto_nlist routine already mentioned, |
| 729 | providing a variable in which to store the results and an indication of its |
| 730 | size (see the !HAVE_SYS_TCPIPSTATS_H case of the ICMP group for an example). |
| 731 | Alternatively, there may be ioctl calls on suitable devices, specific system |
| 732 | calls, or special files that can be read to provide the necessary |
| 733 | information. |
| 734 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 735 | If the available data provides the requested value immediately, then the |
| 736 | individual branch becomes a simple assignment to the appropriate static |
| 737 | return variable - either one of the global static variables (e.g. long_return) |
| 738 | or the local equivalents (such as generated by mib2c). |
| 739 | Otherwise, the requested value may need to be calculated by combining two or |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 740 | more items of data (e.g. IPINHDRERRORS in mibII/ip.c) or by applying a |
| 741 | mapping or other calculation involving available information (e.g. |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 742 | IPFORWARDING from the same group). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 743 | |
| 744 | In each of these cases, the routine should return a pointer to the result |
| 745 | value, casting this to the pseudo-generic (u_char *) |
| 746 | |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 747 | So much for the scalar case. The next part looks at how to handle simple |
| 748 | tables. |
| 749 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 750 | 6. Simple tables |
| 751 | ================ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 752 | |
| 753 | Having considered the simplest style of module implementation, we now turn |
| 754 | our attention to the next style - a simple table. The tabular nature of |
| 755 | these is immediately apparent from the MIB definition file, but the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 756 | qualifier "simple" deserves a word of explanation. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 757 | A simple table, in this context, has four characteristics: |
| 758 | |
| 759 | 1. It is indexed by a single integer value; |
| 760 | 2. Such indices run from 1 to a determinable maximum; |
| 761 | 3. All indices within this range are valid; |
| 762 | 4. The data for a particular index can be retrieved directly |
| 763 | (e.g. by indexing into an underlying data structure). |
| 764 | |
| 765 | If any of the conditions are not met, then the table is not a pure simple |
| 766 | one, and the techniques described here are not applicable. The next section |
| 767 | of this guide will cover the more general case. (In fact, it may be possible |
| 768 | to use the bulk of the techniques covered here, though special handling will |
| 769 | be needed to cope with the invalid assumption or assumptions). Note that |
| 770 | mib2c assumes that all tables are simple. |
| 771 | |
| 772 | As with the scalar case, the variable routine needs to provide two basic |
| 773 | functions - request validation and data retrieval. |
| 774 | |
| 775 | Validation |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 776 | ---------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 777 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 778 | This is provided by the shared utility routine header_simple_table. As with |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 779 | the scalar header routine, this takes the same parameters as the main |
| 780 | variable routine, with one addition - the maximum valid index. Mib2c |
| 781 | generates a dummy token for this, which must be replaced by the appropriate |
| 782 | value. |
| 783 | As with the header routine, it also returns an indication of whether the |
| 784 | request was valid, as well as setting up the return parameters with the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 785 | matching OID information, and defaults for var_len and write_method. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 786 | Note that in releases prior to 3.6, this job was performed by the routine |
| 787 | checkmib. However, the return values of this were the reverse of those for |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 788 | generic_header and header_simple_table. A version of checkmib is still |
| 789 | available for compatability purposes, but you are encouraged to use |
| 790 | header_simple_table instead. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 791 | |
| 792 | The basic code fragment (see ucd-snmp/disk.c) is therefore of the form: |
| 793 | |
| 794 | unsigned char * |
| 795 | var_extensible_disk(vp, name, length, exact, var_len, write_method) |
| 796 | { |
| 797 | if (header_simple_table(vp,name,length,exact,var_len,write_method,numdisks) |
| 798 | == MATCH_FAILED) |
| 799 | return(NULL); |
| 800 | |
| 801 | [ etc, etc, etc ] |
| 802 | |
| 803 | } |
| 804 | |
| 805 | Note that the maximum index value parameter does not have to be a |
| 806 | permanently fixed constant. It specifies the maximum valid index at the time |
| 807 | the request is processed, and a subsequent request may have a different |
| 808 | maximum. |
| 809 | An example of this can be seen in mibII/sysORTable.c where the table is held |
| 810 | purely internally to the agent code, including its size (and hence the |
| 811 | maximum valid index). This maximum could also be retrieved via a system |
| 812 | call, or via a kernel data variable. |
| 813 | |
| 814 | Data Retrieval |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 815 | -------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 816 | |
| 817 | As with the scalar case, the other required function is to retrieve the data |
| 818 | requested. However, given the definition of a simple table this is simply a |
| 819 | matter of using the single, integer index sub-identifier to index into an |
| 820 | existing data structure. This index will always be the last index of the OID |
| 821 | returned by header_simple_table, so can be obtained as name[*length-1]. |
| 822 | A good example of this type of table can be seen in ucd-snmp/disk.c |
| 823 | |
| 824 | With some modules, this underlying table may be relatively large, or only |
| 825 | accessible via a slow or cumbersome interface. The implementation described |
| 826 | so far may prove unacceptably slow, particularly when walking a MIB tree |
| 827 | requires the table to be loaded afresh for each variable requested. |
| 828 | |
| 829 | In these circumstances, a useful technique is to cache the table when it is |
| 830 | first read in, and use that cache for subsequent requests. This can be done |
| 831 | by having a separate routine to read in the table. This uses two static |
| 832 | variables, one a structure or array for the data itself, and the other an |
| 833 | additional timestamp to indicate when the table was last loaded. When a call |
| 834 | is made to this routine to "read" the table, it can first check whether the |
| 835 | cached table is "new enough". If so, it can return immediately, and the |
| 836 | system will use the cached data. |
| 837 | Only if the cached version is sufficiently old that it's probably out of |
| 838 | date, is it necessary to retrieve the current data, updating the cached |
| 839 | version and the timestamp value. |
| 840 | This is particularly useful if the data itself is relatively static, such as |
| 841 | a list of mounted filesystems. There is an example of this technique in the |
| 842 | Host Resources implementation. |
| 843 | |
| 844 | As with the scalar case, mib2c simply provides placeholder dummy return |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 845 | values. It's up to the programmer to fill in the details. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 846 | |
| 847 | The next part concludes the examination of the detailed implementation by |
| 848 | looking at more general tables. |
| 849 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 850 | 7. General Tables |
| 851 | ================= |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 852 | |
| 853 | Some table structures are not suitable for the simple table approach, due to |
| 854 | the failure of one or more of the assumptions listed earlier. Perhaps they |
| 855 | are indexed by something other than a single integer (such as a 4-octet IP |
| 856 | address), or the maximum index is not easily determinable (such as the |
| 857 | interfaces table), or not all indices are valid (running software), or the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 858 | necessary data is not directly accessible (interfaces again). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 859 | In such circumstances, a more general approach is needed. In contrast with |
| 860 | the two styles already covered, this style of module will commonly combine |
| 861 | the two functions of request validation and data retrieval. Note that mib2c |
| 862 | will assume the simple table case, and this will need to be corrected. |
| 863 | |
| 864 | General table algorithm |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 865 | ----------------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 866 | |
| 867 | The basic algorithm is as follows: |
| 868 | |
| 869 | Perform any necessary initialization, then walk through the |
| 870 | underlying instances, retrieving the data for each one, until the |
| 871 | desired instance is found. If no valid entry is found, return |
| 872 | failure. |
| 873 | |
| 874 | For an exact match (GET and similar), identifying the desired instance is |
| 875 | trivial - construct the OID (from the 'vp' variable parameter and the index |
| 876 | value or values), and see whether it matches the requested OID. |
| 877 | For GETNEXT, the situation is not quite so simple. Depending on the |
| 878 | underlying representation of the data, the entries may be returned in the |
| 879 | same order as they should appear in the table (i.e. lexically increasing by |
| 880 | index). However, this is not guaranteed, and the natural way of retrieving |
| 881 | the data may be in some "random" order. In this case, then the whole table |
| 882 | needs to be traversed for each request. in order to determine the |
| 883 | appropriate successor. |
| 884 | This random order is the worst case, and dictates the structure of the code |
| 885 | used in most currently implemented tables. The ordered case can be regarded |
| 886 | as a simplification of this more general one. |
| 887 | |
| 888 | The algorithm outlined above can now be expanded into the following |
| 889 | pseudo-code: |
| 890 | |
| 891 | Init_{Name}_Entry(); // Perform any necessary initialisation |
| 892 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 893 | while (( index = Get_Next_{Name}_Entry() ) != EndMarker ) { |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 894 | // This steps through the underlying table, |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 895 | // returning the current index, |
| 896 | // or some suitable end-marker when all |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 897 | // the entries have been examined. |
| 898 | // Note that this routine should also return the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 899 | // data for this entry, either via a parameter |
| 900 | // or using some external location. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 901 | |
| 902 | construct OID from vp->name and index |
| 903 | compare new OID and request |
| 904 | if valid { |
| 905 | save current data |
| 906 | if finished // exact match, or ordered table |
| 907 | break; // so don't look at any more entries |
| 908 | |
| 909 | } |
| 910 | |
| 911 | // Otherwise, we need to loop round, and examine |
| 912 | // the next entry in the table. Either because |
| 913 | // the entry wasn't valid for this request, |
| 914 | // or the entry was a possible "next" candidate, |
| 915 | // but we don't know that there isn't there's a |
| 916 | // better one later in the table. |
| 917 | } |
| 918 | |
| 919 | if no saved data // Nothing matched |
| 920 | return failure |
| 921 | |
| 922 | // Otherwise, go on to the switch handling |
| 923 | // we've already covered in the earlier styles. |
| 924 | |
| 925 | This is now very close to the actual code used in many current |
| 926 | implementations (such as the the routine header_ifEntry in |
| 927 | mibII/interfaces.c). Notice that the pseudo-code fragment if valid expands |
| 928 | in practise to |
| 929 | |
| 930 | if ((exact && (result == 0)) || |
| 931 | // GET request, and identical OIDs |
| 932 | (!exact && (result < 0)) ) |
| 933 | // GETNEXT, and candidate OID is later |
| 934 | // than requested OID. |
| 935 | |
| 936 | This is a very common expression, that can be seen in most of the table |
| 937 | implementations. |
| 938 | |
| 939 | Notice also that the interfaces table returns immediately the first valid |
| 940 | entry is found, even for GETNEXT requests. This is because entries are |
| 941 | returned in lexical order, so the first succeeding entry will be the one |
| 942 | that's required. |
| 943 | (As an aside, this also means that the underlying data can be saved |
| 944 | implicitly within the 'next entry' routine - not very clean, but it saves |
| 945 | some unnecessary copying). |
| 946 | |
| 947 | The more general case can be seen in the TCP and UDP tables (see mibII/tcp.c |
| 948 | and mibII/udp.c). Here, the if valid fragment expands to: |
| 949 | |
| 950 | if ( exact && (result == 0)) { |
| 951 | // save results |
| 952 | break; |
| 953 | } |
| 954 | else if (!exact && (result < 0)) { |
| 955 | if ( .... ) { // no saved OID, or this OID |
| 956 | // precedes the saved OID |
| 957 | // save this OID into 'lowest' |
| 958 | // save the results into Lowinpcb |
| 959 | // don't break, since we still need to look |
| 960 | // at the rest of the table |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | The GET match handling is just as we've already seen - is this the requested |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 965 | OID or not. If so, save the results and move on to the switch statement. |
| 966 | The GETNEXT case is more complicated. As well as considering whether this |
| 967 | is a possible match (using the same test we've already seen), we also have to |
| 968 | check whether this is a better match than anything we've already seen. This |
| 969 | is done by comparing the current candidate (newname) with the best match found |
| 970 | so far (lowest). |
| 971 | Only if this extra comparison shows that the new OID is earlier than the |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 972 | saved one, do we need to save both the new OID, and any associated data |
| 973 | (such as the inpcb block, and state flag). But having found one better |
| 974 | match, we don't know that there isn't an even better one later on. So we |
| 975 | can't break out of the enclosing loop - we need to keep going and examine |
| 976 | all the remaining entries of the table. |
| 977 | |
| 978 | These two cases (the TCP and UDP tables) also show a more general style of |
| 979 | indexing. Rather than simply appending a single index value to the OID |
| 980 | prefix, these routines have to add the local four-octet IP address plus port |
| 981 | (and the same for the remote end in the case of the TCP table). This is the |
| 982 | purpose of the op and cp section of code that precedes the comparison. |
| 983 | |
| 984 | These two are probably among the most complex cases you are likely to |
| 985 | encounter. If you can follow the code here, then you've probably cracked the |
| 986 | problem of understanding how the agent works. |
| 987 | |
| 988 | Finally, the next part discusses how to implement a writable (or SETable) |
| 989 | object in a MIB module. |
| 990 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 991 | 8. How to implement a SETable object |
| 992 | ==================================== |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 993 | |
| 994 | Finally, the only remaining area to cover is that of setting data - the |
| 995 | handling of SNMPSET. Particular care should be taken here for two reasons. |
| 996 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 997 | Firstly, any errors in the earlier sections can have limited effect. The |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 998 | worst that is likely to happen is that the agent will either return invalid |
| 999 | information, or possibly crash. Either way, this is unlikely to affect the |
| 1000 | operation of the workstation as a whole. If there are problems in the |
| 1001 | writing routine, the results could be catastrophic (particularly if writing |
| 1002 | data directly into kernel memory). |
| 1003 | |
| 1004 | Secondly, this is the least well understood area of the agent, at least by |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1005 | the author. There are relatively few variables that are defined as READ-WRITE |
| 1006 | in the relevant MIBs, and even fewer that have actually been implemented as |
| 1007 | such. I'm therefore describing this from a combination of my understanding |
| 1008 | of how SETs ought to work, personal experience of very simple SET handling |
| 1009 | and what's actually been done by others (which do not necessarily coincide). |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1010 | |
| 1011 | There are also subtle differences between the setting of simple scalar |
| 1012 | variables (or individual entries within a table), and the creation of a new |
| 1013 | row within a table. This will therefore be considered separately. |
| 1014 | |
| 1015 | With these caveats, and a healthy dose of caution, let us proceed. Note that |
| 1016 | the UCD-SNMP development team can accept no responsibility for any damage or |
| 1017 | loss resulting from either following or ignoring the information presented |
| 1018 | here. You coded it - you fix it! |
| 1019 | |
| 1020 | Write routine |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1021 | ------------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1022 | |
| 1023 | The heart of SET handling is the write_method parameter from the variable |
| 1024 | handling routine. This is a pointer to the relevant routine for setting the |
| 1025 | variable in question. Mib2c will generate one such routine for each setable |
| 1026 | variable. This routine should be declared using the template |
| 1027 | |
| 1028 | int |
| 1029 | write_variable( |
| 1030 | int action, |
| 1031 | u_char *var_val, |
| 1032 | u_char var_val_type, |
| 1033 | int var_val_len, |
| 1034 | u_char *statP, |
| 1035 | oid *name, |
| 1036 | int name_len ); |
| 1037 | |
| 1038 | Most of these parameters are fairly self explanatory: |
| 1039 | The last two hold the OID to be set, just as was passed to the main variable |
| 1040 | routine. |
| 1041 | |
| 1042 | The second, third and fourth parameters provide information about the new |
| 1043 | desired value, both the type, value and length. This is very similar to the |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1044 | way that results are returned from the main variable routine. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1045 | |
| 1046 | The return value of the routine is simply an indication of whether the |
| 1047 | current stage of the SET was successful or not. We'll come back to this in a |
| 1048 | minute. Note that it is the responsibility of this routine to check that the |
| 1049 | OID and value provided are appropriate for the variable being implemented. |
| 1050 | This includes (but is not limited to) checking: |
| 1051 | |
| 1052 | * the OID is recognised as one this routine can handle |
| 1053 | (this should be true if the routine only handles the one variable, and |
| 1054 | there are no errors in the main variable routine or driving code, but |
| 1055 | it does no harm to check). |
| 1056 | * the value requested is the correct type expected for this OID |
| 1057 | * the value requested is appropriate for this OID |
| 1058 | (within particular ranges, suitable length, etc, etc) |
| 1059 | |
| 1060 | There are two parameters remaining to be considered. |
| 1061 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1062 | The fifth parameter, statP, is the value that would be returned from a GET |
| 1063 | request on this particular variable. It could be used to check that the |
| 1064 | requested new value is consistent with the current state, but its main use |
| 1065 | is to denote that a new table row is being created. |
| 1066 | In most cases (particularly when dealing with scalar values or single elements |
| 1067 | of tables), you can normally simply ignore this parameter. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1068 | |
| 1069 | Actions |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1070 | ------- |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1071 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1072 | The final parameter to consider is the first one - action. To understand |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1073 | this, it's necessary to know a bit about how SETs are implemented. |
| 1074 | The design of SNMP calls for all variables in a SET request to be done "as |
| 1075 | if simultaneously" - i.e. they should all succeed or all fail. However, in |
| 1076 | practise, the variables are handled in succession. Thus, if one fails, it |
| 1077 | must be possible to "undo" any changes made to the other variables in the |
| 1078 | request. |
| 1079 | This is a well understood requirement in the database world, and is usually |
| 1080 | implemented using a "multi-stage commit". This is certainly the mechanism |
| 1081 | expected within the SNMP community (and has been made explicit in the work |
| 1082 | of the AgentX extensibility group). In other words, the routine to handle |
| 1083 | setting a variable will be called more than once, and the routine must be |
| 1084 | able to perform the appropriate actions depending on how far through the |
| 1085 | process we currently are. This is determined by the value of the action |
| 1086 | parameter. |
| 1087 | |
| 1088 | This is implemented using three basic phases: |
| 1089 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1090 | RESERVE is used to check the syntax of all the variables provided, that the |
| 1091 | values being set are sensible and consistent, and to allocate any resources |
| 1092 | required for performing the SET. After this stage, the expectation is that |
| 1093 | the set ought to succeed, though this is not guaranteed. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1094 | (In fact, with the UCD agent, this is done in two passes - RESERVE1, and |
| 1095 | RESERVE2, to allow for dependancies between variables). |
| 1096 | |
| 1097 | If any of these calls fail (in either pass) the write routines are called |
| 1098 | again with the FREE action, to release any resources that have been |
| 1099 | allocated. The agent will then return a failure response to the requesting |
| 1100 | application. |
| 1101 | |
| 1102 | Assuming that the RESERVE phase was successful, the next stage is indicated |
| 1103 | by the action value ACTION. This is used to actually implement the set |
| 1104 | operation. However, this must either be done into temporary (persistent) |
| 1105 | storage, or the previous value stored similarly, in case any of the |
| 1106 | subsequent ACTION calls fail. |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1107 | This can be seen in the example module, where both write routines have |
| 1108 | static 'old' variables, to hold the previous value of the relevant object. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1109 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1110 | If the ACTION phase does fail (for example due to an apparently valid, but |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1111 | unacceptable value, or an unforeseen problem), then the list of write |
| 1112 | routines are called again, with the UNDO action. This requires the routine |
| 1113 | to reset the value that was changed to its previous value (assuming it was |
| 1114 | actually changed), and then to release any resources that had been |
| 1115 | allocated. As with the FREE phase, the agent will then return an indication |
| 1116 | of the error to the requesting application. |
| 1117 | |
| 1118 | Only once the ACTION phase has completed successfully, can the final COMMIT |
| 1119 | phase be run. This is used to complete any writes that were done into |
| 1120 | temporary storage, and then release any allocated resources. Note that all |
| 1121 | the code in this phase should be "safe" code that cannot possibly fail (cue |
| 1122 | hysterical laughter). The whole intent of the ACTION/COMMIT division is that |
| 1123 | all of the fallible code should be done in the ACTION phase, so that it can |
| 1124 | be backed out if necessary. |
| 1125 | |
| 1126 | Table row creation |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1127 | ------------------ |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1128 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1129 | What about creating new rows in a table, I hear you ask. Good Question. |
| 1130 | This case can often be detected by the fact that a GET request would have |
| 1131 | failed, and hence the fifth parameter, statP, will be null. This contrasts |
| 1132 | with changing the values of an element of an existing row, when the statP |
| 1133 | parameter would hold the previous value. |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1134 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1135 | The details of precisely how to create a new row will clearly depend on the |
| 1136 | underlying format of the table. However, one implementation strategy would |
| 1137 | be as follows: |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1138 | |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1139 | * The first column object to be SET would return a null value from the |
| 1140 | var_name routine. This null statP parameter would be the signal |
| 1141 | to create a new temporary instance of the underlying data structure, |
| 1142 | filled with dummy values. |
| 1143 | * Subsequent column objects would return pointers to the appropriate |
| 1144 | field of this new data structure from the var_name routine, |
| 1145 | which would then be filled in by the write routine. |
| 1146 | * Once all the necessary fields had been SET, the completed temporary |
| 1147 | instance could be moved into the "standard" structure (or copied, |
| 1148 | or otherwise used to set things up appropriately). |
| 1149 | |
| 1150 | However, this is purely a theoretical strategy, and has not been tried |
| 1151 | by the author. No guarantees are given as to whether this would actually |
| 1152 | work. There are also questions regarding how to handle incomplete |
| 1153 | or overlapping SET requests. |
| 1154 | Anyone who has experience of doing this, please get in touch! |
| 1155 | |
Wes Hardaker | f402945 | 1999-03-10 23:07:05 +0000 | [diff] [blame] | 1156 | ------------------------------------------------------------------------ |
| 1157 | And that's it. Congratulations for getting this far. If you understand |
| 1158 | everything that's been said, then you now know as much as the rest of us |
| 1159 | about the inner workings of the UCD-SNMP agent. (Well, very nearly). |
| 1160 | All that remains is to try putting this into practise. Good luck! |
| 1161 | |
| 1162 | And if you've found this helpful, gifts of money, chocolate, alcohol, and |
| 1163 | above all feedback, would be most appreciated :-) |
| 1164 | |
| 1165 | ------------------------------------------------------------------------ |
Dave Shield | 880692e | 2000-01-31 11:41:08 +0000 | [diff] [blame] | 1166 | Copyright 1999, 2000 - D.T.Shield. |
| 1167 | Not to be distributed without the explicit permission of the author. |