-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnetSnmpIETFWGTable_subagent.c
219 lines (195 loc) · 5.38 KB
/
netSnmpIETFWGTable_subagent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Note: this file originally auto-generated by mib2c using
* version $ of $
*/
/*
* standard Net-SNMP includes
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
/*
* include our parent header
*/
#include "netSnmpIETFWGTable.h"
#include <signal.h>
#include "mqtt-handler.h"
/*
* If compiling within the net-snmp source code, this will trigger the feature
* detection mechansim to ensure the agent_check_and_process() function
* is left available even if --enable-minimialist is turned on. If you
* have configured net-snmp using --enable-minimialist and want to compile
* this code externally to the Net-SNMP code base, then please add
* --with-features="agent_check_and_process enable_stderrlog" to your
* configure line.
*/
netsnmp_feature_require(agent_check_and_process)
netsnmp_feature_require(enable_stderrlog)
static int keep_running;
static RETSIGTYPE stop_server(int a) {
keep_running = 0;
}
static void
usage(void) {
printf
("usage: netSnmpIETFWGTable [-D<tokens>] [-f] [-L] [-M] [-H] [LISTENING ADDRESSES]\n"
"\t-f Do not fork() from the calling shell.\n"
"\t-DTOKEN[,TOKEN,...]\n"
"\t\tTurn on debugging output for the given TOKEN(s).\n"
"\t\tWithout any tokens specified, it defaults to printing\n"
"\t\tall the tokens (which is equivalent to the keyword 'ALL').\n"
"\t\tYou might want to try ALL for extremely verbose output.\n"
"\t\tNote: You can't put a space between the -D and the TOKENs.\n"
"\t-H\tDisplay a list of configuration file directives\n"
"\t\tunderstood by the agent and then exit.\n"
"\t-M\tRun as a normal SNMP Agent instead of an AgentX sub-agent.\n"
"\t-x ADDRESS\tconnect to master agent at ADDRESS (default /var/agentx/master).\n"
"\t-L\tDo not open a log file; print all messages to stderr.\n");
exit(0);
}
int
main(int argc, char **argv) {
int agentx_subagent = 1; /* change this if you want to be a SNMP master agent */
/*
* Defs for arg-handling code: handles setting of policy-related variables
*/
int ch;
extern char *optarg;
int dont_fork = 0, use_syslog = 0;
char *agentx_socket = NULL;
struct snmp_mqtt_ietfwg_state_ state;
while ((ch = getopt(argc, argv, "D:fHLMx:")) != EOF)
switch (ch) {
case 'D':
debug_register_tokens(optarg);
snmp_set_do_debugging(1);
break;
case 'f':
dont_fork = 1;
break;
case 'H':
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
init_agent("netSnmpIETFWGTable"); /* register our .conf handlers */
init_netSnmpIETFWGTable();
init_snmp("netSnmpIETFWGTable");
fprintf(stderr, "Configuration directives understood:\n");
read_config_print_usage(" ");
exit(0);
case 'M':
agentx_subagent = 0;
break;
case 'L':
use_syslog = 0; /* use stderr */
break;
case 'x':
agentx_socket = optarg;
break;
default:
fprintf(stderr, "unknown option %c\n", ch);
usage();
}
if (optind < argc) {
int i;
/*
* There are optional transport addresses on the command line.
*/
DEBUGMSGTL(("snmpd/main", "optind %d, argc %d\n", optind, argc));
for (i = optind; i < argc; i++) {
char *c, *astring;
if ((c = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PORTS))) {
astring = malloc(strlen(c) + 2 + strlen(argv[i]));
if (astring == NULL) {
fprintf(stderr, "malloc failure processing argv[%d]\n",
i);
exit(1);
}
sprintf(astring, "%s,%s", c, argv[i]);
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PORTS, astring);
SNMP_FREE(astring);
} else {
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PORTS, argv[i]);
}
}
DEBUGMSGTL(("snmpd/main", "port spec: %s\n",
netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PORTS)));
}
/*
* we're an agentx subagent?
*/
if (agentx_subagent) {
/*
* make us a agentx client.
*/
netsnmp_enable_subagent();
if (NULL != agentx_socket)
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_X_SOCKET,
agentx_socket);
}
snmp_disable_log();
if (use_syslog)
snmp_enable_calllog();
else
snmp_enable_stderrlog();
/*
* daemonize
*/
if (!dont_fork) {
int rc = netsnmp_daemonize(1, !use_syslog);
if (rc)
exit(-1);
}
/*
* initialize tcp/ip if necessary
*/
SOCK_STARTUP;
/*
* initialize the agent library
*/
init_agent("netSnmpIETFWGTable");
/*
* init netSnmpIETFWGTable mib code
*/
state.ietfwg_tdata = init_netSnmpIETFWGTable();
/*
* read netSnmpIETFWGTable.conf files.
*/
init_snmp("netSnmpIETFWGTable");
/*
* 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);
mosq_snmp_setup(&state);
/*
* you're main loop here...
*/
while (keep_running) {
/*
* if you use select(), see snmp_select_info() in snmp_api(3)
*/
/*
* --- OR ---
*/
mosquitto_loop(state.mosq, 50, 1);
agent_check_and_process(0); /* 0 == don't block */
}
/*
* at shutdown time
*/
snmp_shutdown("netSnmpIETFWGTable");
SOCK_CLEANUP;
exit(0);
}