通过net-snmp C语言API,得到agent端返回的数据,各种snmp类型的数据的解析方法
- /* manipuate the information ourselves */
-
for(vars = response->variables; vars; vars = vars->next_variable) //pdu is a snmp_pdu style variable
-
{
-
printf("var type is %d\n", vars->type);
-
if (vars->type == ASN_OCTET_STR) //vars is a variable_list style variable
-
{
-
//判断是字符串还是Hex-STRING
-
int hex = 0;
-
int x;
-
u_char * cp;
-
int allow_realloc = 1;
-
u_char *buf = NULL;
-
size_t buf_len = 256, out_len = 0;
-
-
for (cp = vars->val.string, x = 0; x < (int) vars->val_len; x++, cp++)
-
{
-
if (!isprint(*cp) && !isspace(*cp))
-
{
-
hex = 1;
-
}
-
}
-
if(!hex) //字符串
-
{
-
char *sp = (char *)malloc(1 + vars->val_len);
-
memcpy(sp, vars->val.string, vars->val_len); //netsnmp_vardata is a netsnmp_vardata style variable
-
sp[vars->val_len] = '\0';
-
printf("value #%d is a string: %s\n", count++, sp);
-
free(sp);
-
}
-
else //Hex-STRING
-
{
-
buf = (u_char *) calloc(buf_len, 1);
-
snmp_cstrcat(&buf, &buf_len, &out_len, allow_realloc, "");
-
sprint_realloc_hexstring(&buf, &buf_len, &out_len, allow_realloc,vars->val.string, vars->val_len);
-
printf("value #%d is a hex-string: %s\n", count++, buf);
-
free(buf);
-
}
-
}
-
else if(vars->type == ASN_TIMETICKS)
-
{
-
long timetick = *vars->val.integer;
-
printf("value #%d is a timetick: %d\n", count++, timetick);
-
}
-
else if(vars->type == ASN_OBJECT_ID)
-
{
-
printf("value #%d is a oid: ", count++);
-
for(i=0; i<vars->name_length; i++)
-
{
-
if(*(vars->name_loc+i) == 0)
-
break;
-
printf(".%d", *(vars->name_loc+i));
-
}
-
printf(" and value is ");
-
for(i=0; i<(vars->val_len/sizeof(int)); i++)
-
{
-
printf(".%d", *(vars->val.objid+i));
-
}
-
printf("\n");
-
-
}
-
else if(vars->type == ASN_INTEGER)
-
{
-
printf("value #%d is a integer: %d\n", count++, *(vars->val.integer));
-
}
-
else if(vars->type == ASN_COUNTER)
-
{
-
printf("value #%d is a count: %u\n", count++, (unsigned int)(*vars->val.integer & 0xffffffff));
-
}
-
else if(vars->type == ASN_GAUGE)
-
{
-
printf("value #%d is a gauge: %u\n", count++, *(vars->val.integer));
-
}
-
else if(vars->type == ASN_IPADDRESS)
-
{
-
u_char *ip = vars->val.string;
-
printf("value #%d is a ipaddress: %d.%d.%d.%d\n", count++, ip[0], ip[1], ip[2], ip[3]);
-
}
-
else if(vars->type == ASN_NULL)
-
{
-
printf("value #%d is a null: \n", count++);
-
}
-
else
-
printf("value #%d is NOT a string! Ack!\n", count++);
-
}
-
-
}
阅读(4054) | 评论(0) | 转发(0) |