Snmp timstamp field and retrieval Function

Source: Internet
Author: User
Tags snmp

In SNMP, timestamp refers to the time from the last system initialization when the data client server sends the data.

This unit should be 10 ms, and some software processing is not very standard.

The net-snmp library is used. get_uptime is a function that returns 10 MS:

00569 long00570 get_uptime(void)00571 {00572     long            return_value = 0;00573     DWORD           buffersize = (sizeof(PERF_DATA_BLOCK) +00574                                   sizeof(PERF_OBJECT_TYPE)),00575         type = REG_EXPAND_SZ;00576     PPERF_DATA_BLOCK perfdata = NULL;00577 00578     /*00579      * min requirement is one PERF_DATA_BLOCK plus one PERF_OBJECT_TYPE 00580      */00581     perfdata = (PPERF_DATA_BLOCK) malloc(buffersize);00582     if (!perfdata)00583         return 0;00584 00585     memset(perfdata, 0, buffersize);00586 00587     RegQueryValueEx(HKEY_PERFORMANCE_DATA,00588                     "Global", NULL, &type, (LPBYTE) perfdata, &buffersize);00589 00590     /*00591      * we can not rely on the return value since there is always more so00592      * we check the signature 00593      */00594 00595     if (wcsncmp(perfdata->Signature, L"PERF", 4) == 0) {00596         /*00597          * signature ok, and all we need is in the in the PERF_DATA_BLOCK 00598          */00599         return_value = (long) ((perfdata->PerfTime100nSec.QuadPart /00600                                 (LONGLONG) 100000));00601     } else00602         return_value = GetTickCount() / 10;00603 00604     RegCloseKey(HKEY_PERFORMANCE_DATA);00605     free(perfdata);00606 00607     return return_value;00608 }

We can see that the #00602 operation is divided by 10.

If you do not have source code, you can simply process it yourself:

#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>int main(int argc, char *argv[]){    long sysuptime;    sysuptime = get_uptime();    printf("%ld",sysuptime );    return 0;}

 

After the command is executed, 153852806 is displayed.

Run the following uptime:

10:52:48 up 17 days, 33 users, load average: 0.01, 0.08, 0.11

We can see that the system was started for 17 days and 19 hours.

Perform another Conversion Function (in seconds as the parameter ):

 

def getTime(seconds):    print 'day :%s' % (seconds / (60 * 60 * 24))    print 'hour :%s' % ((seconds / (60 * 60)) % 24)

 

>>> GetTime (1, 153852806)
Hour: 16
Day: 1780

In this way, if the number of days is about 100 times different, get_uptime should return 1 s/100 = 10 ms

When I am tired, I write a blog, find the source code, and look at the sky outside,

To relax, programmers should cherish their bodies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.