snmp timstamp欄位及擷取函數

來源:互聯網
上載者:User

SNMP協議中,timestamp是指資料端伺服器發送該條資料時,距離系統上次初始化的時間。

這個單位應該是10ms,發現有的軟體處理這個不是很標準。

使用的是net-snmp庫,裡面的使用的get_uptime 正是一個返回了10ms的函數:

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 }

 可以看到在#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

工作之餘,累了就,寫寫部落格,找點源碼,看看外面的天空,

放鬆一下心態,程式員更要愛惜自己的身體啊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.