Greetings guys,I am busy writing code, to run though some devices on a network and log and do a basic snmpwalk and log it to MySQL. I am fairly new to NET-SNMP API & C :-) The code is working so far, but I have a few things I am stuck with...
sprintf_objid = how do i only print the oid in numeric format? not the string value? (I want both to store string and numeric in two separate fields within mysql table) Is there a function or way I can find the original oid in numerical format?
Code attached, please advice... -- Christiaan Rademan - JNCIE #661 Mobile: +27 83 419 2078 E-mail: christiaan.rade...@gmail.com
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <time.h> #include <my_global.h> #include <mysql.h> #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include "knock.h" #define NETSNMP_DS_WALK_INCLUDE_REQUESTED 1 #define NETSNMP_DS_WALK_PRINT_STATISTICS 2 #define NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC 3 char lockfile[256] = "/var/lock/snmp2mysql.lck"; struct host { char name[255]; char community[255]; char id[255]; } hosts[255]; struct sql_snmp { char node[255]; char oid[255]; char type[255]; char value[255]; }; char server[255] = ""; char database[255] = ""; char sqluser[255] = ""; char sqlpass[255] = ""; void snmpwalk(char mib[50],char debug, int *insert_c, struct sql_snmp *insert, struct sql_snmp *insert2, struct snmp_session *sp, struct host *hp) { int running; int status; int count; netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; oid name[MAX_OID_LEN]; size_t name_length; oid root[MAX_OID_LEN]; size_t rootlen; char objid[255]; char value[255]; char test[255]; char *value_type; char *value_data; int a; oid objid_mib[7]; if (strcmp(mib,"system") == 0) { objid_mib[0] = 1; objid_mib[1] = 3; objid_mib[2] = 6; objid_mib[3] = 1; objid_mib[4] = 2; objid_mib[5] = 1; objid_mib[6] = 1; } if (strcmp(mib,"interfaces") == 0) { objid_mib[0] = 1; objid_mib[1] = 3; objid_mib[2] = 6; objid_mib[3] = 1; objid_mib[4] = 2; objid_mib[5] = 1; objid_mib[6] = 2; } for (a = 0; a < *insert_c; a++) { insert2++; } memmove(root, objid_mib, sizeof(objid_mib)); rootlen = sizeof(objid_mib) / sizeof(oid); memmove(name, root, rootlen * sizeof(oid)); name_length = rootlen; running = 1; while (running) { pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(pdu, name, name_length); status = snmp_synch_response(sp, pdu, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { for (vars = response->variables; vars; vars = vars->next_variable) { if ((vars->name_length < rootlen) || (memcmp(root, vars->name, rootlen * sizeof(oid)) != 0)) { running = 0; continue; } snprint_objid(objid,255,vars->name, vars->name_length); snprint_value(value,255,vars->name, vars->name_length, vars); snprint_variable(test,255,vars->name, vars->name_length, vars); value_type = strtok(value,":"); value_data = strtok(NULL,""); if (value_data) { if (value_type) { if (value_type[strlen(value_type) - 1] == '\n') { value_type[strlen(value_type) - 1] = '\0'; } if (debug == 'Y') { printf("Testing %s\n",test); printf("Node: [%s] %s Obj: %s Type: (%x) %s Value:%s\n",hp->id,hp->name,objid,vars->type,value_type,value_data); } strcpy(insert2->node,hp->id); strcpy(insert2->oid,objid); strcpy(insert2->type,value_type); strcpy(insert2->value,value_data); insert2++; (*insert_c)++; } } if ((vars->type != SNMP_ENDOFMIBVIEW) && (vars->type != SNMP_NOSUCHOBJECT) && (vars->type != SNMP_NOSUCHINSTANCE)) { if (snmp_oid_compare(name, name_length, vars->name, vars->name_length) >= 0) { fprintf(stderr, "ERROR: OID not increasing: "); fprint_objid(stderr, name, name_length); fprintf(stderr, " >= "); fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); running = 0; } memmove((char *) name, (char *) vars->name, vars->name_length * sizeof(oid)); name_length = vars->name_length; } else { running = 0; } } } else { running = 0; if (response->errstat == SNMP_ERR_NOSUCHNAME) { printf("End of MIB\n"); } else { fprintf(stderr, "ERROR in packet.\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errindex != 0) { fprintf(stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && count != response->errindex; vars = vars->next_variable, count++) { if (vars) { fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); } } } } } } else if (status == STAT_TIMEOUT) { fprintf(stderr, "Timeout: No Response from %s\n",hp->name); running = 0; } else { snmp_sess_perror("snmpwalk", sp); running = 0; } if (response) { snmp_free_pdu(response); } } } void scan(char debug) { struct host *hp; int pidstatus; int nodes_c = 0; MYSQL *conn; int a = 0; char sql_oid[255]; char sql_type[255]; char sql_value[255]; int insert_c = 0; char error[1024]; struct sql_snmp *insert; struct sql_snmp *insert2; int wait[5000]; char *snmpsql; for (hp = hosts; hp->name; hp++) { if (strcmp(hp->name,"") != 0) { pid_t pID = fork(); wait[nodes_c] = pID; if (pID == 0) { conn = mysql_init(NULL); if (conn == NULL) { printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); _exit(1); } if (mysql_real_connect(conn, server, sqluser, sqlpass, database, 0, NULL, 0) == NULL) { printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); _exit(1); } struct snmp_session ss, *sp; snmp_sess_init(&ss); ss.peername = strdup(hp->name); ss.version = SNMP_VERSION_2c; ss.retries = 3; ss.timeout = 500000; ss.community = (unsigned char *)strdup(hp->community); ss.community_len = strlen((char *)ss.community); if (!(sp = snmp_open(&ss))) { snmp_perror("snmp_open"); continue; } insert = (struct sql_snmp *)malloc(1000000 * sizeof(struct sql_snmp)); snmpsql = (char *)malloc(10000000); insert2 = insert; if (insert == NULL) { printf("ERROR: Memory Allocation Failed"); sprintf(error,"ERROR: Memory Allocation Failed"); logit(error); _exit(1); } if (snmpsql == NULL) { printf("ERROR: Memory Allocation Failed"); sprintf(error,"ERROR: Memory Allocation Failed"); logit(error); _exit(1); } snmpwalk("system",debug,&insert_c,insert,insert2,sp,hp); snmpwalk("interfaces",debug,&insert_c,insert,insert2,sp,hp); snmp_close(sp); if (insert_c > 0) { sprintf(snmpsql, "insert into snmp (node,datetime,oid,type,value) values"); for (a = 0, insert2 = insert; a < insert_c; a++,insert2++) { strcpy(sql_oid,insert2->oid); strcpy(sql_type,insert2->type); strcpy(sql_value,insert2->value); mysql_real_escape_string(conn, sql_oid, insert2->oid, strlen(insert2->oid)); mysql_real_escape_string(conn, sql_type, insert2->type, strlen(insert2->type)); mysql_real_escape_string(conn, sql_value, insert2->value, strlen(insert2->value)); if (a == 0) { sprintf(snmpsql, "%s (trim('%s'),now(),trim('%s'),trim('%s'),trim('%s'))",snmpsql,insert2->node,sql_oid,sql_type,sql_value); } else { sprintf(snmpsql, "%s, (trim('%s'),now(),trim('%s'),trim('%s'),trim('%s'))",snmpsql,insert2->node,sql_oid,sql_type,sql_value); } } sprintf(snmpsql, "%s;",snmpsql); if (debug == 'Y') printf("\n%s\n\n",snmpsql); if (mysql_real_query(conn, snmpsql, strlen(snmpsql)) != 0) { printf("\nERROR: Writing to database (%s)\n\n",snmpsql); sprintf(error,"ERROR: Writing to database"); logit(error); _exit(1); } } free(insert); free(snmpsql); _exit(0); } nodes_c++; } else { break; } } if (debug == 'Y') printf("Total Nodes %i\n",nodes_c); for (a = 0; a < nodes_c; a++) { if (debug == 'Y') printf("Waiting for child process: %i\n",wait[a]); waitpid(wait[a],&pidstatus,0); } } void help() { printf("KNOCK SNMP2MYSQL %s\n",appver); printf("EPICO Networks www.epico.co.za\n\n"); printf("Usage: snmp2mysql /../../knock.cnf debug\n\n"); } void archive(MYSQL *conn,char debug) { char sql[255]; char error[1024]; sprintf(sql,"insert into snmpold select * from snmp where datetime < (CURDATE() - INTERVAL 2 DAY);"); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); remove(lockfile); exit(1); } sprintf(sql,"delete from snmp where datetime < (CURDATE() - INTERVAL 2 DAY);"); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); remove(lockfile); exit(1); } } void update_int(MYSQL *conn,char *node,char *ifindex,char *interface,char update[255]) { MYSQL_RES *result; MYSQL_ROW row; char sql[255]; char s_update[255]; char s_ifindex[255]; char s_interface[255]; char s_value[255]; char error[1024]; int i_value; char ifalias[512] = ""; // UPDATE INTERFACE mysql_real_escape_string(conn, s_update, update, strlen(update)); mysql_real_escape_string(conn, s_ifindex, ifindex, strlen(ifindex)); if (strcmp(update,"ifDescr") == 0) { sprintf(sql,"select value from snmp where node = '%s' and oid = 'IF-MIB::ifAlias.%s' order by datetime desc limit 1",node,s_ifindex); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, ifalias, row[0], strlen(row[0])); } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } sprintf(sql,"select value from snmp where node = '%s' and oid = 'IF-MIB::%s.%s' order by datetime desc limit 1",node,s_update,s_ifindex); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, s_interface, interface, strlen(interface)); mysql_real_escape_string(conn, s_value, row[0], strlen(row[0])); if (strcmp(update,"ifHighSpeed") == 0) { if (strcmp(s_value,"") != 0) { i_value = atoi(s_value); if (i_value > 0) { strcpy(s_update,"ifSpeed"); i_value *= 1000000; sprintf(s_value,"%i",i_value); } } } if (strcmp(s_update,"ifHighSpeed") != 0) { if (strcmp(update,"ifDescr") == 0) { if (strcmp(ifalias,"") != 0) { strcpy(s_value,ifalias); } } sprintf(sql,"update interfaces set %s = '%s' where node = '%s' and ifName = '%s';",s_update,s_value,node,interface); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } void update(char debug) { MYSQL *conn; MYSQL_RES *result; MYSQL_RES *result2; MYSQL_RES *result3; MYSQL_RES *result4; MYSQL_RES *result5; MYSQL_RES *result6; MYSQL_ROW row; MYSQL_ROW row2; MYSQL_ROW row3; MYSQL_ROW row4; int rows; int rows2; int rows3; char sql[10000]; char interface[255]; char ifindex[255]; char datetime[100]; char index[100]; char dup; struct host *nodes; char s_value[100000]; char error[1024]; int wait[5000]; int a; int pidstatus; int nodes_c = 0; for (nodes = hosts; nodes->name; nodes++) { if (strcmp(nodes->name,"") != 0) { pid_t pID = fork(); wait[nodes_c] = pID; if (pID == 0) { dup = 'N'; conn = mysql_init(NULL); if (conn == NULL) { printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); _exit(1); } if (mysql_real_connect(conn, server, sqluser, sqlpass, database, 0, NULL, 0) == NULL) { printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); _exit(1); } if (debug == 'Y') printf("Update Node %s\n",nodes->name); // SET NAME for NODE sprintf(sql,"select value from snmp where node = '%s' and oid = 'SNMPv2-MIB::sysName.0' order by datetime desc limit 1;",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { sprintf(sql,"select * from nodes where name = '%s' and id != '%s'",row[0],nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result2 = mysql_store_result(conn); rows = mysql_num_rows(result2); if (rows == 0) { mysql_real_escape_string(conn, s_value, row[0], strlen(row[0])); sprintf(sql,"update nodes set name = '%s' where id = '%s';",s_value,nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: Duplicate Node?: %s\n\n",sql); sprintf(error,"ERROR: Duplicate Node?: %s",sql); logit(error); _exit(1); } } else { sprintf(sql,"update nodes set snmp = 'NO' where id = '%s'",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } dup = 'Y'; } mysql_free_result(result2); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); remove(lockfile); _exit(1); } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } if (dup == 'N') { // SET DESC for NODE sprintf(sql,"select value from snmp where node = '%s' and oid = 'SNMPv2-MIB::sysDescr.0' order by datetime desc limit 1;",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, s_value, row[0], strlen(row[0])); sprintf(sql,"update nodes set description = '%s' where id = '%s';",s_value,nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } // SET LOCATION for NODE sprintf(sql,"select value from snmp where node = '%s' and oid = 'SNMPv2-MIB::sysLocation.0' order by datetime desc limit 1;",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, s_value, row[0], strlen(row[0])); sprintf(sql,"update nodes set location = '%s' where id = '%s';",s_value,nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } // SET CONTACT for NODE sprintf(sql,"select value from snmp where node = '%s' and oid = 'SNMPv2-MIB::sysContact.0' order by datetime desc limit 1;",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, s_value, row[0], strlen(row[0])); sprintf(sql,"update nodes set contact = '%s' where id = '%s';",s_value,nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } sprintf(sql,"update nodes set updated = now(), update_node = 'EVERY 24 HOURS' where id = '%s';",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } // INTERFACES sprintf(sql,"select datetime from snmp where node = '%s' order by datetime desc limit 1",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result5 = mysql_store_result(conn); while ((row2 = mysql_fetch_row(result5))) { mysql_real_escape_string(conn, datetime, row2[0], strlen(row2[0])); sprintf(sql,"select value from snmp where node = '%s' and oid like 'IF-MIB::ifIndex.%c' and datetime = '%s';",nodes->id,'%',datetime); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, index, row[0], strlen(row[0])); sprintf(sql,"select value,oid from snmp where node = '%s' and oid = 'IF-MIB::ifType.%s' and datetime = '%s';",nodes->id,index,datetime); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result3 = mysql_store_result(conn); while ((row3 = mysql_fetch_row(result3))) { sprintf(sql,"select * from int_types where snmp = '%s';",row3[0]); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result4 = mysql_store_result(conn); rows2 = mysql_num_rows(result4); if (rows2 > 0) { sprintf(sql,"select value from snmp where node = '%s' and oid = 'IF-MIB::ifName.%s' and datetime = '%s';",nodes->id,index,datetime); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result6 = mysql_store_result(conn); rows3 = mysql_num_rows(result6); if (rows3 == 1) { while ((row4 = mysql_fetch_row(result6))) { mysql_real_escape_string(conn, s_value, row4[0], strlen(row4[0])); } } else { sprintf(sql,"select value from snmp where node = '%s' and oid = 'IF-MIB::ifDescr.%s' and datetime = '%s';",nodes->id,index,datetime); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result6 = mysql_store_result(conn); rows3 = mysql_num_rows(result6); if (rows3 == 1) { while ((row4 = mysql_fetch_row(result6))) { mysql_real_escape_string(conn, s_value, row4[0], strlen(row4[0])); } } else { strcpy(s_value,"NoName"); } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } if (debug == 'Y') printf("Update Node %s Interface %s (Insert/Index)\n",nodes->name,s_value); sprintf(sql,"select * from interfaces where node = '%s' and ifName = '%s';",nodes->id,s_value); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result2 = mysql_store_result(conn); rows = mysql_num_rows(result2); if (rows == 0) { sprintf(sql,"insert interfaces (node, ifIndex, ifName) values ('%s','%s','%s');",nodes->id,index,s_value); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } else { sprintf(sql,"update interfaces set ifIndex = '%s' where ifName = '%s' and node = '%s';",index,s_value,nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } // INTERFACES REMOVE & UPDATE sprintf(sql,"select ifName,ifIndex from interfaces where node = '%s' order by ifName;",nodes->id); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { mysql_real_escape_string(conn, interface, row[0], strlen(row[0])); mysql_real_escape_string(conn, ifindex, row[1], strlen(row[1])); if (debug == 'Y') printf("Update Node %s Interface %s (Remove/Update)\n",nodes->name,interface); sprintf(sql,"select * from snmp where node = '%s' and value = '%s';",nodes->id,interface); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result2 = mysql_store_result(conn); rows = mysql_num_rows(result2); if (rows > 0) { update_int(conn,nodes->id,ifindex,interface,"ifDescr"); update_int(conn,nodes->id,ifindex,interface,"ifType"); update_int(conn,nodes->id,ifindex,interface,"ifMtu"); update_int(conn,nodes->id,ifindex,interface,"ifSpeed"); update_int(conn,nodes->id,ifindex,interface,"ifHighSpeed"); update_int(conn,nodes->id,ifindex,interface,"ifPhysAddress"); update_int(conn,nodes->id,ifindex,interface,"ifConnectorPresent"); update_int(conn,nodes->id,ifindex,interface,"ifAdminStatus"); update_int(conn,nodes->id,ifindex,interface,"ifOperStatus"); } else { sprintf(sql,"delete from interfaces where node = '%s' and ifName = '%s';",nodes->id,interface); if (mysql_real_query(conn, sql, strlen(sql)) != 0) { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed: %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); _exit(1); } } _exit(0); } nodes_c++; } else { break; } } if (debug == 'Y') printf("Total Nodes %i\n",nodes_c); for (a = 0; a < nodes_c; a++) { if (debug == 'Y') printf("Waiting for child process: %i\n",wait[a]); waitpid(wait[a],&pidstatus,0); } } void nodes(MYSQL *conn,char debug) { MYSQL_RES *result; MYSQL_ROW row; char sql[255]; int a = 0; char error[1024]; sprintf(sql,"select id,inet_ntoa(ip) as ip,community from nodes where snmp = 'YES' and ip2 is null and (update_node = 'NOW' or updated < (NOW() - INTERVAL 24 HOUR)) order by id;"); if (mysql_real_query(conn, sql, strlen(sql)) == 0) { result = mysql_store_result(conn); while ((row = mysql_fetch_row(result))) { strcpy(hosts[a].id,row[0]); strcpy(hosts[a].name,row[1]); strcpy(hosts[a].community,row[2]); if (debug == 'Y') printf("Node: [%s] %s Community: %s \n",row[0],row[1],row[2]); a++; } mysql_free_result(result); } else { printf("\nERROR: SQL Query Failed %s\n\n",sql); sprintf(error,"ERROR: SQL Query Failed: %s",sql); logit(error); remove(lockfile); exit(1); } } int main(int argc, char *argv[]) { strcpy(app,"snmp2mysql"); strcpy(logfile,"/opt/knock/var/log/snmp.log"); FILE *conf; FILE *lock; int a; int b; char debug = 'N'; char config[256] = "/opt/knock/etc/knock.cnf"; char line[256] = ""; char *key; char *data; MYSQL *conn; char error[1024]; time_t start,start2,end,end2; time(&start); conn = mysql_init(NULL); init_snmp("snmp2mysql"); if (conn == NULL) { help(); printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); exit(1); } for (a = 0; a < argc; a++) { if (a > 0) { if (strcmp(argv[a],"debug") != 0) strcpy(config,argv[a]); if (strcmp(argv[a],"debug") == 0) { debug = 'Y'; } } } if ((lock = fopen(lockfile,"r"))) { if (debug == 'Y') printf("\nERROR: Lock file /var/lock/snmp2mysql.lck\n\n"); sprintf(error,"ERROR: Lock file /var/lock/snmp2mysql.lck"); logit(error); exit(1); } else { if ((lock = fopen(lockfile,"w+"))) { fputs("Locked",lock); } } if ((conf = fopen(config,"r"))) { while (!feof(conf)) { if (fgets(line,256,conf)) { if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } key = strtok(line,"="); if (key) { data = strtok(NULL,"="); for (b = 0; key[b]; b++) { key[b] = tolower(key[b]); } if (data) { if (strcmp(key,"server") == 0) strcpy(server,data); if (strcmp(key,"database") == 0) strcpy(database,data); if (strcmp(key,"username") == 0) strcpy(sqluser,data); if (strcmp(key,"password") == 0) strcpy(sqlpass,data); } } } } fclose(conf); if (strcmp(server,"") == 0 || strcmp(database,"") == 0 || strcmp(sqluser,"") == 0) { help(); printf("\nERROR: Config missing paramters %s\n\n",config); sprintf(error,"ERROR: Config missing paramters %s",config); remove(lockfile); logit(error); } else { if (mysql_real_connect(conn, server, sqluser, sqlpass, database, 0, NULL, 0) == NULL) { help(); printf("\nERROR %u: %s\n\n", mysql_errno(conn), mysql_error(conn)); sprintf(error,"ERROR %u: %s", mysql_errno(conn), mysql_error(conn)); logit(error); remove(lockfile); exit(1); } else { time(&start2); nodes(conn,debug); time(&end2); if (debug == 'Y') printf("snmp2mysql collecting nodes time: %i\n",(int)end2-(int)start2); sprintf(error,"collecting nodes time: %i",(int)end2-(int)start2); logit(error); time(&start2); scan(debug); time(&end2); if (debug == 'Y') printf("snmp2mysql collecting snmp data time: %i\n",(int)end2-(int)start2); sprintf(error,"collecting snmp data time: %i",(int)end2-(int)start2); logit(error); time(&start2); update(debug); time(&end2); if (debug == 'Y') printf("snmp2mysql updating knock database time: %i\n",(int)end2-(int)start2); sprintf(error,"updating knock database time: %i",(int)end2-(int)start2); logit(error); time(&start2); archive(conn,debug); time(&end2); if (debug == 'Y') printf("snmp2mysql archive snmpdata time: %i\n",(int)end2-(int)start2); sprintf(error,"archive snmpdata time: %i",(int)end2-(int)start2); logit(error); } } } else { help(); printf("\nERROR: Config File Missing %s\n\n",config); sprintf(error,"ERROR: Config File Missing %s",config); logit(error); } time(&end); if (debug == 'Y') printf("snmp2mysql total execution time: %i\n",(int)end-(int)start); sprintf(error,"total execution time: %i",(int)end-(int)start); logit(error); remove(lockfile); return(0); }
------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________ Net-snmp-users mailing list Net-snmp-users@lists.sourceforge.net Please see the following page to unsubscribe or change other options: https://lists.sourceforge.net/lists/listinfo/net-snmp-users