Commit 9f4ff1ed authored by 's avatar

Changes in packetloss application:

* Corrected mapi_read_results to work properly with reconnection.
  It does not block when a sensor is down with the use of mapi_is_sensor_down.
* Support for offline sensors.
  An offline sensor will be accepted now and program won't exit. It will continually 
  try to connect with the offline sensor.



git-svn-id: file:///home/svn/mapi/trunk@1383 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 28cbee4b
......@@ -162,6 +162,7 @@ struct flows_stat_total {
struct thost {
char *name; // host name from argv
int alive; // 1 if the sensor is up, 0 otherwise
// struct hostent *ent; // hostent from gethostbyname
// ip_addr ip; // ip from gethostbyname
int devices; // number of devices at host
......@@ -320,6 +321,21 @@ static int continue_as_daemon() {
return EXIT_SUCCESS;
}
void startupretry(int hosts, char *argv[], char *hostsdown, unsigned int shm_flows, unsigned int expired_flows_list_size_max, unsigned int packets_count_min, char *filter) {
int h;
for(h = 0; h < hosts; h++) {
if( hostsdown[h] == 1 ) {
if ((host[h].fd = mapi_create_flow(argv[h + 1])) < 0) continue;
if ((host[h].fid_loss = mapi_apply_function(host[h].fd, "BPF_FILTER", filter)) < 0) continue;
if ((host[h].fid_load = mapi_apply_function(host[h].fd, "BYTE_COUNTER")) < 0) continue;
if ((host[h].fid_loss = mapi_apply_function(host[h].fd, "EXPIRED_FLOWS", shm_flows, expired_flows_list_size_max, packets_count_min)) < 0) continue;
if (mapi_connect(host[h].fd) < 0) continue;
hostsdown[h] = 0;
fprintf(stdout, "Succeded connecting to %s\n", host[h].name);
}
}
}
int main(int argc, char *argv[])
{
int i = 0; // universal counters
......@@ -341,7 +357,8 @@ int main(int argc, char *argv[])
unsigned long long pkloss = 0;
char filter[1024]; // pcap filter
char *filter; // pcap filter
char *hostsdown; // retry to connect hosts which are down when application is started
char *p;
char *p2;
......@@ -396,6 +413,7 @@ int main(int argc, char *argv[])
}
hosts = argc - 2;
hostsdown = (char *)calloc(hosts, sizeof(char));
for(h = 0; h < hosts; h++) {
tmp1 = strchr(argv[h + 1], ':');
......@@ -520,6 +538,9 @@ int main(int argc, char *argv[])
// }}}
// Create flows at hosts, build and apply filters, connect {{{
filter = argv[argc - 1];
for(h = 0; h < hosts; h++) {
fprintf(stdout, "packetloss: mapi_create_flow(%s)\n", argv[h + 1]); fflush(stdout);
......@@ -528,7 +549,8 @@ int main(int argc, char *argv[])
fprintf(stderr, "Could not create flow using '%s'\n", argv[h + 1]);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode: %d description: %s\n", err_no, error);
exit(EXIT_FAILURE);
hostsdown[h] = 1;
continue;
}
// build pcap filter
......@@ -543,15 +565,15 @@ int main(int argc, char *argv[])
// h1++;
// }
// }
sprintf(filter, argv[argc - 1]);
fprintf(stdout, "packetloss: apply BPF_FILTER: \"%s\" at %s\n", filter, host[h].name); fflush(stdout);
if ((host[h].fid_loss = mapi_apply_function(host[h].fd, "BPF_FILTER", filter)) < 0) {
fprintf(stderr, "Count not apply function BPF_FILTER to flow %d\n", host[h].fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode: %d description: %s\n", err_no, error);
exit(EXIT_FAILURE);
fprintf(stderr, "Count not apply function BPF_FILTER to flow %d\n", host[h].fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode: %d description: %s\n", err_no, error);
hostsdown[h] = 1;
continue;
}
fprintf(stdout, "packetloss: apply BYTE_COUNTER at %s\n", host[h].name); fflush(stdout);
......@@ -560,7 +582,8 @@ int main(int argc, char *argv[])
fprintf(stderr, "Count not apply function BYTE_COUNTER to flow %d\n", host[h].fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s\n", err_no, error);
exit(EXIT_FAILURE);
hostsdown[h] = 1;
continue;
}
fprintf(stdout, "packetloss: apply EXPIRED_FLOWS at %s\n", host[h].name); fflush(stdout);
......@@ -569,7 +592,8 @@ int main(int argc, char *argv[])
fprintf(stderr, "Count not apply function EXPIRED_FLOWS to flow %d\n", host[h].fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode: %d description: %s\n", err_no, error);
exit(EXIT_FAILURE);
hostsdown[h] = 1;
continue;
}
}
......@@ -578,11 +602,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "Could not connect to flow %d\n", host[h].fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode: %d description: %s\n", err_no, error);
exit(EXIT_FAILURE);
hostsdown[h] = 1;
}
}
// }}}
// RRD Files descriptors - init {{{
if ((fp_packetloss = fopen(RRD_FILENAME, "r")) == NULL) // packetloss graph
my_rrd_create_packetloss(RRD_FILENAME);
......@@ -662,17 +686,51 @@ int main(int argc, char *argv[])
loop_starttime = (unsigned int) time(NULL);
uptime = loop_starttime - starttime;
startupretry(hosts, argv, hostsdown, shm_flows, expired_flows_list_size_max, packets_count_min, filter);
// Read expired_flows results from hosts {{{
for (h = 0; h < hosts; h++) {
fprintf(stdout, "packetloss: reading results from %s...\n", host[h].name); fflush(stdout);
host[h].results_dev = (mapi_results_t *) mapi_read_results(host[h].fd, host[h].fid_loss);
host[h].results_netload_dev = (mapi_results_t *) mapi_read_results(host[h].fd, host[h].fid_load);
if(mapi_is_sensor_down(host[h].fd) != 1 && hostsdown[h] != 1){
if( (host[h].results_dev = (mapi_results_t *) mapi_read_results(host[h].fd, host[h].fid_loss)) == NULL){
fprintf(stderr, "\nMapi read results failed\n");
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
host[h].alive = 0;
printf("%s sensor is down!\n", host[h].name);
continue;
}
else
host[h].alive = 1;
if( (host[h].results_netload_dev = (mapi_results_t *) mapi_read_results(host[h].fd, host[h].fid_load)) == NULL) {
fprintf(stderr, "\nMapi read results failed\n");
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
host[h].alive = 0;
printf("%s sensor is down!\n", host[h].name);
}
else
host[h].alive = 1;
}
else {
host[h].alive = 0;
printf("%s sensor is down!\n", host[h].name);
}
}
// }}}
// Read mapi_stats from hosts {{{
for(h = 0; h < hosts; h++) {
if( host[h].alive == 0 ) {
fprintf(stdout, "packetloss: skipping reading mapi_stats from %s...\n", host[h].name); fflush(stdout);
host[h].mapi_stats.ps_recv = 0;
host[h].mapi_stats.ps_drop = 0;
host[h].mapi_stats.ps_ifdrop = 0;
continue;
}
fprintf(stdout, "packetloss: reading mapi_stats from %s...\n", host[h].name); fflush(stdout);
if (mapi_stats(argv[h + 1], host[h].mapi_stats_dev) < 0) {
fprintf(stderr, "Error in mapi_stats using '%s'\n", argv[h + 1]);
......@@ -697,6 +755,7 @@ int main(int argc, char *argv[])
// Store host results in the hashtable {{{
totalflows = 0;
for (h = 0; h < hosts; h++) {
if( host[h].alive == 0) continue;
// initialize flowsstats
host[h].flowsstats.received = 0;
host[h].flowsstats.expired = 0;
......@@ -746,6 +805,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "packetloss: processing results...\n"); fflush(stdout);
i = 0;
for (h = 0; h < hosts; h++) {
if( host[h].alive == 0) continue;
for (d = 0; d < host[h].devices; d++, i++) {
// GUI - ncurses - report network load {{{
......
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