diff options
author | PATRACHARI, GAYATHRI (gp2421) <gp2421@att.com> | 2018-09-20 08:10:49 -0400 |
---|---|---|
committer | PATRACHARI, GAYATHRI (gp2421) <gp2421@att.com> | 2018-09-20 09:05:51 -0400 |
commit | 16f861742ff1a59f049170773c8edc974cfeb927 (patch) | |
tree | 656ad2737fdf53531476ccc7cfeea97d73f56413 /veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c | |
parent | 6da9a8a7b815a29e15dba2864ab3a5d1b02f27b3 (diff) |
VesAgent VES7.0 updates
Issue-ID: VNFSDK-328
Change-Id: I7e06286e0f058be9b851225055f7b74dbd71ceee
Signed-off-by: PATRACHARI, GAYATHRI (gp2421) <gp2421@att.com>
Diffstat (limited to 'veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c')
-rw-r--r-- | veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c | 1053 |
1 files changed, 924 insertions, 129 deletions
diff --git a/veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c b/veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c index 4cc4ab8..42f9793 100644 --- a/veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c +++ b/veslibrary/ves_clibrary/VESreporting_vFW/vpp_measurement_reporter.c @@ -1,4 +1,3 @@ - /*************************************************************************//** * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. @@ -19,30 +18,86 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <pthread.h> #include <string.h> #include <sys/time.h> +#include <sys/stat.h> #include "evel.h" #define BUFSIZE 128 -#define READ_INTERVAL 10 +#define MAX_INTERFACES 40 + + +void *MeasureThread(void *interfaceId); +void *FaultThread(void *interfaceId); +void *HeartbeatThread(void *threadarg); +void read_common_config_file(); typedef struct dummy_vpp_metrics_struct { - int bytes_in; - int bytes_out; - int packets_in; - int packets_out; + int curr_bytes_in; + int curr_bytes_out; + int curr_packets_in; + int curr_packets_out; + int last_bytes_in; + int last_bytes_out; + int last_packets_in; + int last_packets_out; } vpp_metrics_struct; -void read_vpp_metrics(vpp_metrics_struct *, char *); +typedef struct linkstat { + + char linkname[32]; + char linkdescr[64]; + char linkmode[64]; + int speedmbps; + int fault_raised; + unsigned long long last_epoch; + +}LINKSTAT; + +vpp_metrics_struct meas_intfstat[MAX_INTERFACES]; +LINKSTAT meas_linkstat[MAX_INTERFACES]; +int gm_linkcount = 0; + +vpp_metrics_struct fault_intfstat[MAX_INTERFACES]; +LINKSTAT fault_linkstat[MAX_INTERFACES]; +int gf_faultCheckInterval = 0; +int gf_lowWaterMark = 0; +int gf_linkcount = 0; + +void read_vpp_metrics(vpp_metrics_struct *, LINKSTAT *, int); unsigned long long epoch_start = 0; +time_t common_filetime = 0; +time_t hb_filetime = 0; +time_t meas_filetime = 0; +time_t fault_filetime = 0; +time_t intf_ffiletime = 0; +time_t intf_mfiletime = 0; + +int gh_eventId = 1; +int gm_eventId = 1; +char gh_event_name[BUFSIZE]; +char gm_event_name[BUFSIZE]; +char gf_event_name[BUFSIZE]; +char g_event_type[BUFSIZE]; +char g_nfc_nam_code[BUFSIZE]; +char g_nf_nam_code[BUFSIZE]; +char g_nf_vendor_name[BUFSIZE]; +char g_report_entity_id[BUFSIZE]; +char g_report_entity_name[BUFSIZE]; +char g_source_id[BUFSIZE]; +char g_timezone[BUFSIZE]; +int gm_interval = 0; +int gh_interval = 0; +char g_source_name[BUFSIZE]; + +int request_rate=0; -#ifdef DOCKER int measure_traffic() { - EVEL_ERR_CODES evel_rc = EVEL_SUCCESS; FILE *fp; int status; char count[10]; @@ -50,16 +105,8 @@ int measure_traffic() struct tm * timeinfo; char period [21]; char cmd [100]; - int concurrent_sessions = 0; - int configured_entities = 0; - double mean_request_latency = 0; - double measurement_interval = 1; - double memory_configured = 0; - double memory_used = 0; - int request_rate=0; char secs [3]; int sec; - double loadavg; printf("Checking app traffic\n"); time (&rawtime); @@ -97,10 +144,6 @@ int measure_traffic() return request_rate; } -#endif - - - /**************************************************************************//** * tap live cpu stats *****************************************************************************/ @@ -139,6 +182,7 @@ void evel_get_cpu_stats(EVENT_MEASUREMENT * measurement) cpu_use = evel_measurement_new_cpu_use_add(measurement, "cpu1", usage); if( cpu_use != NULL ){ + evel_measurement_cpu_use_idle_set(cpu_use,idle); //evel_measurement_cpu_use_interrupt_set(cpu_use,intrpt); //evel_measurement_cpu_use_nice_set(cpu_use,nice); @@ -151,57 +195,451 @@ void evel_get_cpu_stats(EVENT_MEASUREMENT * measurement) } } +int file_is_modified(const char *path, time_t *oldMTime) { + struct stat file_stat; + int err = stat(path, &file_stat); + if (err != 0) { + perror(" [file_is_modified] stat"); + exit(1); + } + if( file_stat.st_mtime > *oldMTime) + { + *oldMTime = file_stat.st_mtime; + return 1; + } + else + return 0; +} + +void read_fault_config_file() +{ + char const* const fault_fileName = "fw_flt_config.txt"; + char const* const intf_fileName = "fw_interface_config.txt"; + char *pos; + char line[BUFSIZE]; + char param[BUFSIZE]; + char value[BUFSIZE]; + + read_common_config_file(); + + FILE* file = fopen(fault_fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + if(file_is_modified(fault_fileName, &fault_filetime)) + { + printf("Fault config file changed \n"); + + memset(gf_event_name, 0, BUFSIZE); + gf_faultCheckInterval = 0; + gf_lowWaterMark = 0; + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + //remove_spaces(line); + if (strcmp (param, "eventName") ==0) + { + if (strlen(value) != 0) + { + strcpy(gf_event_name, value); + } + else + { + printf("\nError: eventName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "faultCheckInterval") ==0) + { + if (strlen(value) != 0) + { + gf_faultCheckInterval = atoi(value); + } + else + { + printf("\nError: eventName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "lowWaterMark") ==0) + { + if (strlen(value) != 0) + { + gf_lowWaterMark = atoi(value); + } + else + { + printf("Error: lowWaterMark is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else + { + printf ("Invalid parameter -%s %s\n", param, value); + } + } + } + + /* Now read the interface file */ + file = fopen(intf_fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + if(file_is_modified(intf_fileName, &intf_ffiletime)) + { + printf("Fault config file changed \n"); + + gf_linkcount = 0; + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + if (strlen(param) != 0) + { + strcpy(fault_linkstat[gf_linkcount].linkname, param); + strcpy(fault_linkstat[gf_linkcount].linkdescr, value); + gf_linkcount++; + } + } + } +} + +void read_hb_config_file() +{ + char const* const fileName = "fw_hb_config.txt"; + char *pos; + char line[BUFSIZE]; + char param[BUFSIZE]; + char value[BUFSIZE]; + + read_common_config_file(); + + FILE* file = fopen(fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + if(file_is_modified(fileName, &hb_filetime)) + { + printf("HB config file changed \n"); + + memset(gh_event_name, 0, BUFSIZE); + gh_interval = 0; + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + //remove_spaces(line); + if (strcmp (param, "eventName") ==0) + { + if (strlen(value) != 0) + { + strcpy(gh_event_name, value); + } + else + { + printf("\nError: eventName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "heartbeatInterval") ==0) + { + if (strlen(value) != 0) + { + gh_interval = atoi(value); + } + else + { + printf("Error: heartbeatInterval is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else + { + printf ("Invalid parameter -%s %s\n", param, value); + } + } + } +} + +void read_meas_config_file() +{ + char const* const fileName = "fw_meas_config.txt"; + char const* const intf_fileName = "fw_interface_config.txt"; + char *pos; + char line[BUFSIZE]; + char param[BUFSIZE]; + char value[BUFSIZE]; + + read_common_config_file(); + FILE* file = fopen(fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + if(file_is_modified(fileName, &meas_filetime)) + { + printf("Measurement Config file changed \n"); + + memset(gm_event_name, 0, BUFSIZE); + gm_interval = 0; + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + printf("\n param and value are %s, %s\n", param,value); + //remove_spaces(line); + if (strcmp (param, "eventName") ==0) + { + if (strlen(value) != 0) + { + strcpy(gm_event_name, value); + } + else + { + printf("\nError: eventName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "measurementInterval") ==0) + { + if (strlen(value) != 0) + { + gm_interval = atoi(value); + } + else + { + printf("Error: measurementInterval is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else + { + printf ("Invalid parameter -%s %s\n", param, value); + } + } + } + + /* Now read the interface file */ + file = fopen(intf_fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + + if(file_is_modified(intf_fileName, &intf_mfiletime)) + { + printf("Interface config file changed \n"); + + gf_linkcount = 0; + + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + if (strlen(param) != 0) + { + strcpy(meas_linkstat[gm_linkcount].linkname, param); + strcpy(meas_linkstat[gm_linkcount].linkdescr, value); + gm_linkcount++; + } + } + } +} + +void read_common_config_file() +{ + char const* const fileName = "fw_common_config.txt"; + char *pos; + char line[BUFSIZE]; + char param[BUFSIZE]; + char value[BUFSIZE]; + + FILE* file = fopen(fileName, "r"); /* should check the result */ + if( file == NULL ){ + EVEL_ERROR("Error: file open error or config file is not there\n"); + exit(-1); + } + if(file_is_modified(fileName, &common_filetime)) + { + printf("Common Config file changed \n"); + + memset(g_event_type, 0, BUFSIZE); + memset(g_nfc_nam_code, 0, BUFSIZE); + memset(g_nf_nam_code, 0, BUFSIZE); + memset(g_nf_vendor_name, 0, BUFSIZE); + memset(g_report_entity_id, 0, BUFSIZE); + memset(g_report_entity_name, 0, BUFSIZE); + memset(g_source_id, 0, BUFSIZE); + memset(g_timezone, 0, BUFSIZE); + + while ( file != NULL && fgets(line, sizeof(line), file)) { + /* note that fgets don't strip the terminating \n, checking its + presence would allow to handle lines longer that sizeof(line) */ + memset(param, 0, BUFSIZE); + memset(value, 0, BUFSIZE); + + if ((pos=strchr(line, '\n')) != NULL) + *pos = '\0'; + sscanf(line,"%s %s",param,value); + //remove_spaces(line); + + if (strcmp (param, "eventType") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_event_type, value); + } + } + else if (strcmp (param, "nfcNamingCode") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_nfc_nam_code, value); + } + } + else if (strcmp (param, "nfNamingCode") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_nf_nam_code, value); + } + } + else if (strcmp (param, "nfVendorName") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_nf_vendor_name, value); + } + } + else if (strcmp (param, "reportingEntityId") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_report_entity_id, value); + } + } + else if (strcmp (param, "reportingEntityName") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_report_entity_name, value); + } + else + { + printf("Error: reportingEntityName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "sourceId") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_source_id, value); + } + } + else if (strcmp (param, "sourceName") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_source_name, value); + } + else + { + printf("Error: sourceName is a mandatory parameter, update the config file\n"); + exit(-1); + } + } + else if (strcmp (param, "timeZoneOffset") ==0) + { + if (strlen(value) != 0) + { + strcpy(g_timezone, value); + } + } + else + { + printf ("Invalid parameter -%s %s\n", param, value); + } + } + } +} int main(int argc, char** argv) { - EVEL_ERR_CODES evel_rc = EVEL_SUCCESS; - EVENT_MEASUREMENT* vpp_m = NULL; - EVENT_HEADER* vpp_m_header = NULL; - int bytes_in_this_round; - int bytes_out_this_round; - int packets_in_this_round; - int packets_out_this_round; - vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct)); - vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct)); - struct timeval time_val; - //time_t start_epoch; - //time_t last_epoch; - char hostname[BUFSIZE]; char* fqdn = argv[1]; int port = atoi(argv[2]); - char* vnic = argv[3]; - MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL; - //struct timeval tv_start; - - printf("\nVector Packet Processing (VPP) measurement collection\n"); - fflush(stdout); + int i=0; + int rc; + pthread_attr_t attr; + pthread_t hb_thread; + pthread_t meas_thread; + pthread_t flt_thread; - if (argc != 4) + if (argc != 3) { - fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]); + fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> \n", argv[0]); exit(-1); } - srand(time(NULL)); /**************************************************************************/ /* Initialize */ /**************************************************************************/ if(evel_initialize(fqdn, /* FQDN */ port, /* Port */ + NULL, /* Backup FQDN */ + 0, /* Backup port */ NULL, /* optional path */ NULL, /* optional topic */ 100, /* Ring Buffer size */ 0, /* HTTPS? */ + 0, /* active mode? */ NULL, /* cert file */ NULL, /* key file */ NULL, /* ca info */ NULL, /* ca file */ 0, /* verify peer */ 0, /* verify host */ - "", /* Username */ - "", /* Password */ + "will", /* Username */ + "pill", /* Password */ + "", /* Username2 */ + "", /* Password2 */ NULL, /* Source ip */ + NULL, /* Source ip2 */ EVEL_SOURCE_VIRTUAL_MACHINE, /* Source type */ "vFirewall", /* Role */ 1)) /* Verbosity */ @@ -214,60 +652,410 @@ int main(int argc, char** argv) printf("\nInitialization completed\n"); } - gethostname(hostname, BUFSIZE); - memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct)); - read_vpp_metrics(last_vpp_metrics, vnic); + read_common_config_file(); + + /* Initialize and set thread detached attribute */ + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + printf("Main:Creating thread \n"); + rc = pthread_create(&hb_thread, NULL, HeartbeatThread, &i); + if (rc) + { + printf("ERROR; return code from pthread_create() is %d\n", rc); + exit(-1); + } + printf("Main:Created HB thread \n"); + + rc = pthread_create(&meas_thread, NULL, MeasureThread, &i); + if (rc) + { + printf("ERROR; return code from pthread_create() is %d\n", rc); + exit(-1); + } + printf("Main:Created Measurement thread \n"); + + + rc = pthread_create(&flt_thread, NULL, FaultThread, &i); + if (rc) + { + printf("ERROR; return code from pthread_create() is %d\n", rc); + exit(-1); + } + printf("Main:Created Fault thread \n"); + + pthread_join(hb_thread, NULL); + pthread_join(meas_thread, NULL); + pthread_join(flt_thread, NULL); + + evel_terminate(); + printf("Terminated\n"); + return 0; +} + +void *HeartbeatThread(void *threadarg) +{ + sleep(1); + printf("Running HB thread \n"); + fflush(stdout); + + while(1) + { + EVENT_HEARTBEAT_FIELD * event = NULL; + EVENT_HEADER* hb_header = NULL; + EVEL_ERR_CODES evel_rc = EVEL_SUCCESS; + + /***************************************************************************/ + /* Heartbeat */ + /***************************************************************************/ + read_hb_config_file(); + char event_id1[10] = "heartbeat"; + char event_id2[15] = {0}; + char event_id[BUFSIZE] = {0}; + + memset(event_id, 0, BUFSIZE); + sprintf(event_id2, "%09d", gh_eventId++); + strcat(event_id, event_id1); + strcat(event_id, event_id2); + + event = evel_new_heartbeat_field(gh_interval, gh_event_name, event_id); + + if (event != NULL) + { + hb_header = (EVENT_HEADER *)event; + + if (g_event_type != NULL) + evel_header_type_set(&event->header, g_event_type); + + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec; + + evel_start_epoch_set(&event->header, epoch_start); + evel_last_epoch_set(&event->header, epoch_now); + epoch_start = epoch_now; + + if(g_nfc_nam_code != NULL) + evel_nfcnamingcode_set(&event->header, g_nfc_nam_code); + if(g_nf_nam_code != NULL) + evel_nfnamingcode_set(&event->header, g_nf_nam_code); + evel_reporting_entity_name_set(&event->header, g_report_entity_name); + if(g_report_entity_id != NULL) + evel_reporting_entity_id_set(&event->header, g_report_entity_id); + if(g_timezone != NULL) + evel_time_zone_offset_set(&event->header, g_timezone); + if(g_nf_vendor_name != NULL) + evel_nf_vendor_name_set(&event->header, g_nf_vendor_name); + if(g_source_id != NULL ) + evel_source_id_set(&event->header, g_source_id); + if(g_source_name!= NULL ) + evel_source_name_set(&event->header, g_source_name); + + evel_rc = evel_post_event(hb_header); + if (evel_rc != EVEL_SUCCESS) + { + EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string()); + } + } + else + { + EVEL_ERROR("New Heartbeat failed"); + } + printf(" Processed Heartbeat\n"); + + sleep(gh_interval); + } +} + +void *FaultThread(void *interfaceId) +{ + EVEL_ERR_CODES evel_rc = EVEL_SUCCESS; + EVENT_FAULT * fault = NULL; + EVENT_HEADER* fault_header = NULL; + int bytes_in; + int bytes_out; + int packets_in; + int packets_out; + unsigned long long epoch_now; + + struct timeval time_val; + char event_id1[10] = "fault"; + char event_id2[15] = {0}; + char event_id[BUFSIZE] = {0}; + + sleep(1); + printf("Running Measurement thread \n"); + fflush(stdout); + + memset(&fault_intfstat[0],0,(sizeof(vpp_metrics_struct)* MAX_INTERFACES)); + memset(&fault_linkstat[0],0,(sizeof(LINKSTAT) * MAX_INTERFACES)); + + read_fault_config_file(); + read_vpp_metrics(fault_intfstat, fault_linkstat, gf_linkcount); + gettimeofday(&time_val, NULL); - epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec; - sleep(READ_INTERVAL); + + sleep(gf_faultCheckInterval); /***************************************************************************/ /* Collect metrics from the VNIC */ /***************************************************************************/ while(1) { - memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct)); - read_vpp_metrics(curr_vpp_metrics, vnic); + read_fault_config_file(); + read_vpp_metrics(fault_intfstat, fault_linkstat, gf_linkcount); - if(curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0) { - bytes_in_this_round = curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in; - } - else { - bytes_in_this_round = 0; - } - if(curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0) { - bytes_out_this_round = curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out; - } - else { - bytes_out_this_round = 0; - } - if(curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0) { - packets_in_this_round = curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in; - } - else { - packets_in_this_round = 0; + for (int i = 0; i < gm_linkcount; i++) + { + if(fault_intfstat[i].curr_bytes_in - fault_intfstat[i].last_bytes_in > 0) { + bytes_in = fault_intfstat[i].curr_bytes_in - fault_intfstat[i].last_bytes_in; + } + else { + bytes_in = 0; + } + if(fault_intfstat[i].curr_bytes_out - fault_intfstat[i].last_bytes_out > 0) { + bytes_out = fault_intfstat[i].curr_bytes_out - fault_intfstat[i].last_bytes_out; + } + else { + bytes_out = 0; + } + if(fault_intfstat[i].curr_packets_in - fault_intfstat[i].last_packets_in > 0) { + packets_in = fault_intfstat[i].curr_packets_in - fault_intfstat[i].last_packets_in; + } + else { + packets_in = 0; + } + if(fault_intfstat[i].curr_packets_out - fault_intfstat[i].last_packets_out > 0) { + packets_out = fault_intfstat[i].curr_packets_out - fault_intfstat[i].last_packets_out; + } + else { + packets_out = 0; + } + if (((bytes_in < gf_lowWaterMark) || (bytes_out < gf_lowWaterMark) || + (packets_in < gf_lowWaterMark) || (packets_out < gf_lowWaterMark)) && + (fault_linkstat[i].fault_raised == 0)) + { + memset(event_id, 0, BUFSIZE); + sprintf(event_id2, "%09d", (i+1)); + strcat(event_id, event_id1); + strcat(event_id, event_id2); + + fault = evel_new_fault(gf_event_name, event_id, "linkDown", + "Transmit-Receive issue", + EVEL_PRIORITY_MEDIUM, EVEL_SEVERITY_MAJOR, + EVEL_SOURCE_HOST,EVEL_VF_STATUS_IDLE); + if (fault != NULL) + { + fault_linkstat[i].fault_raised = 1; + + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec; + fault_linkstat[i].last_epoch = epoch_now; + + fault_header = (EVENT_HEADER *)fault; + evel_fault_category_set(fault, "link"); + evel_fault_interface_set(fault, fault_linkstat[i].linkname); + + if (g_event_type != NULL) + evel_fault_type_set(fault, g_event_type); + + evel_start_epoch_set(&fault->header, epoch_now); + evel_last_epoch_set(&fault->header, epoch_now); + if(g_nfc_nam_code != NULL) + evel_nfcnamingcode_set(&fault->header, g_nfc_nam_code); + if(g_nf_nam_code != NULL) + evel_nfnamingcode_set(&fault->header, g_nf_nam_code); + evel_reporting_entity_name_set(&fault->header, g_report_entity_name); + if(g_report_entity_id != NULL) + evel_reporting_entity_id_set(&fault->header, g_report_entity_id); + if(g_timezone != NULL) + evel_time_zone_offset_set(&fault->header, g_timezone); + if(g_nf_vendor_name != NULL) + evel_nf_vendor_name_set(&fault->header, g_nf_vendor_name); + if(g_source_id != NULL ) + evel_source_id_set(&fault->header, g_source_id); + if(g_source_name!= NULL ) + evel_source_name_set(&fault->header, g_source_name); + + evel_rc = evel_post_event(fault_header); + + if(evel_rc == EVEL_SUCCESS) { + printf("Fault event is correctly sent to the collector!\n"); + } + else { + printf("Post failed %d (%s)\n", evel_rc, evel_error_string()); + } + } + else { + printf("New new fault failed (%s)\n", evel_error_string()); + } } - if(curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0) { - packets_out_this_round = curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out; + else if (((bytes_in > gf_lowWaterMark) && (bytes_out > gf_lowWaterMark) && + (packets_in > gf_lowWaterMark) && (packets_out > gf_lowWaterMark)) && + (fault_linkstat[i].fault_raised == 1)) + { + memset(event_id, 0, BUFSIZE); + sprintf(event_id2, "%09d", (i+1)); + strcat(event_id, event_id1); + strcat(event_id, event_id2); + + fault = evel_new_fault(gf_event_name, event_id, "linkDown", + "Transmit-Receive issue", + EVEL_PRIORITY_MEDIUM, EVEL_SEVERITY_NORMAL, + EVEL_SOURCE_HOST, EVEL_VF_STATUS_ACTIVE); + if (fault != NULL) + { + fault_linkstat[i].fault_raised = 0; + + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec; + + fault_header = (EVENT_HEADER *)fault; + evel_fault_category_set(fault, "link"); + evel_fault_interface_set(fault, fault_linkstat[i].linkname); + + if (g_event_type != NULL) + evel_fault_type_set(fault, g_event_type); + + evel_start_epoch_set(&fault->header, fault_linkstat[i].last_epoch); + evel_last_epoch_set(&fault->header, epoch_now); + fault_linkstat[i].last_epoch = 0; + + if(g_nfc_nam_code != NULL) + evel_nfcnamingcode_set(&fault->header, g_nfc_nam_code); + if(g_nf_nam_code != NULL) + evel_nfnamingcode_set(&fault->header, g_nf_nam_code); + evel_reporting_entity_name_set(&fault->header, g_report_entity_name); + if(g_report_entity_id != NULL) + evel_reporting_entity_id_set(&fault->header, g_report_entity_id); + if(g_timezone != NULL) + evel_time_zone_offset_set(&fault->header, g_timezone); + if(g_nf_vendor_name != NULL) + evel_nf_vendor_name_set(&fault->header, g_nf_vendor_name); + if(g_source_id != NULL ) + evel_source_id_set(&fault->header, g_source_id); + if(g_source_name!= NULL ) + evel_source_name_set(&fault->header, g_source_name); + + evel_rc = evel_post_event(fault_header); + + if(evel_rc == EVEL_SUCCESS) { + printf("Fault event is correctly sent to the collector!\n"); + } + else { + printf("Post failed %d (%s)\n", evel_rc, evel_error_string()); + } + } + else { + printf("New fault failed (%s)\n", evel_error_string()); + } + } - else { - packets_out_this_round = 0; } - vpp_m = evel_new_measurement(READ_INTERVAL,"vFirewallBroadcastPackets","TrafficStats_1.2.3.4"); - vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true"); - evel_meas_vnic_performance_add(vpp_m, vnic_performance); + sleep(gf_faultCheckInterval); + } + + /***************************************************************************/ + /* Terminate */ + /***************************************************************************/ + sleep(1); +} + +void *MeasureThread(void *interfaceId) +{ + EVEL_ERR_CODES evel_rc = EVEL_SUCCESS; + EVENT_MEASUREMENT* vpp_m = NULL; + EVENT_HEADER* vpp_m_header = NULL; + int bytes_in; + int bytes_out; + int packets_in; + int packets_out; + + struct timeval time_val; + MEASUREMENT_NIC_PERFORMANCE * vnic_performance = NULL; + //struct timeval tv_start; + char event_id1[10] = "mvfs"; + char event_id2[15] = {0}; + char event_id[BUFSIZE] = {0}; + + printf("\nVector Packet Processing (VPP) measurement collection\n"); + fflush(stdout); + + memset(&meas_intfstat[0],0,(sizeof(vpp_metrics_struct) * MAX_INTERFACES)); + memset(&meas_linkstat[0],0,(sizeof(LINKSTAT) * MAX_INTERFACES)); + + read_meas_config_file(); + + read_vpp_metrics(meas_intfstat, meas_linkstat, gm_linkcount); + + gettimeofday(&time_val, NULL); + epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec; + + sleep(gm_interval); + + /***************************************************************************/ + /* Collect metrics from the VNIC */ + /***************************************************************************/ + while(1) { + request_rate = rand()%10000; + read_meas_config_file(); + read_vpp_metrics(meas_intfstat, meas_linkstat, gm_linkcount); + + memset(event_id, 0, BUFSIZE); + sprintf(event_id2, "%09d", gm_eventId++); + strcat(event_id, event_id1); + strcat(event_id, event_id2); + + vpp_m = evel_new_measurement(gm_interval, gm_event_name, event_id); if(vpp_m != NULL) { printf("New measurement report created...\n"); + if (g_event_type != NULL) + evel_measurement_type_set(vpp_m, g_event_type); - evel_measurement_type_set(vpp_m, "HTTP request rate"); - evel_measurement_request_rate_set(vpp_m, rand()%10000); + for (int i = 0; i < gm_linkcount; i++) + { + if(meas_intfstat[i].curr_bytes_in - meas_intfstat[i].last_bytes_in > 0) { + bytes_in = meas_intfstat[i].curr_bytes_in - meas_intfstat[i].last_bytes_in; + } + else { + bytes_in = 0; + } + if(meas_intfstat[i].curr_bytes_out - meas_intfstat[i].last_bytes_out > 0) { + bytes_out = meas_intfstat[i].curr_bytes_out - meas_intfstat[i].last_bytes_out; + } + else { + bytes_out = 0; + } + if(meas_intfstat[i].curr_packets_in - meas_intfstat[i].last_packets_in > 0) { + packets_in = meas_intfstat[i].curr_packets_in - meas_intfstat[i].last_packets_in; + } + else { + packets_in = 0; + } + if(meas_intfstat[i].curr_packets_out - meas_intfstat[i].last_packets_out > 0) { + packets_out = meas_intfstat[i].curr_packets_out - meas_intfstat[i].last_packets_out; + } + else { + packets_out = 0; + } + vnic_performance = (MEASUREMENT_NIC_PERFORMANCE *)evel_measurement_new_nic_performance(meas_linkstat[i].linkname, "true"); + evel_meas_nic_performance_add(vpp_m, vnic_performance); + evel_nic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in); + evel_nic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out); - evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round); - evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round); + evel_nic_performance_rx_octets_delta_set(vnic_performance, bytes_in); + evel_nic_performance_tx_octets_delta_set(vnic_performance, bytes_out); + + if (strcmp(meas_linkstat[i].linkname, "docker") == 0) + { + request_rate = measure_traffic(); + } + } + evel_measurement_request_rate_set(vpp_m, request_rate); - evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round); - evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round); evel_get_cpu_stats(vpp_m); /***************************************************************************/ @@ -284,13 +1072,24 @@ int main(int argc, char** argv) evel_start_epoch_set(&vpp_m->header, epoch_start); evel_last_epoch_set(&vpp_m->header, epoch_now); epoch_start = epoch_now; - - evel_nfcnamingcode_set(&vpp_m->header, "vVNF"); - evel_nfnamingcode_set(&vpp_m->header, "vVNF"); + if(g_nfc_nam_code != NULL) + evel_nfcnamingcode_set(&vpp_m->header, g_nfc_nam_code); + if(g_nf_nam_code != NULL) + evel_nfnamingcode_set(&vpp_m->header, g_nf_nam_code); //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available"); //strcpy(vpp_m_header->reporting_entity_name, hostname); - evel_reporting_entity_name_set(&vpp_m->header, "fwll"); - evel_reporting_entity_id_set(&vpp_m->header, "No UUID available"); + evel_reporting_entity_name_set(&vpp_m->header, g_report_entity_name); + if(g_report_entity_id != NULL) + evel_reporting_entity_id_set(&vpp_m->header, g_report_entity_id); + if(g_timezone != NULL) + evel_time_zone_offset_set(&vpp_m->header, g_timezone); + if(g_nf_vendor_name != NULL) + evel_nf_vendor_name_set(&vpp_m->header, g_nf_vendor_name); + if(g_source_id != NULL ) + evel_source_id_set(&vpp_m->header, g_source_id); + if(g_source_name!= NULL ) + evel_source_name_set(&vpp_m->header, g_source_name); + evel_rc = evel_post_event(vpp_m_header); if(evel_rc == EVEL_SUCCESS) { @@ -304,29 +1103,17 @@ int main(int argc, char** argv) printf("New measurement report failed (%s)\n", evel_error_string()); } - last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in; - last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out; - last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in; - last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out; - //gettimeofday(&time_val, NULL); - //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec; - - sleep(READ_INTERVAL); + sleep(gm_interval); } /***************************************************************************/ /* Terminate */ /***************************************************************************/ sleep(1); - free(last_vpp_metrics); - free(curr_vpp_metrics); - evel_terminate(); - printf("Terminated\n"); - - return 0; } -void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) { +void read_vpp_metrics(vpp_metrics_struct *intfstats, LINKSTAT *linkstat, int linkcount) +{ // Define an array of char that contains the parameters of the unix 'cut' command char* params[] = {"-f3", "-f11", "-f4", "-f12"}; // Define the unix command to execute in order to read metrics from the vNIC @@ -337,36 +1124,44 @@ void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) { char buf[BUFSIZE]; /* buffer used to store VPP metrics */ int temp[] = {0, 0, 0, 0}; /* temp array that contains VPP values */ FILE *fp; /* file descriptor to pipe cmd to shell */ - int i; - - for(i = 0; i < 4; i++) { - // Clear buffers - memset(buf, 0, BUFSIZE); - memset(cmd, 0, BUFSIZE); - // Build shell command to read metrics from the vNIC - strcat(cmd, cmd_prefix); - strcat(cmd, vnic); - strcat(cmd, cmd_mid); - strcat(cmd, params[i]); + int i, j; + + for(i = 0; i < linkcount; i++) + { + for(j = 0; j < 4; j++) { + // Clear buffers + memset(buf, 0, BUFSIZE); + memset(cmd, 0, BUFSIZE); + // Build shell command to read metrics from the vNIC + strcat(cmd, cmd_prefix); + strcat(cmd, linkstat[i].linkname); + strcat(cmd, cmd_mid); + strcat(cmd, params[j]); - // Open a pipe and read VPP values - if ((fp = popen(cmd, "r")) == NULL) { - printf("Error opening pipe!\n"); - return; - } + // Open a pipe and read VPP values + if ((fp = popen(cmd, "r")) == NULL) { + printf("Error opening pipe!\n"); + return; + } - while (fgets(buf, BUFSIZE, fp) != NULL); - temp[i] = atoi(buf); + while (fgets(buf, BUFSIZE, fp) != NULL); + temp[j] = atoi(buf); - if(pclose(fp)) { - printf("Command not found or exited with error status\n"); - return; + if(pclose(fp)) { + printf("Command not found or exited with error status\n"); + return; + } } - } + // Store the current metric in the last metric + intfstats[i].last_bytes_in = intfstats[i].curr_bytes_in; + intfstats[i].last_bytes_out = intfstats[i].curr_bytes_out; + intfstats[i].last_packets_in = intfstats[i].curr_packets_in; + intfstats[i].last_packets_out = intfstats[i].curr_packets_out; - // Store metrics read from the vNIC in the struct passed from the main function - vpp_metrics->bytes_in = temp[0]; - vpp_metrics->bytes_out = temp[1]; - vpp_metrics->packets_in = temp[2]; - vpp_metrics->packets_out = temp[3]; + // Store metrics read from the vNIC in the current + intfstats[i].curr_bytes_in = temp[0]; + intfstats[i].curr_bytes_out = temp[1]; + intfstats[i].curr_packets_in = temp[2]; + intfstats[i].curr_packets_out = temp[3]; + } } |