- 论坛徽章:
- 0
|
Author: Tony Qian
Date: May 1st, 2006
Contact: tonyqian@gmail.com
Net-SNMP provide traditional API usage and Single API Usage, however their example online only provide tradition usage, thereby I change the code a little to make it a single api usage.
The advantage for single api use is thread-safe.
You can enable mutiple thread for get set and query.
In this example I use asynchr sending with select and call back support . This is the original post and please mention the original when you post anywhere else
Here is the command to compile
gcc -I/usr/local/include -g -c -o snmp1.o snmp1.c
gcc -o snmp1 snmp1.o -L/usr/local/lib -lm -lnetsnmp -lssl -lcrypto
Here is the code
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
int active_hosts = 1;
int print_result (int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
char buf[1024];
struct variable_list *vp;
int ix;
struct timeval now;
struct timezone tz;
struct tm *tm;
gettimeofday(&now, &tz);
tm = localtime(&now.tv_sec);
fprintf(stdout, "%.2d:%.2d:%.2d.%.6li ", tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec);
switch (status) {
case STAT_SUCCESS:
vp = pdu->variables;
if (pdu->errstat == SNMP_ERR_NOERROR) {
while (vp) {
snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
fprintf(stdout, "%s: %s\n", sp->peername, buf);
vp = vp->next_variable;
}
}
else {
for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++)
;
if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
else strcpy(buf, "(none)");
fprintf(stdout, "%s: %s: %s\n",
sp->peername, buf, snmp_errstring(pdu->errstat));
}
return 1;
case STAT_TIMEOUT:
fprintf(stdout, "%s: Timeout\n", sp->peername);
return 0;
case STAT_ERROR:
snmp_perror(sp->peername);
return 0;
}
return 0;
}
int asynch_response(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic)
{
struct snmp_session *sess = (struct snmp_session *)magic;
if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
if (print_result(STAT_SUCCESS, sess, pdu)) {
}
}else{
print_result(STAT_TIMEOUT, sess, pdu);
}
/* something went wrong (or end of variables)
* this host not active any more
*/
active_hosts--;
return 1;
}
int main(int argc, char **argv)
{
int liberr, syserr;
char *errstr;
void *sessp; /* <-- an opaque pointer, not a struct pointer */
struct snmp_session Session;
struct snmp_session *sptr;
struct snmp_pdu *pdu;
oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;
//struct variable_list *vars;
/*
* Initialize the SNMP library
*/
init_snmp("snmputil");
/*
* Initialize a "session" defines who we're going to talk to
*/
snmp_sess_init(&Session);
Session.peername = strdup("10.255.51.2");
Session.version = SNMP_VERSION_2c;
Session.callback = asynch_response;
//Session.callback_magic = sptr;
Session.community = strdup("public");
//Session.community_len = strlen(Session.community);
sessp = snmp_sess_open(&Session);
if (sessp == NULL) {
/* Error codes found in open calling argument */
snmp_error(&Session, &liberr, &syserr, &errstr);
printf("SNMP create error %s.\n", errstr);
free(errstr);
return 0;
}
sptr = snmp_sess_session(sessp); /* <-- get the snmp_session pointer */
/* Pass sptr to snmp_sess_error from here forward */
/* Change the community name */
//printf("TEST:%s\n",sptr->peername);
free(sptr->community);
sptr->community = strdup("public");
sptr->community_len = strlen("public");
sptr->callback_magic = sptr;
/*
* Create the PDU for the data for our request.
* 1) We're going to GET the system.sysDescr.0 node.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
#if OTHER_METHODS
get_node("sysDescr.0", anOID, &anOID_len);
read_objid("system.sysDescr.0", anOID, &anOID_len);
#endif
snmp_add_null_var(pdu, anOID, anOID_len);
if (0 == snmp_sess_send(sessp, pdu)) {
snmp_sess_error(sessp, &liberr, &syserr, &errstr);
printf("SNMP write error %s.\n", errstr);
free(errstr);
return 0;
}
while (active_hosts) {
int fds = 0, block = 1;
fd_set fdset;
struct timeval timeout;
FD_ZERO(&fdset);
snmp_sess_select_info(sessp, &fds, &fdset, &timeout, &block);
fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
if (fds < 0) {
perror("select failed");
exit(1);
}
if (fds)
snmp_sess_read(sessp, &fdset);
else
snmp_sess_timeout(sessp);
}
snmp_sess_close(sessp);
SOCK_CLEANUP;
return (0);
}
Sample Text |
|