Domanda

I'm trying to get the information from an apc ups from winapi but my code is failing SnmpMgrRequest and when checked the error code it is giving it as 40 which is not mentioned in the SNMP ERROR code list for winapi. I have posted the code below . Any help in fixing this would be great .

#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;
    }
}
È stato utile?

Soluzione

the error code it is giving it as 40 which is not mentioned in the SNMP ERROR code list for winapi

If SnmpMgrRequest() is returning 0 and GetLastError() is returning 40, then that is SNMP_MGMTAPI_TIMEOUT, which is defined in Mgmtapi.h:

#define SNMP_MGMTAPI_TIMEOUT                40

SNMP_MGMTAPI_TIMEOUT is documented as a possible error code from SnmpMgrRequest():

If the function fails, the return value is NULL. To get extended error information, call GetLastError, which may return one of the following error codes.

SNMP_MGMTAPI_TIMEOUT The request timed-out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top