Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483295
  • 博文数量: 111
  • 博客积分: 2332
  • 博客等级: 大尉
  • 技术积分: 1187
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-29 11:22
文章分类

全部博文(111)

文章存档

2013年(9)

2012年(28)

2011年(17)

2010年(28)

2009年(29)

我的朋友

分类: C/C++

2011-12-14 15:19:29

通过net-snmp C语言API,得到agent端返回的数据,各种snmp类型的数据的解析方法
  1. /* manipuate the information ourselves */
  2.         for(vars = response->variables; vars; vars = vars->next_variable) //pdu is a snmp_pdu style variable
  3.         {
  4.             printf("var type is %d\n", vars->type);
  5.             if (vars->type == ASN_OCTET_STR) //vars is a variable_list style variable
  6.             {
  7.                 //判断是字符串还是Hex-STRING
  8.                 int hex = 0;
  9.                 int x;
  10.                 u_char * cp;
  11.                 int allow_realloc = 1;
  12.                 u_char *buf = NULL;
  13.                 size_t buf_len = 256, out_len = 0;

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

  84.     }

阅读(4054) | 评论(0) | 转发(0) |
0

上一篇:mysql5.5只编译客户端

下一篇:rsync使用

给主人留下些什么吧!~~