net-snmp獲取各個類型的數據

  1. 原文地址http://blog.chinaunix.net/uid-17260303-id-3042593.html

  2. /* manipuate the information ourselves */
  3.         for(vars = response->variables; vars; vars = vars->next_variable) //pdu is a snmp_pdu style variable
  4.         {
  5.             printf("var type is %d\n", vars->type);
  6.             if (vars->type == ASN_OCTET_STR) //vars is a variable_list style variable
  7.             {
  8.                 //判斷是字符串還是Hex-STRING
  9.                 int hex = 0;
  10.                 int x;
  11.                 u_char * cp;
  12.                 int allow_realloc = 1;
  13.                 u_char *buf = NULL;
  14.                 size_t buf_len = 256, out_len = 0;

  15.                 for (cp = vars->val.string, x = 0; x < (int) vars->val_len; x++, cp++) 
  16.                 {
  17.                     if (!isprint(*cp) && !isspace(*cp)) 
  18.                     {
  19.                         hex = 1;
  20.                     }
  21.                 }
  22.                 if(!hex) //字符串
  23.                 {
  24.                     char *sp = (char *)malloc(+ vars->val_len);
  25.                     memcpy(sp, vars->val.string, vars->val_len); //netsnmp_vardata is a netsnmp_vardata style variable
  26.      sp[vars->val_len] = '\0';
  27.                     printf("value #%d is a string: %s\n", count++, sp);
  28.                     free(sp);
  29.                 }
  30.                 else //Hex-STRING
  31.                 {
  32.                     buf = (u_char *) calloc(buf_len, 1);
  33.                     snmp_cstrcat(&buf, &buf_len, &out_len, allow_realloc, "");
  34.                     sprint_realloc_hexstring(&buf, &buf_len, &out_len, allow_realloc,vars->val.string, vars->val_len);
  35.                     printf("value #%d is a hex-string: %s\n", count++, buf);
  36.                     free(buf);
  37.                 }
  38.             }
  39.             else if(vars->type == ASN_TIMETICKS)
  40.             {
  41.                 long timetick = *vars->val.integer;
  42.                 printf("value #%d is a timetick: %d\n", count++, timetick);
  43.             }
  44.             else if(vars->type == ASN_OBJECT_ID)
  45.             {
  46.                  printf("value #%d is a oid: ", count++);
  47.                  for(i=0; i<vars->name_length; i++)
  48.                  {
  49.                      if(*(vars->name_loc+i) == 0)
  50.                          break;
  51.                      printf(".%d", *(vars->name_loc+i));
  52.                  }
  53.                  printf(" and value is ");
  54.                  for(i=0; i<(vars->val_len/sizeof(int)); i++)
  55.                  {
  56.                      printf(".%d", *(vars->val.objid+i));
  57.                  }
  58.                  printf("\n");
  59.                 
  60.             }
  61.             else if(vars->type == ASN_INTEGER)
  62.             {
  63.                 printf("value #%d is a integer: %d\n", count++, *(vars->val.integer));
  64.             }
  65.             else if(vars->type == ASN_COUNTER)
  66.             {
  67.                 printf("value #%d is a count: %u\n", count++, (unsigned int)(*vars->val.integer & 0xffffffff));
  68.             }
  69.             else if(vars->type == ASN_GAUGE)
  70.             {
  71.                 printf("value #%d is a gauge: %u\n", count++, *(vars->val.integer));
  72.             }
  73.             else if(vars->type == ASN_IPADDRESS)
  74.             {
  75.                 u_char *ip = vars->val.string;
  76.                 printf("value #%d is a ipaddress: %d.%d.%d.%d\n", count++, ip[0], ip[1], ip[2], ip[3]);
  77.             }
  78.             else if(vars->type == ASN_NULL)
  79.             {
  80.                 printf("value #%d is a null: \n", count++);
  81.             }
  82.             else
  83.               printf("value #%d is NOT a string! Ack!\n", count++);
  84.         }

  85.     }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章