基于libsnmp++,libagent++,编写snmp服务,可动态更新oid数据

基于libsnmp++,libagent++,编写snmp服务,可动态更新oid数据

做网络数据转发,项目上要求通过snmp服务转发

// snmp_server.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <thread>
#include "agent_pp/agent++.h"
#include "agent_pp/snmp_group.h"
#include "agent_pp/system_group.h"
#include "agent_pp/snmp_target_mib.h"
#include "agent_pp/snmp_notification_mib.h"
#include "agent_pp/notification_originator.h"
#include "agent_pp/mib_complex_entry.h"
#include "agent_pp/v3_mib.h"
#include "agent_pp/vacm.h"
#include "snmp_pp/oid_def.h"
#include "snmp_pp/mp_v3.h"
#include "snmp_pp/log.h"
#include "snmp_pp/snmp_pp.h"
#include "libsnmp.h"
#ifdef SNMP_PP_NAMESPACE
using namespace Snmp_pp;
#endif
#ifdef AGENTPP_NAMESPACE
using namespace Agentpp;
#endif
#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"libsnmp.lib")
#pragma comment(lib,"libagent.lib")
using namespace std;
OctetStr readcommunity("public");
OctetStr writecommunity("public");
const index_info indDynamicTable[] = {
  { sNMP_SYNTAX_INT, FALSE, 1, 1 }
}; 
MibTable * table = new MibTable("1.3.6.1.4.1.12345", indDynamicTable, 1); //动态表
void upoidvalue(const string & oid, const string & m_value) {
	Oid m_oid(oid.c_str());
	table->start_synch();
	MibTableRow* row = table->find_index(table->index(m_oid));
	if (row) //找到OID
	{
		row->get_nth(0)->set_value(OctetStr(m_value.c_str())); //更新值
	}
	else
	{
		row = table->add_row(table->index(m_oid));
		row->get_nth(0)->replace_value(new OctetStr(m_value.c_str())); //不存在新建
		row->set_access(READONLY);
	}
	table->end_synch();
}
static void UpdataThread() {
	while (1) {
		if (table) {
			for (int i = 1; i < 100; i++) {
				srand((unsigned)time(NULL));
				const char * base_oid = "1.3.6.1.4.1.12345.1.%d";
				char oid[50] = { 0 };
				sprintf(oid, base_oid, i);
				double val = (rand() % 1000 + 400) / 10.0;
				char _val[50] = { 0 };
				sprintf(_val, "%0.2f", (float)val);
				upoidvalue(oid, _val);
			}
			Sleep(1000);
		}
	}
}
int main()
{
	int status = 0;
	Snmp::socket_startup();  // Initialize socket subsystem
	Snmpx * snmp_server = new Snmpx(status, 1361);
	Mib* mib = new Mib();
	RequestList * reqlist = new RequestList();
	
	reqlist->set_read_community(readcommunity);
	reqlist->set_write_community(writecommunity);
	mib->set_request_list(reqlist);
	mib->add(new sysDescr("SnmpServerTest"));//描述信息
	mib->add(new sysGroup("Sample Agent Test v1.0", "1.3.6.1.4.1.12345", 100));
	table->add_col(new MibLeaf("1", READWRITE, new SnmpInt32(0), VMODE_NONE));
	mib->add(table);
	mib->init();
	reqlist->set_snmp(snmp_server);
	Request* req = NULL;
	thread start(UpdataThread); start.detach();
	while (true) {
		req = reqlist->receive(1);
		if (req) {
			mib->process_request(req);
		}
		else {
			mib->cleanup();
		}
	}
	delete mib;
	delete reqlist;
	delete snmp_server;
	Snmp::socket_cleanup();  // Shut down socket subsystem
	return 0;
}

效果

在这里插入图片描述

libsnmp.lib,libagent.lib 编译方法可参考
https://blog.csdn.net/weixin_41817169/article/details/118960812

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值