⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpdelivertrap_netsnmp.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Packer.h>#include <Pegasus/Common/Tracer.h>#include "snmpDeliverTrap_netsnmp.h"PEGASUS_NAMESPACE_BEGINvoid snmpDeliverTrap_netsnmp::initialize(){    PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::initialize");    // Defined default MIB modules (in net-snmp-config.h) do not need to be    // loaded and loading them can cause some stderr;    // use environment variable MIBS to override the default MIB modules.    // If there is no MIBS environment variable, add it in.    char* envVar;    envVar = getenv("MIBS");    if (envVar == NULL)    {        putenv("MIBS=");    }    // Initialize the mib reader    netsnmp_set_mib_directory("");    init_mib();    // Initializes the SNMP library    init_snmp("snmpIndicationHandler");    // windows32 specific initialization (is a NOOP on unix)    SOCK_STARTUP;    PEG_METHOD_EXIT();}void snmpDeliverTrap_netsnmp::terminate(){    PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::terminate");    SOCK_CLEANUP;    PEG_METHOD_EXIT();}void snmpDeliverTrap_netsnmp::deliverTrap(    const String& trapOid,    const String& securityName,    const String& targetHost,    const Uint16& targetHostFormat,    const String& otherTargetHostFormat,    const Uint32& portNumber,    const Uint16& snmpVersion,    const String& engineID,    const Array<String>& vbOids,    const Array<String>& vbTypes,    const Array<String>& vbValues){    PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::deliverTrap");    void* sessionHandle;    struct snmp_session* sessionPtr;    struct snmp_pdu* snmpPdu;    // Creates a SNMP session    _createSession(targetHost, portNumber, securityName,                   sessionHandle, sessionPtr);    try    {        _createPdu(snmpVersion, trapOid, sessionPtr, snmpPdu);    }    catch (...)    {        _destroySession(sessionHandle);        PEG_METHOD_EXIT();        throw;    }    // Pack OIDs into the PDU    try    {        _packOidsIntoPdu(vbOids, vbTypes, vbValues, snmpPdu);    }    catch (Exception& e)    {        PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, e.getMessage());        Logger::put_l(Logger::STANDARD_LOG, System::CIMSERVER,                      Logger::WARNING,                      _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED_KEY,                      _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED,                      e.getMessage());    }    catch (...)    {        PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,            "Snmp Indication Handler failed to pack a CIM "                "Property into the SNMP PDU: Unknown exception.");    }    // Send the trap to the destination    if (snmp_sess_send(sessionHandle, snmpPdu) == 0)    {        Sint32 libErr, sysErr;        char* errStr;        // snmp_sess_send failed        // get library, system errno        snmp_sess_error(sessionHandle, &libErr, &sysErr, &errStr);        String exceptionStr = _MSG_SESSION_SEND_FAILED;        exceptionStr.append(errStr);        free(errStr);        _destroySession(sessionHandle);        PEG_METHOD_EXIT();        throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,            MessageLoaderParms(_MSG_SESSION_SEND_FAILED_KEY, exceptionStr));    }    _destroySession(sessionHandle);    PEG_METHOD_EXIT();}// Creates a SNMP sessionvoid snmpDeliverTrap_netsnmp::_createSession(    const String& targetHost,    Uint32 portNumber,    const String& securityName,    void*& sessionHandle,    snmp_session*& sessionPtr){    PEG_METHOD_ENTER(TRC_IND_HANDLER,        "snmpDeliverTrap_netsnmp::_createSession");    Sint32 libErr, sysErr;    char* errStr;    String exceptionStr;    struct snmp_session snmpSession;    {        AutoMutex autoMut(_sessionInitMutex);        snmp_sess_init(&snmpSession);        CString targetHostCStr = targetHost.getCString();        // peername has format: targetHost:portNumber        snmpSession.peername =            (char*)malloc((size_t)(strlen(targetHostCStr) + 1 + 32));        sprintf(snmpSession.peername, "%s:%u",            (const char*)targetHostCStr,            portNumber);        sessionHandle = snmp_sess_open(&snmpSession);    }    if (sessionHandle == NULL)    {        exceptionStr = _MSG_SESSION_OPEN_FAILED;        // Get library, system errno        snmp_error(&snmpSession, &libErr, &sysErr, &errStr);        exceptionStr.append(errStr);        free(errStr);        PEG_METHOD_EXIT();        throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,            MessageLoaderParms(_MSG_SESSION_OPEN_FAILED_KEY, exceptionStr));    }    try    {        // get the snmp_session pointer        sessionPtr = snmp_sess_session(sessionHandle);        if (sessionPtr == NULL)        {            exceptionStr = _MSG_GET_SESSION_POINT_FAILED;            // Get library, system errno            snmp_sess_error(&snmpSession, &libErr, &sysErr, &errStr);            exceptionStr.append(errStr);            free(errStr);            throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(                _MSG_GET_SESSION_POINTER_FAILED_KEY,                exceptionStr));        }        // Community Name, default is public        String communityName;        if (securityName.size() == 0)        {            communityName.assign("public");        }        else        {            communityName = securityName;        }        free(snmpSession.peername);        free(sessionPtr->community);        CString communityNameCStr = communityName.getCString();        size_t communityNameLen = strlen(communityNameCStr);        sessionPtr->community = (u_char*)malloc(communityNameLen);        memcpy(sessionPtr->community, (const char*)communityNameCStr,               communityNameLen);        sessionPtr->community_len = communityNameLen;    }    catch (...)    {        _destroySession(sessionHandle);        PEG_METHOD_EXIT();        throw;    }    PEG_METHOD_EXIT();}// Creates a SNMP sessionvoid snmpDeliverTrap_netsnmp::_destroySession(    void* sessionHandle){    PEG_METHOD_ENTER(TRC_IND_HANDLER,        "snmpDeliverTrap_netsnmp::_destroySession");    snmp_sess_close(sessionHandle);    PEG_METHOD_EXIT();}// Creates a SNMP TRAP PDUvoid snmpDeliverTrap_netsnmp::_createPdu(    Uint16 snmpVersion,    const String& trapOid,    snmp_session*& sessionPtr,    snmp_pdu*& snmpPdu){    PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::_createPdu");    oid _SYSTEM_UP_TIME_OID [] = {1,3,6,1,2,1,1,3,0};    oid _SNMPTRAP_OID [] = {1,3,6,1,6,3,1,1,4,1,0};    in_addr_t* pduInAddr;    switch (snmpVersion)    {        case _SNMPv1_TRAP:        {            sessionPtr->version = SNMP_VERSION_1;            // Create the PDU            snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);            // Failed to create pdu            if (!snmpPdu)            {                PEG_METHOD_EXIT();                throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,                    MessageLoaderParms(                        _MSG_PDU_CREATE_FAILED_KEY,                        _MSG_PDU_CREATE_FAILED));            }            // Make sure that the v1 trap PDU includes the local IP address            pduInAddr = (in_addr_t*) snmpPdu->agent_addr;            *pduInAddr = get_myaddr();            // get system up time            snmpPdu->time = get_uptime();            // Pack trap information into the PDU            try            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -