如何在net-snmp中打印整数和Counter32值

时间:2011-12-19 14:30:41

标签: c++ linux network-programming network-protocols net-snmp

我创建了这个打印字符串值的代码,它运行正常:

 add_mibdir("."); 
 pdu = snmp_pdu_create(SNMP_MSG_GET);

 read_objid(if_index, id_oid, &id_len);
 snmp_add_null_var(pdu, id_oid, id_len);    
 status = snmp_synch_response(sess_handle,pdu,&response);

 int count=1;
 for(vars = response->variables; vars; vars = vars->next_variable) 
  {
    if (vars->type == ASN_OCTET_STR) 
    {
        char *sp = (char *)malloc(1 + vars->val_len);
        memcpy(sp, vars->val.string, vars->val_len);
        sp[vars->val_len] = '\0';
        printf("value #%d is a string: %s\n", count++, sp);
        printf("%s\n",vars->val.string);
        free(sp);
}

  }

示例:对于SNMPv2-SMI::mib-2.47.1.1.1.1.2.1012 = STRING: "GigabitEthernet Container",它会返回"GigabitEthernet Container";

但是我尝试了一个整数值而且它不起作用:

for(vars = response->variables; vars; vars = vars->next_variable)
    printf("%ld",(long int)vars->val.integer);//it returns large numbers;

示例:对于SNMPv2-SMI::mib-2.17.2.11.0 = INTEGER: 1500,我想要返回1500而对于IF-MIB::ifOutBroadcastPkts.10103 = Counter32: 14011112我想要返回14011112

如果我使用:

for(vars = response->variables; vars; vars = vars->next_variable)
    print_variable(vars->name, vars->name_length, vars);

它会返回Counter32:12132,或INTEGER:12324STRING:Gi0/1(但我想解析此结果并将其用于某些变量,而不使用数据类型,例如:in var {{ 1}}来存储a)。

谢谢!

3 个答案:

答案 0 :(得分:1)

如果有人仍有这方面的问题,我想出了一个不需要使用net-snmp 5.7.3解析的替代机制:

template<typename T>
static boost::shared_ptr< std::vector<T> > GetVector(std::string user_oid, struct snmp_session * snmp_session) {

    // Other Initialization ....

    char temp_buf[BUFSIZ];
    size_t temp_buf_len = BUFSIZ;
    bool orig_config_val_qp = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
    bool orig_config_val_bv = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE);

    // Enforce this for correct output in snprint_variable functions
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE, 1);

    // oid conversion routines ...

    // Processing loop for response vars (similar to netsnmp/app/snmpbulkwalk.c) ...
    if (vars->type == ASN_OCTET_STR) 
    {
        temp_buf_len = BUFSIZ;
        snprint_variable(temp_buf, temp_buf_len, vars->name, vars->name_length, vars);
        result->push_back(boost::lexical_cast<T>(temp_buf));
    }
    // End Processing loop...

    // Restore configuration
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, orig_config_val_qp);
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE, orig_config_val_bv);

    return result;
} 

这将保留您的应用程序配置的其余部分,并且仅吐出值,因此您不需要进行标记化。我缩写了循环,因此它侧重于重要方面。

答案 1 :(得分:0)

好吧,你总是可以直接操作数据......如你所说,计数器存储在var-&gt; val.integer中。但是,从print_variable的输出中删除所有LABEL:前缀的更快方法是设置NETSNMP_DS_LIB_QUICK_PRINT变量,如下所示:

netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);

将删除“Counter32:”前缀。

答案 2 :(得分:0)

我遇到同样的问题我正在使用它......

      unsigned long val64;
      val64 = vars->val.counter64->high;
      printf("value #%lu is a COUNTER32:\n", val64);