Hello,
 
I've been searching and working my way with net-snmp, I've installed it, 
configure it and everything was working smoothly.
 
Then I tried to program a C++ API to get it to poll several server using snmp 
agents and gather information in a database. On the net-snmp tutorial website, 
there is a example code about asynchronous polling which is perfect for my 
demand, but is coded in C.
I tried compiling it with C++, but I get one error that makes my application 
fail, and this error does not show if I compile in C.
I hope you could help me with this matter, here is my code:
 
/*
* SNMPStats.cpp
*
* Created on: 2008-07-23
* Author: XXXXXXXXX
*
* Description: Retreives the stats from all CCS servers and process, and
* then outputs it in the database
*
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <mysql.h>
#include <stdio.h>
#include <string.h>

 
 
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif

 

/*
* a list of hosts to query
*/
struct host {
const char *name;
const char *community;
} 

hosts[] = {
{ "10.255.9.10", "newpublic" },
{ NULL }
};

/*
* a list of variables to query for
*/
struct oid {
const char *Name;
oid Oid[MAX_OID_LEN];
int OidLen;
}
oids[] = {
{ "ASTERISK-MIB::astChanTypeChannels.1" },
{ "SNMPv2-MIB::sysDescr.0" },
{ "ASTERISK-MIB::astChanTypeName.1" },
{ "ASTERISK-MIB::astChanTypeName.2"},
{ "ASTERISK-MIB::astChanTypeName.3" },
{ "ASTERISK-MIB::astChanTypeName.4" },
{ "ASTERISK-MIB::astNumChanTypes.0" },
{ "IF-MIB::ifNumber.0" },
{ NULL }
};

/*
* initialize
*/
void initialize (void)
{
struct oid *op = oids;

/* Win32: init winsock */
SOCK_STARTUP;

/* initialize library */
init_snmp("asynchapp");

/* parse the oids */
while (op->Name) {
op->OidLen = sizeof(op->Oid)/sizeof(op->Oid[0]);
if (!read_objid(op->Name, op->Oid, &op->OidLen)) {
snmp_perror("read_objid");
exit(1);
}
op++;
}
}

/*
* simple printing of returned data
*/
int print_result (int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
char buf[1024];
struct variable_list *vp;
int ix;
char var1[1024], var2[200], var3[200], var4[200];
char cmd1[10], cmd2[10], cmd3[10], cmd4[10], cmd5[10];
int result, i;
double fp;
struct timeval now;
struct timezone tz;
struct tm *tm;


MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;


char *server = "10.255.9.10";
char *user = "aheevaccs";
char *password = "aheevaccs"; /* set me first */
char *database = "menagerie";

conn = mysql_init(NULL);

/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

/* send SQL query */
if (!mysql_query(conn, "CREATE TABLE SNMP (Serveur VARCHAR(50), Composante 
VARCHAR(50), Metrique VARCHAR(50), Valeur VARCHAR(50))")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

 
gettimeofday(&now, &tz);
tm = localtime(&now.tv_sec);
fprintf(stdout, "%.2d:%.2d:%.2d.%.6d ", 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);
sscanf(buf,"%s", var2);
fprintf(stdout,"%s: %s\n", sp->peername, var2);

if ( strcmp(var2, "ASTERISK-MIB::astChanTypeChannels.1")==0)
{
fprintf(stdout, "%s: %s\n","Nothing to Print", var1);
// mysql_query(conn, "REPLACE INTO SNMP VALUES('10.255.9.10', 'Asterisk', 
'System Descritption', 'looooooasdsadsaooool')");

}


if( strcmp(var2, "SNMPv2-MIB::sysDescr.0")==0)
{
sscanf(buf, "%s %*s %s %s %s %s %s %s %*s %s", var1);
fprintf(stdout,"%s: %s\n", sp->peername, var1);
// strcpy(cmd1, var1);

const char *sql_replace= "REPLACE INTO SNMP 
VALUES('0.0.0.0','Asterisk','descrittion','\%s')";
char query[4092];
sprintf(query, sql_replace, var1);
mysql_query(conn, query);

}



else if( strcmp(var2, "ASTERISK-MIB::astChanTypeName.1")==0)
{
fprintf(stdout,"i am here");
sscanf(buf, "%s %*s %s %*s %s %s %*s %s %s %s %s", var1);
fprintf(stdout,"%s: %s\n", sp->peername, var1);


const char *sql_replace= "REPLACE INTO SNMP 
VALUES('0.0.0.0','Asterisk','Channel','\%s')";
char query[4092];
sprintf(query, sql_replace, var1);
mysql_query(conn, query);
}

else if( strcmp(var2, "ASTERISK-MIB::astChanTypeName.2")==0)
{
sscanf(buf, "%s %*s %*s %s %s %*s %s %s %s %s", var1);
// fprintf(stdout,"%s: %s\n", sp->peername, var1);
strcpy(cmd3, var1);
// mysql_query(conn, "REPLACE INTO SNMP VALUES('10.255.9.10', 'Asterisk', 
'Channel Type Name', cmd3)");
}

else if( strcmp(var2, "ASTERISK-MIB::astChanTypeName.3")==0)
{
sscanf(buf, "%s %*s %*s %s %s %*s %s %s %s %s", var1);
// fprintf(stdout,"%s: %s\n", sp->peername, var1);
strcpy(cmd4, var1);
// mysql_query(conn, "REPLACE INTO SNMP VALUES('10.255.9.10', 'Asterisk', 
'Channel Type Name', cmd4)");
}

