환경 : Window 11

윈도우 키 -> 선택적 기능

-> 기능 추가

-> SNMP 체크

-> WMI SNMP 공급자,SNMP 다운로드

윈도우 키 -> 서비스

-> snmp 서비스 (활성화 시켜줌)
속성
링커 > 입력> 추가 종속성에 wsnmp32.lib 추가
라이브러리 경로에 wsnmp32.lib가 있는 경로 추가
링커 > 입력> 추가 종속성에 snmpapi.lib 추가
코드
#include <snmp.h>
#include "WinSnmp.h"
// 예시 UPS OID
#define UPS_BATTERY_STATUS_OID "1.3.6.1.2.1.33.1.2.1.0"
void GetUpsBatteryStatus()
{
HSNMP_SESSION hSession;
HSNMP_ENTITY hEntity;
HSNMP_CONTEXT hContext;
HSNMP_VBL hVbl;
HSNMP_PDU hPdu;
SNMPAPI_STATUS status;
status = SnmpStartup(NULL, NULL, NULL, NULL, NULL);
if (status != SNMPAPI_SUCCESS) {
AfxMessageBox(_T("SNMP 초기화에 실패했습니다."));
return;
}
hSession = SnmpCreateSession(NULL, 0, NULL, NULL);
hEntity = SnmpStrToEntity(hSession, "192.168.1.100"); // UPS IP 주소
hContext = SnmpStrToContext(hSession, NULL);
hVbl = SnmpCreateVbl(hSession, UPS_BATTERY_STATUS_OID, NULL);
hPdu = SnmpCreatePdu(hSession, SNMP_PDU_GET, 0, 0, 0, hVbl);
if (SnmpSendMsg(hSession, hEntity, hContext, hPdu) != SNMPAPI_SUCCESS) {
AfxMessageBox(_T("SNMP 메시지 전송에 실패했습니다."));
} else {
HSNMP_VBL hRespVbl;
HSNMP_PDU hRespPdu;
if (SnmpRecvMsg(hSession, &hEntity, &hContext, &hRespPdu) == SNMPAPI_SUCCESS) {
hRespVbl = SnmpGetVbl(hRespPdu);
// hRespVbl로부터 UPS 배터리 상태 값을 추출하여 사용
}
SnmpFreePdu(hRespPdu);
}
SnmpFreePdu(hPdu);
SnmpFreeVbl(hVbl);
SnmpFreeContext(hContext);
SnmpFreeEntity(hEntity);
SnmpDeleteSession(hSession);
SnmpCleanup();
}
'C++, MFC' 카테고리의 다른 글
[C++/STL] Array, Vector, List, Map 차이 (0) | 2024.08.13 |
---|---|
[C++] template 함수 (0) | 2024.08.13 |
[C++,MFC] std::string <--> CString (0) | 2024.07.31 |
[C++] Pointer <포인터> (0) | 2024.07.29 |
[MFC] CString 문자열 비교 Compare() (0) | 2024.07.23 |