Update License text
[vnfsdk/compliance.git] / veslibrary / ves_clibrary / VESreporting_vLB / vpp_measurement_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Unless otherwise specified, all software contained herein is
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ****************************************************************************/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <math.h>
26
27 #include "evel.h"
28
29 #define BUFSIZE 128
30 #define READ_INTERVAL 10
31
32 typedef struct dummy_vpp_metrics_struct {
33   int bytes_in;
34   int bytes_out;
35   int packets_in;
36   int packets_out;
37 } vpp_metrics_struct;
38
39 void read_vpp_metrics(vpp_metrics_struct *, char *);
40
41 unsigned long long epoch_start = 0;
42
43
44 #ifdef DOCKER
45 int measure_traffic() 
46 {
47
48   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
49   FILE *fp;
50   int status;
51   char count[10];
52   time_t rawtime;
53   struct tm * timeinfo;
54   char period [21];
55   char cmd [100];
56   int concurrent_sessions = 0;
57   int configured_entities = 0;
58   double mean_request_latency = 0;
59   double measurement_interval = 1;
60   double memory_configured = 0;
61   double memory_used = 0;
62   int request_rate=0;
63   char secs [3];
64   int sec;
65   double loadavg;
66
67   printf("Checking app traffic\n");
68   time (&rawtime);
69   timeinfo = localtime (&rawtime);
70   strftime(period,21,"%d/%b/%Y:%H:%M:",timeinfo);
71   strftime(secs,3,"%S",timeinfo);
72   sec = atoi(secs);
73   if (sec == 0) sec = 59;
74   sprintf(secs, "%02d", sec);
75   strncat(period, secs, 21);
76   // ....x....1....x....2.
77   // 15/Oct/2016:17:51:19
78   strcpy(cmd, "sudo docker logs vHello | grep -c ");
79   strncat(cmd, period, 100);
80
81   fp = popen(cmd, "r");
82   if (fp == NULL) {
83     EVEL_ERROR("popen failed to execute command");
84   }
85
86   if (fgets(count, 10, fp) != NULL) {
87     request_rate = atoi(count);
88     printf("Reporting request rate for second: %s as %d\n", period, request_rate);
89
90     }
91     else {
92       EVEL_ERROR("New Measurement failed");
93     }
94     printf("Processed measurement\n");
95   
96   status = pclose(fp);
97   if (status == -1) {
98     EVEL_ERROR("pclose returned an error");
99   }
100   return request_rate;
101 }
102
103 #endif
104
105
106 int main(int argc, char** argv)
107 {
108   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
109   EVENT_MEASUREMENT* vpp_m = NULL;
110   EVENT_HEADER* vpp_m_header = NULL;
111   int bytes_in_this_round;
112   int bytes_out_this_round;
113   int packets_in_this_round;
114   int packets_out_this_round;
115   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
116   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
117   struct timeval time_val;
118   //time_t start_epoch;
119   //time_t last_epoch;
120   char hostname[BUFSIZE];
121   char* fqdn = argv[1];
122   int port = atoi(argv[2]);
123   char* vnic = argv[3];
124   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
125
126   printf("\nVector Packet Processing (VPP) measurement collection\n");
127   fflush(stdout);
128
129   if (argc != 4)
130   {
131     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
132     exit(-1);
133   }
134   srand(time(NULL));
135
136   /**************************************************************************/
137   /* Initialize                                                             */
138   /**************************************************************************/
139   if(evel_initialize(fqdn,                         /* FQDN                  */
140                      port,                         /* Port                  */
141                      NULL,                         /* optional path         */
142                      NULL,                         /* optional topic        */
143                      0,                            /* HTTPS?                */
144                      "",                           /* Username              */
145                      "",                           /* Password              */
146                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
147                      "vLoadBalancer",              /* Role                  */
148                      1))                           /* Verbosity             */
149   {
150     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
151     exit(-1);
152   }
153   else
154   {
155     printf("\nInitialization completed\n");
156   }
157
158   gethostname(hostname, BUFSIZE);
159   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
160   read_vpp_metrics(last_vpp_metrics, vnic);
161   gettimeofday(&time_val, NULL);
162   epoch_start = time_val.tv_sec * 1000000 + time_val.tv_usec;
163   sleep(READ_INTERVAL);
164
165   /***************************************************************************/
166   /* Collect metrics from the VNIC                                           */
167   /***************************************************************************/
168   while(1) {
169     // Read the number of vDNSs currently active
170     int active_dns = 0;
171     FILE* fd = fopen("active_dns.txt", "r");
172     while(!feof(fd)) {
173       if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
174         break;
175     }
176
177     if(fclose(fd))  {
178       printf("Error when closing file\n");
179       return 1;
180     }
181
182     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
183     read_vpp_metrics(curr_vpp_metrics, vnic);
184
185     if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
186       bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
187     }
188     else {
189       bytes_in_this_round = 0;
190     }
191     if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
192       bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
193     }
194     else {
195       bytes_out_this_round = 0;
196     }
197     if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
198       packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
199     }
200     else {
201       packets_in_this_round = 0;
202     }
203     if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
204       packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
205     }
206     else {
207       packets_out_this_round = 0;
208     }
209
210     vpp_m = evel_new_measurement(READ_INTERVAL,"Measurement_vVNF","TrafficStats_1.2.3.4");
211     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
212     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
213
214     if(vpp_m != NULL) {
215       printf("New measurement report created...\n");
216
217       evel_measurement_type_set(vpp_m, "HTTP request rate");
218       evel_measurement_request_rate_set(vpp_m, rand()%10000);
219
220       evel_vnic_performance_rx_total_pkt_delta_set(vnic_performance, packets_in_this_round);
221       evel_vnic_performance_tx_total_pkt_delta_set(vnic_performance, packets_out_this_round);
222
223       evel_vnic_performance_rx_octets_delta_set(vnic_performance, bytes_in_this_round);
224       evel_vnic_performance_tx_octets_delta_set(vnic_performance, bytes_out_this_round);
225
226       /***************************************************************************/
227       /* Set parameters in the MEASUREMENT header packet                         */
228       /***************************************************************************/
229       struct timeval tv_now;
230       gettimeofday(&tv_now, NULL);
231       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
232
233       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
234       vpp_m_header = (EVENT_HEADER *)vpp_m;
235       //vpp_m_header->start_epoch_microsec = start_epoch;
236       //vpp_m_header->last_epoch_microsec = last_epoch;
237       evel_start_epoch_set(&vpp_m->header, epoch_start);
238       evel_last_epoch_set(&vpp_m->header, epoch_now);
239       epoch_start = epoch_now;
240
241       evel_nfcnamingcode_set(&vpp_m->header, "vVNF");
242       evel_nfnamingcode_set(&vpp_m->header, "vVNF");
243       //strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
244       //strcpy(vpp_m_header->reporting_entity_name, hostname);
245       evel_reporting_entity_name_set(&vpp_m->header, "lbll");
246       evel_reporting_entity_id_set(&vpp_m->header, "No UUID available");
247       evel_rc = evel_post_event(vpp_m_header);
248
249       if(evel_rc == EVEL_SUCCESS) {
250         printf("Measurement report correctly sent to the collector!\n");
251       }
252       else {
253         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
254       }
255     }
256     else {
257       printf("New measurement report failed (%s)\n", evel_error_string());
258     }
259
260     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
261     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
262     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
263     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
264     //gettimeofday(&time_val, NULL);
265     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
266
267     sleep(READ_INTERVAL);
268   }
269
270   /***************************************************************************/
271   /* Terminate                                                               */
272   /***************************************************************************/
273   sleep(1);
274   evel_free_measurement(vpp_m);
275   free(last_vpp_metrics);
276   free(curr_vpp_metrics);
277   evel_terminate();
278   printf("Terminated\n");
279
280   return 0;
281 }
282
283 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
284   // Define an array of char that contains the parameters of the unix 'cut' command
285   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
286   // Define the unix command to execute in order to read metrics from the vNIC
287   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
288   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
289   char cmd[BUFSIZE];
290   // Define other variables
291   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
292   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
293   FILE *fp;                     /* file descriptor to pipe cmd to shell */
294   int i;
295
296   for(i = 0; i < 4; i++) {
297     // Clear buffers
298     memset(buf, 0, BUFSIZE);
299     memset(cmd, 0, BUFSIZE);
300     // Build shell command to read metrics from the vNIC
301     strcat(cmd, cmd_prefix);
302     strcat(cmd, vnic);
303     strcat(cmd, cmd_mid);
304     strcat(cmd, params[i]);
305     
306     // Open a pipe and read VPP values
307     if ((fp = popen(cmd, "r")) == NULL) {
308       printf("Error opening pipe!\n");
309       return;
310     }
311
312     while (fgets(buf, BUFSIZE, fp) != NULL);
313     temp[i] = atoi(buf);
314
315     if(pclose(fp))  {
316       printf("Command not found or exited with error status\n");
317       return;
318     }
319   }
320
321   // Store metrics read from the vNIC in the struct passed from the main function
322   vpp_metrics->bytes_in = temp[0];
323   vpp_metrics->bytes_out = temp[1];
324   vpp_metrics->packets_in = temp[2];
325   vpp_metrics->packets_out = temp[3];
326 }