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