#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/snmp_api.h>
#include <net-snmp/agent/ds_agent.h>
int netsnmp_running = 1;
/************************************************************************
**
**
************************************************************************/
int snmp_input(int op, netsnmp_session *session, int reqid, netsnmp_pdu *pdu, void *magic)
{
printf("We got a trap:\n");
struct variable_list *vars;
for(vars = pdu->variables; vars; vars = vars->next_variable)
{
print_variable(vars->name, vars->name_length, vars);
}
return 1;
}
static int pre_parse(netsnmp_session * session, netsnmp_transport *transport,
void *transport_data, int transport_data_length)
{
return 1;
}
static netsnmp_session *snmptrapd_add_session(netsnmp_transport *t)
{
netsnmp_session sess, *session = &sess, *rc = NULL;
snmp_sess_init(session);
session->peername = SNMP_DEFAULT_PEERNAME; /* Original code had NULL here */
session->version = SNMP_DEFAULT_VERSION;
session->community_len = SNMP_DEFAULT_COMMUNITY_LEN;
session->retries = SNMP_DEFAULT_RETRIES;
session->timeout = SNMP_DEFAULT_TIMEOUT;
session->callback = snmp_input;
session->callback_magic = (void *) t;
session->authenticator = NULL;
sess.isAuthoritative = SNMP_SESS_UNKNOWNAUTH;
rc = snmp_add(session, t, pre_parse, NULL);
if (rc == NULL)
{
snmp_sess_perror("snmptrapd", session);
}
return rc;
}
static void snmptrapd_close_sessions(netsnmp_session * sess_list)
{
netsnmp_session *s = NULL, *next = NULL;
for (s = sess_list; s != NULL; s = next)
{
next = s->next;
snmp_close(s);
}
}
static void snmptrapd_main_loop(void)
{
int count, numfds, block;
fd_set readfds,writefds,exceptfds;
struct timeval timeout, *tvp;
printf("snmptrapd_main_loop\n");
while (netsnmp_running)
{
numfds = 0;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
block = 0;
tvp = &timeout;
timerclear(tvp);
tvp->tv_sec = 5;
snmp_select_info(&numfds, &readfds, tvp, &block);
if (block == 1)
{
tvp = NULL; /* block without timeout */
}
#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
netsnmp_external_event_info(&numfds, &readfds, &writefds, &exceptfds);
#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
count = select(numfds, &readfds, &writefds, &exceptfds, tvp);
printf("count = %d\n", count);
if (count > 0)
{
#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
netsnmp_dispatch_external_events(&count, &readfds, &writefds, &exceptfds);
#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
/* If there are any more events after external events, then try SNMP events. */
if (count > 0)
{
snmp_read(&readfds);
}
}
else
{
switch (count)
{
case 0:
snmp_timeout();
break;
case -1:
//if (errno == EINTR) continue; // ??
snmp_log_perror("select");
netsnmp_running = 0;
break;
default:
fprintf(stderr, "select returned %d\n", count);
netsnmp_running = 0;
}
}
run_alarms();
}
}
/************************************************************************
**
**
* Returns:
* 2 Always succeeds. (?)
*
*
* Setup and start the trap receiver daemon.
*
* Also successfully EXITs with zero for some options.
*
************************************************************************/
int main()
{
netsnmp_session *sess_list = NULL, *ss = NULL;
netsnmp_transport *transport = NULL;
int arg, i = 0;
int uid = 0, gid = 0;
char *cp, *listen_ports = NULL;
#if defined(USING_AGENTX_SUBAGENT_MODULE) && !defined(NETSNMP_SNMPTRAPD_DISABLE_AGENTX)
int agentx_subagent = 1;
#endif
/*
* Initialize the world.
*/
init_snmp("snmptrapd");
if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_QUIT_IMMEDIATELY))
{
/*
* just starting up to process specific configuration and then
* shutting down immediately.
*/
netsnmp_running = 0;
}
/*
* if no logging options on command line or in conf files, use syslog
*/
if (0 == snmp_get_do_logging())
{
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
snmp_enable_syslog_ident("snmptrapd", LOG_DAEMON);
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
}
if (listen_ports)
{
cp = listen_ports;
}
else
{
cp = "udp:162"; /* Default default port */;
}
while (cp != NULL)
{
char *sep = strchr(cp, ',');
if (sep != NULL)
{
*sep = 0;
}
transport = netsnmp_transport_open_server("snmptrap", cp); //cp=udp:162
if (transport == NULL)
{
//snmp_log(LOG_ERR, "couldn't open %s -- errno %d (\"%s\")\n", cp, errno, strerror(errno));
printf("ERR: couldn't open %s ", cp);
snmptrapd_close_sessions(sess_list);
SOCK_CLEANUP;
return(-1);
}
else
{
ss = snmptrapd_add_session(transport);
if (ss == NULL)
{
/*
* Shouldn't happen? We have already opened the transport
* successfully, so what could have gone wrong?
*/
snmptrapd_close_sessions(sess_list);
netsnmp_transport_free(transport);
//snmp_log(LOG_ERR, "couldn't open snmp - %s", strerror(errno));
printf("couldn't open snmp");
SOCK_CLEANUP;
return(-1);
}
else
{
ss->next = sess_list;
sess_list = ss;
}
}
/*
* Process next listen address, if there is one.
*/
if (sep != NULL)
{
*sep = ',';
cp = sep + 1;
}
else
{
cp = NULL;
}
}
printf("snmp free listen_ports\n");
SNMP_FREE(listen_ports); /* done with them */
#ifdef NETSNMP_USE_MYSQL
if( netsnmp_mysql_init() )
{
fprintf(stderr, "MySQL initialization failed\n");
return(-1);
}
#endif
snmp_log(LOG_INFO, "NET-SNMP version %s\n", netsnmp_get_version());
snmptrapd_main_loop();
if (snmp_get_do_logging())
{
struct tm *tm;
time_t timer;
time(&timer);
tm = localtime(&timer);
snmp_log(LOG_INFO,
"%.4d-%.2d-%.2d %.2d:%.2d:%.2d NET-SNMP version %s Stopped.\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
tm->tm_min, tm->tm_sec, netsnmp_get_version());
}
snmp_log(LOG_INFO, "Stopping snmptrapd\n");
snmptrapd_close_sessions(sess_list);
snmp_shutdown("snmptrapd");
snmp_disable_log();
SOCK_CLEANUP;
return 2;
}
- 1
- 2
- 3
- 4
- 5
前往页