/* Init SNMP Session * alorbach, 2008-02-12 */ static rsRetVal omsnmp_initSession(instanceData *pData) { DEFiRet; netsnmp_session session; /* should not happen, but if session is not cleared yet - we do it now! */ if (pData->snmpsession != NULL) omsnmp_exitSession(pData); dbgprintf( "omsnmp_initSession: ENTER - Target = '%s' on Port = '%d'\n", pData->szTarget, pData->iPort); putenv(strdup("POSIXLY_CORRECT=1")); snmp_sess_init(&session); session.version = pData->iSNMPVersion; session.callback = NULL; /* NOT NEEDED */ session.callback_magic = NULL; session.peername = (char*) pData->szTargetAndPort; /* Set SNMP Community */ if (session.version == SNMP_VERSION_1 || session.version == SNMP_VERSION_2c) { session.community = (unsigned char *) pData->szCommunity; session.community_len = strlen((char*) pData->szCommunity); } pData->snmpsession = snmp_open(&session); if (pData->snmpsession == NULL) { errmsg.LogError(0, RS_RET_SUSPENDED, "omsnmp_initSession: snmp_open to host '%s' on Port '%d' failed\n", pData->szTarget, pData->iPort); /* Stay suspended */ iRet = RS_RET_SUSPENDED; } RETiRet; }
/* Init SNMP Session * alorbach, 2008-02-12 */ static rsRetVal omsnmp_initSession(wrkrInstanceData_t *pWrkrData) { netsnmp_session session; instanceData *pData; char szTargetAndPort[MAXHOSTNAMELEN+128]; /* work buffer for specifying a full target and port string */ DEFiRet; /* should not happen, but if session is not cleared yet - we do it now! */ if (pWrkrData->snmpsession != NULL) omsnmp_exitSession(pWrkrData); pData = pWrkrData->pData; snprintf((char*)szTargetAndPort, sizeof(szTargetAndPort), "%s:%s:%d", (pData->szTransport == NULL) ? "udp" : (char*)pData->szTransport, pData->szTarget, pData->iPort == 0 ? 162 : pData->iPort); dbgprintf( "omsnmp_initSession: ENTER - Target = '%s' on Port = '%d'\n", pData->szTarget, pData->iPort); if (setenv("POSIXLY_CORRECT", "1", 1) == -1) ABORT_FINALIZE(RS_RET_ERR); snmp_sess_init(&session); session.version = pData->iSNMPVersion; session.callback = NULL; /* NOT NEEDED */ session.callback_magic = NULL; session.peername = (char*) szTargetAndPort; /* Set SNMP Community */ if (session.version == SNMP_VERSION_1 || session.version == SNMP_VERSION_2c) { session.community = (unsigned char *) pData->szCommunity == NULL ? (uchar*)"public" : pData->szCommunity; session.community_len = strlen((char*) session.community); } pWrkrData->snmpsession = snmp_open(&session); if (pWrkrData->snmpsession == NULL) { errmsg.LogError(0, RS_RET_SUSPENDED, "omsnmp_initSession: snmp_open to host '%s' on Port '%d' " "failed\n", pData->szTarget, pData->iPort); /* Stay suspended */ iRet = RS_RET_SUSPENDED; } finalize_it: RETiRet; }
static rsRetVal omsnmp_sendsnmp(wrkrInstanceData_t *pWrkrData, uchar *psz) { DEFiRet; netsnmp_pdu *pdu = NULL; oid enterpriseoid[MAX_OID_LEN]; size_t enterpriseoidlen = MAX_OID_LEN; oid oidSyslogMessage[MAX_OID_LEN]; size_t oLen = MAX_OID_LEN; int status; char *trap = NULL; const char *strErr = NULL; instanceData *pData; pData = pWrkrData->pData; /* Init SNMP Session if necessary */ if (pWrkrData->snmpsession == NULL) { CHKiRet(omsnmp_initSession(pWrkrData)); } /* String should not be NULL */ ASSERT(psz != NULL); dbgprintf( "omsnmp_sendsnmp: ENTER - Syslogmessage = '%s'\n", (char*)psz); /* If SNMP Version1 is configured !*/ if(pWrkrData->snmpsession->version == SNMP_VERSION_1) { pdu = snmp_pdu_create(SNMP_MSG_TRAP); /* Set enterprise */ if(!snmp_parse_oid(pData->szEnterpriseOID == NULL ? "1.3.6.1.4.1.3.1.1" : (char*)pData->szEnterpriseOID, enterpriseoid, &enterpriseoidlen )) { strErr = snmp_api_errstring(snmp_errno); errmsg.LogError(0, RS_RET_DISABLE_ACTION, "omsnmp_sendsnmp: Parsing EnterpriseOID " "failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); ABORT_FINALIZE(RS_RET_DISABLE_ACTION); } pdu->enterprise = (oid *) MALLOC(enterpriseoidlen * sizeof(oid)); memcpy(pdu->enterprise, enterpriseoid, enterpriseoidlen * sizeof(oid)); pdu->enterprise_length = enterpriseoidlen; /* Set Traptype */ pdu->trap_type = pData->iTrapType; /* Set SpecificType */ pdu->specific_type = pData->iSpecificType; /* Set Updtime */ pdu->time = get_uptime(); } /* If SNMP Version2c is configured !*/ else if (pWrkrData->snmpsession->version == SNMP_VERSION_2c) { long sysuptime; char csysuptime[20]; /* Create PDU */ pdu = snmp_pdu_create(SNMP_MSG_TRAP2); /* Set uptime */ sysuptime = get_uptime(); snprintf( csysuptime, sizeof(csysuptime) , "%ld", sysuptime); trap = csysuptime; snmp_add_var(pdu, objid_sysuptime, sizeof(objid_sysuptime) / sizeof(oid), 't', trap); /* Now set the SyslogMessage Trap OID */ if ( snmp_add_var(pdu, objid_snmptrap, sizeof(objid_snmptrap) / sizeof(oid), 'o', pData->szSnmpTrapOID == NULL ? "1.3.6.1.4.1.19406.1.2.1" : (char*) pData->szSnmpTrapOID ) != 0) { strErr = snmp_api_errstring(snmp_errno); errmsg.LogError(0, RS_RET_DISABLE_ACTION, "omsnmp_sendsnmp: Adding trap OID failed '%s' with error '%s' \n", pData->szSnmpTrapOID, strErr); ABORT_FINALIZE(RS_RET_DISABLE_ACTION); } } /* SET TRAP PARAMETER for SyslogMessage! */ /* dbgprintf( "omsnmp_sendsnmp: SyslogMessage '%s'\n", psz );*/ /* First create new OID object */ if (snmp_parse_oid(pData->szSyslogMessageOID == NULL ? "1.3.6.1.4.1.19406.1.1.2.1" : (char*)pData->szSyslogMessageOID, oidSyslogMessage, &oLen)) { int iErrCode = snmp_add_var(pdu, oidSyslogMessage, oLen, 's', (char*) psz); if (iErrCode) { const char *str = snmp_api_errstring(iErrCode); errmsg.LogError(0, RS_RET_DISABLE_ACTION, "omsnmp_sendsnmp: Invalid SyslogMessage OID, error code '%d' - '%s'\n", iErrCode, str ); ABORT_FINALIZE(RS_RET_DISABLE_ACTION); } } else { strErr = snmp_api_errstring(snmp_errno); errmsg.LogError(0, RS_RET_DISABLE_ACTION, "omsnmp_sendsnmp: Parsing SyslogMessageOID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); ABORT_FINALIZE(RS_RET_DISABLE_ACTION); } /* Send the TRAP */ status = snmp_send(pWrkrData->snmpsession, pdu) == 0; if (status) { /* Debug Output! */ int iErrorCode = pWrkrData->snmpsession->s_snmp_errno; errmsg.LogError(0, RS_RET_SUSPENDED, "omsnmp_sendsnmp: snmp_send failed error '%d', Description='%s'\n", iErrorCode*(-1), api_errors[iErrorCode*(-1)]); /* Clear Session */ omsnmp_exitSession(pWrkrData); ABORT_FINALIZE(RS_RET_SUSPENDED); } finalize_it: if(iRet != RS_RET_OK) { if(pdu != NULL) { snmp_free_pdu(pdu); } } dbgprintf( "omsnmp_sendsnmp: LEAVE\n"); RETiRet; }