windows下使用net-snmp实现agent扩展(一)
生活随笔
收集整理的這篇文章主要介紹了
windows下使用net-snmp实现agent扩展(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目上需要用snmp來做告警監控管理,達到對系統的運行狀態的監測。這幾天研究了一下,發現網上資料比較少,大多數抄來抄去,能夠正確運行的更少。所以,總結了一下,把相關的代碼放上來,希望能夠幫助同樣遇到困惑的朋友。 havenzhao?
http://vcsky.net
[cpp] view plaincopy print? #include?<net-snmp/net-snmp-config.h>??? #include?<net-snmp/net-snmp-includes.h>??? #include?<net-snmp/agent/net-snmp-agent-includes.h>??? #include?<signal.h>?? #include?"nstAgentSubagentObject.h"?? static?int?keep_running;?? RETSIGTYPE??? stop_server(int?a)?{??? ??????????keep_running?=?0;??? }?? int?main?(int?argc,?char?**argv)?{??? ????????//int?agentx_subagent=1;?/*?change?this?if?you?want?to?be?a?SNMP?master?agent?*/??? ????????int?agentx_subagent=0;??? ????????int?background?=?0;?/*?change?this?if?you?want?to?run?in?the?background?*/??? ????????int?syslog?=?0;?/*?change?this?if?you?want?to?use?syslog?*/?? ????????/*?print?log?errors?to?syslog?or?stderr?*/??? ????????if?(syslog)??? ?????????;??? ??????????//snmp_enable_calllog();??? ????????else??? ??????????snmp_enable_stderrlog();?? ????????/*?we're?an?agentx?subagent??*/??? ????????if?(agentx_subagent)?{??? ??????????/*?make?us?a?agentx?client.?*/??? ??????????netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,?NETSNMP_DS_AGENT_ROLE,?1);??? ????????}?? ????????/*?run?in?background,?if?requested?*/??? ????????if?(background?&&?netsnmp_daemonize(1,?!syslog))??? ????????????exit(1);?? ????????/*?initialize?tcpip,?if?necessary?*/??? ????????SOCK_STARTUP;?? ????????/*?initialize?the?agent?library?*/??? ????????init_agent("example-demon");?? ????????/*?initialize?mib?code?here?*/?? ????????/*?mib?code:?init_nstAgentSubagentObject?from?nstAgentSubagentObject.C?*/??? ????????init_nstAgentSubagentObject();??? ????????/*?initialize?vacm/usm?access?control????????*/??? ????????if?(!agentx_subagent)?{??? ????????????init_vacm_vars();??? ????????????init_usmUser();??? ????????}?? ????????/*?example-demon?will?be?used?to?read?example-demon.conf?files.?*/??? ????????/*在這里讀取一個example-demon.conf的配置文件,這是關鍵*/??? ????????init_snmp("example-demon");?? ????????/*?If?we're?going?to?be?a?snmp?master?agent,?initial?the?ports?*/??? ????????if?(!agentx_subagent)??? ??????????init_master_agent();????????/*?open?the?port?to?listen?on?(defaults?to?udp:161)?*/?? ????????/*?In?case?we?recevie?a?request?to?stop?(kill?-TERM?or?kill?-INT)?*/??? ????????keep_running?=?1;??? ????????signal(SIGTERM,?stop_server);??? ????????signal(SIGINT,?stop_server);?? ????????snmp_log(LOG_INFO,"example-demon?is?up?and?running.\n");?? ????????/*?your?main?loop?here...?*/??? ????????while(keep_running)?{??? ??????????/*?if?you?use?select(),?see?snmp_select_info()?in?snmp_api(3)?*/??? ??????????/*???????????---?OR?---????????*/??? ??????????agent_check_and_process(1);?/*?0?==?don't?block?*/??? ????????}?? ????????/*?at?shutdown?time?*/??? ????????snmp_shutdown("example-demon");??? ????????SOCK_CLEANUP;?? ????????return?0;??? }??
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <signal.h>
#include "nstAgentSubagentObject.h"
static int keep_running;
RETSIGTYPE
stop_server(int a) { keep_running = 0;
}
int main (int argc, char **argv) { //int agentx_subagent=1; /* change this if you want to be a SNMP master agent */ int agentx_subagent=0; int background = 0; /* change this if you want to run in the background */ int syslog = 0; /* change this if you want to use syslog *//* print log errors to syslog or stderr */ if (syslog) ; //snmp_enable_calllog(); else snmp_enable_stderrlog();/* we're an agentx subagent? */ if (agentx_subagent) { /* make us a agentx client. */ netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); }/* run in background, if requested */ if (background && netsnmp_daemonize(1, !syslog)) exit(1);/* initialize tcpip, if necessary */ SOCK_STARTUP;/* initialize the agent library */ init_agent("example-demon");/* initialize mib code here *//* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */ init_nstAgentSubagentObject(); /* initialize vacm/usm access control */ if (!agentx_subagent) { init_vacm_vars(); init_usmUser(); }/* example-demon will be used to read example-demon.conf files. */ /*在這里讀取一個example-demon.conf的配置文件,這是關鍵*/ init_snmp("example-demon");/* If we're going to be a snmp master agent, initial the ports */ if (!agentx_subagent) init_master_agent(); /* open the port to listen on (defaults to udp:161) *//* In case we recevie a request to stop (kill -TERM or kill -INT) */ keep_running = 1; signal(SIGTERM, stop_server); signal(SIGINT, stop_server);snmp_log(LOG_INFO,"example-demon is up and running.\n");/* your main loop here... */ while(keep_running) { /* if you use select(), see snmp_select_info() in snmp_api(3) */ /* --- OR --- */ agent_check_and_process(1); /* 0 == don't block */ }/* at shutdown time */ snmp_shutdown("example-demon"); SOCK_CLEANUP;return 0;
}
nstAgentSubagentObject.h [cpp] view plaincopy print?/*?? *?Note:?this?file?originally?auto-generated?by?mib2c?using?? *??????????????:?mib2c.int_watch.conf,v?5.0?2002/04/20?07:30:13?hardaker?Exp?$?? */??? #ifndef?NSTAGENTSUBAGENTOBJECT_H??? #define?NSTAGENTSUBAGENTOBJECT_H?? /*?? *?function?declarations?? */??? void??????????????????init_nstAgentSubagentObject(void);?? #endif????????????????????????????????/*?NSTAGENTSUBAGENTOBJECT_H?*/??
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
*/
#ifndef NSTAGENTSUBAGENTOBJECT_H
#define NSTAGENTSUBAGENTOBJECT_H
/*
* function declarations
*/
void init_nstAgentSubagentObject(void);
#endif /* NSTAGENTSUBAGENTOBJECT_H */
nstAgentSubagentObject.c [cpp] view plaincopy print?/*?? *?Note:?this?file?originally?auto-generated?by?mib2c?using?? *??????????????:?mib2c.int_watch.conf,v?5.0?2002/04/20?07:30:13?hardaker?Exp?$?? */?? #include?<net-snmp/net-snmp-config.h>??? #include?<net-snmp/net-snmp-includes.h>??? #include?<net-snmp/agent/net-snmp-agent-includes.h>??? #include?"nstAgentSubagentObject.h"?? /*?? *?the?variable?we?want?to?tie?an?OID?to.????????The?agent?will?handle?all?? *?*?GET?and?SET?requests?to?this?variable?changing?it's?value?as?needed.?? */?? static?int??nstAgentSubagentObject?=?6;?? /*?? *?our?initialization?routine,?automatically?called?by?the?agent?? *?(to?get?called,?the?function?name?must?match?init_FILENAME())?? */??? void??? init_nstAgentSubagentObject(void)??? {??? ??????????static?oid????????????nstAgentSubagentObject_oid[]?=??? ??????????????{?1,?3,?6,?1,?4,?1,?8072,?2,?4,?1,?1,?2,?0?};?? ??????????/*?? ???????????*?a?debugging?statement.????????Run?the?agent?with?-DnstAgentSubagentObject?to?see?? ???????????*?the?output?of?this?debugging?statement.?? ???????????*/??? ??????????DEBUGMSGTL(("nstAgentSubagentObject",??? ??????????????????????"Initializing?the?nstAgentSubagentObject?module\n"));?? ??????????/*?? ???????????*?the?line?below?registers?our?variables?defined?above?as?? ???????????*?accessible?and?makes?it?writable.????????A?read?only?version?of?any?? ???????????*?of?these?registration?would?merely?call?? ???????????*?register_read_only_int_instance()?instead.????????The?functions?? ???????????*?called?below?should?be?consistent?with?your?MIB,?however.?? ???????????*?? ???????????*?If?we?wanted?a?callback?when?the?value?was?retrieved?or?set?? ???????????*?(even?though?the?details?of?doing?this?are?handled?for?you),?? ???????????*?you?could?change?the?NULL?pointer?below?to?a?valid?handler?? ???????????*?function.?? ???????????*/??? ??????????DEBUGMSGTL(("nstAgentSubagentObject",??? ??????????????????????"Initalizing?nstAgentSubagentObject?scalar?integer.????????Default?value?=?%d\n",??? ??????????????????????nstAgentSubagentObject));?? ??????????netsnmp_register_int_instance("nstAgentSubagentObject",??? ????????????????????????????????????????nstAgentSubagentObject_oid,??? ????????????????????????????????????????OID_LENGTH(nstAgentSubagentObject_oid),??? ????????????????????????????????????????&nstAgentSubagentObject,?NULL);?? ??????????DEBUGMSGTL(("nstAgentSubagentObject",??? ??????????????????????"Done?initalizing?nstAgentSubagentObject?module\n"));??? }???
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "nstAgentSubagentObject.h"
/*
* the variable we want to tie an OID to. The agent will handle all
* * GET and SET requests to this variable changing it's value as needed.
*/
static int nstAgentSubagentObject = 6;
/*
* our initialization routine, automatically called by the agent
* (to get called, the function name must match init_FILENAME())
*/
void
init_nstAgentSubagentObject(void)
{ static oid nstAgentSubagentObject_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 4, 1, 1, 2, 0 };/* * a debugging statement. Run the agent with -DnstAgentSubagentObject to see * the output of this debugging statement. */ DEBUGMSGTL(("nstAgentSubagentObject", "Initializing the nstAgentSubagentObject module\n"));/* * the line below registers our variables defined above as * accessible and makes it writable. A read only version of any * of these registration would merely call * register_read_only_int_instance() instead. The functions * called below should be consistent with your MIB, however. * * If we wanted a callback when the value was retrieved or set * (even though the details of doing this are handled for you), * you could change the NULL pointer below to a valid handler * function. */ DEBUGMSGTL(("nstAgentSubagentObject", "Initalizing nstAgentSubagentObject scalar integer. Default value = %d\n", nstAgentSubagentObject));netsnmp_register_int_instance("nstAgentSubagentObject", nstAgentSubagentObject_oid, OID_LENGTH(nstAgentSubagentObject_oid), &nstAgentSubagentObject, NULL);DEBUGMSGTL(("nstAgentSubagentObject", "Done initalizing nstAgentSubagentObject module\n"));
}
nstAgentSubagentObject.h [cpp] view plaincopy print?
nstAgentSubagentObject.c [cpp] view plaincopy print?
總結
以上是生活随笔為你收集整理的windows下使用net-snmp实现agent扩展(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OC学习篇之---归档和解挡
- 下一篇: 【CentOS】磁盘管理与vim编译器