Merge "removed vFW vLB extra executables"
[demo.git] / vnfs / VESreporting_vLB5.0 / vpp_measurement_reporter.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ****************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include <math.h>
25
26 #include "evel.h"
27
28 #define BUFSIZE 128
29 #define READ_INTERVAL 10
30
31 typedef struct dummy_vpp_metrics_struct {
32   int bytes_in;
33   int bytes_out;
34   int packets_in;
35   int packets_out;
36 } vpp_metrics_struct;
37
38 void read_vpp_metrics(vpp_metrics_struct *, char *);
39
40 int main(int argc, char** argv)
41 {
42   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
43   EVENT_MEASUREMENT* vpp_m = NULL;
44   EVENT_HEADER* vpp_m_header = NULL;
45   int bytes_in_this_round;
46   int bytes_out_this_round;
47   int packets_in_this_round;
48   int packets_out_this_round;
49   vpp_metrics_struct* last_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
50   vpp_metrics_struct* curr_vpp_metrics = malloc(sizeof(vpp_metrics_struct));
51   struct timeval time_val;
52   //time_t start_epoch;
53   //time_t last_epoch;
54   char hostname[BUFSIZE];
55   char* fqdn = argv[1];
56   int port = atoi(argv[2]);
57   char* vnic = argv[3];
58   MEASUREMENT_VNIC_PERFORMANCE * vnic_performance = NULL;
59
60   printf("\nVector Packet Processing (VPP) measurement collection\n");
61   fflush(stdout);
62
63   if (argc != 4)
64   {
65     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <interface>\n", argv[0]);
66     exit(-1);
67   }
68
69   /**************************************************************************/
70   /* Initialize                                                             */
71   /**************************************************************************/
72   if(evel_initialize(fqdn,                         /* FQDN                  */
73                      port,                         /* Port                  */
74                      NULL,                         /* optional path         */
75                      NULL,                         /* optional topic        */
76                      0,                            /* HTTPS?                */
77                      "",                           /* Username              */
78                      "",                           /* Password              */
79                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
80                      "vLoadBalancer",              /* Role                  */
81                      1))                           /* Verbosity             */
82   {
83     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
84     exit(-1);
85   }
86   else
87   {
88     printf("\nInitialization completed\n");
89   }
90
91   gethostname(hostname, BUFSIZE);
92   memset(last_vpp_metrics, 0, sizeof(vpp_metrics_struct));
93   read_vpp_metrics(last_vpp_metrics, vnic);
94   gettimeofday(&time_val, NULL);
95   start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
96   sleep(READ_INTERVAL);
97
98   /***************************************************************************/
99   /* Collect metrics from the VNIC                                           */
100   /***************************************************************************/
101   while(1) {
102     // Read the number of vDNSs currently active
103     int active_dns = 0;
104     FILE* fd = fopen("active_dns.txt", "r");
105     while(!feof(fd)) {
106       if(fscanf(fd, "%d", &active_dns) != 1) /* Avoid infinite loops if the file doesn't contain exactly one number */
107         break;
108     }
109
110     if(fclose(fd))  {
111       printf("Error when closing file\n");
112       return 1;
113     }
114
115     memset(curr_vpp_metrics, 0, sizeof(vpp_metrics_struct));
116     read_vpp_metrics(curr_vpp_metrics, vnic);
117
118     if(active_dns > 0 && (curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in > 0)) {
119       bytes_in_this_round = (int) round((curr_vpp_metrics->bytes_in - last_vpp_metrics->bytes_in) / active_dns);
120     }
121     else {
122       bytes_in_this_round = 0;
123     }
124     if(active_dns > 0 && (curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out > 0)) {
125       bytes_out_this_round = (int) round((curr_vpp_metrics->bytes_out - last_vpp_metrics->bytes_out) / active_dns);
126     }
127     else {
128       bytes_out_this_round = 0;
129     }
130     if(active_dns > 0 && (curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in > 0)) {
131       packets_in_this_round = (int) round((curr_vpp_metrics->packets_in - last_vpp_metrics->packets_in) / active_dns);
132     }
133     else {
134       packets_in_this_round = 0;
135     }
136     if(active_dns > 0 && (curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out > 0)) {
137       packets_out_this_round = (int) round((curr_vpp_metrics->packets_out - last_vpp_metrics->packets_out) / active_dns);
138     }
139     else {
140       packets_out_this_round = 0;
141     }
142
143     vpp_m = evel_new_measurement(READ_INTERVAL);
144     vnic_performance = (MEASUREMENT_VNIC_PERFORMANCE *)evel_measurement_new_vnic_performance("eth0", "true");
145     evel_meas_vnic_performance_add(vpp_m, vnic_performance);
146
147     if(vpp_m != NULL) {
148       printf("New measurement report created...\n");
149
150       evel_vnic_performance_rx_total_pkt_acc_set(vnic_performance, packets_in_this_round);
151       evel_vnic_performance_tx_total_pkt_acc_set(vnic_performance, packets_out_this_round);
152
153       evel_vnic_performance_rx_octets_acc_set(vnic_performance, bytes_in_this_round);
154       evel_vnic_performance_tx_octets_acc_set(vnic_performance, bytes_out_this_round);
155
156       /***************************************************************************/
157       /* Set parameters in the MEASUREMENT header packet                         */
158       /***************************************************************************/
159       struct timeval tv_now;
160       gettimeofday(&tv_now, NULL);
161       unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
162
163       //last_epoch = start_epoch + READ_INTERVAL * 1000000;
164       vpp_m_header = (EVENT_HEADER *)vpp_m;
165       //vpp_m_header->start_epoch_microsec = start_epoch;
166       //vpp_m_header->last_epoch_microsec = last_epoch;
167       evel_start_epoch_set(&vpp_m->header, epoch_start);
168       evel_last_epoch_set(&vpp_m->header, epoch_now);
169       epoch_start = epoch_now;
170
171       strcpy(vpp_m_header->reporting_entity_id.value, "No UUID available");
172       strcpy(vpp_m_header->reporting_entity_name, hostname);
173       evel_rc = evel_post_event(vpp_m_header);
174
175       if(evel_rc == EVEL_SUCCESS) {
176         printf("Measurement report correctly sent to the collector!\n");
177       }
178       else {
179         printf("Post failed %d (%s)\n", evel_rc, evel_error_string());
180       }
181     }
182     else {
183       printf("New measurement report failed (%s)\n", evel_error_string());
184     }
185     evel_measurement_free_vnic_performance(vnic_performance);
186
187     last_vpp_metrics->bytes_in = curr_vpp_metrics->bytes_in;
188     last_vpp_metrics->bytes_out = curr_vpp_metrics->bytes_out;
189     last_vpp_metrics->packets_in = curr_vpp_metrics->packets_in;
190     last_vpp_metrics->packets_out = curr_vpp_metrics->packets_out;
191     //gettimeofday(&time_val, NULL);
192     //start_epoch = time_val.tv_sec * 1000000 + time_val.tv_usec;
193
194     sleep(READ_INTERVAL);
195   }
196
197   /***************************************************************************/
198   /* Terminate                                                               */
199   /***************************************************************************/
200   sleep(1);
201   evel_free_measurement(vpp_m);
202   free(last_vpp_metrics);
203   free(curr_vpp_metrics);
204   evel_terminate();
205   printf("Terminated\n");
206
207   return 0;
208 }
209
210 void read_vpp_metrics(vpp_metrics_struct *vpp_metrics, char *vnic) {
211   // Define an array of char that contains the parameters of the unix 'cut' command
212   char* params[] = {"-f3", "-f11", "-f4", "-f12"};
213   // Define the unix command to execute in order to read metrics from the vNIC
214   char* cmd_prefix = "sudo cat /proc/net/dev | grep \"";
215   char* cmd_mid = "\" | tr -s \' \' | cut -d\' \' ";
216   char cmd[BUFSIZE];
217   // Define other variables
218   char buf[BUFSIZE];            /* buffer used to store VPP metrics     */
219   int temp[] = {0, 0, 0, 0};    /* temp array that contains VPP values  */
220   FILE *fp;                     /* file descriptor to pipe cmd to shell */
221   int i;
222
223   for(i = 0; i < 4; i++) {
224     // Clear buffers
225     memset(buf, 0, BUFSIZE);
226     memset(cmd, 0, BUFSIZE);
227     // Build shell command to read metrics from the vNIC
228     strcat(cmd, cmd_prefix);
229     strcat(cmd, vnic);
230     strcat(cmd, cmd_mid);
231     strcat(cmd, params[i]);
232     
233     // Open a pipe and read VPP values
234     if ((fp = popen(cmd, "r")) == NULL) {
235       printf("Error opening pipe!\n");
236       return;
237     }
238
239     while (fgets(buf, BUFSIZE, fp) != NULL);
240     temp[i] = atoi(buf);
241
242     if(pclose(fp))  {
243       printf("Command not found or exited with error status\n");
244       return;
245     }
246   }
247
248   // Store metrics read from the vNIC in the struct passed from the main function
249   vpp_metrics->bytes_in = temp[0];
250   vpp_metrics->bytes_out = temp[1];
251   vpp_metrics->packets_in = temp[2];
252   vpp_metrics->packets_out = temp[3];
253 }