else if( strcmp(var2, "ASTERISK-MIB::astChanTypeName.4")==0)
{
sscanf(buf, "%s %*s %*s %s %s %*s %s %s %s %s", var1);
// fprintf(stdout,"%s: %s\n", sp->peername, var1);
strcpy(cmd5, var1);
// mysql_query(conn, "REPLACE INTO SNMP VALUES('10.255.9.10', 'Asterisk', 
'Channel Type Name', cmd5)");
}


// res = mysql_use_result(conn);

/* close connection */
// mysql_free_result(res);
// mysql_close(conn);

///////////A VOIR!!!???////////////////



// sscanf(buf, "%s %*s %*s %s %s %*s %s %s %s %s", var1);
//fp = atof(var1);
//i = atoi(var2);
// fprintf(stdout,"%s: %s\n", sp->peername, var1);
// fprintf(stdout,"%s: %s\n", var2);
//fprintf(stdout,"%s: %s\n", var3);
//fprintf(stdout,"%s: %s\n", var4);

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;
}

 
/*****************************************************************************/

/*
* poll all hosts in parallel
*/
struct session {
struct snmp_session *sess; /* SNMP session data */
struct oid *current_oid; /* How far in our poll are we */
}

sessions[sizeof(hosts)/sizeof(hosts[0])];

int active_hosts; /* hosts that we have not completed */

/*
* response handler
*/
int asynch_response(int operation, struct snmp_session *sp, int reqid,
struct snmp_pdu *pdu, void *magic)
{
struct session *host = (struct session *)magic;
struct snmp_pdu *req;

if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {

if (print_result(STAT_SUCCESS, host->sess, pdu)) {
host->current_oid++; /* send next GET (if any) */
if (host->current_oid->Name) {
req = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen);
if (snmp_send(host->sess, req))
return 1;
else {
snmp_perror("snmp_send");
snmp_free_pdu(req);
}
}
}
}
else
print_result(STAT_TIMEOUT, host->sess, pdu);

/* something went wrong (or end of variables) 
* this host not active any more
*/
active_hosts--;
return 1;
}

void asynchronous(void)
{
struct session *hs;
struct host *hp;

/* startup all hosts */

for (hs = sessions, hp = hosts; hp->name; hs++, hp++) {
struct snmp_pdu *req;
struct snmp_session sess;
snmp_sess_init(&sess); /* initialize session */
sess.version = SNMP_VERSION_2c;
sess.peername = strdup(hp->name);

// unsigned char comm[50]= hp->community;

sess.community = strdup(hp->community); 
sess.community_len = strlen(hp->community);
sess.callback = asynch_response; /* default callback */
sess.callback_magic = hs;
if (!(hs->sess = snmp_open(&sess))) {
snmp_perror("snmp_open");
continue;
}
hs->current_oid = oids;
req = snmp_pdu_create(SNMP_MSG_GET); /* send the first GET */
snmp_add_null_var(req, hs->current_oid->Oid, hs->current_oid->OidLen);
if (snmp_send(hs->sess, req))
active_hosts++;
else {
snmp_perror("snmp_send");
snmp_free_pdu(req);
}
}

/* loop while any active hosts */

while (active_hosts) {
int fds = 0, block = 1;
fd_set fdset;
struct timeval timeout;

FD_ZERO(&fdset);
snmp_select_info(&fds, &fdset, &timeout, &block);
fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
if (fds < 0) {
perror("select failed");
exit(1);
}
if (fds)
snmp_read(&fdset);
else
snmp_timeout();
}

/* cleanup */

for (hp = hosts, hs = sessions; hp->name; hs++, hp++) {
if (hs->sess) snmp_close(hs->sess);
}
}

/*****************************************************************************/

int main (int argc, char **argv)
{
initialize();

printf("---------- asynchronous -----------\n");
asynchronous();

return 0;
}
 
And the errors I get are for the struc oid:
 
g++ -I/usr/local/include -I/usr/include/mysql -g   -c Testincpp.cpp -o 
Testincpp.oTestincpp.cpp:41: error: conflicting declaration 'struct 
oid'/usr/include/net-snmp/types.h:47: error: 'oid' has a previous declaration 
as `typedef u_long oid'Testincpp.cpp:43: error: field `Oid' has incomplete 
typeTestincpp.cpp: In function `void initialize()':Testincpp.cpp:73: error: 
'struct oid' has no member named 'Oid'Testincpp.cpp:73: error: 'struct oid' has 
no member named 'Oid'Testincpp.cpp:74: error: 'struct oid' has no member named 
'Oid'Testincpp.cpp: In function `int asynch_response(int, snmp_session*, int, 
snmp_pdu*, void*)':Testincpp.cpp:273: error: 'struct oid' has no member named 
'Oid'Testincpp.cpp: In function `void asynchronous()':Testincpp.cpp:309: error: 
invalid conversion from `char*' to `u_char*'Testincpp.cpp:319: error: 'struct 
oid' has no member named 'Oid'
 
I hope you would be able to help me, I've been stuck on this 3 days now..
 
Thank you for your time,
 
Christophe Melki

_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. 
It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to