首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SNMP查询以获取UPS信息

SNMP查询以获取UPS信息
EN

Stack Overflow用户
提问于 2013-07-18 18:30:06
回答 1查看 1.3K关注 0票数 2

我正在尝试从winapi获取apc的信息,但是我的代码SnmpMgrRequest失败了,当检查错误代码时,它会将其作为40,这在用于winapi的SNMP错误代码列表中没有提到。我已经张贴了下面的代码。如果能帮上忙,那就太好了。

代码语言:javascript
复制
#include <stdio.h>
#include <windows.h>
#include <WINSNMP.H>
#include <tchar.h>
#include <Strsafe.h>
#include <WinNT.h>
#include <Mgmtapi.h>
#include <Snmp.h>

#pragma comment (lib, "WSNMP32.LIB")
#pragma comment (lib, "Mgmtapi.lib")
#pragma comment (lib, "Snmpapi.lib")    

int main()
{
    int choice;
    LPSNMP_MGR_SESSION MgrSession;     
    INT nRetries = 1;
    AsnObjectIdentifier oid;
    RFC1157VarBindList variableBindings;
    AsnInteger errorStatus = 0;
    AsnInteger errorIndex = 0;
    char lpAgentAddress[50] ;

    LPSTR lpAgentCommunity ="public";
    variableBindings.list = NULL;
    variableBindings.len = 0;
    SNMPAPI MgrStatus;

    printf("\n SNMP Program \n ");
    printf("\n What You want to know ? \n ");
    printf("1---UPS Type \n");
    printf("2---Battery capacity \n");
    printf("3---Battery Temperature \n ");
    printf("4---Battery runtime remain \n ");
    printf("Enter your choice : ");
    scanf("%d",&choice);
    printf("\n Enter the ip address : ");
    scanf_s(" %s",lpAgentAddress);

    switch(choice)
    {
        case 1 :
            variableBindings.len++;
            SnmpMgrStrToOid(".1.3.6.1.4.1.318.1.1.1.1.1.1.0", &oid); // oid for apc ups 
            variableBindings.list = (SnmpVarBind *)SNMP_realloc(variableBindings.list, sizeof(SnmpVarBind) *variableBindings.len);
            SnmpUtilOidCpy(&variableBindings.list[0].name,&oid);
            variableBindings.list->value.asnType = ASN_NULL;
            MgrSession = SnmpMgrOpen((LPSTR)lpAgentAddress,lpAgentCommunity,1500,2);
            if (MgrSession == NULL)
            {
                printf("\n Session has failed ");
                SnmpUtilVarBindListFree(&variableBindings);
                return false;
            }
            MgrStatus = SnmpMgrRequest(MgrSession,SNMP_PDU_GET,&variableBindings,&errorStatus,&errorIndex);
            if (MgrStatus == NULL)
            {
                printf("\n Could not query the device ");
                printf("\n Error1 = %d",GetLastError());

                SnmpUtilVarBindListFree(&variableBindings);
                return false;
            } 
            else
            {
                if (errorStatus > 0){
                    printf("Error Returned from the agent ");

                    SnmpUtilVarBindListFree(&variableBindings);
                    return false;
                } 
                else
                {
                    for(unsigned int i=0;i<variableBindings.len;i++)
                    {
                        int infolen;
                        char systeminfo[2048];
                        char *snmpstring;
                        infolen = variableBindings.list[i].value.asnValue.string.length;
                        snmpstring =(char *) variableBindings.list[i].value.asnValue.string.stream;

                        memcpy(systeminfo, snmpstring,infolen);  //get the system information and copy in the array systeminfo 
                        systeminfo[infolen] = '\0'; 
                        printf("%s",systeminfo);
                    }
                }
            }
            break;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-18 21:13:41

它给出的错误代码为40,这在winapi的SNMP错误代码列表中没有提到。

如果SnmpMgrRequest()返回0,GetLastError()返回40,那么这就是SNMP_MGMTAPI_TIMEOUT,它在Mgmtaci.h中定义:

代码语言:javascript
复制
#define SNMP_MGMTAPI_TIMEOUT                40

SNMP_MGMTAPI_TIMEOUT 被证明为来自SnmpMgrRequest()的可能的错误代码。

如果函数失败,则返回值为NULL。若要获得扩展错误信息,请调用GetLastError,它可能返回下列错误代码之一。 SNMP_MGMTAPI_TIMEOUT请求超时.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17731279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档