23,217
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
int main(int argc, char **argv)
{
struct snmp_session session, *sess_handle;
struct snmp_pdu *pdu, *response;
struct variable_list *vars;
const char *aOidName[2] = {"SNMPv2-MIB::sysObjectID.0", "SNMPv2-MIB::sysDescr.0"};
oid aOid[2][MAX_OID_LEN];
size_t aLen[2] = {MAX_OID_LEN, MAX_OID_LEN};
int i, status;
int iErrno1 = 0, iErrno2 = 0;
char *pErr;
if( argv[1] == NULL )
{
printf("Please supply a hostname\n");
exit(1);
}
/* Initialize the SNMP library */
init_snmp("snmpapp");
/* Initialize a "session" that defines who we're going to talk to */
snmp_sess_init( &session );
session.version = SNMP_VERSION_2c;
session.community = "public";
session.community_len = strlen(session.community);
session.peername = argv[1];
session.timeout = (argc > 2 ? atol(argv[2]): -1);
/* Open the session */
if( (sess_handle = snmp_open(&session)) == NULL )
{
snmp_perror("snmp_open");
exit(1);
}
printf("timeout=%d\n", session.timeout);
/* Create the PDU for the data */
if( (pdu = snmp_pdu_create(SNMP_MSG_GET)) == NULL )
{
snmp_perror("snmp_pdu_create");
snmp_close(sess_handle);
exit(1);
}
/* Add variables */
for( i = 0; i < 2; i++ )
{
if( read_objid(aOidName[i], aOid[i], &(aLen[i])) == 0 || snmp_add_null_var(pdu, aOid[i], aLen[i]) == NULL )
{
snmp_perror("read_objid");
snmp_close(sess_handle);
exit(1);
}
}
/* For SNMP_MSG_SET, add a variable like this */
/* snmp_pdu_add_variable(pdu, name, name_length, ASN_INTEGER, &snmpsetvalue, sizeof(snmpsetvalue)); */
/* Send the request out, and read the response */
status = snmp_synch_response(sess_handle, pdu, &response);
printf("timeout=%d\n", sess_handle->timeout);
if( status != STAT_SUCCESS || response == NULL || response->errstat != SNMP_ERR_NOERROR )
{
snmp_error(sess_handle, &iErrno1, &iErrno2, &pErr);
fprintf(stderr, "errno1=%d, errno2=%d, emsg=%s\n", iErrno1, iErrno2, pErr);
free(pErr);
if( status == STAT_SUCCESS && response )
fprintf(stderr, "Error in packet, Reason: %s\n", snmp_errstring(response->errstat));
else
snmp_sess_perror("snmpget", sess_handle);
exit(1);
}
for( vars = response->variables; vars; vars = vars->next_variable )
print_value(vars->name, vars->name_length, vars);
snmp_free_pdu(response);
snmp_close(sess_handle);
return (0);
}
void
agentx_parse_agentx_timeout(const char *token, char *cptr)
{
int x = netsnmp_string_time_to_secs(cptr);
DEBUGMSGTL(("agentx/config/timeout", "%s\n", cptr));
if (x == -1) {
config_perror("Invalid timeout value");
return;
}
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_AGENTX_TIMEOUT, x * ONE_SEC);
}
#define ONE_SEC 1000000L
/**
* Takes a time string like 4h and converts it to seconds.
* The string time given may end in 's' for seconds (the default
* anyway if no suffix is specified),
* 'm' for minutes, 'h' for hours, 'd' for days, or 'w' for weeks. The
* upper case versions are also accepted.
*
* @param time_string The time string to convert.
*
* @return seconds converted from the string
* @return -1 : on failure
*/
int
netsnmp_string_time_to_secs(const char *time_string) {
int secs = -1;
if (!time_string || !time_string[0])
return secs;
secs = atoi(time_string);
if (isdigit((unsigned char)time_string[strlen(time_string)-1]))
return secs; /* no letter specified, it's already in seconds */
switch (time_string[strlen(time_string)-1]) {
case 's':
case 'S':
/* already in seconds */
break;
case 'm':
case 'M':
secs = secs * 60;
break;
case 'h':
case 'H':
secs = secs * 60 * 60;
break;
case 'd':
case 'D':
secs = secs * 60 * 60 * 24;
break;
case 'w':
case 'W':
secs = secs * 60 * 60 * 24 * 7;
break;
default:
snmp_log(LOG_ERR, "time string %s contains an invalid suffix letter\n",
time_string);
return -1;
}
DEBUGMSGTL(("string_time_to_secs", "Converted time string %s to %d\n",
time_string, secs));
return secs;
}
session.timeout = (snmp_timeout * 1000); /* net-snmp likes microseconds */
参考
http://sourcecodebrowser.com/cacti-spine/0.8.7c/snmp_8c.html