Additional SNMP library output formatting options - #21502
Conversation
0a5be4a to
443eb75
Compare
443eb75 to
1de112d
Compare
|
The net-snmp library has quite a few options for controlling how OIDs and values are displayed. PHP already has a number of examples of these: The base purpose of this PR is to extend the number of options available to include all options supported by the net-snmp library. The SNMP library configuration is stored in global memory, so some of the code takes care of handling the state of the library when using the PHP SNMP object. The implementation is to add 3 new functions that control all options for MIB parsing, string output and value output. These do duplicate the existing functions like snmp_set_enum_print(), snmp_set_quick_print(), but having both doesn't cause any issues. I think the new interface is more flexible (we only need a new ENUM for each new options instead of a get/set), and there is no harm in having both functions available for existing options. There is an option to deprecate the old functions if you want to standardise the way people control the library (and also to add get functions to read the current state of a given option). I also added all new options as parameters to the PHP SNMP object. Whenever a SNMP query is run from a PHP SNMP object, the existing state of the net-snmp library needs to be saved, then updated to match what the SNMP object wants, and finally restored back to the original state. I have created 2 functions (save_snmplib_output_options() and set_snmplib_output_options()) to make it easier to save and restore the state. I also discovered that changes to the SNMP library state were persisting across PHP-FPM requests. I did this by running a script that updated the state, then running another script many times that performed a SNMP query and printed the output. The query script would produce inconsistent output depending on which FPM process handles the request. This led me to add PHP_RINIT_FUNCTION() and PHP_RSHUTDOWN_FUNCTION() code into the SNMP module to save the state of the SNMP library before a FPM request, and then restore it at the end. |
| /** | ||
| * @var int | ||
| * @cvalue NETSNMP_STRING_OUTPUT_GUESS | ||
| */ | ||
| const SNMP_STRING_OUTPUT_GUESS = UNKNOWN; | ||
| /** | ||
| * @var int | ||
| * @cvalue NETSNMP_STRING_OUTPUT_ASCII | ||
| */ | ||
| const SNMP_STRING_OUTPUT_ASCII = UNKNOWN; | ||
| /** | ||
| * @var int | ||
| * @cvalue NETSNMP_STRING_OUTPUT_HEX | ||
| */ | ||
| const SNMP_STRING_OUTPUT_HEX = UNKNOWN; |
There was a problem hiding this comment.
Can these be combined? If not maybe an enum is better so there is type safety.
There was a problem hiding this comment.
What do you mean by type safety? I've extended the original technique used in the php-snmp code to copy the NETSNMP* enums directly so I can use pass them through to the netsnmp library calls. All values are checked before being used.
There was a problem hiding this comment.
What I mean was to use a PHP enum that translates to the C values (by using and use Z_PARAM_ENUM), rather than passing an int. It's type safety for the end user writing PHP code.
But if those contants can be bitmasked, that technique doesn't work.
There was a problem hiding this comment.
How does this look now?
The only issue I can see is that the proposal was to merge this into the next release for all current versions, but Z_PARAM_ENUM() is not implemented in PHP 8.4 (and 8.5?)
… return type of zend_result
a23a795 to
a468bbf
Compare
…llback to allow integers
…ing the enum field
RFC: https://wiki.php.net/rfc/snmp_improvements_2026
The SNMP library has more formatting options than php-snmp allows. This PR adds some additional functions to control how the net-snmp library returns results to PHP.