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