[rtg] My SQL based targets and PHP based target maker
Daniel Shereck
dshereck at omniglobenet.com
Thu Aug 30 09:54:43 EDT 2007
I have adapted the latest release of the code, so that it pulls information
form a target table, instead of file and redid the target maker in PHP, to
accommodate for the changes and speed up creation of
Targets.
Here is the main change, instead of hash_target_file…
int hash_target_db(MYSQL * mysql) {
FILE *fp;
target_t *new = NULL;
char buffer[BUFSIZE];
char query[256] = "SELECT * from target";
MYSQL_RES *response;
MYSQL_ROW row;
char maxspeed[30];
int entries = 0;
int removed = 0;
mark_targets(STALE);
/* Read each unique target into hash table */
if (set.verbose >= HIGH)
printf("SQL: %s\n", query);
if (mysql_real_query(mysql, query, strlen(query))) {
if (set.verbose >= LOW) {
fprintf(stderr, "** MySQL Error: %s\n",
mysql_error(mysql));
exit(-1);
}
}
if( (response = mysql_store_result(mysql)) == 0){
if (set.verbose >= LOW) {
fprintf(stderr, "** MySQL Error: %s\n",
mysql_error(mysql));
exit(-1);
}
}
while (row = mysql_fetch_row(response)) {
new = (target_t *) malloc(sizeof(target_t));
if (!new) {
printf("Fatal target malloc error!\n");
unlink(PIDFILE);
exit(-1);
}
strcpy(new->host, row[0]);
strcpy(new->objoid, row[1]);
new->bits = atoi(row[2]);
strcpy(new->community,row[3]);
strcpy(new->table,row[4]);
new->iid = atoi(row[5]);
new->maxspeed = atoi(row[7]);
if (alldigits(maxspeed)) {
#ifdef HAVE_STRTOLL
new->maxspeed = strtoll(maxspeed, NULL, 0);
#else
new->maxspeed = strtol(maxspeed, NULL, 0);
#endif
} else {
new->maxspeed = set.out_of_range;
}
if (set.verbose > DEBUG)
printf("Host[OID][OutOfRange]:%s[%s][%lld]\n",
new->host, new->objoid, new->maxspeed);
new->init = NEW;
new->last_value = 0;
new->next = NULL;
entries += add_hash_entry(new);
}
if(response)
mysql_free_result(response);
removed = delete_targets(STALE);
if (set.verbose >= LOW) {
printf("Successfully hashed [%d] new targets, (%d bytes).\n",
entries, entries * sizeof(target_t));
if (removed > 0)
printf("Removed [%d] stale targets from hash.\n",
removed);
}
return (entries);
}
DROP TABLE IF EXISTS `target`;
CREATE TABLE `target` (
`ip_address` varchar(32) NOT NULL,
`oid` varchar(128) NOT NULL,
`bits` int(11) NOT NULL,
`ro_snmp` varchar(64) NOT NULL,
`table_name` varchar(64) NOT NULL,
`id` int(11) NOT NULL,
`descr` varchar(255) default NULL,
`speed` bigint(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Regards,
Daniel
--
Software Engineer
Omniglobe Networks, Inc
Suite 750, 6600 Trans-Canada Hwy,
Pointre-Claire, Montreal, Quebec
H9R 4S2, Canada
Tel: +1-514-693-8949
Fax: +1-514-697-0186
Eml: HYPERLINK "mailto:dshereck at omniglobenet.com"dshereck at omniglobenet.com
Web: HYPERLINK "http://www.omniglobenet.com"http://www.omniglobenet.com
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.12.12/979 - Release Date: 8/29/2007
8:21 PM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.grdata.com/pipermail/rtg/attachments/20070830/240cb828/attachment-0001.html
More information about the RTG
mailing list