DateandTime is a data type in SNMPv2, which mainly provides a description of date and time. When I use the SNMP development package, I find that it does not provide support for the DATEANDTIME type, and sometimes it needs to be used, so I wrote a DateTime class extension SNMP . The formatting of the DATEANDTIME type can be implemented through this class, and each time item can be easily extracted. The class is described below.
To implement a DateTime class, you first want to understand the definition of the DateandTime type. Below is the definition of the DateandTime in the RFC:
DateandTime :: = OcTet String (Size (8 | 11))
- a Date-Time Specification for the Local Time of Day.
- this data type is intended to provide a consistent
- Method of Reporting Date Information.
-
- Field OcTes Contents Range
- _____ ______ ________ _____
- 1 1-2 year 0..65536
- (in NetWork Byte ORDER)
- 2 3 Month 1..12
- 3 4 day 1..31
- 4 5 Hour 0..23
- 5 6 minutes 0..59
- 6 7 SECONDS 0..60
- (Use 60 for leap-second)
- 7 8 Deci-Seconds 0..9
- 8 9 Direction from UTC " " / "-"
- (in Ascii NOTATION)
- 9 10 Hours from Utc 0..11
- 10 11 minutes from utc 0..59
-
- Note That if Only Local Time Is Known, Then
- Timezone Information (Fields 8-10) Is Not Present INOT PRESENT.
As can be seen from the definition, DateandTime is still the data of the OCTET STRING type, just a specific definition of each byte. For example, the first two bytes represent the year, the fifth byte representation hours. So if there is no DateAndTime type in a development package, you can replace it with an OCTET type. But this is just some meaningless garbled. Therefore, the key to implementation is to make correct parsing of special OCTET data in accordance with definitions. Since DATEANDTIME is also OCTET STRING, we inherit the class DateTime by inheriting through the OctetStr class of SNMP . In this way, the underlying work of extracting data can be handed over to SNMP , which will be based on the OCTET STRING. In SNMP , all SMI data types have a get_printable function that returns a formatted string for program printout. This function is a virtual function, and the specific classes have different implementation methods. If we call the Get_Printable function of the OCTETSTR class to output the DateAndTime type data, it will be garbled like the previously described, so we have to override the function. In addition, each SMI data type class has a get_syntax function that returns a 32-bit integer that represents the current data type code, which is also a virtual function, which also needs to be overridden.
Let's take a look at the definition of the DateTime class:
#include "../snmp_include/oct.h"
#define snmp_syntax_datetime (asn_application | Asn_primitive | 0x09)
/ * This class import the dateandtime type defined in SNMPv2,
The ORINGINAL SNMP Doesn't Directly Support this type * /
Class datetime: public oplicTstr {
CHAR FORMAT_STR [128]; / / Save the formatted time string
Int year;
Int Month;
Int day;
Int Hour;
Int minute;
INT Second;
Int msecond;
PUBLIC:
INT get_year ();
INT GET_MONTH ();
INT GET_DAY ();
INT get_HOUR ();
INT GET_MINUTE ();
INT GET_SECOND ();
INT get_msecond ();
Void format ();
// The Virtual Function Overwrite Get_Printble In Class OcTStr
Smiuint32 get_syntax ();
CHAR * GET_PRINTABLE ();
}
Where format_str saves the formatted string, you will return this string when calling the Get_Printable function. The remaining data items are known as the time items of the annual month, and there is a corresponding GET function to get its value. #define snmp_syntax_datetime (asn_application | Asn_primitive | 0x09) is the definition of code for this data type, in general, this line should be placed in the smi.h header file. After defining this number, you must modify the get_syntax () function, and overload it as:
SMIUINT32 datetime :: get_syntax ()
{
RETURN SNMP_SYNTAX_DATETIME;
}
In a member function, the most important thing is the Format () function, which will implement parsing of each time data and formatting the original string. Here is its implementation: void datetime :: format () {// The following is to extract time data
Char * pbuf = (char *) THIS-> DATA ();
Char BUF [10];
For (int i = 0; i <10; i ) {
BUF [I] = 0;
}
STRCPY (BUF, PBUF);
Year = buf [0] * 256 256 BUF [1];
Month = BUF [2];
Day = BUF [3];
Hour = BUF [4];
Minute = BUF [5];
SECOND = BUF [6];
msecond = BUF [7];
/ / The following is a string
INT INDEX = 3;
INT TEMP = YEAR;
For (; index> = 0; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_STR [4] = '-';
INDEX = 6;
TEMP = MONTH;
For (; index> = 5; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_STR [7] = '-';
INDEX = 9;
Temp = day;
For (; index> = 8; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_str [10] = ',';
Index = 12;
Temp = HOUR;
For (; index> = 11; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_str [13] = ':';
INDEX = 15;
Temp = minute;
For (; index> = 14; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_STR [16] = ':';
INDEX = 18;
TEMP = Second;
For (; index> = 17; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_str [19] = '.';
INDEX = 21;
Temp = msecond;
For (; index> = 20; index -) {
Format_str [index] = 48 (TEMP-TEMP / 10 * 10);
TEMP / = 10;
}
Format_STR [22] = 0;
}
During the processing, since the byte is a number, the processing is also relatively simple, and only the corresponding byte can be assigned to the number represented by the corresponding byte, such as the month to use Month = BUF [2]. Only when the year is dealt, it is slightly different because the year is represented by two bytes, so use the high-level byte by 256 to add the low byte, processing the statement as Year = BUF [0] * 256 256 buf [1 ]. The standard time output format should be YYYY-MM-DD, HH: mm: ss.mm, so the time string of this format will be stored in the processed Format_STR string. See the code that transforms numbers into characters above, this is no longer explained here. To explain that this code can be simplified with a digital string conversion function.
Once processed, you can overload the GET_PRINTABLE () function, because the data is updated each time the function is called, so you must call Format to format it before returning the value.
Char * datetime :: get_printable ()
{
Format ();
Return Format_STR;
}
From this, we got a relatively complete DateTime class. After testing, it proves that it can operate correctly. For example, the following is a line of data on a software installation table I get in a machine:
HrswinStalledIndex: 41
HrswinStalledname: Java 2 SDK, SE V
1.4.2
_01
HrswinStalledType: 4
HrswinStalledDate:
2004-07-01
19: 29: 50.00
It can be seen that the DateandTime type data can be correctly parsed using this class. By expanding, we can also generate more SNMP non-direct data types, which is also one of the benefits of inherited to us in object-oriented technologies.