• snmp timstamp字段及获取函数


    SNMP协议中,timestamp是指数据端服务器发送该条数据时,距离系统上次初始化的时间。

    这个单位应该是10ms,发现有的软件处理这个不是很标准。

    使用的是net-snmp库,里面的使用的get_uptime 正是一个返回了10ms的函数:

    00569 long
    00570 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 so
    00592      * 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     } else
    00602         return_value = GetTickCount() / 10;
    00603 
    00604     RegCloseKey(HKEY_PERFORMANCE_DATA);
    00605     free(perfdata);
    00606 
    00607     return return_value;
    00608 }
    

     可以看到在#00602 做了除以10的操作。

    同样没有源代码时,可以自己简单的处理一下:

    #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;
    }
    

    执行后得到:153852806

    执行下uptime:

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

    可以看到,系统启动了17天零19个小时

     再做一个转换函数(以秒作为参数):

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

    这样,天数相差100倍左右,则get_uptime应该是返回1s/100 = 10ms

    :) 工作之余,累了就,写写博客,找点源码,看看外面的天空,

    放松一下心态,程序员更要爱惜自己的身体啊

  • 相关阅读:
    架构优秀文章汇总
    SpringCloud-Hystrix 注解配置Hystrix && 和FeignClient集成 (转)
    SpringCloud-Hystrix(服务熔断,服务降级)(1)
    java 锁 -乐观锁、悲观锁  Volatile
    JS OOP -02 深入认识JS中的函数
    JS OOP -01 面向对象的基础
    JS OOP 概述
    拆表策略
    双重检查加锁机制(并发insert情况下数据重复插入问题的解决方案)
    C# 理解lock
  • 原文地址:https://www.cnblogs.com/amaoxiaozhu/p/2762216.html
Copyright © 2020-2023  润新知