Commit ea35f663 authored by 's avatar
Browse files

Added support for --conf_from_mysql and bug fixes


git-svn-id: file:///home/svn/mapi/trunk@394 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent d73c9f4f
......@@ -34,7 +34,7 @@ int main(int argc, char *argv[])
/* Create first flow specification, subject and parameters so that they
can be filled-in by command-line options */
if ((global_spec.flow_spec[0]=new_flow_spec())==NULL) {
/* if ((global_spec.flow_spec[0]=new_flow_spec())==NULL) {
fprintf(stderr, "%s: new_flow_spec() failed\n", __func__);
return -1;
}
......@@ -45,7 +45,7 @@ int main(int argc, char *argv[])
if ((global_spec.parameters[0]=new_parameters())==NULL) {
fprintf(stderr, "%s: new_parameters() failed\n", __func__);
return -1;
}
} */
/* Read command line */
......@@ -80,6 +80,13 @@ int main(int argc, char *argv[])
}
}
/* Read MySQL password if needed */
if (read_password(&global_spec)<0) {
fprintf(stderr, "%s: read_password() failed\n", __func__);
return -1;
}
if (global_spec.db) {
if (abw_mysql_start(&global_spec)<0) {
fprintf(stderr, "%s: abw_start_mysql() failed\n", __func__);
......
......@@ -532,20 +532,7 @@ int read_conf_file(global_spec_t *global_spec)
return 0;
} /* read_conf_file() */
int check_conf(global_spec_t *global_spec) {
flow_spec_t *p, **pp;
subject_t *q, **qq;
parameters_t *r, **rr;
int protocols_no;
int i, j;
if (global_spec==NULL) {
fprintf(stderr, "%s: empty configuration\n", __func__);
return -1;
}
/* Check global configuration */
int read_password(global_spec_t *global_spec) {
if (global_spec->conf_from_mysql || global_spec->conf_to_mysql ||
global_spec->clear_conf || global_spec->db) {
......@@ -561,6 +548,21 @@ int check_conf(global_spec_t *global_spec) {
}
}
}
return 0;
} /* read_password() */
int check_conf(global_spec_t *global_spec) {
flow_spec_t *p, **pp;
subject_t *q, **qq;
parameters_t *r, **rr;
int protocols_no;
int i, j;
if (global_spec==NULL) {
fprintf(stderr, "%s: empty configuration\n", __func__);
return -1;
}
/* Check subjects and parameters begore checking flow specifications,
because flow specification refers to ids in subjects and parameters
......
......@@ -2,6 +2,7 @@
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mysql/mysql.h>
......@@ -620,7 +621,286 @@ int abw_mysql_clear_conf(MYSQL *mysql) {
} /* abw_mysql_clear_conf() */
int abw_mysql_read_conf(global_spec_t *global_spec) {
int payload_strings_no;
int flow_spec_no, subject_no, parameters_no;
flow_spec_t *p, **pp;
subject_t *q, **qq;
parameters_t *r, **rr;
char query[MAX_QUERY+1];
MYSQL_RES *mysql_res;
MYSQL_ROW mysql_row;
int num;
int i;
if (global_spec==NULL || global_spec->mysql==NULL) {
fprintf(stderr, "%s: some of the required arguments were not specified\n",
__func__);
return -1;
}
flow_spec_no=subject_no=parameters_no=0;
pp=global_spec->flow_spec;
qq=global_spec->subject;
rr=global_spec->parameters;
/* Retrieve measurements */
sprintf(query, "SELECT * FROM tMeasurement;");
printf("%s: query: %s\n", __func__, query);
if (mysql_query(global_spec->mysql, query)) {
fprintf(stderr, "%s: mysql_query(%s) failed\n", __func__, query);
return -1;
}
if ((mysql_res=mysql_store_result(global_spec->mysql))==NULL) {
fprintf(stderr, "%s: mysql_store_result() failed\n", __func__);
return -1;
}
if ((num=mysql_affected_rows(global_spec->mysql))<0) {
fprintf(stderr, "%s: mysql_affected_rows() failed\n", __func__);
return -1;
}
for (i=0; i<num; i++) {
if ((mysql_row=mysql_fetch_row(mysql_res))==NULL) {
fprintf(stderr, "%s: mysql_fetch_row() failed\n", __func__);
return -1;
}
if (flow_spec_no>=MAX_FLOWS) {
fprintf(stderr, "%s: maximum number of %d measurements exceeded\n",
__func__, MAX_FLOWS);
return -1;
}
if (*pp==NULL) {
if ((*pp=new_flow_spec())==NULL) {
fprintf(stderr, "%s: new_flow_spec() failed\n", __func__);
return -1;
}
}
flow_spec_no++;
p=*pp;
pp++;
p->subject_id=atoi(mysql_row[1]);
p->parameters_id=atoi(mysql_row[2]);
if (mysql_row[3] && strlen(mysql_row[3])) {
time_t timestamp;
struct tm tm;
timestamp=atoi(mysql_row[3]);
if (localtime_r(&timestamp, &tm)==NULL) {
fprintf(stderr, "%s: localtime_t() failed\n", __func__);
return -1;
}
if ((p->start_time_string=malloc(20))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
memset(p->start_time_string, 0, 20);
if (strftime(p->start_time_string, 20, "%d.%m.%Y-%H:%M:%S", &tm)<=0) {
fprintf(stderr, "%s: strftime() failed\n", __func__);
return -1;
}
}
if (mysql_row[4] && strlen(mysql_row[4])) {
time_t timestamp;
struct tm tm;
timestamp=atoi(mysql_row[4]);
if (localtime_r(&timestamp, &tm)==NULL) {
fprintf(stderr, "%s: localtime_t() failed\n", __func__);
return -1;
}
if ((p->end_time_string=malloc(20))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
memset(p->end_time_string, 0, 20);
if (strftime(p->end_time_string, 20, "%d.%m.%Y-%H:%M:%S", &tm)<=0) {
fprintf(stderr, "%s: strftime() failed\n", __func__);
return -1;
}
}
if (mysql_row[5] && strlen(mysql_row[5])) {
if ((p->protocols=malloc(strlen(mysql_row[5])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(p->protocols, mysql_row[5]);
}
} /* for (i=0; i<num; i++) */
/* Retrieve subjects */
sprintf(query, "SELECT * FROM tSubject, tMeasurement WHERE tSubject.id=tMeasurement.subject_id;");
printf("%s: query: %s\n", __func__, query);
if (mysql_query(global_spec->mysql, query)) {
fprintf(stderr, "%s: mysql_query(%s) failed\n", __func__, query);
return -1;
}
if ((mysql_res=mysql_store_result(global_spec->mysql))==NULL) {
fprintf(stderr, "%s: mysql_store_result() failed\n", __func__);
return -1;
}
if ((num=mysql_affected_rows(global_spec->mysql))<0) {
fprintf(stderr, "%s: mysql_affected_rows() failed\n", __func__);
return -1;
}
for (i=0; i<num; i++) {
if ((mysql_row=mysql_fetch_row(mysql_res))==NULL) {
fprintf(stderr, "%s: mysql_fetch_row() failed\n", __func__);
return -1;
}
if (subject_no>=MAX_FLOWS) {
fprintf(stderr, "%s: maximum number of %d subjects exceeded\n",
__func__, MAX_FLOWS);
return -1;
}
if (*qq==NULL) {
if ((*qq=new_subject())==NULL) {
fprintf(stderr, "%s: new_subject() failed\n", __func__);
return -1;
}
}
subject_no++;
q=*qq;
qq++;
q->id=atoi(mysql_row[0]);
if (mysql_row[1] && strlen(mysql_row[1])) {
if ((q->hostname=malloc(strlen(mysql_row[1])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(q->hostname, mysql_row[1]);
}
if (mysql_row[2] && strlen(mysql_row[2])) {
if ((q->ip=malloc(strlen(mysql_row[2])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(q->ip, mysql_row[2]);
}
if (mysql_row[3] && strlen(mysql_row[3])) {
if ((q->interface=malloc(strlen(mysql_row[3])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(q->interface, mysql_row[3]);
}
if (mysql_row[4] && strlen(mysql_row[4])) {
if ((q->direction=malloc(strlen(mysql_row[4])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(q->direction, mysql_row[4]);
}
if (mysql_row[5] && strlen(mysql_row[5]))
q->link_mbps=atoi(mysql_row[5]);
} /* for (i=0; i<num; i++) */
/* Retrieve parameters */
sprintf(query, "SELECT * FROM tParameters, tMeasurement WHERE tParameters.id=tMeasurement.parameters_id;");
printf("%s: query: %s\n", __func__, query);
if (mysql_query(global_spec->mysql, query)) {
fprintf(stderr, "%s: mysql_query(%s) failed\n", __func__, query);
return -1;
}
if ((mysql_res=mysql_store_result(global_spec->mysql))==NULL) {
fprintf(stderr, "%s: mysql_store_result() failed\n", __func__);
return -1;
}
if ((num=mysql_affected_rows(global_spec->mysql))<0) {
fprintf(stderr, "%s: mysql_affected_rows() failed\n", __func__);
return -1;
}
for (i=0; i<num; i++) {
if ((mysql_row=mysql_fetch_row(mysql_res))==NULL) {
fprintf(stderr, "%s: mysql_fetch_row() failed\n", __func__);
return -1;
}
if (parameters_no>=MAX_FLOWS) {
fprintf(stderr, "%s: maximum number of %d parameters exceeded\n",
__func__, MAX_FLOWS);
return -1;
}
if (*rr==NULL) {
if ((*rr=new_parameters())==NULL) {
fprintf(stderr, "%s: new_parameters() failed\n", __func__);
return -1;
}
}
parameters_no++;
r=*rr;
rr++;
payload_strings_no=0;
r->id=atoi(mysql_row[0]);
if (mysql_row[1] && strlen(mysql_row[1])) {
if ((r->header_filter=malloc(strlen(mysql_row[1])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(r->header_filter, mysql_row[1]);
}
if (mysql_row[2] && strlen(mysql_row[2]))
r->sau_mode=mysql_row[2][0];
if (mysql_row[3] && strlen(mysql_row[3]))
r->sau_threshold=atof(mysql_row[3]);
if (mysql_row[4] && strlen(mysql_row[4])) {
if ((r->payload_strings[payload_strings_no]=
malloc(strlen(mysql_row[4])+1))==NULL) {
fprintf(stderr, "%s: malloc() failed\n", __func__);
return -1;
}
strcpy(r->payload_strings[payload_strings_no], mysql_row[4]);
payload_strings_no++;
}
if (mysql_row[5] && strlen(mysql_row[5]))
r->interval.tv_sec=atoi(mysql_row[5]);
if (mysql_row[6] && strlen(mysql_row[6]))
r->interval.tv_usec=atoi(mysql_row[6]);
} /* for (i=0; i<num; i++) */
return 0;
} /* abw_mysql_read_conf() */
int abw_mysql_write_conf(global_spec_t *global_spec) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment